background preloader

Practice

Facebook Twitter

Ruby Quiz. The Puzzle Toad. You are organizing a conference in social choice theory, with a festive dinner on the first day. The catering service has 1024 different dinner choices they know how to make, out of which you need to choose 10 to be in the dinner menu (each participant will choose one of these during the dinner). You send an email to the 6875 participants of the conference, with the list of all 1024 choices, asking them to rank the choices in linear order from their favorite to their unfavorite. You want to find a list L of 10 choices, such that for any dinner choice d not in the list L, if we run a vote of d against L, at least half of people will favor one of the choices in L over d (it may be different dish for different people).

Is it always possible to produce such a list? AlanATrandomDOTmathDOTcmuDOTedu. Programming Practice Problems. Easy Problems 1. Do you like treasure hunts? In this problem you are to write a program to explore the above array for a treasure. The values in the array are clues. Each cell contains an integer between 11 and 55; for each value the ten's digit represents the row number and the unit's digit represents the column number of the cell containing the next clue.

Starting in the upper left corner (at 1,1), use the clues to guide your search of the array. (The first three clues are 11, 34, 42). 2. 3. 4. Ifmanwas meanttos tayonthe groundgo dwouldha vegivenu sroots The coded message is obtained by reading down the columns going left to right. Imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau In your program, have the user enter a message in english with no spaces between the words.

Input Outputhaveaniceday hae and via ecyfeedthedog fto ehg ee ddchillout clu hlt io 5. A. B. C. D. E. F. Moderate Problems For example, given this text:Black is white. 1. "Invalid character in input. Recursion: Towers of Hanoi. Up: Recursion. Prev: Recursion. Next: Program trace. Using recursion often involves a key insight that makes everything simpler. Often the insight is determining what data exactly we are recursing on - we ask, what is the essential feature of the problem that should change as we call ourselves? In the case of isAJew, the feature is the person in question: At the top level, we are asking about a person; a level deeper, we ask about the person's mother; in the next level, the grandmother; and so on.

In our Towers of Hanoi solution, we recurse on the largest disk to be moved. At the top level, we will want to move the entire tower, so we want to move disks 5 and smaller from peg A to peg B. Move disks 4 and smaller from peg A (source) to peg C (spare), using peg B (dest) as a spare. In pseudocode, this looks like the following. Note that the pseudocode adds a base case: When disk is 0, the smallest disk.

If this doesn't make sense to you, maybe an example run will help. Next: Program trace.