Level of Difficulty:
Objective::
Very easy:
Create a function that takes a number as an argument, increments the number by +1 and returns the result.
Examples:
addition(0) ➞ 1
addition(9) ➞ 10
addition(-3) ➞ -2
Notes
• Don’t forget to return the result.
Easy:
Given a number, return a list containing the two halves of the number. If the number is odd, make the rightmost number higher.
Examples:
number_split(4) ➞ [2, 2]
number_split(10) ➞ [5, 5]
number_split(11) ➞ [5, 6]
number_split(-9) ➞ [-5, -4]
Notes
• All numbers will be integers.
• You can expect negative numbers too.
Medium:
Create a function that takes two numbers as arguments (num, length) and returns a list of multiples of num until the list length reaches length.
Examples:
list_of_multiples(7, 5) ➞ [7, 14, 21, 28, 35]
list_of_multiples(12, 10) ➞ [12, 24, 36, 48, 60, 72, 84, 96, 108, 120]
list_of_multiples(17, 6) ➞ [17, 34, 51, 68, 85, 102]
Notes
Notice that num is also included in the returned list.
Hard:
Create a function to perform basic arithmetic operations that includes addition, subtraction, multiplication and division on a string number (e.g. “12 + 24” or “23 - 21” or “12 // 12” or “12 * 21”).
Here, we have 1 followed by a space, operator followed by another space and 2. For the challenge, we are going to have only two numbers between 1 valid operator. The return value should be a number.
eval() is not allowed. In case of division, whenever the second number equals “0” return -1.
Examples:
arithmetic_operation("12 + 12") ➞ 24
arithmetic_operation("12 - 12") ➞ 0
arithmetic_operation("12 * 12") ➞ 144
arithmetic_operation("12 // 0") ➞ -1
Notes
• All the inputs are only integers.
• The operators are * - + and //.
• Hint: Think about the single space that appears before and after the arithmetic operator.
Simply post your code and a screenshot of your results.
Please format your Python code and blur it or place it in a hidden section.
This workout will be released on Thursday April 27, 2023, and the author’s solution will be posted on Wednesday May 3, 2023.