|
| 1 | +# Python Problems |
| 2 | + |
| 3 | +- [Gas Stations](#gas-stations) |
| 4 | + - [Signature ](#signature-) |
| 5 | + - [Test examples](#test-examples) |
| 6 | +- [Is Number Balanced](#is-number-balanced) |
| 7 | + - [Signature ](#signature--1) |
| 8 | + - [Test examples](#test-examples-1) |
| 9 | +- [Increasing and Decreasing Sequences](#increasing-and-decreasing-dequences) |
| 10 | + - [Signature ](#signature--2) |
| 11 | + - [Test examples](#test-examples-2) |
| 12 | +- [Largest Palindrome](#largest-palindrome) |
| 13 | + - [Signature ](#signature--3) |
| 14 | + - [Test examples](#test-examples-3) |
| 15 | +- [Sum all numbers in a given string](#sum-all-numbers-in-a-given-string) |
| 16 | + - [Signature ](#signature--4) |
| 17 | + - [Test examples](#test-examples-4) |
| 18 | +- [Birthday Ranges](#birthday-ranges) |
| 19 | + - [Signature ](#signature--5) |
| 20 | + - [Test examples](#test-examples-5) |
| 21 | +- [100 SMS](#100-sms) |
| 22 | + - [Signature ](#signature--6) |
| 23 | + - [Test examples](#test-examples-6) |
| 24 | + |
| 25 | +In a file called `week2_solutions.py`, solve the following problems: |
| 26 | + |
| 27 | +## Gas Stations |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +We are implementing a smart GPS software. |
| 32 | + |
| 33 | +- We are taking a long trip from Sofia to Bourgas and we know the distance between the two cities. It is a positive integer and we mark it as `distance`. |
| 34 | + |
| 35 | +- We know how much our car can ride with a full tank of gas. It is a positive integer in kilometers. We mark it as `tank_size`. |
| 36 | + |
| 37 | +- We have a list of gas stations. We know the distance between Sofia and the current gas station. `stations = [50, 80, 110, 180, 220, 290]` Notice, the list is sorted! |
| 38 | + |
| 39 | +By using this information we will implement a function that returns the shortest `list` of gas stations that we have to visit in order to travel from Sofia to Bourgas. We allways start with a full tank_size! |
| 40 | + |
| 41 | +### Signature |
| 42 | + |
| 43 | +```python |
| 44 | +def gas_stations(distance, tank_size, stations): |
| 45 | + pass |
| 46 | +``` |
| 47 | + |
| 48 | +### Test Example |
| 49 | + |
| 50 | +```python |
| 51 | +>>> gas_stations(320, 90, [50, 80, 140, 180, 220, 290]) |
| 52 | +[80, 140, 220, 290] |
| 53 | +>>> gas_stations(390, 80, [70, 90, 140, 210, 240, 280, 350]) |
| 54 | +[70, 140, 210, 280, 350] |
| 55 | +``` |
| 56 | + |
| 57 | +## Is Number Balanced |
| 58 | + |
| 59 | +A number is called balanced, if we take the middle of it and the sums of the left and right parts are equal. |
| 60 | + |
| 61 | +For example, the number `1238033` is balanced, because it's left part is `123` and right part is `033`. |
| 62 | + |
| 63 | +We have: `1 + 2 + 3 = 0 + 3 + 3 = 6`. |
| 64 | + |
| 65 | +- A number with only one digit is always balanced! |
| 66 | + |
| 67 | +### Signature |
| 68 | + |
| 69 | +```python |
| 70 | +def is_number_balanced(number): |
| 71 | + pass |
| 72 | +``` |
| 73 | + |
| 74 | +### Test Examples |
| 75 | + |
| 76 | +```python |
| 77 | +>>> is_number_balanced(9) |
| 78 | +True |
| 79 | +>>> is_number_balanced(4518) |
| 80 | +True |
| 81 | +>>> is_number_balanced(28471) |
| 82 | +False |
| 83 | +>>> is_number_balanced(1238033) |
| 84 | +True |
| 85 | +``` |
| 86 | + |
| 87 | +## Increasing and Decreasing Sequences |
| 88 | + |
| 89 | +Implement a function, called `increasing_or_decreasing(seq)` where the `seq` parameter is a `list` of integers. |
| 90 | + |
| 91 | +### Signature |
| 92 | + |
| 93 | +```python |
| 94 | +def increasing_or_decreasing(seq): |
| 95 | + pass |
| 96 | +``` |
| 97 | + |
| 98 | +The function should return `Up!`, if the given sequence is monotonously increasing. |
| 99 | +If monotonously decreasing return `Down!` . |
| 100 | +If both of the conditions are not satisfied, then return `False`. |
| 101 | + |
| 102 | +And before you skip this problem, because of the math terminology, let me explain: |
| 103 | + |
| 104 | +**A sequence is monotonously increasing if for every two elements `a` and `b`, that are next to each other (`a` is before `b`), we have `a` < `b`.** |
| 105 | + |
| 106 | +For example, `[1,2,3,4,5]` is monotonously increasing, but `[1,2,3,4,5,1]` is not. |
| 107 | + |
| 108 | +### Test Examples |
| 109 | + |
| 110 | +```python |
| 111 | +>>> increasing_or_decreasing([1,2,3,4,5]) |
| 112 | +Up! |
| 113 | +>>> increasing_or_decreasing([5,6,-10]) |
| 114 | +False |
| 115 | +>>> increasing_or_decreasing([1,1,1,1]) |
| 116 | +False |
| 117 | +>>> increasing_or_decreasing([9,8,7,6]) |
| 118 | +Down! |
| 119 | +``` |
| 120 | + |
| 121 | +## Largest Palindrome |
| 122 | + |
| 123 | +Implement a function `get_largest_palindrome`, which returns the largest palindrome smaller than `n`. Given number `n` can also be a palindrome. |
| 124 | + |
| 125 | +### Signature |
| 126 | + |
| 127 | +```python |
| 128 | +def get_largest_palindrome(n): |
| 129 | + pass |
| 130 | +``` |
| 131 | + |
| 132 | +### Test Examples |
| 133 | + |
| 134 | +```python |
| 135 | +>>> get_largest_palindrome(99) |
| 136 | +88 |
| 137 | +>>> get_largest_palindrome(252) |
| 138 | +242 |
| 139 | +>>> get_largest_palindrome(994687) |
| 140 | +994499 |
| 141 | +>>> get_largest_palindrome(754649) |
| 142 | +754457 |
| 143 | +``` |
| 144 | + |
| 145 | +## Sum all numbers in a given string |
| 146 | + |
| 147 | +You are given a string, where there can be numbers. Return the sum of all numbers in that string(**not digits, numbers**). |
| 148 | + |
| 149 | +### Signature |
| 150 | + |
| 151 | +```python |
| 152 | +def sum_of_numbers(input_string): |
| 153 | + pass |
| 154 | +``` |
| 155 | + |
| 156 | +### Test Examples |
| 157 | + |
| 158 | +```python |
| 159 | +>>> sum_of_numbers("ab125cd3") |
| 160 | +128 |
| 161 | +>>> sum_of_numbers("ab12") |
| 162 | +12 |
| 163 | +>>> sum_of_numbers("ab") |
| 164 | +0 |
| 165 | +>>> sum_of_numbers("1101") |
| 166 | +1101 |
| 167 | +>>> sum_of_numbers("1111O") |
| 168 | +1111 |
| 169 | +>>> sum_of_numbers("1abc33xyz22") |
| 170 | +56 |
| 171 | +>>> sum_of_numbers("0hfabnek") |
| 172 | +0 |
| 173 | +``` |
| 174 | + |
| 175 | +## Birthday Ranges |
| 176 | + |
| 177 | +Implement a function that calculates how many people are born in a range of `start` and `end` date(`end` is included in the range). The input parameters are: |
| 178 | + |
| 179 | +- `birthdays` - a list of integers, which are in the range from 1 to 365 inclusive. |
| 180 | +- `ranges` - a list of tuples, where each tuple has only two integer values(the first one represents the `start` date and the second - `end` date). All values are in the range from 1 to 365 inclusive. |
| 181 | + |
| 182 | +### Signature |
| 183 | + |
| 184 | +```python |
| 185 | +def birthday_ranges(birthdays, ranges): |
| 186 | + pass |
| 187 | +``` |
| 188 | + |
| 189 | +Calculate, for each tuple, how many people are born in that between the `start` and `end` date. |
| 190 | + |
| 191 | +### Test Examples |
| 192 | + |
| 193 | +```python |
| 194 | +>>> birthday_ranges([1, 2, 3, 4, 5], [(1, 2), (1, 3), (1, 4), (1, 5), (4, 6)]) |
| 195 | +[2, 3, 4, 5, 2] |
| 196 | +>>> birthday_ranges([5, 10, 6, 7, 3, 4, 5, 11, 21, 300, 15], [(4, 9), (6, 7), (200, 225), (300, 365)]) |
| 197 | +[5, 2, 0, 1] |
| 198 | +``` |
| 199 | + |
| 200 | +## 100 SMS |
| 201 | + |
| 202 | +A long time ago, before the smartphones, when you had to write some messages, the keypads looked like that: |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | +For example, on such keypad, if you want to write **Ruby**, you had to press the following sequence of numbers: |
| 207 | + |
| 208 | +``` |
| 209 | +7778822999 |
| 210 | +``` |
| 211 | + |
| 212 | +Each key contains some letters from the alphabet. And by pressing that key, you rotate the letters until you get to your desired one. |
| 213 | + |
| 214 | +It's time to implement some encode / decode functions for the old keypads! |
| 215 | + |
| 216 | +First, implement a function that takes a list of integers - the sequence of numbers that have been pressed. The returned value should be the corresponding string of the message. |
| 217 | + |
| 218 | +### Signature: |
| 219 | + |
| 220 | +```python |
| 221 | +def numbers_to_message(pressed_sequence): |
| 222 | + pass |
| 223 | +``` |
| 224 | + |
| 225 | +There are some special rules: |
| 226 | + |
| 227 | +- If you press `1`, the next letter is going to be capitalized |
| 228 | +- If you press `0`, this will insert a single white-space |
| 229 | +- If you press a number and wait for a few seconds, the special breaking number `-1` enters the sequence. This is the way to write different letters from only one keypad! |
| 230 | + |
| 231 | +### Test examples: |
| 232 | + |
| 233 | +```python |
| 234 | +>>> numbers_to_message([2, -1, 2, 2, -1, 2, 2, 2]) |
| 235 | +"abc" |
| 236 | +>>> numbers_to_message([2, 2, 2, 2]) |
| 237 | +"a" |
| 238 | +>>> numbers_to_message([1, 4, 4, 4, 8, 8, 8, 6, 6, 6, 0, 3, 3, 0, 1, 7, 7, 7, 7, 7, 2, 6, 6, 3, 2]) |
| 239 | +"Ivo e Panda" |
| 240 | +``` |
| 241 | + |
| 242 | +Now it is time to convert the message to a sequence of numbers. This function takes a string - the `message` and returns the **minimal** keystrokes that you need to write that `message` |
| 243 | + |
| 244 | +### Signature: |
| 245 | + |
| 246 | +```python |
| 247 | +def message_to_numbers(message): |
| 248 | + pass |
| 249 | +``` |
| 250 | + |
| 251 | +### Test examples: |
| 252 | + |
| 253 | +```python |
| 254 | +>>> message_to_numbers("abc") |
| 255 | +[2, -1, 2, 2, -1, 2, 2, 2] |
| 256 | +>>> message_to_numbers("a") |
| 257 | +[2] |
| 258 | +>>> message_to_numbers("Ivo e Panda") |
| 259 | +[1, 4, 4, 4, 8, 8, 8, 6, 6, 6, 0, 3, 3, 0, 1, 7, 2, 6, 6, 3, 2] |
| 260 | +>>> message_to_numbers("aabbcc") |
| 261 | +[2, -1, 2, -1, 2, 2, -1, 2, 2, -1, 2, 2, 2, -1, 2, 2, 2] |
| 262 | +``` |
0 commit comments