Python's dynamic typing allows for lists and tuples which are nested to an unknown depth. For this problem you will be working with data structures of varying depth using recursion.

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

In python

Python's dynamic typing allows for lists and tuples which
are nested to an unknown depth. For this problem you
will be working with data structures of varying depth
using recursion.
Write a function collect_words which will accept a
single argument collection. collection will be of
type str, int, float, list, or tuple. The function should
return a list of all of the strings contained within the
collection. This means that:
• If collection is an int or float, an empty list
should be returned.
•
If collection is a string, a list with a single
element should be returned.
•
If collection is a tuple or a list, a list should
be returned with all of the strings contained within
any level of collection regardless of nesting
depth.
Hints:
You can check the type of a variable using the
isinstance function or the type function.
O For example, [6, 3, 2] is a list, so
isinstance([6, 3, 2], list)
would return True, but
isinstance ([6, 3, 2], int) would
return false.
The type function will return the type of an
object, so type ([6, 3, 2]) will return
the class list, and type ([6, 3, 2])
list will return True
O
==
● Not only must you iterate through nested data
structures, you must also iterate through elements
in a list or tuple.
• Tuples can be indexed or sliced just like lists can -
you just can't use mutator methods like .append
on them.
. Both tuple and lists are sequence type, and the
documentation for them can be found here
Constraints:
You must use recursion in collect_words
● You may not use a loop of any kind to solve this
problem
Transcribed Image Text:Python's dynamic typing allows for lists and tuples which are nested to an unknown depth. For this problem you will be working with data structures of varying depth using recursion. Write a function collect_words which will accept a single argument collection. collection will be of type str, int, float, list, or tuple. The function should return a list of all of the strings contained within the collection. This means that: • If collection is an int or float, an empty list should be returned. • If collection is a string, a list with a single element should be returned. • If collection is a tuple or a list, a list should be returned with all of the strings contained within any level of collection regardless of nesting depth. Hints: You can check the type of a variable using the isinstance function or the type function. O For example, [6, 3, 2] is a list, so isinstance([6, 3, 2], list) would return True, but isinstance ([6, 3, 2], int) would return false. The type function will return the type of an object, so type ([6, 3, 2]) will return the class list, and type ([6, 3, 2]) list will return True O == ● Not only must you iterate through nested data structures, you must also iterate through elements in a list or tuple. • Tuples can be indexed or sliced just like lists can - you just can't use mutator methods like .append on them. . Both tuple and lists are sequence type, and the documentation for them can be found here Constraints: You must use recursion in collect_words ● You may not use a loop of any kind to solve this problem
Examples:
>>> collect_words (5)
[]
>>> collect_words(5.5)
[]
>>> collect_words("Hello")
['Hello']
>>> collect_words(["Hello", "Zoe", "how",
"are", "you"])
['Hello', 'Zoe', 'how', 'are', 'you']
>>> collect_words(["Hello", ("Alice",
["how"]), [(["are"])], "you"])
['Hello', 'Alice', 'how', 'are', 'you']
Transcribed Image Text:Examples: >>> collect_words (5) [] >>> collect_words(5.5) [] >>> collect_words("Hello") ['Hello'] >>> collect_words(["Hello", "Zoe", "how", "are", "you"]) ['Hello', 'Zoe', 'how', 'are', 'you'] >>> collect_words(["Hello", ("Alice", ["how"]), [(["are"])], "you"]) ['Hello', 'Alice', 'how', 'are', 'you']
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 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