Note: This lab contains a collection of exercises to get more comfortable with writing functions. Exercises within the same question may not seem as related as exercises in previous labs.
Create a string called my_string
with value "addiscoder"
.
Print the length of my_string
.
Given the list of strings below, use a for
loop to print each string in the list.
my_strings = ["tibs", "kitfo", "shiro", "injera"]
# Your code here:
Use a for
loop to print the length of each string in my_strings
.
Create a function that takes a list of strings and prints the length of the longest string in the list.
def longest_string(lst):
# Your code goes here
longest_string(my_strings)
# answer: 6
Create a list called my_list
containing 5 integers.
Find the length of my_list
using len
.
Use modulo to check that 4 is even and print True
if it is.
Using a for
loop and range
, print every element in my_list
Write a function that takes a list of integers and prints the odd numbers in the list.
def odd_numbers(lst):
# Your code here
print(odd_numbers([1, 5, 23, 8, 9]))
# answer:
# 1
# 5
# 23
# 9
def primeNumbers(lst):
# Your code here
print(primeNumbers([1, 5, 23, 8, 9]))
Write a function that takes a list of integers and returns a list containing the elements in odd positions in the input list.
def odd_positions(lst):
# Your code here
print(odd_positions([1, 5, 23, 8, 9]))
# answer: [5, 8]
Write a function that takes a list of integers as input and returns the sum of the numbers in the list. You can use the skeleton below if you are stuck.
# Create a list called my_list that has 10 integer values.
# This is the function header for sum_list which takes in a list argument as lst.
def sum_list(lst):
# Create a loop that sums the elements in lst
sum_list(my_list)
Write a function that takes an integer x and returns the sum of all numbers from 0 - x (inclusive).
# This is the function header for sum_to_x which takes in an integer argument as x.
def sum_to_x(x):
# Your code goes here
# This will check your function against the correct sums for some example inputs
print(sum_to_x(10))
print(sum_to_x(4))
print(sum_to_x(100))
# answer: 55
# answer: 10
# answer: 5050
Write a function that takes two lists as input and combines them in alternating order.
Ex: [a,b,c], [1,2,3] → [a,1,b,2,c,3]
Note: The input lists will be the same length.
# This is the function header for combine_lists which takes in two lists and
# returns the combined lists in alternating order.
def combine_lists(lst1, lst2):
# Your code goes here
print(combine_lists(['a', 'b', 'c'], [1, 2, 3]))
print(combine_lists(['hello', 'pineapple',
'mango', 'strawberry'], [4, 7, 3, 5]))
# answer: [a, 1, b, 2, c, 3]
# answer: ['hello', 4, 'pineapple', 7, 'mango', 3, 'strawberry', 5]
def combine_lists(lst1, lst2):
# Your code here.
print(combine_lists(['a', 'b', 'c'], [1, 2, 3]))
Write a function that takes an integer x and returns a list of all multiples of 3 between 0 and x (inclusive).
# This is the function header for mults_of_3, which takes in an integer x
def mults_of_3(x):
# Your code goes here
mults_of_3(15)
# answer: [3, 6, 9, 12, 15]
Write a function that takes a list of integers and returns the largest number in the list.
# This is the function header for largest_in_list which takes in a list lst
def largest_in_list(lst):
# Your code here.
largest_in_list([1, 7, 2, 10])
# answer: 10
Write a function that takes a list of integers and returns True
if the list is strictly increasing and False
otherwise.
Note: A list is strictly increasing* if every number in the list is larger than the number before it.*
Ex: [1, 2, 3, 4] → True
Ex: [1, 2, 2, 3] → False
# This is the function header for is_increasing which takes in a list lst
def is_increasing(lst):
# Your code goes here
print(is_increasing([1, 2, 3, 4, 5]))
print(is_increasing([1, 3, 2, 5]))
# answer: True
# answer: False
Write a function that takes in a string x and returns True
if it is a palindrome and False
otherwise.
def is_num_palindrome_strings(x):
# Your code goes here
print(is_num_palindrome_strings('1331'))
print(is_num_palindrome_strings('40000'))
print(is_num_palindrome_strings('10'))
# answer: True
# answer: False
# answer: False
Write a function that takes in an integer x and returns True
if it is a palindrome and False
otherwise.
def is_num_palindrome(x):
# Your code goes here
print(is_num_palindrome(1331))
print(is_num_palindrome(40000))
print(is_num_palindrome(10))
# answer: True
# answer: False
# answer: False
Write a function that takes an integer x and prints a right triangle with height x.
Ex:
build_right_triangle(4) →
#
##
###
####
def build_right_triangle(x):
# Your code goes here
build_right_triangle(4)
Write a function that takes an integer x and prints an x by x checkerboard of hashes.
Ex:
build_checkerboard(4) →
# # # #
# # # #
# # # #
# # # #
def build_checkerboard(x):
# Your code goes here
build_checkerboard(4)
Write a function that takes an integer x and prints a pyramid with height x.
Ex:
build_pyramid(4) →
#
# #
# # #
# # # #
def build_pyramid(x):
# Your code goes here
build_pyramid(4)