id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

peter@norvig.com > shannon_puzzle
prev  |  next  |  chance

Claude Shannon, the inventor of Information Theory, and the person most responsible for bringing Boolean Logic to computers, posed the following logic puzzle: It is known that salesmen always tell the truth and engineers always tell lies. B and E are salesmen. C states that D is an engineer. A declares that B affirms that C asserts that D says that E insists that F denies that G is a salesman. If A is an engineer, how many engineers are there? To turn this into a codingbat problem, we will generalize it somewhat. The function shannon_puzzle will take seven arguments, each a string that tells the possible professions for each of the seven people. The string "ES" means the person might be either engineer or salesman; "E" means he must be an engineer; "S" means he must be a salesman. So Shannon's original problem is coded as shannon_puzzle("E", "S", "ES", "ES", "S", "ES", "ES"), because A is an engineer and B and E are salesmen. The function should return a sorted list of the possible number of engineers. If there is only one possibility, this list should be one element long. If there are several possibilities that are consistent with the premises, return the list of all possible numbers in sorted, increasing order. If there is no possibility that is consistent with the premises, return the empty list. (Don't repeat any numbers, even if they can be realized in multiple ways.)


shannon_puzzle('ES', 'ES', 'ES', 'ES', 'ES', 'ES', 'ES') → [1, 3, 5]
shannon_puzzle('E', 'ES', 'ES', 'ES', 'ES', 'ES', 'ES') → [3, 5]
shannon_puzzle('ES', 'S', 'ES', 'ES', 'S', 'ES', 'ES') → [3]

...Save, Compile, Run (ctrl-enter)

def shannon_puzzle(As, Bs, Cs, Ds, Es, Fs, Gs):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Difficulty: 500 Post-solution available

Copyright Nick Parlante 2017 - privacy