ce you to use recursion, you may not use the in operator. Rather, you should use recur ss s one character at a time and determine if c is contained in s. kample: contains('hello', 'e') ult: True contains('hello', 'l') ult: True
ce you to use recursion, you may not use the in operator. Rather, you should use recur ss s one character at a time and determine if c is contained in s. kample: contains('hello', 'e') ult: True contains('hello', 'l') ult: True
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question

Transcribed Image Text:2. Write a function contains(s, c) that takes an arbitrary string s and a single-character c as inputs and
uses recursion to determine if s contains the character c, returning True if it does and False if it does
not.
To force you to use recursion, you may not use the in operator. Rather, you should use recursion to
process s one character at a time and determine if c is contained in s.
For example:
>> contains('hello', 'e')
result: True
>>> contains('hello', 'l')
result: True
>> contains('hello', 'x')
result: False
>> contains('', 'x')
result: False
Warning
Your function should return a boolean value - either True or False, without any quotes around them.
If you see quotes around the result when you make the calls above from the console, you must fix your
function to remove the quotes.
Hint: You will need two base cases:
a. one base case for the smallest possible input for the parameter s
b. one base case for when the first character in the string is the one you are looking for; this case is a base
case because you don't need to reduce the problem any further in order to know what value to return.
You must check for these base cases in the order given above. Otherwise, you can end up with an error!
![1. Write a function add(vals1, vals2) that takes
inputs two lists of numbers, vals1 and vals2, and that
uses recursion to construct and return a new list in which each element is the sum of the elements at the
corresponding positions in the original lists. For example:
>>> add([1, 2, 3], [4, 5, 6])
result: [5, 7, 9]
Note that the elements in the result are obtained by adding the corresponding elements from the original
lists (1+4, 2+5, and 3+6).
Special cases: If the two lists do not have the same length or if either list is empty, the function should
return an empty list ([]).
Here are some other examples:
>>> add([5, 3], [6, 4])
result: [11, 7]
>> add([1, 2, 3, 4], [20, 30, 50, 80])
result: [21, 32, 53, 84]
# lists with different lengths
>>> add([5, 3], [6])
result: []
This function is somewhat similar to the replace function from lecture. That function recursively creates a
new string from an existing string, whereas this function will recursively create a new list from two existing
lists.
Important: In the recursive case, when you use concatenation to build a larger list, make sure that both of
the operands are lists. For example, if x is an integer and vals is a list, the following will produce an error:
X + vals
# error: cannot add an integer to a list
Instead, you would need to turn x into a list before adding it:
[x] + vals
# correct!](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F0a85d49e-8c8b-4552-a96b-b47a5fbf7366%2F5fbe36a0-fc16-4e27-a904-680df7b2d22c%2Fkn4cox5_processed.jpeg&w=3840&q=75)
Transcribed Image Text:1. Write a function add(vals1, vals2) that takes
inputs two lists of numbers, vals1 and vals2, and that
uses recursion to construct and return a new list in which each element is the sum of the elements at the
corresponding positions in the original lists. For example:
>>> add([1, 2, 3], [4, 5, 6])
result: [5, 7, 9]
Note that the elements in the result are obtained by adding the corresponding elements from the original
lists (1+4, 2+5, and 3+6).
Special cases: If the two lists do not have the same length or if either list is empty, the function should
return an empty list ([]).
Here are some other examples:
>>> add([5, 3], [6, 4])
result: [11, 7]
>> add([1, 2, 3, 4], [20, 30, 50, 80])
result: [21, 32, 53, 84]
# lists with different lengths
>>> add([5, 3], [6])
result: []
This function is somewhat similar to the replace function from lecture. That function recursively creates a
new string from an existing string, whereas this function will recursively create a new list from two existing
lists.
Important: In the recursive case, when you use concatenation to build a larger list, make sure that both of
the operands are lists. For example, if x is an integer and vals is a list, the following will produce an error:
X + vals
# error: cannot add an integer to a list
Instead, you would need to turn x into a list before adding it:
[x] + vals
# correct!
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images

Recommended textbooks for you

Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON

Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science

Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning

Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON

Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science

Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning

Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning

Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education

Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY