Section5.36Functions and Loops Mixed-Up Code Questions
Activity5.36.1.
Create a function called nums_x_to_y that takes in two integer parameter, x and y and uses a for loop to create and return a list with numbers x to y. Note: ignore cases when y is not larger than x. For example, nums_x_to_y(1,10) would return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].
def nums_x_to_y(x,y):
---
def nums_x_to_y: #paired
---
x_to_y_list = []
---
for num in range(x, y + 1):
---
for num in range(y): #paired
---
for num in range(x, y):
---
x_to_y_list.append(num)
---
return x_to_y_list
Create a function count_by_x_to_y that takes in two integer parameter, x and y and uses a for loop to create and return a list with numbers x to y. Note: ignore cases when y is not larger than x. Use a for loop to create and return a list with numbers up to y by skip counting by x. For example, count_by_x_to_y(5,20) should return [0, 5, 10, 15, 20].
def count_by_x_to_y(x,y):
---
def count_by_x_to_y: #paired
---
x_to_y_lst = []
---
for num in range (0, y + 1, x):
---
for num in range (0, y, x+1): #paired
---
x_to_y_lst.append(num)
---
x_to_y_lst += num #paired
---
return x_to_y_lst
Create a function called countdown that takes in one integer parameter seconds and creates a list of numbers that counts down from seconds to 1, and then returns that list. Note: seconds must be greater than or equal to 1. For example, countdown(3) would return [3, 2, 1].
Create a function add_odds_or_floor_division that takes in a parameter num_list and loops through the num_list. If the number in num_list is odd, it gets added to result. Otherwise, it gets divided from the result, having the result be the floor value. Then, return result. For example, add_odds_or_floor_division([-3, -5, -2, -9, 7.5, 10001, -5.3]) would return -1887.0.
def add_odds_or_floor_division(num_list):
---
def add_odds_or_floor_division(): #paired
---
result = 0
---
for num in num_list:
---
if num % 2 == 1:
---
if num / 2 == 1: #paired
---
result += num
---
else:
---
result //= num
---
result /= num #paired
---
return result
Create a function strings_chars_less_than_len that takes in a strings_list and returns a sorted new list with strings that are shorter in length than the original list. For example, strings_chars_less_than_len(['hello', 'bye', 'me', 'mississippi', 'miss']) would return [โbyeโ, โmeโ, โmissโ].
Create a function print_lists(nums1, nums2) that takes two lists of numbers with the same length, nums1 and nums2, and returns a list of strings in the form: โNum1: num1, Num2: num2โ for each pair of items in the two lists. For example, print_lists([3, 2], [8, 4]) would return ["Num1: 3, Num2: 8", "Num1: 2, Num2: 4"].
Create a function sum_lists(nums1, nums2) that takes two lists of numbers with the same length, nums1 and nums2, and returns a list of the totals of the two numbers for each pair of items in the two lists. For example, sum_lists([3, 2], [8, 4]) would return [11, 6].
def sum_lists(nums1, nums2):
---
out_list = []
---
for i in range(len(nums1)):
---
for i in range(nums1): #paired
---
for val1 in nums1:
for val2 in nums2:
---
val1 = nums1[i]
val2 = nums2[i]
---
total = val1 + val2
---
out_list.append(total)
---
out_list += total #paired
---
return out_list