Python Workout 09: More Functions

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.

Here are my solutions:

Challenge 1: Incrementing a Number

def addition(num):
    return num + 1

Challenge 2: Splitting a Number

def number_split(num):
    half = num // 2
    if num % 2 == 0:
        return [half, half]
    else:
        return [half, half + 1]

Challenge 3: Generating a List of Multiples

def list_of_multiples(num, length):
    multiples = []
    for i in range(1, length + 1):
        multiples.append(num * i)
    return multiples

Challenge 4: Performing Basic Arithmetic Operations

def arithmetic_operation(s):
    num1, operator, num2 = s.split()
    num1 = int(num1)
    num2 = int(num2)
    if operator == '+':
        return num1 + num2
    elif operator == '-':
        return num1 - num2
    elif operator == '*':
        return num1 * num2
    elif operator == '//':
        if num2 == 0:
            return -1
        else:
            return num1 // num2

Here are some corresponding results:

Challenge 1: Incrementing a Number

print(addition(0))  # Output: 1

print(addition(9))  # Output: 10

print(addition(-3))  # Output: -2

Challenge 2: Splitting a Number

print(number_split(4))  # Output: [2, 2]

print(number_split(10))  # Output: [5, 5]

print(number_split(11))  # Output: [5, 6]

print(number_split(-9))  # Output: [-5, -4]

Challenge 3: Generating a List of Multiples

print(list_of_multiples(7, 5))  # Output: [7, 14, 21, 28, 35]

print(list_of_multiples(12, 10))  # Output: [12, 24, 36, 48, 60, 72, 84, 96, 108, 120]

print(list_of_multiples(17, 6))  # Output: [17, 34, 51, 68, 85, 102]

Challenge 4: Performing Basic Arithmetic Operations

print(arithmetic_operation("12 + 12"))  # Output: 24

print(arithmetic_operation("12 - 12"))  # Output: 0

print(arithmetic_operation("12 * 12"))  # Output: 144

print(arithmetic_operation("12 // 0"))  # Output: -1
1 Like

Great job @SamMcKay. Love the consistency

Here are my solutions:

#Challenge 1: Incrementing a Number

def addition(num):
    return num+1

#Challenge 2: Splitting a Number
import math

def number_split(num):
    bigger_half = math.ceil(num/2)
    half = math.floor(num/2)
    if num%2 != 0:
        return [half, bigger_half]
    else:
        return [half,half]

#Challenge 3: Generating a List of Multiples

def list_of_multiples(num, length):
    mult_list = []
    for i in range(1, length+1):
        mult_list.append(num*i)
    return mult_list

#Challenge 4: Performing Basic Arithmetic Operations
import operator

operators = {'+':operator.add, '-':operator.sub, '*':operator.mul, '//':operator.floordiv}

def arithmetic_operation(st):
    lt = st.split(' ')
    current_operator = operators[lt[1]]
    n1, n2 = int(lt[0]), int(lt[2])
    if lt[1] == '//' and n2 == 0:
        return -1
    else:
        return current_operator(n1, n2)

Screenshot of my results:

1 Like

Great work here @ERD

Very Easy

def increment_number(num):
    return num + 1

Easy

def split_number(num):
    num_str = str(num)
    half_len = (len(num_str) + 1) // 2
    left_half = int(num_str[:half_len])
    right_half = int(num_str[half_len:])
    return [left_half, right_half]

Medium

def generate_multiples(num, length):
    multiples = []
    for i in range(length):
        multiples.append(num * (i+1))
    return multiples

Hard

def perform_operation(s):
    nums = s.split()
    num1 = int(nums[0])
    num2 = int(nums[2])
    op = nums[1]

    if op == '+':
        return num1 + num2
    elif op == '-':
        return num1 - num2
    elif op == '*':
        return num1 * num2
    elif op == '//':
        if num2 == 0:
            return -1
        else:
            return num1 // num2
    else:
        return None

Here is my submission for this workout. I used Jupyter Notebook, the code for each of the challenges shows the function and a result/output of an example

Very Easy

Summary

Challenge1

Easy

Summary

Challenge2

Medium

Summary

Challenge3

Hard

Summary