Created
November 7, 2019 17:49
-
-
Save ondoheer/b759c1af655ae996ec8af8aac55c7f88 to your computer and use it in GitHub Desktop.
Talently HR 1st problems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def almostSorted(arr): | |
s = sorted(arr) | |
diffcount = 0 | |
diff1 = None | |
diff2 = None | |
for i in range(len(arr)): | |
if s[i] != arr[i]: # if both positions arent the same then we record a difference | |
diffcount += 1 | |
if diff1 == None: # if no difference has been set yet we assign it to the first appearance | |
diff1 = i | |
elif diffcount > 1: # if there is already a difference we record the second apprerance | |
diff2 = i | |
if diffcount == 2: #if the difference is exactely 2 items, we swap them | |
arr[diff1], arr[diff2] = arr[diff2], arr[diff1] | |
if arr == s: # if this creates an ordered list great! | |
print("yes") | |
print("swap {} {}".format(diff1+1, diff2+1)) | |
else: | |
print("no") | |
elif diffcount > 2: # if the difference is bigger then we need to check if reverse works | |
arr = arr[:diff1] + arr[diff1:diff2+1][::-1] + arr[diff2+1:] | |
if arr == s: | |
print("yes") | |
print("reverse {} {}".format(diff1+1, diff2+1)) | |
else: | |
print("no") | |
elif arr == s: | |
print("yes") | |
assert(almostSorted([4,2])== "yes\n swap 1 2") | |
assert(almostSorted([3,2,1]) =="no") | |
assert(almostSorted([1,5,4,3,2,6]) =="yes \n reverse 2 5") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import reduce | |
def convert_grid_to_num(grid): | |
rows = grid.split("\n")[1:-1] | |
ngrid = [] | |
for r in rows: | |
newrow = [] | |
for c in r: | |
if c == ".": | |
newrow.append(0) | |
elif c == "O": | |
newrow.append(3) | |
ngrid.append(newrow) | |
return ngrid | |
def convert_grid_to_rep(grid): | |
ngrid = [] | |
for r in grid: | |
newrow = [] | |
for c in r: | |
if c == 0: | |
newrow.append(".") | |
else : | |
newrow.append("O") | |
ngrid.append(newrow) | |
text_rep = "" | |
for r in ngrid: | |
text_rep += "".join(r) | |
text_rep += "\n" | |
return text_rep | |
# need enumerate | |
def pass_time(grid): | |
ngrid = [] | |
blowup = [] | |
for i, r in enumerate(grid): | |
newrow = [] | |
for j,c in enumerate(r): | |
if c > 1: | |
newrow.append(c-1) | |
elif c == 0: | |
newrow.append(3) | |
# blows up | |
else: | |
newrow.append(0) | |
blowup.append((i,j)) | |
ngrid.append(newrow) | |
for exp in blowup: | |
#try: #exp(5,0) | |
# arriba | |
if exp[0] > 0: | |
ngrid[exp[0]-1][exp[1]] = 0 #(4,0) | |
# izq | |
if exp[1] > 0: | |
ngrid[exp[0]][exp[1]-1] = 0 # no entra | |
# abajo | |
if exp[0] < len(ngrid): | |
ngrid[exp[0]][exp[1]] = 0 # (5,0) | |
# derecha | |
if exp[1] < len(ngrid[0]): | |
ngrid[exp[0]][exp[1]+1] = 0 # (5, 2) | |
return ngrid | |
def bomberMan(n, grid): | |
turn = 0 | |
# initial setup | |
grid = convert_grid_to_num(grid) | |
while turn < n: | |
grid = pass_time(grid) | |
turn +=1 | |
#grid = reduce(lambda x,y: x+y ,grid) | |
#print(convert_grid_to_rep(grid)) | |
grid_1 = """ | |
....... | |
...O... | |
....O.. | |
....... | |
OO..... | |
OO..... | |
""" | |
assert(bomberMan(3, grid_1) == """ | |
OOO.OOO | |
OO...OO | |
OOO...O | |
..OO.OO | |
...OOOO | |
...OOOO | |
""") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# suboptimal? | |
def compareTriplets(a, b): | |
r = [0,0] | |
for i in range(len(a)): | |
if a[i]>b[i]: | |
r[0] += 1 | |
elif b[i] > a[i]: | |
r[1] +=1 | |
return r | |
assert(compareTriplets([5,6,7], [3,6,10])== [1, 1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# suboptimal but passed all the tests | |
def findDigits(n): | |
d = f"{n}" | |
count = 0 | |
for i in d: | |
if int(i): | |
if n % int(i) == 0: | |
count +=1 | |
return count | |
assert(findDigits(12)==2) | |
assert(findDigits(1012)==3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# suboptimal but still passed | |
def migratoryBirds(arr): | |
s = set(arr) | |
d = {} | |
for bird in s: | |
d[bird] = arr.count(bird) | |
return max(d.keys(), key=(lambda k: d[k])) | |
assert(migratoryBirds([1,4,4,4,5,3])==4) | |
assert(migratoryBirds([3,3,4,4,4,4,3,3])==3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# n = Number of prisioners | |
# m = number of candy | |
# s = starting seat | |
# SubOptimal bruteforce | |
def saveThePrisoner(n, m, s): | |
while m > 1: | |
if s == n: | |
s = 0 | |
s += 1 | |
m -= 1 | |
return s | |
# best | |
def saveThePrisoner(n,m,s): | |
# modulo number of candy and prisioners | |
remain = m % n | |
if(remain + s -1)% n == 0: | |
return n | |
else: | |
return (remain + s -1) % n | |
# if bigger than 0 add that s | |
# if less than 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def timeInWords(h, m): | |
rep = { | |
1: "one", | |
2: "two", | |
3: "three", | |
4: "four", | |
5: "five", | |
6: "six", | |
7: "seven", | |
8: "eight", | |
9: "nine", | |
10: "ten", | |
11: "eleven", | |
12: "twelve", | |
13: "thirteen", | |
14: "fourteen", | |
15: "quarter", | |
16: "sixteen", | |
17: "seventeen", | |
18: "eighteen", | |
19: "nineteen", | |
20: "twenty", | |
30: "thirty" | |
} | |
if m == 0: | |
return f"{rep[h]} o' clock" | |
elif m == 30: | |
return f"half past {rep[h]}" | |
elif m == 15: | |
return f"quarter past {rep[h]}" | |
elif m == 45: | |
return f"quarter to {rep[h +1]}" | |
elif m > 30: | |
if m < 40: | |
return f"twenty {rep[m % 30]} minutes to {rep[h +1]}" | |
return f"{rep[60 - m]} minutes to {rep[h +1]}" | |
elif m < 30: | |
if m == 1: | |
return f"{rep[m]} minute past {rep[h]}" | |
if m > 20: | |
return f"twenty {rep[m % 20]} minutes past {rep[h]}" | |
return f"{rep[m]} minutes past {rep[h]}" | |
assert(timeInWords(5,47) == "thirteen minutes to six") | |
assert(timeInWords(3,00) == "three o' clock") | |
assert(timeInWords(7,15) == "quarter past seven") | |
assert(timeInWords(6, 28) == "twenty eight minutes past six") | |
assert(timeInWords(6,35) == "twenty five minutes to seven") | |
assert(timeInWords(5,45) == "quarter to six") | |
assert(timeInWords(1,1) == "one minute past one") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment