We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I agree, the question was worded wrong. My answer is wrong for contains the possibilities displayed by the instructions (taking into consideration floor levels)
# Create towers
tower_dic = {tower+1: [floor+1 for floor in range(m)] for tower in range(n)}
winner = 2
turn = cycle_1_2()
# While we have towers (greater than 1 floor)
while len(tower_dic) > 0:
# If only one tower, make it 1 (remove from dic - end turn)
if len(tower_dic) == 1:
winner = next(turn)
break
# Elif odd number of towers, make last one 1(remove from dic - end turn)
elif len(tower_dic) % 2 != 0:
tower_dic.popitem()
winner = next(turn)
break
# Else we have even number of towers
else:
# Try to make the other person get an even number too
# For each tower
for tower in tower_dic:
# If even number of floors
if len(tower_dic[tower]) % 2 == 0:
# turn into 2 floors (end turn)
tower_dic[tower] = [1,2]
winner = next(turn)
break
# Else we odd number of floors
else:
#Check if there are any divisible items besides from 1
for floor in tower_dic[tower]:
# If so, use that (end turn)
if floor > 8 and len(tower_dic[tower]) % floor == 0:
tower_dic[tower] = [i+1 for i in range(floor)]
winner = next(turn)
break
# Else move to next tower
# Since there weren't any towers with number of floors with divisible numbers other tan 1
# Turn first tower to 1 (remove from dic - end turn)
tower_dic.popitem()
winner = next(turn)
break
return winner
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tower Breakers
You are viewing a single comment's thread. Return to all comments →
I agree, the question was worded wrong. My answer is wrong for contains the possibilities displayed by the instructions (taking into consideration floor levels)