{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Problem solving\n", "\n", "This lecture is dedicated for you to practice your problem solving skills. All problems below were taken from [programming competitions](https://en.wikipedia.org/wiki/Competitive_programming).\n", "\n", "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.\n", "\n", "Think about the solutions before coding. Explain the steps on paper as detailed as possible, and convince yourself that your solution works.\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Frequently Asked Questions\n", "\n", "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.\n", "\n", "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 class scheduling for the next semester, she asked you to help her with this task.\n", "\n", "Implement the function `faq(L, n)` that takes as input:\n", "- `L`: a list with the identifiers of the questions asked by the students, and \n", "- `n`: the number of times a question needs to be asked to be considered frequent.\n", "\n", "This function returns the number of questions that will be added to Scotty.\n", "\n", "For example:\n", "```\n", "faq([10, 40, 20, 10, 40], 2) == 2\n", "faq([1, 4, 2, 1, 3], 2) == 1\n", "faq([1, 1, 3, 5, 4, 6, 3, 4], 3) == 0\n", "``` " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def faq(L, n):\n", " return 42" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Detective Watson\n", "\n", "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.\n", "\n", "\"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.\n", "\n", "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 nor Watson are computer scientists, their list indices start from 1, and not 0.\n", "\n", "For example:\n", "```\n", "guilty([3,5,2]) == 1\n", "guilty([1,15,3,5,2]) == 4\n", "```" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def guilty(L):\n", " return 42" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Corridor\n", "\n", "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.\n", "\n", "For example, in the setting of the picture, the character can get at most 12 mana, enterng through door 2 and exiting through 4.\n", "\n", "![The corridor game](corridor-img.png)\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def mana(L):\n", " return 42" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Door Lock\n", "\n", "Omar was coming home one day when he realized he had lost the key to the door. Desperate, he decided to ask his friend Hasan for help, who in a few seconds managed to open the door using his tools.\n", "\n", "Amazed at this feat, Omar asked Hasan how he did this. Hasan explained:\n", "\n", "\"A lock is a set of N pins arranged horizontally which can be moved up or down with the aid of a metal key. When this metal key is inserted in the lock, it can increase or decrease by 1 mm any two consecutive pins, or the last one by itself. When all the pins are at the same pre-determined height H, the door is unlocked.\"\n", "\n", "Omar wanted to know the least number of movements he needed to make to unlock a certain door, where one movement is increasing or decreasing the height of two consecutive pins by 1mm. He also knows what is the height H the pins need to be at.\n", "\n", "Help Omar by implementing a function `doorUnlock(L, h)` that takes as input a list `L` with the initial heights of the pins, and the target height `h` for unlocking the door. The function returns the minimum number of movements one needs to make to unlock the door.\n", "\n", "For example:\n", "```\n", "doorUnlock([45,45,55,55], 50) == 10\n", "doorUnlock([84,39,17,72,94], 84) == 77\n", "```" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def doorUnlock(L, h):\n", " return 42" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.3" } }, "nbformat": 4, "nbformat_minor": 2 }