Edil 92 VIW maons Run C Code Q4 - Comparisons using Indexing (20 points) Do the following comparisons using indexing: ↑
Posted: Tue Jul 12, 2022 8:10 am
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 []: # 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 [1: # YOUR CODE HERE raise NotImplementedError() div 4 In 1: 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 1: assert isinstance(num_type, list) assert (len(num_type) == len(data list)) assert sum([1 for it in num_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