Programming Examples

Python Program to find the successor and predecessor of the largest element in list


Python Program to find the successor and predecessor of the largest element in list

Solution

mylist=eval(input("Enter any List : "))
m=max(mylist)
mi=mylist.index(m)
#print(m," -> ",mi)
if mi>0:
    print("Predecessor of Largest No. ",mylist[mi-1])
else:
    print("No Predecessor Exist")

if mi<len(mylist)-1:
    print("Successor of Largest No. ",mylist[mi+1])
else:
    print("No Successor Exist")
Output/ Explanation:


Enter any List : [1,2,3,5,2,1]

Predecessor of Largest No.  3

Successor of Largest No.  2

>>> 

======================

Enter any List : [1,2,3,4]

Predecessor of Largest No.  3

No Successor Exist

>>> 

======================

Enter any List : [4,3,2,1]

No Predecessor Exist

Successor of Largest No.  3

Latest Current Affairs 2024 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad