Skip to content

Instantly share code, notes, and snippets.

@pazthor
Last active March 22, 2020 19:55
Show Gist options
  • Save pazthor/5917fddfc0a3dd70254a02e81205dc67 to your computer and use it in GitHub Desktop.
Save pazthor/5917fddfc0a3dd70254a02e81205dc67 to your computer and use it in GitHub Desktop.
# Esta es una lista de problemas que hice en hacker rank como prueba tecnica para una empresa.
# Arrays: Left Rotation
# a es uin arreglo, d es el número de veces a rotar.
def rotLeft(a, d):
return a[d:] + a[:d]
# New Year Chaos
# queue: una lista de elementos
# numero de elementos a intercambiar || 'too chaotic'
def functionResult(queue):
lastIndex = len(queue) - 1
swaps = 0
swaped = False
# check if the queue is too chaotic
for i, v in enumerate(queue):
if (v - 1) - i > 2:
return ("Too chaotic")
# bubble sorting to find the answer
for i in range(0, lastIndex):
for j in range(0, lastIndex):
if queue[j] > queue[j+1]:
temp = queue[j]
queue[j] = queue[j+1]
queue[j+1] = temp
swaps += 1
swaped = True
if swaped:
swaped = False
else:
break
return( swaps)
## Dictionary and hashmap
# Hash Tables: Ransom Note
# Magazine, ransom: lista de palabras
# devuelve falso si No se puede replicar, verdadero en caso contrario
def checkMagazine(magazine, ransom):
magazine.sort()
ransom.sort()
for word in ransom:
if word not in magazine:
return False
magazine.remove(word)
return True
# Sherlock and Anagrams
# recibe un lista de palabras
# devuelve un número de cuantos pares pueden generar
def sherlockAndAnagrams(s):
count = 0
for i in range(1,len(s)+1):
a = ["".join(sorted(s[j:j+i])) for j in range(len(s)-i+1)]
b = Counter(a)
for j in b:
count+=b[j]*(b[j]-1)/2
return int(count)
### Sorting ###
## Este código fue hecho en C++
#Sorting: Bubble Sort
# Complete the countSwaps function below.
void countSwaps(vector<int> a) {
int arraySorted = 0;
int firstElement = 0;
int lastElement = 0;
for(int i=0; i< a.size(); i++){
for(int j=0; j<a.size()-1; j++)
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j+ 1];
a[j+1] = temp;
// swap(a[j], a[j + 1]);
arraySorted++;
}
}
cout<< "Array is sorted in " <<arraySorted << " swaps." << '\n';
cout<< "First Element: "<< a[0] << '\n';
cout<< "Last Element: "<< a[a.size()-1] << '\n';
}
# Sorting comparator
##// complete this method
static int comparator(Player a, Player b) {
if(a.score > b.score) return 1;
if(a.score < b.score) return -1;
if(a.name.compare(b.name) > 0 ) return -1;
else return 1;
return 0;
}
## bloque C++ termina
## STRING Manipulation
#bloque C
## Making anagram
int funct(char *a , char *b){
int sum = 0;
int i;
for( i = 0; a[i] !='\0' ; i++){
A[a[i] - 'a'] ++;
}
for( i = 0; b[i] !='\0' ; i++){
A[b[i] - 'a']--;
}
for(int i = 0; i < 30; i++){
sum+= abs(A[i]);
}
return sum;
}
## fin bloque C
# Sherlock and the Valid String
# Given , we would need to remove two characters, both c and d aabb or a and b abcd, to make it valid.
# We are limited to removing only one character, so is invalid.
def isValid(s):
freq=[v for k,v in Counter(s).items()]
pivot=freq[0]
different_freqs=[]
for i in freq[1::]:
if(pivot!=i):
different_freqs.append(i)
if(len(different_freqs)>1):
return 'NO'
else:
return 'YES'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment