Python Workout 10

Level of Difficulty:

Very easy:

Create a function that takes voltage and current and returns the calculated power.

Power = Voltage x Current

Examples:

circuit_power(230, 10) ➞ 2300
circuit_power(110, 3) ➞ 330 
circuit_power(480, 20) ➞ 9600 

Easy:

Create a function that replaces all the vowels in a string with a specified character.

Examples:

replace_vowels("the aardvark", "#") ➞ "th# ##rdv#rk"

replace_vowels("minnie mouse", "?") ➞ "m?nn?? m??s?"

replace_vowels("shakespeare", "*") ➞ "sh*k*sp**r*"

Notes All characters will be in lower case.

Medium:

Given three lists of integers: lst1, lst2, lst3, return the sum of integers which are common in all three lists. Examples:

sum_common([1, 2, 3], [5, 3, 2], [7, 3, 2]) ➞ 5

sum_common([1, 2, 2, 3], [5, 3, 2, 2], [7, 3, 2, 2]) ➞ 7

sum_common([1], [1], [2]) ➞ 0

Hard:
“Loves me, loves me not” is a traditional game in which a person plucks off all the petals of a flower one by one, saying the phrase “Loves me” and “Loves me not” when determining whether the one that they love, loves them back. Given a number of petals, return a string which repeats the phrases “Loves me” and “Loves me not” for every alternating petal, and return the last phrase in all caps. Remember to put a comma and space between phrases. Examples:

loves_me(3) ➞ "Loves me, Loves me not, LOVES ME"

loves_me(6) ➞ "Loves me, Loves me not, Loves me, Loves me not, Loves me, LOVES ME NOT"

loves_me(1) ➞ "LOVES ME"

Notes
Remember to return a string.

The first phrase is always “Loves me”.

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 Monday May 1, 2023, and the author’s solution will be posted on Sunday May 7, 2023.

Hello,
Here are my solutions:

#Challenge 1: Create a function that takes voltage and current and returns the calculated power.

def circuit_power(voltage, current):
    return voltage*current

#Challenge 2: Create a function that replaces all the vowels in a string with a specified character.

def replace_vowels(txt, character):
    vowels = ['a','e','i','o','u','y']
    for i in vowels:
        if i in txt:
            txt = txt.replace(i, character)
    return txt

#Challenge 3: Return the sum of integers which are common in all three lists

def sum_common(lst1, lst2, lst3):
    st1 = set(lst1)
    st2 = set(lst2)
    st3 = set(lst3)
    common = st1 & st2 & st3
    
    res = 0
    
    min_lst = min(lst1, lst2, lst3, key=len)
    
    for elm in min_lst:
        if elm in common:
            res += elm
    return res

#Challenge 4: “Loves me, loves me not”

def loves_me(num):
    txt = 'Loves me'
    txt_even = ', Loves me not'
    txt_odd = ', Loves me'
    for i in range(1, num+1):
        if i%2 != 0:
            if i == num:
                txt += txt_even.upper()
            else:
                txt += txt_even
        else:
            if i == num:
                txt += txt_odd.upper()
            else:
                txt += txt_odd
    
    return txt

Screenshot of my results:
image
image
image
image

1 Like

Great work @ERD

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

Task 1

Summary

Challenge1

Task 2

Summary

Task 3

Summary

Challenge3

Task 4

Summary

Thanks @kedeisha1 for the workout

Very easy

def calculate_power(voltage, current):
    """
    Calculate power based on the given voltage and current.

    """
    power = voltage * current
    return power

Easy

def replace_vowels(input_string, replacement_char):
    """
    Replace all vowels in a string with a specified character.

    Returns:
    str: The modified string with all vowels replaced.
    """
    vowels = "aeiouAEIOU"
    output_string = ""
    
    for char in input_string:
        if char in vowels:
            output_string += replacement_char
        else:
            output_string += char

    return output_string

Medium

def sum_common_elements(lst1, lst2, lst3):
    """
    Return the sum of integers which are common in all three lists.

    Returns:
    int: The sum of common integers in all lists.
    """
    common_elements = set(lst1) & set(lst2) & set(lst3)
    return sum(common_elements)

Hard

def loves_me(n):
    """
    Determine the outcome of the "Loves me, Loves me not" game.

    Returns:
    str: The sequence of "Loves me" and "Loves me not" phrases, with the last phrase in all caps.
    """
    phrases = []
    for i in range(1, n + 1):
        if i % 2 != 0:  # if i is odd
            phrases.append("Loves me")
        else:
            phrases.append("Loves me not")
            
    phrases[-1] = phrases[-1].upper()  # capitalize the last phrase
    
    return ', '.join(phrases)