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
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
ADVERTISEMENT