9 N COINS FLIP
1.) If there are M heads-up coins in N coins then pick randomly M coins from given coins and create two piles. (M coins, (N-M) coins)
2.) Flip all coins from a pile of M coins. You’re done.
from random import randint
# Random function that returns N Coins with M Heads
def random_coins():
# generate random number either 1 or 0
r = randint(0, 1)
# return Head if we got number 1, else return T
return H if (r == 1) else T
# Generate Two Piles with Same Number of Heads-up Coins
if __name__ == '__main__':
# Head
H = 1
# Tail
T = 0
head_count = tail_count = 0
#Number of coins, let's take 100
N = 100
coins = []
for i in range(N):
val = random_coins()
if val == 1:
head_count += 1
coins.append(1)
else:
tail_count += 1
coins.append(0)
print('Randomly Generated Coins')
print(coins)
print('In single pile of', N, 'coins:', head_count, 'are Heads and ', tail_count, ' are Tails')
print('***Solution for splitting the coins into two piles***')
pile1_coins = coins[0:head_count]
print('Size of 1st piles coins:', len(pile1_coins))
print('1st piles coins:', pile1_coins)
pile2_coins = coins[len(pile1_coins):N]
print('Size of 2nd piles coins:', len(pile2_coins))
print('2nd piles coins:', pile2_coins)
pile1_coins_head = pile2_coins_head = 0
print('flip all coins of 1st pile')
for g in range(len(pile1_coins)):
if pile1_coins[g] == 1:
pile1_coins[g] = 0
else:
pile1_coins[g] = 1
print('1st piles coins after a flip:', pile1_coins)
for j in range(len(pile1_coins)):
if pile1_coins[j] == 1:
pile1_coins_head += 1
for k in range(len(pile2_coins)):
if pile2_coins[k] == 1:
pile2_coins_head += 1
print('Number of coins with head in 1st pile:', pile1_coins_head)
print('Number of coins with head in 2nd pile:', pile2_coins_head)
Output:
Randomly Generated Coins
[0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1]
In single pile of 100 coins: 47 are Heads and 53 are Tails
***Solution for splitting the coins into two piles***
Size of 1st piles coins: 47
1st piles coins: [0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1]
Size of 2nd piles coins: 53
2nd piles coins: [1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1]
flip all coins of 1st pile
1st piles coins after a flip: [1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0]
Number of coins with head in 1st pile: 29
Number of coins with head in 2nd pile: 29
Run again and output:
Randomly Generated Coins
[0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1]
In single pile of 100 coins: 50 are Heads and 50 are Tails
***Solution for splitting the coins into two piles***
Size of 1st piles coins: 50
1st piles coins: [0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1]
Size of 2nd piles coins: 50
2nd piles coins: [0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1]
flip all coins of 1st pile
1st piles coins after a flip: [1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0]
Number of coins with head in 1st pile: 30
Number of coins with head in 2nd pile: 30