Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3. Write a recursive function q3(item1, item2) that returns True or False depending on whether or not the two given input items are similar according

3. Write a recursive function q3(item1, item2) that returns True or False depending on whether or not the two given input items are "similar" according to the following definition. Two items are similar if:
  1. they are the same type or both are numbers (i.e. int or float type), and
  2. if they are lists, they are of the same length and each pair of corresponding list items (i.e. items with the same index) is similar.

For example,

>>> q3(True, False) items are same type and are not lists True >>> q3(1, 'a') items are different types False >>> q3[[],[]) True >>> q3([],[3]) list lengths differ False >>> q3(['c'],[3]) lists of same length but index 0 items are not similar False >>> q3([5.0],[3]) lists of same length and corresponding lists items are similar True >>> q3([1,2,['a','b']],[3,4, [1,2,3]]) items at index 2 are not similar False >>> q3([1,2,[False, 'b']],[3, 4, [True, 'hello']]) True >>> q3([[[[],[2],[],['hi', [0]]]]], [[[[],[-2],[],['bye', [1]]]]]) True >>> q3([[[[],[2],[],['hi', [0]]]]], [[[[],[-2],[],['bye', 0]]]]) False 

HINT: although this function must be recursive, it may also contain a loop. One approach loops over the items of the lists, making recursive calls to test similarity of corresponding items.image text in transcribed

3. Write a recursive function q3(iteml, item2) that returns True or False depending on whether or not the two given input items are "similar" according to the following definition. Two items are similar if: 1. they are the same type or both are numbers (i.e. int or float type), and 2. if they are lists, they are of the same length and each pair of corresponding list items (i.e. items with the same index) is similar. For example, >>> 93(True, False) items are same type and are not lists True >>> 93(1, 'a') items are different types False >>> 93[[],[]) True >>> 93([],[3]) list lengths differ False >>> 93(['c'],[3]) lists of same length but index 0 items are not similar False >>> 93([5.0],[3]) lists of same length and corresponding lists items are similar True >>> 43([1,2,['a','b']],[3,4, [1,2,3]]) items at index 2 are not similar False >>> 93([1,2, [False, 'b']],[3, 4, [True, 'hello']]) True >>> 93([[[[],[2],[],['hi', [0]]]]], [[[[],[-2],[],['bye', [1]]]]]) True >>> 93([[[[],[2],[],['hi', [0]]]]], [[[[],[-2],[],['bye', o]]]]) False

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