Memory
Time limit1sMemory limit256 MB
Play Memory with 50 face-down cards (25 letter pairs) by calling faceup(C); find each matching pair so Jack collects all 25 candies.
- Level
Easy3 of 10
- Topics
- Simulation, Implementation, Array, Hash map
- Solved
- No attempts yet
Problem

A game called Memory is played with 50 cards. Each card has one of the letters from A to Y (ASCII 65 to 89) printed on its face, so that each letter appears on exactly two cards. The cards are shuffled into a random order and dealt face down on the table.
Jack plays the game by turning two cards face up so the letters are visible. For each of the 25 letters, Jack gets a candy from his mother the first time he sees both copies of the letter on the two face-up cards. For example, the first time Jack turns over both cards that contain the letter M, he gets a candy. Regardless of whether the letters were equal or not, Jack then turns both cards face down again. The process is repeated until Jack receives 25 candies, one for each letter.
You must implement a procedure play that plays the game. Your implementation should call the procedure faceup(C), which is implemented by the grader. C is a number between 1 and 50 denoting a particular card you wish to be turned face up. The card must not currently be face up. faceup(C) returns the character printed on card C.
After every second call to faceup, the grader automatically turns both cards face down again.
Your procedure play may terminate only once Jack has received all 25 candies. It is allowed to make calls to faceup(C) even after the moment when Jack gets the last candy.