In: Computer Science
Python Problem
Problem 1:
In this problem you are asked to write a Python program to find the greatest and smallest elements in the list.
The user gives the size of the list and its elements (positive and negative integers) as the input.
Sample Output:
Enter size of the list: 7
Enter element: 2
Enter element: 3
Enter element: 4
Enter element: 6
Enter element: 8
Enter element: 10
Enter element: 12
Greatest element in the list is: 12
Smallest element in the list is: 2
Hit enter to end program
------------------------------------------------------------------------------------------
Problem 2:
In this problem you are asked to write a Python program you are asked to perform multiple operations on a list.
What you need to do:
Ask the user what is the length of the list.
Ask the user to enter string values into a list.
Print the first 4 and last 4 elements in the list.
Ask the user if he/ she wants to insert elements to the list or delete the elements from the list.
Repeat step 4 until the user chooses a ‘NO’.
Sample Output:
Enter size of list: 5
Enter a string value: john Enter a string value: mary
Enter a string value: steve
Enter a string value: julia
Enter a string value: jake
The elements in the list:
['john', 'mary', 'steve', 'julia', 'jake']
The first four elements in the list: ['john', 'mary', 'steve', 'julia']
The last four elements in the list: ['mary', 'steve', 'julia', 'jake']
Want to insert (i) or delete (d) elements in the list: i Enter
the position to insert: 5
Enter the value to insert: lara
The modified list:
['john', 'mary', 'steve', 'julia', 'jake', 'lara']
Want to insert or delete more elements (y/n): y
Want to insert (i) or delete (d) elements in the list: i Enter
the position to insert: 2
Enter the value to insert: Jacob
The modified list:
['john', 'mary', 'jacob', 'steve', 'julia', 'jake', 'lara']
Want to insert or delete more elements (y/n): y
Want to insert (i) or delete (d) elements in the list: d Enter the index of the value to be deleted: 4
The modified list:
['john', 'mary', 'jacob', 'steve', 'jake', 'lara']
Want to insert or delete more elements (y/n): n
Hit enter to end program
# Problem 1 size = int(input('Enter size of the list: ')) smallest = largest = 0 for i in range(size): num = int(input('Enter element: ')) if i == 0: smallest = largest = num if smallest > num: smallest = num if largest < num: largest = num print('Greatest element in the list is:',largest) print('Smallest element in the list is:',smallest) input('Hit enter to end program')
# Problem 2 size = int(input('Enter size of list: ')) word_list = [] for i in range(size): word = input('Enter a string value:') word_list.append(word) print('The elements in the list:') print(word_list) print('The first elements in the list:', word_list[:4]) print('The last elements in the list:', word_list[-4:]) while True: choice = input('Want to insert (i) or delete (d) elements in the list: ') if choice=='i': pos=int(input('Enter the position to insert: ')) if 0<=pos and pos <len(word_list): word=input('Enter the value to insert: ') word_list.insert(pos,word) print('The modified list:') print(word_list) elif choice=='d': pos = int(input('Enter the index of the value to be deleted: ')) if 0 <= pos and pos < len(word_list): del word_list[pos] print('The modified list:') print(word_list) choice=input('Want to insert or delete more elements (y/n): ') if choice=='n': break input('Hit enter to end program')