Skip to content

Commit

Permalink
functions' exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
ariefrahmansyah committed Aug 12, 2024
1 parent d9f7aae commit 6a4aa37
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions docs/python/python_fundamentals_1/functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,88 @@
"two_power_of_five = power(2)(5)\n",
"print(two_power_of_five)"
]
},
{
"cell_type": "markdown",
"id": "831ddf53-5cf3-48b4-922a-70dddc894651",
"metadata": {},
"source": [
"## Exercises\n",
"\n",
"### Exercises: Level 1\n",
"\n",
"1. Declare a function _add_two_numbers_. It takes two parameters and it returns a sum.\n",
"2. Area of a circle is calculated as follows: area = π x r x r. Write a function that calculates _area_of_circle_.\n",
"3. Write a function called add_all_nums which takes arbitrary number of arguments and sums all the arguments. Check if all the list items are number types. If not do give a reasonable feedback.\n",
"4. Temperature in °C can be converted to °F using this formula: °F = (°C x 9/5) + 32. Write a function which converts °C to °F, _convert_celsius_to-fahrenheit_.\n",
"5. Write a function called check-season, it takes a month parameter and returns the season: Autumn, Winter, Spring or Summer.\n",
"6. Write a function called calculate_slope which return the slope of a linear equation\n",
"7. Quadratic equation is calculated as follows: ax² + bx + c = 0. Write a function which calculates solution set of a quadratic equation, _solve_quadratic_eqn_.\n",
"8. Declare a function named print_list. It takes a list as a parameter and it prints out each element of the list.\n",
"9. Declare a function named reverse_list. It takes an array as a parameter and it returns the reverse of the array (use loops).\n",
"\n",
"```py\n",
"print(reverse_list([1, 2, 3, 4, 5]))\n",
"# [5, 4, 3, 2, 1]\n",
"print(reverse_list1([\"A\", \"B\", \"C\"]))\n",
"# [\"C\", \"B\", \"A\"]\n",
"```\n",
"\n",
"10. Declare a function named capitalize_list_items. It takes a list as a parameter and it returns a capitalized list of items\n",
"11. Declare a function named add_item. It takes a list and an item parameters. It returns a list with the item added at the end.\n",
"\n",
"```py\n",
"food_staff = ['Potato', 'Tomato', 'Mango', 'Milk'];\n",
"print(add_item(food_staff, 'Meat')) # ['Potato', 'Tomato', 'Mango', 'Milk','Meat'];\n",
"numbers = [2, 3, 7, 9];\n",
"print(add_item(numbers, 5)) [2, 3, 7, 9, 5]\n",
"```\n",
"\n",
"12. Declare a function named remove_item. It takes a list and an item parameters. It returns a list with the item removed from it.\n",
"\n",
"```py\n",
"food_staff = ['Potato', 'Tomato', 'Mango', 'Milk'];\n",
"print(remove_item(food_staff, 'Mango')) # ['Potato', 'Tomato', 'Milk'];\n",
"numbers = [2, 3, 7, 9];\n",
"print(remove_item(numbers, 3)) # [2, 7, 9]\n",
"```\n",
"\n",
"13. Declare a function named sum_of_numbers. It takes a number parameter and it adds all the numbers in that range.\n",
"\n",
"```py\n",
"print(sum_of_numbers(5)) # 15\n",
"print(sum_all_numbers(10)) # 55\n",
"print(sum_all_numbers(100)) # 5050\n",
"```\n",
"\n",
"14. Declare a function named sum_of_odds. It takes a number parameter and it adds all the odd numbers in that range.\n",
"15. Declare a function named sum_of_even. It takes a number parameter and it adds all the even numbers in that - range.\n",
"\n",
"### Exercises: Level 2\n",
"\n",
"1. Declare a function named evens_and_odds . It takes a positive integer as parameter and it counts number of evens and odds in the number.\n",
"\n",
" ```py\n",
" print(evens_and_odds(100))\n",
" # The number of odds are 50.\n",
" # The number of evens are 51.\n",
" ```\n",
"\n",
"1. Call your function factorial, it takes a whole number as a parameter and it return a factorial of the number\n",
"1. Call your function _is_empty_, it takes a parameter and it checks if it is empty or not\n",
"1. Write different functions which take lists. They should calculate_mean, calculate_median, calculate_mode, calculate_range, calculate_variance, calculate_std (standard deviation).\n",
"m\n",
"### Exercises: Level 3\n",
"\n",
"1. Write a function called is_prime, which checks if a number is prime.\n",
"1. Write a functions which checks if all items are unique in the list.\n",
"1. Write a function which checks if all the items of the list are of the same data type.\n",
"1. Write a function which check if provided variable is a valid python variable\n",
"1. Go to the data folder and access the countries-data.py file.\n",
"\n",
" - Create a function called the most_spoken_languages in the world. It should return 10 or 20 most spoken languages in the world in descending order\n",
" - Create a function called the most_populated_countries. It should return 10 or 20 most populated countries in descending order."
]
}
],
"metadata": {
Expand Down

0 comments on commit 6a4aa37

Please sign in to comment.