Skip to content

Instantly share code, notes, and snippets.

@roxsula
roxsula / vectors.cpp
Created February 21, 2019 21:24
Vector of Vectors in C++
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
@roxsula
roxsula / map_in_cplusplus.cpp
Created February 14, 2019 20:41
Dictionary in C++
#include <cstdio>
#include <vector>
#include <iostream>
#include <string.h>
#include <map>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
@roxsula
roxsula / sherlock_and_the_valid_string.cpp
Created January 28, 2019 22:41
Sherlock considers a string to be valid if all characters of the string appear the same number of times. It is also valid if he can remove just character at index in the string, and the remaining characters will occur the same number of times. Given a string , determine if it is valid. If so, return YES, otherwise return NO.
string isValid(string s) {
std::map<char, int> freq;
vector<int> list;
vector<int> vals;
vector<int>::iterator list_unique;
if(s.length() == 1) return "YES";
else
{
for(int i = 0; i < s.length(); i++){
@roxsula
roxsula / alternating_characters.c
Created January 28, 2019 21:42
Alternating Characters HackerRank - Refreshing C knowledge - return an integer representing the minimum number of deletions to make the alternating string.
int alternatingCharacters(char* s) {
int c = 0;
char* AB = "AB";
char* BA = "BA";
char *ptr1 = strstr(s, AB);
char *ptr2 = strstr(s, AB);
if (ptr1 != NULL || ptr2 != NULL)
{
for (int i = 0; i < strlen(s) - 1; i++) {
if (s[i] == s[i + 1]) {
@roxsula
roxsula / making_anagrams.c
Created January 28, 2019 16:14
HackerRank Strings: Making Anagrams - I'm trying to refresh my C knowledge. This program returns an integer representing the minimum total characters that must be deleted to make the strings anagrams.
int Count(char* s, char c) {
// Count variable
int res = 0;
for (int i = 0; i < strlen(s); i++)
// checking character in string
if (s[i] == c)
res++;
@roxsula
roxsula / strcmpws.c
Created January 23, 2019 15:31
Program in C to compare strings. The strings will be equal if there are white spaces in the tail.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int strcmpws(const char *s1, const char *s2)
{
char *r1 = strdup(s1);
char *r2 = strdup(s2);
int f = 0;
@roxsula
roxsula / invert_endianness.py
Created January 22, 2019 17:33
make endianness conversion between big endian and little endian
def invert_endianness(number):
new_bits = []
total_bits = number.bit_length()
check_len = True
bits = 8
while(check_len):
if (bits - total_bits) >= 0:
check_len = False
@roxsula
roxsula / atoit.py
Created December 12, 2018 21:54
convert a string to integer without int() when the string is alphanumeric convert until find the first letter
def atoi(s):
integer = 0
sign = 1
s0 = s[0]
if s0 in '+-':
s = s[1:]
if s0 == '-':
sign = -1
for d in s:
@roxsula
roxsula / linked_list_test_cases.py
Created November 29, 2018 19:31
This an example of how to use my previous post about a generator of test cases for linked lists
'''
min_max_sum_linked_list():
Walk a linked list (passed in as a parameter) and return a min, max, and average of the values at the end.”
Function returns min, max, and average only if linked list contains numbers with these conditions: int, long, float, positive, negative.
Average is returned in float to provide an accurate result
Test Suite:
Node class
LinkedList class with methods insert_tail(data) and monitoring()
genTestCases class with methods generateCustomTestCase(values), generateRandomTestCase(max_len, max_val), generateEqualValuesTestCase(max_len, val)
@roxsula
roxsula / linked_list_input_cases.py
Created October 23, 2018 23:39
Generator of input test cases for linked lists
'''
Test Suite:
Node class
LinkedList class with methods insert_tail(data) and monitoring()
genTestCases class with methods generateCustomTestCase(values), generateRandomTestCase(max_len, max_val), generateEqualValuesTestCase(max_len, val)
runTests():
Custom your tests
DebugL1 will show the results of each TestCase
DebugL2 will show the linked list generated