Question

In: Computer Science

that, given an integer N and an integer K, returns the minimum number of rounds that...

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.

Solutions

Expert Solution


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


Related Solutions

Given a positive integer k and an array A[1..n] that contains the quiz scores of n...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n students in ascending order, design a divide and conquer algorithm to efficiently count the number of students that have quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. Let group i be the set of students with quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. The counting result should be stored in G[1..k],...
Problem 5 Given a table of n integers and an integer k, make a program in...
Problem 5 Given a table of n integers and an integer k, make a program in C++ that: a) Read n b) Read the table of numbers. c) Determine the number of elements in the table less than k d) Determine the number of elements in the table equal to k e) Determine the number of elements in the table greater than k f) That displays the values found
There is a special calculator that when adding numbers rounds the number to the closest integer....
There is a special calculator that when adding numbers rounds the number to the closest integer. For example, 1.1 + 2 + 3.6 will be calculated as 1 + 2 + 4. The error from each addition follows a uniform distribution of (-0.5, 0.5). a. When 1500 numbers are added, what is the probability that the absolute value of the total error is greater than 15? b. How many numbers can be added until the probability of the absolute value...
4. Let n ≥ 8 be an even integer and let k be an integer with...
4. Let n ≥ 8 be an even integer and let k be an integer with 2 ≤ k ≤ n/2. Consider k-element subsets of the set S = {1, 2, . . . , n}. How many such subsets contain at least two even numbers?
For all integers n > 2, show that the number of integer partitions of n in...
For all integers n > 2, show that the number of integer partitions of n in which each part is greater than one is given by p(n)-p(n-1), where p(n) is the number of integer partitions of n.
Write a method sumTo that accepts an integer parameter n and returns the sum of the...
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words: sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should print an error message and return -1 if passed a value less than 0. Include a loop. Please help for Java programming.
in C++ Description: For a given integer n > 1, the smallest integer d > 1...
in C++ Description: For a given integer n > 1, the smallest integer d > 1 that divides n is a prime factor. We can find the prime factorization of n if we find d and then replace n by the quotient of n divided by d, repeating this until n becomes 1.             Write a program that determines the prime factorization of n in this manner, but that displays the prime factors in descending order. (When you find a...
A positive integer N is a power if it is of the form q^k, where q,...
A positive integer N is a power if it is of the form q^k, where q, k are positive integers and k > 1. Give an efficient algorithm that takes as input a number N and determines whether it is a square, that is, whether it can be written as q^2 for some positive integer q. What is the running time of your algorithm? write the pseudocode for the algorithm.
Show that for any square-free integer n > 1, √ n is an irrational number
Show that for any square-free integer n > 1, √ n is an irrational number
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We...
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We want to return the k smallest element of A[1.....n], in non-decreasing order. For example: A = [5, 4, 6, 2, 10] and k = 4, the algorithm returns [2, 4, 5, 6]. There are at least the following four approaches: a. heapify A and then extract k elements one by one b. sort the array (e.g. using MergeSort or HeapSort) and then read the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT