Skip to content

Commit

Permalink
fixed strings
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLeoni committed Oct 1, 2019
1 parent c788970 commit 7c89d94
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
10 changes: 5 additions & 5 deletions exercises/errors-and-testing/errors-and-testing-solution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -581,11 +581,11 @@
"\n",
"They asked you to develop a program to control a nuclear reactor. The reactor produces a lot of energy, but requires at least 20 meters of water to cool down, and your program needs to regulate the water level. Without enough water, you risk a meltdown. You do not feel exactly up to the job, and start sweating.\n",
"\n",
"Nervously, you write the code. You check and recheck the code - everythin looks alright.\n",
"Nervously, you write the code. You check and recheck the code - everything looks fine.\n",
"\n",
"On inauguration day, the reactor is turned on. Unexpectedly, the water level goes down to 5 meters, and an uncontrolled chain reaction occurs. Plutoniom fireworks follow. \n",
"\n",
"Could have avoided all of this? We often believe everything is all right but then for some reason we find variables with unexpected values. The wrong program described above might have been written like so:\n"
"Could have avoided all of this? We often believe everything is good but then for some reason we find variables with unexpected values. The wrong program described above might have been written like so:\n"
]
},
{
Expand Down Expand Up @@ -876,7 +876,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Note how nuclear reactorn was _not_ turned on."
"Note how the reactor was _not_ turned on."
]
},
{
Expand All @@ -903,7 +903,7 @@
"source": [
"### Where Is Your Software?\n",
"\n",
"As a Data Scientist, you might likely end up with code which is algorithmically complex, but maybe not too big in size. Either way, when red line is crossed you should start testing properly:\n",
"As a data scientist, you might likely end up with code which is algorithmically complex, but maybe not too big in size. Either way, when red line is crossed you should start testing properly:\n",
"\n",
"![](img/where-is-your-software.png)\n"
]
Expand Down Expand Up @@ -1895,7 +1895,7 @@
"text": [
"..\n",
"----------------------------------------------------------------------\n",
"Ran 2 tests in 0.002s\n",
"Ran 2 tests in 0.001s\n",
"\n",
"OK\n"
]
Expand Down
34 changes: 24 additions & 10 deletions exercises/strings/strings-solution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,12 @@
"\n",
"HINT: use _slices_\n",
"\n",
"NOTE 1: PRINTing is different from RETURNing !!! Whatever gets printed is shown to the user but Python cannot reuse it for calculations.\n",
"\n",
"NOTE 2: if a function does not explicitly return anything, Python implicitly returns `None`. \n",
"\n",
"NOTE 3: Resorting to prints on error conditions is not actually good practice, here we use it as invitation to think about what happens when you print something and do not return anything. You can read a discussion about it in [Errors handling and testing page](https://datasciprolab.readthedocs.io/en/latest/exercises/errors-and-testing/errors-and-testing-solution.html#Unforeseen-situations)\n",
"\n",
"\n",
"```python\n",
">>> x = invertilet(\"ciao\", \"world\")\n",
Expand Down Expand Up @@ -913,10 +919,16 @@
"source": [
"### nspace\n",
"\n",
"✪ Write the function `nspace` that given a string `s` in input, RETURN a new stringa in which the `n`-character is a space. So, if the given the string \n",
"✪ Write the function `nspace` that given a string `s` in input, RETURN a new string in which the `n`-character is a space. \n",
"\n",
"For example, given the string `'largamente'` and the index `5`, the program should RETURN the string `'larga ente'`. NOTE: if the number is too big (for example, the word has 6 characters and you pass the number 9), the program PRINTS _error!_.\n",
"\n",
"NOTE 1: if the number is too big (for example, the word has 6 character and you pass the number 9), the program PRINTS _error!_.\n",
"\n",
"NOTE 2: PRINTing is different from RETURNing !!! Whatever gets printed is shown to the user but Python cannot reuse it for other calculations.\n",
"\n",
"NOTE 3: Resorting to prints on error conditions is not actually a good practice, here we use it as invitation to think about what happens when you print something and do not return anything. You can read a discussion about it in [Errors handling and testing page](https://datasciprolab.readthedocs.io/en/latest/exercises/errors-and-testing/errors-and-testing-solution.html#Unforeseen-situations)\n",
"\n",
"Scrivi la funzione `nspazio` che data una stringa in input, RITORNA una nuova stringa in\n",
"cui l’n-esimo carattere è uno spazio. For example, given the string 'largamente' and the index 5, the program should RETURN the string `'larga ente'`. NOTE: if the number is too big (for example, the word has 6 character and you pass the number 9), the program PRINTS _error!_.\n",
"\n",
"```python\n",
">>> x = nspazio('largamente', 5)\n",
Expand Down Expand Up @@ -946,7 +958,7 @@
"def nspace(word, index):\n",
" if index >= len(word):\n",
" print(\"error!\")\n",
" return word[:index]+' '+word[index+1:]\n",
" return word[:index] + ' ' + word[index+1:]\n",
"\n",
"#nspace(\"largamente\", 5)"
]
Expand Down Expand Up @@ -1043,7 +1055,7 @@
"\n",
"✪ RETURN `True` if `word` contains `char`, `False` otherwise\n",
" \n",
"- use `while` cycle"
"- use `while` cycle (just for didactical purposes, using `in` would certainly be faster & shorter)"
]
},
{
Expand Down Expand Up @@ -1087,7 +1099,7 @@
"\n",
"NOTE: I DO NOT WANT A PRINT, IT MUST *RETURN* THE VALUE !\n",
" \n",
"- Use the cycle for in"
"- Use the cycle for in (just for didactical purposes, strings already provide a method to do it fast - which one?)"
]
},
{
Expand Down Expand Up @@ -1270,7 +1282,9 @@
">>> x\n",
"False\n",
"\n",
"```"
"```\n",
"\n",
"There are various ways to solve this problems, some actually easy & elegant. Try to find at least a couple of them (don't need to bang your head with the recursive one ..)."
]
},
{
Expand All @@ -1283,7 +1297,7 @@
"def palindrome(word):\n",
" #jupman-raise\n",
" for i in range(len(word) // 2):\n",
" if word[i] != word[len(word)- i - 1]:\n",
" if word[i] != word[len(word)- i - 1]: \n",
" return False\n",
" \n",
" return True # note it is OUTSIDE for: after passing all controls,\n",
Expand Down Expand Up @@ -1433,15 +1447,15 @@
"\n",
"✪✪ We now want to extract the province prefix from phone numbers (see previous exercise) - the ones we consider as valid are in `province_prefixes` list.\n",
"\n",
"Note some numbers are from mobile operators and you can distinguish them by prefixes like `328` - the ones we consider are in an `mobile_prefixes` list.\n",
"Note some numbers are from mobile operators and you can distinguish them by prefixes like `328` - the ones we consider are in `mobile_prefixes` list.\n",
"\n",
"Implement a function that RETURN the prefix of the phone as a string. Remember first to make it canonical !! \n",
"\n",
"- If phone is mobile, RETURN string `'mobile'`. If it is not a phone nor a mobile, RETURN the string `'unrecognized'`\n",
"\n",
"- To determine if the phone is mobile or from province, use `province_prefixes` and `mobile_prefixes` lists.\n",
"\n",
"- DO USE THE PREVIOUSLY DEFINED FUCTION canon_phnoe(phone)\n"
"- DO USE THE PREVIOUSLY DEFINED FUNCTION `phone_canon(phone)`\n"
]
},
{
Expand Down
3 changes: 1 addition & 2 deletions slides.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@
"* [Third Google Colab](https://colab.research.google.com/drive/10MKRM4zvjez0r7a_jIFZ14THJ3GodOLX)\n",
" (for code shown during lesson)\n",
"\n",
"* Lesson: [Strings](exercises/strings/strings-solution.ipynb) (NOTE: redownload it, was updated) \n",
"* (Maybe) [Lists](exercises/lists/lists-solution.ipynb)"
"* Lesson: [Strings](exercises/strings/strings-solution.ipynb) (NOTE: redownload it, was updated) "
]
},
{
Expand Down

0 comments on commit 7c89d94

Please sign in to comment.