Skip to content

Instantly share code, notes, and snippets.

def url_encode(text):
"""
This function receives a string of words and returns that string with all the whitespace characters
converted to %20, except for leading and trailing whitespace.
"""
# Trim leading and trailing whitespace
trimmed_text = text.strip()
# Encode spaces using a loop
encoded_text = ''
def instructorWithLongestName(instructors):
"""
This function receives a list of instructor dictionaries and returns the one with the longest name.
"""
# Initialize the instructor with the longest name so far
longest_instructor = instructors[0]
# Iterate through the list of instructors to find the one with the longest name
for instructor in instructors:
if len(instructor["name"]) > len(longest_instructor["name"]):
def numberOfVowels(data):
"""
This function counts the number of vowels in a given string.
Vowels are considered as: a, e, i, o, and u.
"""
# Define the set of vowels
vowels = set("aeiou")
# Count the number of vowels in the string
count = sum(1 for char in data if char in vowels)
def conditionalSum(values, condition):
"""
This function calculates the sum of numbers in 'values' based on the 'condition'.
It sums up either even or odd numbers based on the 'condition' parameter.
"""
# Check for condition and sum accordingly
if condition == "even":
return sum(value for value in values if value % 2 == 0)
elif condition == "odd":
return sum(value for value in values if value % 2 != 0)
def sumLargestNumbers(numbers):
"""
This function receives a list of numbers and returns the sum of the two largest numbers in the list.
"""
sorted_numbers = sorted(numbers, reverse=True)
return sorted_numbers[0] + sorted_numbers[1]
print(sumLargestNumbers([1, 10])) # Expected output: 11
print(sumLargestNumbers([1, 2, 3])) # Expected output: 5