Skip to main content

Practice Problems: Basic Function Design (Python)

Tests are given for all practice problems for you to run against your own implementation. To run the tests, either run python myfile.py or pytest myfile.py where you have placed the code you are working on in myfile.py.

Problem 1: Temperature and Unit Conversions

Tasks:

  1. celsius_to_fahrenheit - convert Celsius to Fahrenheit
  2. meters_to_feet - convert meters to feet (1 meter = 3.3 feet)
  3. calculate_bmi - calculate Body Mass Index from weight (kg) and height (m)

Template:

def celsius_to_fahrenheit(celsius: float) -> float:
"""write your docstring here"""
# write your implementation here
pass

def meters_to_feet(meters: float) -> float:
"""write your docstring here"""
# write your implementation here
pass

def calculate_bmi(weight_kg: float, height_m: float) -> float:
"""write your docstring here"""
# write your implementation here
pass

def test_celsius_to_fahrenheit():
assert celsius_to_fahrenheit(0) == 32.0
assert celsius_to_fahrenheit(100) == 212.0
assert celsius_to_fahrenheit(-40) == -40.0

def test_meters_to_feet():
assert meters_to_feet(1) == 3.3
assert meters_to_feet(10) == 33.0
assert meters_to_feet(0) == 0.0
assert meters_to_feet(0.5) == 1.65

def test_calculate_bmi():
assert abs(calculate_bmi(70, 1.75) - 22.86) < 0.01
assert abs(calculate_bmi(80, 1.80) - 24.69) < 0.01
assert abs(calculate_bmi(60, 1.50) - 26.67) < 0.01

try:
calculate_bmi(70, 0)
assert False, "Should raise ValueError"
except ValueError:
pass

try:
calculate_bmi(70, -1.75)
assert False, "Should raise ValueError"
except ValueError:
pass

if __name__ == "__main__":
test_celsius_to_fahrenheit()
test_fahrenheit_to_celsius()
test_meters_to_feet()
test_calculate_bmi()
print("All tests passed!")

Problem 2: String Processing Functions

Tasks:

  1. count_vowels - count the number of vowels in a string
  2. reverse_words - reverse the order of words in a sentence
  3. is_palindrome - check if a string reads the same forwards and backwards
  4. extract_numbers - extract all numbers from a string and return as a list

Template:

def count_vowels(text: str) -> int:
"""write your docstring here"""
# write your implementation here
pass

def reverse_words(sentence: str) -> str:
"""write your docstring here"""
# write your implementation here
pass

def is_palindrome(text: str) -> bool:
"""write your docstring here"""
# write your implementation here
pass

def extract_numbers(text: str) -> list[str]:
"""write your docstring here"""
# write your implementation here
pass

def test_count_vowels():
assert count_vowels("hello") == 2
assert count_vowels("HELLO") == 2
assert count_vowels("aeiou") == 5
assert count_vowels("programming") == 3
assert count_vowels("") == 0
assert count_vowels("xyz") == 0
assert count_vowels("a") == 1

def test_reverse_words():
assert reverse_words("hello world") == "world hello"
assert reverse_words("the quick brown fox") == "fox brown quick the"
assert reverse_words("") == ""
assert reverse_words("single") == "single"

def test_is_palindrome():
assert is_palindrome("racecar") == True
assert is_palindrome("hello") == False
assert is_palindrome("") == True
assert is_palindrome("a") == True
assert is_palindrome("aa") == True
assert is_palindrome("ab") == False

def test_extract_numbers():
assert extract_numbers("I have 5 apples and 3 oranges") == ["5", "3"]
assert extract_numbers("The price is $29.99") == ["29.99"]
assert extract_numbers("No numbers here") == []
assert extract_numbers("123") == ["123"]
assert extract_numbers("3.14159") == ["3.14159"]

if __name__ == "__main__":
test_count_vowels()
test_reverse_words()
test_is_palindrome()
test_extract_numbers()
print("All tests passed!")