Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a function dot(vals1, vals2) that takes as inputs two lists of numbers, vals1 and vals2, and uses recursion to compute and return the dot

Write a function dot(vals1, vals2) that takes as inputs two lists of numbers, vals1 and vals2, and uses recursion to compute and return the dot product of those lists i.e., the sum of the products of the elements in the same positions in the two lists. For example, the dot product of [1, 2, 3] and [4, 5, 6] is 1*4 + 2*5 + 3*6 or 32. If the two lists do not have the same length or if either list is empty, dot should return 0.0. Here are some other examples: >>> dot([5, 3], [6, 4]) # get a decimal because the base case returns 0.0 result: 42.0 >>> dot([1, 2, 3, 4], [10, 100, 1000, 10000]) result: 43210.0 >>> dot([5, 3], [6]) result: 0.0 This function is somewhat similar to the the mylen function from lecture, but it needs to deal with lists instead of strings, and it needs to process two lists at the same time. You can find a modified version of mylen that handles both strings and lists here. Write a function add_spaces(s) that takes an arbitrary string s as input and uses recursion to form and return the string formed by adding a space between each pair of adjacent characters in the string. If the string has fewer than two characters, it should be returned without any changes. Here are three examples: >>> add_spaces('hello') result: 'h e l l o' >>> add_spaces('hangman') result: 'h a n g m a n' >>> add_spaces('x') result: 'x' This function is somewhat similar to the the replace function from lecture, because it needs to recursively create a new string from an existing string. However, your function will be simpler, because it wont need to decide what to do after the recursive call returns.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What is Larmors formula? Explain with a suitable example.

Answered: 1 week ago