Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Phyton program Convert into C++ program Fake Coin import random real_coin = 1 fake_coin = 0 def randomize_coins(n): global coins coins= [real_coin] * (n -

Phyton program Convert into C++ program Fake Coin

import random real_coin = 1 fake_coin = 0 def randomize_coins(n): global coins coins= [real_coin] * (n - 1) + [fake_coin] * (1) random.shuffle(coins) print(coins) return coins def testing_fake_one(x,y,n): if n==1: print(1,"st coin is the fake one") else: if n % 3==0 or n%3==1: A=[coins[i] for i in range(x,x+(y-x)//3 )] B=[coins[i] for i in range(x+(y-x)//3,x+2*(y-x)//3)] C=[coins[i] for i in range(x+2*(y-x)//3,y)] coins_index_A=[i+1 for i in range(x,x+(y-x)//3)] coins_index_B=[i+1 for i in range(x+(y-x)//3,x+2*(y-x)//3)] coins_index_C=[i+1 for i in range(x+2*(y-x)//3,y)] if sum(A)1: testing_fake_one(x,x+(y-x)//3,(y-x)//3) if len(A)==1: return print(coins_index_A[0],"th coin is the fake coin") elif sum(A)>sum(B): if len(B)>1: testing_fake_one(x+(y-x)//3,x+2*(y-x)//3,(y-x)//3) if len(B)==1: return print(coins_index_B[0],"th coin is the fake coin") else: if len(C)>1: testing_fake_one(x+2*(y-x)//3,y,y-2*(y-x)//3-x) if len(C)==1: return print(coins_index_C[0],"th coin is the fake coin") else: A=[coins[i] for i in range(x,x+(y-x)//3+1)] B=[coins[i] for i in range(x+(y-x)//3+1,x+2*(y-x)//3+1)] C=[coins[i] for i in range(x+2*(y-x)//3+1,y)] coins_index_A=[i+1 for i in range(x,x+(y-x)//3+1)] coins_index_B=[i+1 for i in range(x+(y-x)//3+1,x+2*(y-x)//3+1)] coins_index_C=[i+1 for i in range(x+2*(y-x)//3+1,y)] if sum(A)1: testing_fake_one(x,x+(y-x)//3+1,(y-x)//3+1) if len(A)==1: return print(coins_index_A[0],"th coin is the fake coin") elif sum(A)>sum(B): if len(A)>1: testing_fake_one(x+(y-x)//3+1,x+2*(y-x)//3+1,(y-x)//3) if len(A)==1: return print(coins_index_B[0],"th coin is the fake coin") else: if len(A)>1: testing_fake_one(x+2*(y-x)//3+1,y,y-2*(y-x)//3-1-x) if len(A)==1: return print(coins_index_C[0],"th coin is the fake coin") n=int(input("Enter the number of coins:")) randomize_coins(n) testing_fake_one(0,n,n)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students also viewed these Databases questions