Created
April 30, 2021 20:34
-
-
Save IvanFrecia/0bbb78a2636cc6841fe42ace517a2f60 to your computer and use it in GitHub Desktop.
Python Functions, Files, and Dictionaries - Week 4 - Assessment: More about Iteration - course_2_assessment_6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python Functions, Files, and Dictionaries - Week 4 - Assessment: More about Iteration - course_2_assessment_6 | |
# 1) Write a function, sublist, that takes in a list of numbers as the parameter. | |
# In the function, use a while loop to return a sublist of the input list. | |
# The sublist should contain the same values of the original list up until it reaches the number 5 ( | |
# it should not contain the number 5). | |
# Answer: | |
def sublist(lst): | |
accum = 0 | |
sub_list = [] | |
while accum < len(lst): | |
if lst[accum] == 5: | |
break | |
else: | |
sub_list.append(lst[accum]) | |
accum = accum + 1 | |
return(sub_list) | |
original_list = [9, 8, 1, 2, 7, 3, 5, 9, 8, 6, 2] | |
sublist(original_list) | |
print(original_list) | |
print(sublist(original_list)) | |
# 2) Write a function called check_nums that takes a list as its parameter, and contains a while loop that | |
# only stops once the element of the list is the number 7. What is returned is a list of all of the numbers | |
# up until it reaches 7. | |
# Answer: | |
def check_nums(lst): | |
accum = 0 | |
sub_list = [] | |
while accum < len(lst): | |
if lst[accum] == 7: | |
break | |
else: | |
sub_list.append(lst[accum]) | |
accum = accum + 1 | |
return(sub_list) | |
original_list = [9, 8, 1, 2, 7, 3, 5, 9, 8, 6, 2] | |
check_nums(original_list) | |
print(original_list) | |
print(check_nums(original_list)) | |
# 3) Write a function, sublist, that takes in a list of strings as the parameter. | |
# In the function, use a while loop to return a sublist of the input list. | |
# The sublist should contain the same values of the original list up until it reaches the string “STOP” | |
# (it should not contain the string “STOP”). | |
# Answer: | |
def sublist(string_list): | |
accum = 0 | |
sub_list = [] | |
while accum < len(string_list): | |
if string_list[accum] == "STOP": | |
break | |
else: | |
sub_list.append(string_list[accum]) | |
accum = accum + 1 | |
return sub_list | |
original_list = ['New', 'list', 'will', 'STOP', 'before', 'stop'] | |
sublist(original_list) | |
print(original_list) | |
print(sublist(original_list)) | |
# 4) Write a function called stop_at_z that iterates through a list of strings. | |
# Using a while loop, append each string to a new list until the string that appears is “z”. | |
# The function should return the new list. | |
# Answer: | |
def stop_at_z(lst): | |
accum = 0 | |
new_list = [] | |
while accum < len(lst): | |
if lst[accum] == 'z': | |
break | |
else: | |
new_list.append(lst[accum]) | |
accum = accum + 1 | |
return new_list | |
original_list = ['k', 'j', 'f', 's', 'k', 'j', 'b', 'k', 'j', 'z', 'q', 'k','j'] | |
stop_at_z(original_list) | |
print(original_list) | |
print(stop_at_z(original_list)) | |
# 5) Below is a for loop that works. Underneath the for loop, rewrite the problem so that it does the same | |
# thing, but using a while loop instead of a for loop. Assign the accumulated total in the while loop code | |
# to the variable sum2. Once complete, sum2 should equal sum1. | |
# Answer: | |
sum1 = 0 | |
sum2 = 0 | |
i = 0 | |
lst = [65, 78, 21, 33] | |
for x in lst: | |
sum1 = sum1 + x | |
while i < len(lst): | |
sum2 = sum2 + lst[i] | |
i = i + 1 | |
print(sum1) | |
print(sum2) | |
# Challenge: Write a function called beginning that takes a list as input and contains a while loop that | |
# only stops once the element of the list is the string ‘bye’. What is returned is a list that contains | |
# up to the first 10 strings, regardless of where the loop stops. (i.e., if it stops on the 32nd element, | |
# the first 10 are returned. If “bye” is the 5th element, the first 4 are returned.) | |
# If you want to make this even more of a challenge, do this without slicing | |
# Answer: | |
def beginning(lst): | |
accum = 0 | |
new_list = [] | |
while "bye" not in lst[accum] and accum < 10: | |
new_list.append(lst[accum]) | |
accum = accum + 1 | |
return new_list | |
original_list = ['New', 'list', 'will', 'now', 'bye', 'stoped', 'before', 'accumulating', '10', 'items'] | |
beginning(original_list) | |
print(original_list) | |
print(beginning(original_list)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment