Question: Write a function sum _ squares ( vals ) that takes as input a list of numbers vals and uses a loop to compute and

Write a function sum_squares(vals) that takes as input a list of numbers vals and uses a loop to compute and return the sum of the squares of the individual numbers in the list. For example:
>>> sum_squares([2,5,3]) result: 38>>> sum_squares([1,2,3,4]) result: 30>>> sum_squares([6,8]) result: 100>>> sum_squares([]) result: 0
You solved a similar problem in Problem Set 2 using recursion, but for this problem set you must use a loop. You may not use recursion or a list comprehension.
Here is a template that you can use to get started:
def sum_squares(vals): """ your docstring goes here """ total =0 for ____ in ______: ...
Hint: This function should perform a cumulative computation that gradually builds up the sum of the squares of the numbers.
Write a function double(s, target) that takes as input an arbitrary string s and and a single-character string target, and that uses a loop to determine and return a new string in which all occurrences of target in s are doubled/repeated. For example:
>>> double('program','r') result: 'prrogrram' >>> double('program','o') result: 'proogram' >>> double('banana','a') result: 'baanaanaa'
You solved this problem using recursion in Problem Set 2, but for this problem set you must use a loop. You may not use recursion or a list comprehension.
Heres a template that you can use to get started:
def double(s, target): """ your docstring goes here """ result ='' for ____ in ______: ...
Hint: This function should perform a cumulative computation that gradually builds up a string. We discussed a similar function (a loop-based remove_vowels) in Mondays lecture.
Write a function process(vals, x) that takes as input a list of 0 or more integers vals and a single integer x, and that uses a loop to create and return a new list in which each element of the original list that is larger than x is replaced with a 0. For example:
>>> process([5,8,3,12],6) result: [5,0,3,0]>>> process([5,8,3,12],4) result: [0,0,3,0]>>> process([5,8,3,12],15) result: [5,8,3,12]>>> process([],2) result: []
You solved a similar problem in Problem Set 2 using recursion, but for this problem set you must use a loop. You may not use recursion or a list comprehension.
Hint: This function should perform a cumulative computation that gradually builds up a list. Well write a similar function in this weeks lab.
Write a function diff(vals1, vals2) that takes as inputs two arbitrary lists of non-negative integers vals1 and vals2 and uses a loop to construct and return a new list in which each element is the the absolute value of the difference of the corresponding elements from the original lists. For example:
>>> diff([3,4,2,5],[7,2,9,5]) result: [4,2,7,0] :::python >>> diff([5,6,2],[1,9,15]) result: [4,3,13]>>> diff([],[]) result: []
If one of the lists is longer than the other, its extra elements the ones with no counterparts in the shorter list should appear immediately after the differences (if any) in the returned list. For example:
>>> diff([0,3,6,9],[1,1]) result: [1,2,6,9]>>> diff([3,1,9],[2,4,5,7]) result: [1,3,4,7])>>> diff([1,2],[]) result: [1,2]>>> diff([],[3,4,5]) result: [3,4,5]
You solved a similar problem using recursion in Problem Set 2, but for this problem set you must use a loop. You may not use recursion or a list comprehension.
Hints:
You will need to use an index-based loop so that you can access the corresponding elements from vals1 and vals2 at the same time. Here is a template that you can use to get started:
def diff(vals1, vals2): """ your docstring goes here """ result =[] len_shorter = min(len(vals1), len(vals2)) for i in range(len_shorter): ...
Note that we determine the length of the shorter list before beginning the loop, because the loop should only consider the index values that are present in both lists.
After the loop, you will need to handle any extra numbers from the longer list (for cases in which the lists dont have the same length). One way to do that is to use conditional execution (e.g., an if-else statement), although other approaches may also be possible.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!