Q1-Making Lists (15 points) Below are the results of a hypothetical experiment of measuring the height of a class in a l
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Q1-Making Lists (15 points) Below are the results of a hypothetical experiment of measuring the height of a class in a l
Q2-Lists of Lists (20 points) You can also make lists that are filled with lists! List-ception. First, create three different lists: . A list called . A list called . A list called boolean list that contains three boolean (can be any booleans) Then, create a new list, called nested list which contains the three lists you created above (in the order specified in the above bullet points). string list that contains three strings (can be any strings) number_list that contains three numbers (can be any numbers - Int or float) In [ ]: # YOUR CODE HERE raise NotImplementedError() In 1: assert isinstance(string list, list). assert len(string list) 3 assert isinstance(string_list[0], str) In 1: assert isinstance(number_list, list) assert len(number_list) 3 assert isinstance(number_list[0], int) or isinstance(number_list[e], float) In [ ]: assert isinstance(boolean_list, list) assert len(boolean_list) #3 assert isinstance(boolean_list[0], bool) In 1: assert isinstance(nested list, list) assert len(nested list) 3 assert isinstance(nested list[e], list)
Part 2: Indexing This part covers indexing lists. In [ ] # For Q3 & Q4, the following lists are provided for you list 1 = [10, 20, 30, 40] list_2 = [13, 15, 17, 19, 21, 23] list 3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] list_4 = [21, 9, 98, 289, 438]
Q3 - Indexing (20 points) Do the following using indexing: . Store the first value in list 1 to index 1 . Store the second value from the end of list 2 to index 2 . Store the first four values of list 3 to index_3 Store the last four values of list 4 to index 4 Note: Values must be stored in the same order they appear in the original list in your output. . In [ ]: # YOUR CODE HERE raise NotImplementedError() In [ ]: assert isinstance(index_1, int) assert index 1 == 10 In [ ]: assert isinstance(index_2, int) assert index_2 == 21 In [ ]: assert isinstance(index_3, list) assert len(index_3) ==4 assert index 3 == [1, 2, 3, 4]
In [ ]: assert isinstance(index_4, list) assert len(index_4) ==4 assert index 4 == [9, 98, 289, 438]
Q4 - Comparisons using Indexing (20 points) Do the following comparisons using indexing: . Check if the second value of list_1 is one of the last three values in list 3 (Hint: remember operator in) . Store the result in comp_result 1 . Check if the third value of list 4 is greater than the fourth value in list_4 . Store the result in comp_result 2 . Check if the last value of list 4 is less than the second value in list_4 . Store the result in comp_result 3 . Check if the second value of list 2 multiplied by the fifth value of list 3 is less than the second to last value in list_4 . Store the result in comp_result 4 Keep in mind that you are storing the result of a comparison to a variable, so resulting variables should all be booleans. In [ ]: # YOUR CODE HERE raise NotImplementedError() In 1: assert isinstance(comp_result_1, bool) assert comp_result 1 False In [ ]: assert isinstance(comp_result 2, bool) assert comp_result 2 == False In [ ]: assert isinstance(comp_result_3, bool) assert comp_result_3= False
In [ ]: assert isinstance (comp_result_4, bool) assert comp_result_4 == True
Part 3: Loops This part covers using loops. Q5-For Loop (5 points) What is the sum of all odd numbers between 30 and 51 (inclusive)? Answer this question using a for loop, and using range. Save the result out to a variable called sum_result. In [] # YOUR CODE HERE raise NotImplementedError() In [ ]: assert isinstance (sum_result, int) assert sum_result == 451
Q6Loops: Multiplication (5 points) Using the provided variable num list, write a for loop to loop across each value, multiplying it by -1. Store the result for each value in a list called eval 1 by defining eval 1 before the loop and using append inside the loop. In [1: # This creates a list of all numbers from 1-12, inclusive num_list = list (range(1, 13)) In [ ]: # YOUR CODE HERE raise NotImplementedError() In [ ]: assert isinstance(eval 1, list). assert isinstance (eval_1[0], int) assert eval_1 == list (reversed (range(-12, 0)))
Part 4: Combining Loops & Conditionals This part covers integrating collections and loops with conditionals and operators. In [ ]: # This List provided to use for the following questions data_list = [-1, 20, -100, -44, 32, 97, -101, 45, -79, 96] Q7 - Loops: Division (5 points) Write some code that uses a loop to find all the values from data list that are perfectly divisible by 4. Use a for loop with a conditional to do this, and save any values from data list that meet this condition into a new list called div 4. In [ ]: # YOUR CODE HERE raise NotImplementedError() div_4 In [ ]: assert isinstance(div 4, list) assert (len (div_4) ==5) assert div 4 == [20, -100, -44, 32, 96]
Q8 - Loops: Odd or Even (5 points) Write a for loop that is going to check whether the values in data list are even or odd. Use a for loop with a conditional to do this. For each value, it should add True to a new list is even if the value is even, and False to is even if the value is odd. In [ ]: # YOUR CODE HERE raise NotImplementedError() is even In ]: assert isinstance(is_even, list) assert (len(is_even) == len(data list)) assert sum(is_even) == 5
Q9-Loops: Multiple Conditions (5 points) Write a for loop that will check each value in data list to see if they are even or odd and whether they are positive or negative. . If they are both positive and even, encode this as the number 1 . If they are both negative and odd, encode this as the number -1 Otherwise, encode this as the number 0 . Store the result for each value into a list called num_type . In []: # YOUR CODE HERE raise NotImplementedError() In []: assert isinstance(num_type, list). assert(len(num_type) e len(data_list)) assert sum([1 for it in nun type if it == 1 ]) 3 assert sum([1 for it in num type if it -1]) == 3 assert sum([1 for it in num type if it == 0 ]) == 4