# 5.1 Functions
Run to view results
# A. Write a function that accepts one parameter, name
# The function should print the message "Hello, _____." filling in the blank with the name
Run to view results
def anyone(name):
return f'Good morning, {name}.'
anyone("Alexander")
Run to view results
# B. Write a function that accepts one parameter, a number
# The function should return true or false depending on whether the number is even
Run to view results
def even(number):
return number % 2 ==0
print(even(15))
print(even(20))
Run to view results
# C. Write a function that accepts one parameter, a list
# The function should return the sum total of all numbers in the list, using a loop
Run to view results
def lista(numbers):
return sum(numbers)
lista([1,3,5,7,9,11])
Run to view results
# D. Write a function that accepts one parameter, a dictionary
# The function should print the message "Introducing ______ ______, ________!"
# Fill in the blanks with the "first_name", "last_name", and "title" properties of the dictionary
Run to view results
def diccionario(dict):
return print(f'Introduciendo {dict["first_name"]} {dict["last_name"]}, {dict["title"]}!')
diccionario({"first_name": "Alexander", "last_name": "Ramirez", "title":"Specialist"})
Run to view results
# 5.2 Problem-Solving Functions
Run to view results
# A. Find the longest string in a given list of strings
Run to view results
def longest_string(strings):
longest_len = 0
longest_word = ''
for i in strings:
if len(i) > longest_len:
longest_len = len(i)
longest_word = i
return longest_word
resoult = longest_string(['Iron man', 'Thor', 'Capitan America'])
print(f"the longest string word is: {resoult}")
Run to view results
# B. Determine whether a given list contains a given value
Run to view results
def a(lista, value):
for item in lista:
if item == value:
return True
return False
print(a(['Alexander','red'], 'red'))
print(a(['Alexander',3],3))
print(a(['Alexander',3],8))
Run to view results
# C. Count how many times a given string contains a given letter
Run to view results
def count_letter(string, letter):
counter = 0
for i in string:
if letter == i:
counter += 1
return counter
def count_letter1(string, letter):
return string.count(letter)
print(count_letter('The lord of the rings', 'r'))
print(count_letter1('The lord of the rings and the return of the king', 'r'))
Run to view results
# D. Find the most common letter in a given string, and use the previous function in your answer
Run to view results
name = "Daineris"
letter_count = {}
for i in name:
if i in letter_count.keys():
letter_count[i] += 1
else:
letter_count[i] = 1
max_count = 0
most_letters = ''
for letter in set(name):
if letter_count[letter] > max_count:
max_count = letter_count[letter]
most_letters = letter
print(f'most frequest letter in "{name}" is "{most_letters}"')
Run to view results
# E. Find the area of a triangle given its length and height as parameters
Run to view results
def area_triangle(l,h):
return 0.5*l*h
area_triangle(10,15)
Run to view results