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 have written the below code and its working fine. Since its taking random values, its not matched with the expected values. Can you please me identify whats wrong in this ?
from random import choices
n=input()
p=[1,2,3,4,5,6]
for i in range(0,int(n)):
w=input()
w = [float(e) for e in w.split(',')]
ls = input().split(',')
ladders=ls[0]
snakes=ls[1]
b=input()
ladder_dict = {}
ladder_pos=b.split()
for i in range(len(ladder_pos)):
ladder_map = ladder_pos[i].split(',')
ladder_dict[ladder_map[0]] = [ladder_map[1]]
print(ladder_dict)
s=input()
snake_dict = {}
snake_pos=s.split()
for i in range(len(snake_pos)):
snake_map = snake_pos[i].split(',')
snake_dict[snake_map[0]] = [snake_map[1]]
print(snake_dict)
def roll(p,w):
return choices(p,w)
dice_turn = 0
i = 1
counter = 0
while i <= 100 :
#print(i , dice_turn)
#print(counter)
if i == 100:
break
counter += 1
dice_turn = roll(p,w)
if (i+int(dice_turn[0])) <= 100:
i = i+int(dice_turn[0])
for start,stop in ladder_dict.items():
#print(i , start , stop)
if i == int(start):
#print("value matched ladder")
i = int(stop[0])
for start,stop in snake_dict.items():
#print(i , start , stop)
if i == int(start):
#print("value matched snake")
i = int(stop[0])
print(counter)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Markov's Snakes And Ladders
You are viewing a single comment's thread. Return to all comments →
Hi
I have written the below code and its working fine. Since its taking random values, its not matched with the expected values. Can you please me identify whats wrong in this ?
from random import choices
n=input()
p=[1,2,3,4,5,6]
for i in range(0,int(n)):
print(ladder_dict)
print(snake_dict)
#print(i , dice_turn) #print(counter) if i == 100: break counter += 1 dice_turn = roll(p,w) if (i+int(dice_turn[0])) <= 100: i = i+int(dice_turn[0]) for start,stop in ladder_dict.items(): #print(i , start , stop) if i == int(start): #print("value matched ladder") i = int(stop[0]) for start,stop in snake_dict.items(): #print(i , start , stop) if i == int(start): #print("value matched snake") i = int(stop[0])