-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinding_Greater_Values_in_List.py
More file actions
67 lines (49 loc) · 2.11 KB
/
Finding_Greater_Values_in_List.py
File metadata and controls
67 lines (49 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Write a Python Program to implement your own myreduce() function which works exactly like
# Python's built-in function reduce()
# reduce(function, sequence)
# Finding the greatest of values from the given list
# Import statement for reduce()
from functools import reduce
lst=[28,27,35,45,98,1098,987]
max_func=lambda a,b: a if (a>b) else b # lambda function
greatest_Value = reduce(max_func,lst) # using reduce function, passing the function and lst to
# find the greater values
print("The Greater value using reduce function is:",greatest_Value)
# Finding the greater value with out reduce()
# Method 1: (Using Sort())
lst=[28,27,35,45,98,1098,987]
lst = sorted(lst)
print("-------------Method 1 using sort()-------------------")
print(lst)
print("The Greater Value is: ", lst[-1])
# Method 2: (Using max())
lst=[28,27,35,45,98,1098,987]
print("-------------Method 2 using max()-------------------")
print("The Greater Value is: ", max(lst))
# Method 3: (Using for() and appending technique)
lst=[28,27,35,45,98,1098,987]
lst1=[]
for i in lst:
lst1.append(i)
print("-------------Method 3 using for loop and appending values-------------------")
print("The Greater Value is: ", max(lst1))
# Method 4: Greatest of Number using defining function
def greater_fn(lst):
# Assuming the first number in the list is max
max = lst[0]
for i in lst: # Iterating the values in the list
if i > max: # Comparing the itertaed value with the max value defined above
max = i # If iterated value is greater than max value, then assigning the iterated value to max
return max # returning the max value
lst=[28,27,35,45,98,1098,987]
print("-------------Method 4 using function()-------------------")
print("The Greater Value is:", greater_fn(lst))
# Method 5: Sorted and for loop
num =[8,27,35,45,98,1098,987]
greater=sorted(num)
sum=0
for i in greater:
if i>sum:
sum=i
print("-------------Method 5 using sorted and for loop-------------------")
print("The Greater Value is:",sum)