InterviewSolution
| 1. |
Write a program to perform various list operations after displaying menu. |
|
Answer» ch = 0 list = [] while true : print “List Menu” print “1. Adding” print “2.Modify” print “3.Delete” print “4.Sort list” print “5.Display list” print “6.Exit” ch = int (raw_input (“Enter your choice :”)) if ch = = 1 : print “1.Add element” print “2.Add a List” ch1 = int (raw_input (“Enter choice 1 or 2:”)) if chi = = 1: item = int (raw_input (“Enter element:”)) pos = int (raw_input (“Enter position to add : “)) list.insert (pos, item) elif chi = = 2 : list2 = eval (raw_input (“Enter list:”)) list.extend (lst2) else: print “Valid choices are 1 or 2” print “Successfully added” elif ch = = 2 : pos = int (raw_input (“Enter position :”)) intem = int (raw_input (“Enter new value :”)) old = list[pos] list[.pos] = item print old, ‘modified with vlaue”, item elif ch = = 3: print “1.Delete element by position” print “2.Delete element by value” ch1 = int (raw_input (“Enter you choice 1 or 2’)) if chi = = 1: pos = int (raw_input (“Enter position :”)) item = list.pop (pos) print item, “deleted” elif ch1 = = 2: item = int (raw_input (“Enter element:”)) pos = list.remove (item) print “Succcessfully delted” else : print “Valid choices are 1 or 2” elif ch = = 4 : print “l.Ascending” print “2.Descending” chi = int (raw input (“Enter choice 1 or 2”)) if chi = = 1: list. sot () elif ch1 = = 2: list.sort (reverse = True) else : print “Valid choices are 1 or 2” elif ch = = 5: print list elif ch = = 6: break else : print “valid choice 1 to 6” |
|