Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Python IMPORTANT NOTE: You may not use Python's built-in max, min, or sum functions. If helpful, you may use the sorted() function in Q2.

In Python

IMPORTANT NOTE: You may not use Python's built-in max, min, or sum functions. If helpful, you may use the sorted() function in Q2.

1. Write a function, q1(listOfXYs), that takes as input a list of two-element lists of numbers. Each two-element list represents a point [x,y] in 2D. No two points in listsOfXYs have the same x value. And the points are ordered by increasing x value so that if i < j, then listOfXYs[i][0] < listOfXYs[j][0]. If you plot the points on a graph, connecting consecutive points with line segments, the plot will contain zero or more "peaks", where each neighboring point has lower y, and zero or more "valleys", where each neighboring point has higher y. Note: the first and last points are neither peaks nor valleys. Your function must return a list of six values (in this order):

  • the number of peaks
  • the number of valleys
  • the y value of the lowest peak (or None if there are no peaks)
  • the y value of the highest valley (or None if there are no valleys)
  • the maximum distance (in x) between two neighboring peaks (or None if there are not at least two peaks)
  • the minimum distance (in x) between two neighboring valleys (or None if there are not at least two valleys)

For example:

>>> q1([[10,100], [20,200], [35,100], [45,200], [55,100], [60,250], [70,100]]) [3, 2, 200, 100, 25, 20] >>> q1([[10, 10], [20,10], [30,15]]) [0, 0, None, None, None, None] 

2. Write function q2(listOfLists, infoDict). listOfLists is a list of zero or more lists of zero or more numbers. infoDict is a dictionary with numbers as keys. The values associated with keys can be of any type. A number is considered "red" if the number is a key in the dictionary and has value "red". A number is considered "blue" if it is a key in the dictionary and has value "blue". Other numbers are considered "green". A sublist of listOfLists is considered "red" if contains more red items than blue ones and more red items than green ones. A sublist is considered "blue" if it contains more blue items than red ones and more blue items than green ones. Other sublists are considered "green". q2 returns a list such that item i is

  • the smallest element in listsOfLists[i] if listOfLists[i] is red,
  • the largest element in listsOfLists[i] if listOfLists[i] is blue,
  • the most common element in listsOfLists[i] if listOfLists[i] is green (if there is a tie for most common, take the smallest one, and use None if there are no elements)

For example

>>> q2([[2], [2,3,3], [1,2,3]], {1:"purple", 2:"red", 3:"blue", 25:"red"}) [2, 3, 1] 

3. Write functions testQ1() and testQ2() that test your Q1 and Q2 on a variety of inputs. Add one additional line in each of your test functions indicating whether or not you think your Q1 and Q2 functions are correct. For instance, you might add "print("I am confident my Q1 is correct.") in testQ1. Submit to ICON one python file containing all the required functions. The file must not contain any code that is not part of a function definition.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Iv Special Issue On Database Systems For Biomedical Applications Lncs 6990

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Christian Bohm ,Johann Eder ,Claudia Plant

2011th Edition

3642237398, 978-3642237393

More Books

Students also viewed these Databases questions

Question

4. Label problematic uses of language and their remedies

Answered: 1 week ago