Practice recursion on lists Practice multiple base conditions Combine recursion call with and,or not operators Search for an element in a list using recursion Instructions In this lab, we will write a recursive function for searching a list of integers/strings. Write a recursive function to determine if a list contains an element. Name the function recursive_search(aList, value), where aList is a list and value if primitive type is the object we want to search for. Return a boolean, specifically True if and only if value is an element of aList else return False from the function Examples: recursive_search([1,2,3], 2) == True recursive_search([1,2,3], 4) == False recursive_search([ ], 4) == False recursive_search([ [ 1 ], 2 ], 1) == False # -----> (because the list contains [ 1 ] and 2, not 1) Hint: Think about what the base case is? When is it obvious that the element is not in the list? If we are not at the base case, how can you use the information about the first element of the list to possibly see if the "value" exists in the list? Call the function recursively on a list of the last n-1 elements, essentially asking "does the element exist somewhere down the list"? Use that information when returning a value (True/False). For on a list of size n, make the recursive call on the last n-1 elements, also called the tail of the list.

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...
icon
Related questions
Question
  1. Practice recursion on lists
  2. Practice multiple base conditions
  3. Combine recursion call with and,or not operators
  4. Search for an element in a list using recursion

Instructions

In this lab, we will write a recursive function for searching a list of integers/strings.

Write a recursive function to determine if a list contains an element.

Name the function recursive_search(aList, value), where aList is a list and value if primitive type is the object we want to search for.

Return a boolean, specifically True if and only if value is an element of aList else return False from the function

Examples:

recursive_search([1,2,3], 2) == True recursive_search([1,2,3], 4) == False recursive_search([ ], 4) == False recursive_search([ [ 1 ], 2 ], 1) == False # -----> (because the list contains [ 1 ] and 2, not 1)

Hint:

Think about what the base case is? When is it obvious that the element is not in the list? If we are not at the base case, how can you use the information about the first element of the list to possibly see if the "value" exists in the list?

Call the function recursively on a list of the last n-1 elements, essentially asking "does the element exist somewhere down the list"? Use that information when returning a value (True/False).

For on a list of size n, make the recursive call on the last n-1 elements, also called the tail of the list.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
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 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)
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
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY