Brain Teasers with Coding

Coding Data Science Study

Brain teasers, kind of puzzles built for lateral thinking, can help to improve cognitive skills. Lateral thinking is a way of solving problems using an indirect and creative approach via reasoning that is not immediately obvious. It involves ideas that may not be obtainable using only traditional step-by-step logic. 

Traditionally during software engineering or analytics positions hiring, some companies may ask brain teaser(s) to test how a candidate thinks and approaches a new problem when faced with one. Data Science started with statistics has evolved to include the concepts of software engineering and so does its hiring practices. Whether you are trying to improve your coding skills or preparing for a data science interview, you should at least try on them to test your critical thinking and logic. In data science, statistics is a great supplement to the traditional coding brain teasers.

Updated: Brain teasers from my book “Brain Teasers with Coding For Data Scientist” are published on this link. Also, don’t forget to check my puzzle books “Brain Teasers with Coding For Data Scientist 2: 9 New Computational Puzzles” and “Puzzles with Coding: Puzzles for Everyone” and “Computational Puzzles To Flex Your Brain: 50 original puzzles to sharpen computing mind and mathematical skills at Amazon. For purchasing my books or related queries, feel free to contact me. In these books, you will find interesting brain teasers those are suitable for coders as well as non-coders. Coding solutions are also provided with Python code. So, grab a copy of those books and jump into the world of fascinating and exciting brain teasers. Let’s take one brain teaser example.

N pirates of different ages have a treasure of 100 gold coins.

On their ship, they decide to split the coins using this scheme:

The oldest pirate proposes how to share the coins, and all pirates (including the oldest) vote for or against it.

If 50% or more of the pirates vote for it, then the coins will be shared that way. Otherwise, the pirate proposing the scheme will be thrown overboard, and the process is repeated with the pirates that remain.

As pirates tend to be a bloodthirsty bunch, if a pirate would get the same number of coins if he voted for or against a proposal, he will vote against so that the pirate who proposed the plan will be thrown overboard.

Assuming that all N pirates are intelligent, rational, greedy, and do not wish to die, (and are rather good at math for pirates) what will happen? (Provided condition: N<50)

.

.

.

.

.

.

B

Y

T

E

M

A

N

.

.

.

.

.

.

.

Solution:

(Keeping non-coder readers in mind, code optimization is not fully utilized.)

Let’s take N=5 for example.

Let us name the pirates (from oldest to youngest): ‘A’, ‘B’, ‘C’, ‘D’ and ‘E’.

Working backwards:

If N=2 (‘D’ & ‘E’ only)

2 Pirates: ‘D’ splits the coins 100 : 0 (giving himself all the gold). His vote (50%) is enough to ensure the deal.

If N=3 (‘C’, ‘D’ & ‘E’)

3 Pirates: ‘C’ splits the coins 99 : 0 : 1. ‘E’ will accept this deal (getting just 1 coin), because he knows that if he rejects the deal there will be only two pirates left, and he gets nothing.

If N=4 (‘B’, ‘C’, ‘D’ & ‘E’)

4 Pirates: ‘B’ splits the coins 99 : 0 : 1 : 0. By the same reasoning as before, ‘D’ will support this deal. ‘B’ would not waste a spare coin on ‘C’, because ‘C’ knows that if he rejects the proposal, he will pocket 99 coins once ‘B’ is thrown overboard. ‘B’ would also not give a coin to ‘E’, because ‘E’ knows that if he rejects the proposal, he will receive a coin from ‘C’ in the next round anyway.

If N=5 (‘A’, ‘B’, ‘C’, ‘D’ & ‘E’)

5 Pirates: ‘A’ splits the coins 98 : 0 : 1 : 0 : 1. By offering a gold coin to ‘C’ (who would otherwise get nothing) he is assured of a deal.

(Note: In the final deal ‘A’ would not give a coin to ‘B’, who knows he can pocket 99 coins if he votes against ‘A’s proposal and ‘A’ goes overboard. Likewise, ‘A’ would not give a coin to ‘D’, because ‘D’ knows that if he votes against the proposal, ‘A’ will be voted overboard and ‘B’ will propose to offer ‘D’ the same single coin as ‘A’. All else equal, ‘D’ would rather see ‘A’ go overboard and collect his one coin from ‘B’.)

Working forwards:

What if, 6 pirates(‘A’, ‘B’, ‘C’, ‘D’, ‘E’ & ‘F’)

If N=6

The oldest pirate splits the coins 98 : 0 : 1 : 0 : 1 : 0

What if, 7 pirates(‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’ & ‘G’)

If N=7

The oldest pirate splits the coins 97 : 0 : 1 : 0 : 1 : 0 : 1

Thus, if N is odd number than the youngest one gets 0 gold coin else he gets 1 gold coin.

If N is odd then

The oldest pirate splits the coins (100-((N-1)/2) ) : 0 : 1 :0 … : 0 :  1

If N is even then

The oldest pirate splits the coins (100-((N-1)/2) ) : 0 : 1 :0 … :  1 :  0

import math

#You can add a check for no_of_pirates <50
no_of_pirates = int(input('Enter number of pirates:'))

print("Oldest to youngest pirate coins distribution")

if (no_of_pirates % 2 == 0):
    oldest_pirate_coins = 100 - ((no_of_pirates - 1) / 2)
    print(no_of_pirates, 'number (oldest) pirate gets', math.ceil(oldest_pirate_coins), 'coins')
    for i in range(no_of_pirates-1,0,-1):
        if(i % 2):
            print(i,'number pirate gets','0 Coin')
        else:
            print(i,'number pirate gets','1 Coin')

else:
    oldest_pirate_coins = 100 - ((no_of_pirates - 1) / 2)
    print(no_of_pirates, 'number (oldest) pirate gets', math.ceil(oldest_pirate_coins), 'coins')
    for i in range(no_of_pirates - 1, 0, -1):
        if (i % 2):
            print(i,'number pirate gets', '1 Coin')
        else:
            print(i,'number pirate gets','0 Coin')

Test Output:
Enter number of pirates:7
Oldest to youngest pirate coins distribution
7 number (oldest) pirate gets 97 coins
6 number pirate gets 0 Coin
5 number pirate gets 1 Coin
4 number pirate gets 0 Coin
3 number pirate gets 1 Coin
2 number pirate gets 0 Coin
1 number pirate gets 1 Coin

.

Feel free to contact us for buying books or related queries!!!