Question

write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple


1 write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple. Hint (can be done in one pass, you are not allowed to use built-on min and max functions.)

max, min = find_max_min(my_list):

2 write a Python function that takes in a list of integers (elements), and an integer number (num). The functions should count and return number of integers in elements greater than, less than, and equal to num.

gt, lt, eq = find_gt_lt_eq(my_list, num = 3)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)Python code:

#defining find_max_min function
def find_max_min(my_list):
#setting max and min as the first element
max,min=my_list[0],my_list[0]
#looping each element in the list
for i in my_list:
#checking if it is the max element
if(i>max):
#setting max as i
max=i
#checking if it is the min element
if(i #setting min as i
min=i
#returning tuple of max and min element
return((max,min))
#initializing a sample list
my_list=[1,2,3,4,5]
#calling find_max_min function
max,min=find_max_min(my_list)
#printing max and min
print(max,min)


Screenshot:


Output:

2)Python code:

#defining find_gt_lt_eq function
def find_gt_lt_eq(my_list,num):
#initialzing gt,lt,eq as 0
gt,lt,eq=0,0,0
#looping each item in list
for i in my_list:
#checking if it is greater than num
if(i>num):
#incrementing gt
gt+=1
#checking if it is less than num
elif(i #incrementing lt
lt+=1
else:
#incrementing eq
eq+=1
#returning gt,lt and eq
return(gt,lt,eq)
#initialzing sample my_list
my_list=[1,2,3,4,5]
#calling find_gt_lt_eq and finding gt,lt,eq
gt,lt,eq=find_gt_lt_eq(my_list,num=3)
#printing gt,lt,eq
print(gt,lt,eq)


Screenshot:


Output:

Add a comment
Know the answer?
Add Answer to:
write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function maxList that takes in a list of integers as argument and returns their...

    Write a function maxList that takes in a list of integers as argument and returns their highest. You may not use the max() or the sorted() Python built-in functions.

  • in python A. Define a function called contains_only_integers() that takes a tuple, returns True if all...

    in python A. Define a function called contains_only_integers() that takes a tuple, returns True if all the items in the tuple are integers(2), and returns False otherwise. For example, contains_only_integers (3, 5, 17, 257, 65537) ), contains_only_integers( (-1,) ), and contains_only_integers( ) should all return True, but contains_only_integers (2.0,4.0)) and contains_only_integers (8, 4, "2", 1)) should both return false. Your function should use a while loop to do this calculation. 121 Hint: the is instance() built-in function provides the most...

  • Python homework Write a function called insert_value(my_list, value, insert_position) that takes a list, a value and...

    Python homework Write a function called insert_value(my_list, value, insert_position) that takes a list, a value and an insert_position as parameters. The function returns a copy of the list with the value inserted into the list (my_list) at the index specified by insert_position. Check for the insert_position value exceeding the list (my_list) bounds. if the insert_position is greater than the length of the list, insert the value at the end of the list. if the insert_position is less than or equal...

  • Use the FDR to design and write a function, maxValTimes, that takes a list of integers...

    Use the FDR to design and write a function, maxValTimes, that takes a list of integers and returns a set containing the value(s) that occur the same number of times as the maximum value in the list. If the list is empty, return an empty set.   For example if the list was [2,1,1,2,3,3,1] the function would return {2,3} as the maximum value is 3 which occurs twice, and 2 also occurs twice (but 1 occurs 3 times). For full marks,...

  • Write a Python function that creates and returns a list of prime numbers between 2 and...

    Write a Python function that creates and returns a list of prime numbers between 2 and N, inclusive, sorted in increasing order. A prime number is a number that is divisible only by 1 and itself. This function takes in an integer and returns a list of integers.

  • Haskell Function program Write a function negateOdds that takes a list of integers and returns a...

    Haskell Function program Write a function negateOdds that takes a list of integers and returns a list of integers with all of the odd integers negated. negateOdds :: [Integer] -> [Integer] example: [1,2,3,4,5] should return [-1,2,-3,4,-5]

  • Problem 1. Write a Python function times_i_at_odd(L) that takes as arguments a list L and returns...

    Problem 1. Write a Python function times_i_at_odd(L) that takes as arguments a list L and returns a list consisting of the elements of L multiplied by the index number of the element at odd positions. (Use list comprehensions) >>> times_i_at_odd([1,2,3,4,5,6,7,8,9,10]) [2, 12, 30, 56, 90] Problem 2. Write a recursive function sum_cols(grid, n) that takes a list of lists of integers grid and integer n and returns the sum of column n in grid. For example, the call sum_cols([[1,2,3,4], [10,20,30,40],...

  • In PYTHON: Write a function that receives a list of integers and returns the number of...

    In PYTHON: Write a function that receives a list of integers and returns the number of integers that are larger than the element immediately before them. For example, given [1,2,3,-5,0,-5,-10] the function should return 3 (since 1<2, 2<3, and -5<0).

  • PYTHON The function longest that returns the longest of two strings. The function count_over that takes...

    PYTHON The function longest that returns the longest of two strings. The function count_over that takes a list of numbers and an integer nand returns the count of the numbers that are over n. The function bmi that takes two parameters height and weight and returns the value of the body mass index: a person's weight in kg divided by the square of their height in meters. def longest(string1, string2): """Return the longest string from the two arguments, if the...

  • Python Write a move function that takes a list of tuples, where each tuple is an...

    Python Write a move function that takes a list of tuples, where each tuple is an x-y-z coordinate pair. This function should update the original list in-place to shift/move/translate the coordinates by an x_move, y_move, and z_move (respectively) and return None.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT