Files
learn-python3/notebooks/beginner/exercises/07_functions_exercise.ipynb
2023-04-24 13:13:43 +03:00

139 lines
3.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. Fill the missing pieces of the `count_even_numbers` function\n",
"Fill `____` pieces of the `count_even_numbers` implemention in order to pass the assertions. You can assume that `numbers` argument is a list of integers."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"____ count_even_numbers(numbers):\n",
" count = 0\n",
" for num in ____:\n",
" if ____ % 2 == ____:\n",
" count += ____\n",
" _____ _____"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"assert count_even_numbers([1, 2, 3, 4, 5, 6]) == 3\n",
"assert count_even_numbers([1, 3, 5, 7]) == 0\n",
"assert count_even_numbers([-2, 2, -10, 8]) == 4"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. Searching for wanted people\n",
"Implement `find_wanted_people` function which takes a list of names (strings) as argument. The function should return a list of names which are present both in `WANTED_PEOPLE` and in the name list given as argument to the function."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"WANTED_PEOPLE = [\"John Doe\", \"Clint Eastwood\", \"Chuck Norris\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your implementation here"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"people_to_check1 = [\"Donald Duck\", \"Clint Eastwood\", \"John Doe\", \"Barack Obama\"]\n",
"wanted1 = find_wanted_people(people_to_check1)\n",
"assert len(wanted1) == 2\n",
"assert \"John Doe\" in wanted1\n",
"assert \"Clint Eastwood\" in wanted1\n",
"\n",
"people_to_check2 = [\"Donald Duck\", \"Mickey Mouse\", \"Zorro\", \"Superman\", \"Robin Hood\"]\n",
"wanted2 = find_wanted_people(people_to_check2)\n",
"assert wanted2 == []"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3. Counting average length of words in a sentence\n",
"Create a function `average_length_of_words` which takes a string as an argument and returns the average length of the words in the string. You can assume that there is a single space between each word and that the input does not have punctuation. The result should be rounded to one decimal place (hint: see [`round`](https://docs.python.org/3/library/functions.html#round))."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your implementation here"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"assert average_length_of_words(\"only four lett erwo rdss\") == 4\n",
"assert average_length_of_words(\"one two three\") == 3.7\n",
"assert average_length_of_words(\"one two three four\") == 3.8\n",
"assert average_length_of_words(\"\") == 0"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}