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 sumsquaresvals 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:
sumsquares result: sumsquares result: sumsquares result: sumsquares result:
You solved a similar problem in Problem Set 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 sumsquaresvals: your docstring goes here total 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 doubles target that takes as input an arbitrary string s and and a singlecharacter string target, and that uses a loop to determine and return a new string in which all occurrences of target in s are doubledrepeated For example:
doubleprogramr result: 'prrogrram' doubleprogramo result: 'proogram' doublebananaa result: 'baanaanaa'
You solved this problem using recursion in Problem Set 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 doubles 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 loopbased removevowels in Mondays lecture.
Write a function processvals x that takes as input a list of 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 For example:
process result: process result: process result: process result:
You solved a similar problem in Problem Set 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 diffvals vals that takes as inputs two arbitrary lists of nonnegative integers vals and vals 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 result: :::python diff result: 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 result: diff result: diff result: diff result:
You solved a similar problem using recursion in Problem Set 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 indexbased loop so that you can access the corresponding elements from vals and vals at the same time. Here is a template that you can use to get started:
def diffvals vals: your docstring goes here result lenshorter minlenvals lenvals for i in rangelenshorter:
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 eg an ifelse 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
