In: Computer Science
that, given an integer N and an integer K, returns the minimum number of rounds that are necessary for John to leave the casino with N chips, having played all-in no more than K times.
def solution(n,k):
all_in = 0
dic = dict()
while(1):
#it creates a directionary of betting exactly one chip or betting all-in
if n==2:
dic[n] = "He bet 1"
break
if n%2==0 and all_in<k:
dic[n] = "All-in"
all_in+=1
n=int(n/2)
else:
dic[n] = "He bet 1"
n-=1
print("At the beginning: 1\n")
count=1
for key in sorted(dic):
#betting after 2nd round
if count%10==2:
temp = str(count)+"nd"
#betting after 3rd round
elif count%10==3:
temp = str(count)+"rd"
#betting after 1st round
elif count==1:
temp = str(count)+"st"
#else remaining rounds
else:
temp = str(count)+"th"
print("After {} round: {} ({})".format(temp,key,dic[key])) #prints the output
count+=1
print("\nHe played all-in {} times".format(all_in))
#user input for N and K
N = int(input("Enter N:"))
K = int(input("Enter K:"))
solution(N,K) #function call
Output::
If any query please comment