Description: Write a function that takes two numbers as arguments and returns their sum.
Input: Two integers, num1
and num2
.
Output: The sum of num1
and num2
.
# Example Usage:
result = add_numbers(5, 7)
print(result) # Output: 12
# Solution Code:
def add_numbers(num1, num2):
return num1 + num2
Description: Create a function that receives a list of numbers and returns the largest number in the list.
Input: A list of numbers, numbers_list
.
Output: The largest number in numbers_list
.
# Example Usage:
result = find_maximum([3, 1, 4, 7, 2])
print(result) # Output: 7
# Solution Code:
def find_maximum(numbers_list):
return max(numbers_list)
Description: Implement a function that calculates the area of a circle given its radius.
Input: A single float or integer, radius
.
Output: The area of the circle.
# Example Usage:
area = circle_area(5)
print(area) # Output: 78.54
# Solution Code:
def circle_area(radius):
return 3.14159 * radius * radius
Description: Write a function that takes a string and returns the number of vowels in the string.
Input: A string, input_string
.
Output: The number of vowels in input_string
.
# Example Usage:
vowel_count = count_vowels("hello world")
print(vowel_count) # Output: 3
# Solution Code:
def count_vowels(input_string):
vowels = 'aeiou'
return sum(1 for char in input_string if char in vowels)
Description: Create a function that takes a string and returns the string in reversed order.
Input: A string, input_string
.
Output: input_string
reversed.
# Example Usage:
reversed_string = reverse_string("hello")
print(reversed_string) # Output: "olleh"
# Solution Code:
def reverse_string(input_string):
return input_string[::-1]
Description: Write a function that checks whether a given string is a palindrome (reads the same backward as forward).
Input: A string, input_string
.
Output: True
if input_string
is a palindrome, otherwise False
.
# Example Usage:
is_palindrome = check_palindrome("radar")
print(is_palindrome) # Output: True
# Solution Code:
def check_palindrome(input_string):
return input_string == input_string[::-1]
Description: Implement a function that returns a list of even numbers up to a given number.
Input: An integer, limit
.
Output: A list of even numbers from 0 up to limit
.
# Example Usage:
even_numbers = generate_even_numbers(10)
print(even_numbers) # Output: [0, 2, 4, 6, 8, 10]
# Solution Code:
def generate_even_numbers(limit):
return [num for num in range(limit + 1) if num % 2 == 0]
Description: Create a function that calculates the factorial of a given number.
Input: An integer, number
.
Output: The factorial of number
.
# Example Usage:
factorial_result = factorial(5)
print(factorial_result) # Output: 120
# Solution Code:
def factorial(number):
if number == 0:
return 1
else:
return number * factorial(number - 1)
Description: Write a function that converts a temperature from Celsius to Fahrenheit.
Input: A float or integer, celsius
.
Output: The temperature in Fahrenheit.
# Example Usage:
fahrenheit = celsius_to_fahrenheit(0)
print(fahrenheit) # Output: 32
# Solution Code:
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
Description: Implement a function that returns
a list of all prime numbers up to a given number.
Input: An integer, limit
.
Output: A list of prime numbers up to limit
.
# Example Usage:
primes = find_primes(10)
print(primes) # Output: [2, 3, 5, 7]
# Solution Code:
def find_primes(limit):
primes = []
for num in range(2, limit + 1):
for i in range(2, num):
if (num % i) == 0:
break
else:
primes.append(num)
return primes
Description: Write a function that merges two lists into one list, alternating elements from each list. If one list is longer, append the remaining elements at the end.
Input: Two lists, list1
and list2
.
Output: A merged list.
# Example Usage:
merged_list = merge_lists([1, 3, 5], [2, 4])
print(merged_list) # Output: [1, 2, 3, 4, 5]
# Solution Code:
def merge_lists(list1, list2):
merged = []
for i in range(max(len(list1), len(list2))):
if i < len(list1):
merged.append(list1[i])
if i < len(list2):
merged.append(list2[i])
return merged
Description: Implement a function that returns the Fibonacci sequence up to the n
-th term.
Input: An integer, n
.
Output: A list representing the Fibonacci sequence up to the n
-th term.
# Example Usage:
fib_sequence = fibonacci(5)
print(fib_sequence) # Output: [0, 1, 1, 2, 3]
# Solution Code:
def fibonacci(n):
fib = [0, 1]
for i in range(2, n):
fib.append(fib[i-1] + fib[i-2])
return fib[:n]
Description: Create a function that removes duplicate values from a list.
Input: A list, input_list
.
Output: A list with duplicates removed.
# Example Usage:
unique_list = remove_duplicates([1, 2, 2, 3, 3, 3])
print(unique_list) # Output: [1, 2, 3]
# Solution Code:
def remove_duplicates(input_list):
return list(set(input_list))
Description: Write a function that finds the second largest number in a list.
Input: A list of numbers, numbers_list
.
Output: The second largest number in numbers_list
.
# Example Usage:
second_largest = find_second_largest([10, 40, 30, 20, 50])
print(second_largest) # Output: 40
# Solution Code:
def find_second_largest(numbers_list):
unique_numbers = list(set(numbers_list))
unique_numbers.sort()
return unique_numbers[-2]
Description: Implement a function that converts hours into seconds.
Input: A number, hours
.
Output: The equivalent number of seconds.
# Example Usage:
seconds = hours_to_seconds(2)
print(seconds) # Output: 7200
# Solution Code:
def hours_to_seconds(hours):
return hours * 3600
Description: Create a function that takes a string containing numbers and letters and returns the sum of the numbers in the string.
Input: A string, input_string
.
Output: The sum of the numbers in input_string
.
# Example Usage:
total = sum_of_numbers("1abc23")
print(total) # Output: 24
# Solution Code:
import re
def sum_of_numbers(input_string):
numbers = re.findall(r'\d+', input_string)
return sum(map(int, numbers))
Description: Write a function that checks if a number is even or odd.
Input: An integer, num
.
Output: The string "Even"
if the number is even, "Odd"
if the number is odd.
# Example Usage:
result = even_or_odd(7)
print(result) # Output: "Odd"
# Solution Code:
def even_or_odd(num):
return "Even" if num