lab6-kgashok.py
Table of Contents
Lab 6: find the maximum of a list of numbers.
[TOC]
Problem statement
Implement a python program that finds the maximum in a list of numbers. The program must call a python function get_maxnumber
that takes as argument a list of numbers (maximum of 10 integers). It must then return the maximum number in that list.
Solution Key
CloudCoder Exercise
http://cloudcoder.kgisl.com/cloudcoder/#exercise?c=1,p=81
Pre-Lab Questions
If the list is a sorted list in ascending order, for e.g.
[1, 2, 3, 4, 5, 6]
, what is the maximum value? What is the python code that will access the maximum value in the list?What is the single line python code that will return the maximum value any list that is sorted in the ascending order?
If the list is a sorted list in descending order, for e.g.
[5, 4, 3, 2, -1]
, what is the maximum value? What is the python code that will access the maximum value in the list?What is the single line python code that will return the maximum value in any list that is sorted in the descending order?
What is the code for finding out the index of the minimum value of the element in the list. Assume the list contains only integers. Is there a one-line code for this?
Assume that the list is already sorted, but it is not known whether it is in ascending order or descending order. In that case, what is the single line python code that will return the maximum value in the list?
What are some of the methods that are available to the
list
python data type?What is the output of the following python code?
What is the output of the following python code? for elem in [ 1, 2, 3, 'abc', 99]: print (elem*2)
What is the code for printing the elements of a list in the reverse order, without actually modifying the list in any way?
What is the python code for outputting all the odd integers from 3 to 44?
A list contains
n
elements (wheren
is a positive integer and0 > n > 10
. Write the necessary python code to produce a list that contains only the lastn-1
elements. Is there a version of the code that does not use anylist
methods whatsoever to achieve the same result?Complete the Hackerrank problem - https://www.hackerrank.com/challenges/python-lists
Post-Lab Questions
Both the functions
get_maxnumber
andget_list_of_numbers
are not fruitful functions. True or False?How will you find the minimum of the values in a list of numbers? What change will you make to the code?
If the list is empty, what will be the value returned from the function?
If the list happens to be empty, then
get_maxnumber
function must return the string"N.A.".
. What changes will you make to the code? Sample Input: [] Sample Output: "N.A."For Point 2, refactor the code that you have written using a ternary operator.
Write the recursive version for finding the maximum value in a list.
How can you create a static variable inside a python function? Give an example.
Bonus 1
Suppose the user wanted the freedom not to be restricted to a maximum of 10 numbers, what change would you do to the code?
Bonus 2
How will you rewrite the function
get_maxnumber()
using the in-built functionmax
(https://docs.python.org/3/library/functions.html?highlight=max#max)?Can you reduce the function to a one-liner?
Under what conditions will the one-liner function fail? Can this failure be handled in a pythonic (aka graceful) fashion? Clue: There are at least conditions.
How will the code be modified so that both the
max_value
and themax_index
at which themax_value
occurs is returned?
Bonus 2.1
Assume a list contains only lists as its elements. Write a program to return the element (i.e. a list) in that that contains the maximum number of elements. Use the built-in max
to come up with the appropriate logic.
For e.g.,
Bonus 3
How will you find out the 2nd largest number in the list of numbers? Take into consideration that the list may contain duplicate numbers as well.
Bonus 4
Write a program that asks the user to input 10 integers, and then prints the largest odd number that was entered. If no odd number was entered, it should print a message to that effect.
Interview Grade
If n
is the size of the list, and 1 < k < n
, how will you find the k
th largest number in the list of numbers? (a #GTG challenge, a Google interview question).
Related Links
PythonTutor for visualizing the code related to list processing. https://goo.gl/v7ephq
Last updated