Problem solving

This lecture is dedicated for you to practice your problem solving skills. All problems below were taken from programming competitions.

Take this opportunity to look back and see how far you have come, and how much you have learned! A few weeks ago, these problems might have looked much more cryptic, and maybe you wouldn't even know how to begin. Hopefully by now you are able to understand them, and start having ideas on how to solve them.

Think about the solutions before coding. Explain the steps on paper as detailed as possible, and convince yourself and others that your solution works.

You can work in teams.

Submit your solutions to Gradescope. The team that solves more problems faster wins.

The competition lasts until Thursday, September 15th, 2:30pm.


Multiple Choice Answers

Multiple choice exams usually have an answer sheet like this:

answer sheet

After students fill in the answer, an optical reader will scan the paper and assign shades of grey to each bubble of each question. A value of 0 indicates that the bubble is all black (i.e. the student marked that bubble), and a value of 255 indicates that the bubble is all white (i.e. the student did not mark that bubble). Since students fill in the bubbles in various different ways and with pens/pencils of different shades, the convention is that shades less than or equal to 127 are considered marked bubbles, and shades greater than 127 are considered unmarked bubbles.

The result of the scan of one answer sheet is represented as a list of lists. The inner lists have 5 elements, a number between 0 and 255, representing the shades of grey scanned for the bubbles A, B, C, D, and E. Your job is to implement the function answers(A) that takes as input the list of lists as described above, and returns a list containing the answers on that answer sheet. The answers are mapped to integers in the following way: 1 for A, 2 for B, 3 for C, 4 for D, and 5 for E. If no or multiple answers are selected for a question, map it to -1.

For example, let:

A = [ [0,   255, 255, 255, 255],
      [255, 255, 255, 255, 0  ],
      [255, 255, 127, 255, 255] ]

then answers(A) should return [1,5,3].

Frequently Asked Questions

Lots of sites in the internet have a page called "Frequently Asked Questions" that, as the name suggests, contains the questions that users make frequently.

Jarrin receives lots of questions from students, so she decided that it would be a good idea to add a Frequently Asked Questions page to Scotty. As Jarrin is very busy these days with the class scheduling for next semester, she asked you to help her with this task.

Implement the function faq(L, n) that takes as input:

This function returns a list with the identifiers of questions that will be added to to Scotty. The list should be sorted in ascending order.

For example:

faq([10, 40, 20, 10, 40, 10], 2) == [10, 40]
faq([1, 4, 2, 1, 3], 2) == [1]
faq([1, 1, 3, 5, 4, 6, 3, 4], 3) == []

In the first example, questions that were asked 2 or more times will be added to the FAQ. In this case, questions 10 and 40 will be included since they were asked three and two times, respectively.

Detective Watson

John Watson, after years working besides Sherlock Holmes, never understood how he was able to guess who was guilty so easily. On a certain night, however, Sherlock finally told John his secret.

"Elementary, dear Watson", said Sherlock Holmes. "It is never the most suspicious, but the second most suspicious". After he knew the secret, John decided to solve a crime on his own to test Sherlock's method.

Implement the function guilty(L) that takes a list with integers representing how much each person is a suspect. The higher the number, the more suspicious the person. The function should return at which position in the list the guilty person is, according to Sherlock's method. Since neither Sherlock or Watson are computer scientists, their list indices start from 1, and not 0.

For example:

guilty([3,5,2]) == 1
guilty([1,15,3,5,2]) == 4

In the first example, the second most suspicious person in the group is the first one in the list.

Corridor

Aisha is programming a game, and in one of the phases the character must enter a corridor of rooms through one of its side doors, and exit through another. In each room, there is a certain amount of mana that the character will collect. But to make things interesting, Aisha decided that some of the rooms will have negative mana. The goal of the character is to choose the entry and exit point such that the amount of collected mana is maximized.

For example, in the setting of the picture, the character can get at most 12 mana, enterng through door 2 and exiting through 4.

The corridor game

Implement the function mana(L) that takes as input a list of manas in each of the rooms, and returns the maximum amount of mana the character can collect.

For example:

assert(mana([-2,5,-1,8,-11,7,3]) == 12)
assert(mana([-1,2,-3]) == 2)