PROG0149 - MacArthurs number game

General Douglas MacArthur played an important part in both the First and Second World War and the Korean War. In 1944, he was ranked General of the Army, with five stars as a recognition. As such, he is one of only 5 generals that were granted five stars in American history and one of only eight that carried the title ever. Although his character was controversial, he possessed an extraordinary amount of leadership qualities.

Douglas MacArthur
Douglas MacArthur (1880-1964)

To preserve his image, for his own amusement, and to entertain his guests, MacArthur started using a mathematical trick he called "that good old 115". Here, he asks one of his guests to write down the number of the month they are born in. January has number 1, February number 2, and so on. He then clearly turns his back to the guests so as to show his guests he can't see what they are writing down. He instructs them to write down the following:

  1. give the number of the month you were born in: 2 (we use February as an example)
  2. double the number: 4
  3. add five: 9
  4. multiply by 50: 450
  5. add your age 490 (we use 40 as an example)
  6. subtract 365: 125

Now the truth comes out. He asks his guest to say the number out loud. In our example that is 125. With a little of mental arithmetic he then quickly adds 115 to the number the guest says. In our example he now has the number 240. He then says without hesitating: "Well, my friend, you were born in February and you are 40 years old."

This is how he does it. The first digit of the number — together with the second digit in case the number consists of four digits — forms the number of the month of birth of the guest, if the good old 115 is added correctly. The last two digits indicate the person's age.

Assignment

  1. Write a function guest that prints the number the guest has on his paper based on the number of the month of birth and the age of the guest — both of which should be given to the function as an argument — after executing the orders of general MacArthur.
    def guest(month, age)
  2. Write a function macArthur that prints a tuple (see section 6.7 in the text book) with the number of the month of birth and the age of the guest, based on the number the guest has said out loud. This number should be given as an argument to the function. General MacArthur presumes that his guests are no older than 100.
    def macArthur(number)

Example

>>> guest(2, 40)
125
>>> macArthur(125)
(2, 40)
>>> macArthur(guest(2, 40))
(2, 40)
>>> guest(12,35)
1120
>>> macArthur(1120)
(12, 35)
>>> guest(12,28)
1113
>>> macArthur(1113)
(12, 28)

Generaal Douglas MacArthur speelde een belangrijke rol in zowel de Eerste en de Tweede Wereldoorlog als in de Koreaanse Oorlog. In 1944 kreeg hij de rang van General of the Army, met vijf sterren als onderscheidingstekens. Hij is daarmee in de geschiedenis van de USA één van de slechts 5 generaals die vijf sterren hebben gekregen en één van slechts acht die die titel hebben gedragen. Hoewel MacArthur controversieel was qua karakter, beschikte hij over uitzonderlijke leiderscapaciteiten.

Douglas MacArthur
Douglas MacArthur (1880-1964)

Om zijn imago in stand te houden, zichzelf wat te amuseren, en zijn gasten te entertainen begon MacArthur een wiskundetrucje te gebruiken dat hij "die goeie ouwe 115" noemde. Hierbij stelt hij aan een van zijn gasten de vraag om het rangnummer van de maand waarin hij of zij geboren is op een papiertje te schrijven. De maand januari heeft rangnummer 1, februari heeft rangnummer 2, enzoverder. Op dit ogenblik draait hij ostentatief zijn rug naar zijn gasten zodat hij duidelijk niet kan zien wat er op het papiertje neergekrabbeld wordt als reactie op zijn instructies. De instructies die hij geeft zijn om achtereenvolgens volgende zaken op het papiertje te schrijven:

  1. geef het rangnummer van je geboortemaand: 2 (we gebruiken februari als voorbeeld)
  2. verdubbel het getal: 4
  3. tel er vijf bij op: 9
  4. vermenigvuldig het getal met 50: 450
  5. tel er je leeftijd bij op: 490 (we gebruiken 40 als voorbeeld)
  6. trek er 365 van af: 125

Nu komt de aap uit de mouw. Hij vraagt zijn gast om het laatste getal luidop voor te lezen. In dit geval dus het getal 125. Met wat eenvoudig hoofdrekenen telt hij snel 115 op bij het getal dat zijn gast heeft uitgesproken. Voor ons voorbeeld bekomt hij dus het getal 240. Daarop zegt hij zonder aarzelen en tot ieders verrassing: "Wel, mijn vriend, je bent geboren in de maand februari en je bent 40 jaar oud."

Dit is hoe hij het doet. Het eerste cijfer van het bekomen getal — samen met het tweede cijfer indien het getal uit vier cijfers bestaat — vormt het rangnummer van de geboortemaand van de gast, als de goeie ouwe 115 er correct werd bij opgeteld. De laatste twee cijfers geven de leeftijd van de gast aan.

Opgave

  1. Schrijf een functie gast die op basis van het rangnummer van de geboortemaand en de leeftijd van de gast — beide moeten als argument aan de functie teruggegeven worden — het getal teruggeeft dat de gast op zijn papiertje heeft staan nadat hij of zij de instructies van generaal MacArthur heeft uitgevoerd.
    def gast(maand, leeftijd)
  2. Schrijf een functie macArthur die een tuple (zie sectie 6.7 in het handboek) met het rangnummer van de geboortedatum en de leeftijd van de gast teruggeeft, op basis van het getal dat de gast luidop heeft voorgelezen van zijn papiertje. Dit laatste getal moet als argument aan de functie doorgegeven worden. Generaal MacArthur gaat ervan uit dat zijn gast niet ouder is dan 100 jaar.
    def macArthur(getal)

Voorbeelden

>>> gast(2, 40)
125
>>> macArthur(125)
(2, 40)
>>> macArthur(gast(2, 40))
(2, 40)
>>> gast(12,35)
1120
>>> macArthur(1120)
(12, 35)
>>> gast(12,28)
1113
>>> macArthur(1113)
(12, 28)

Added by:Peter Dawyndt
Date:2011-08-09
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:PY_NBC
Resource:None

© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.