Question

In: Computer Science

Python Problem Problem 1: In this problem you are asked to write a Python program to...

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:

  1. Ask the user what is the length of the list.

  2. Ask the user to enter string values into a list.

  3. Print the first 4 and last 4 elements in the list.

  4. Ask the user if he/ she wants to insert elements to the list or delete the elements from the list.

  5. 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

Solutions

Expert Solution

# 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')


Related Solutions

For Python: In this assignment you are asked to write a Python program to determine the...
For Python: In this assignment you are asked to write a Python program to determine the Academic Standing of a studentbased on their CGPA. The program should do the following: Prompt the user to enter his name. Prompt the user to enter his major. Prompt the user to enter grades for 3 subjects (A, B, C, D, F). Calculate the CGPA of the student. To calculate CGPA use the formula: CGPA = (quality points * credit hours) / credit hours...
Coding Problem 1: In this program, you are asked to write a program in assembly (MIPS)...
Coding Problem 1: In this program, you are asked to write a program in assembly (MIPS) which works as a simple calculator. The program will get two integer numbers, and based on the requested operation, the result should be shown to the user. a. The program should print a meaningful phrase for each input, and the result. i. “Enter the first number” ii. “Enter the second number” iii. “Enter the operation type” iv. “The result is” b. The user should...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement all functions in a module called problem1.py. (10 points) Write a recursive function called remove char with two parameters: a string astr and a character ch. The function returns a string in which all occurrences of ch in astr are removed. For example, remove char("object oriented", ’e’) returns the string "objct orintd". Your implementation should not contain any loops and may use only the...
PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and...
PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and prints/displays the computed values. The program will behave as in the following example. Note that in the two lines, Enter length and Enter width, the program does not display 10.0 or 8.0. They are values typed in by the user and read in by the program. The first two lines are text displayed by the program informing the user what the program does. This...
In python, write a program that asked the user to enter the monthly costs for the...
In python, write a program that asked the user to enter the monthly costs for the following expenses incurred from operating his automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should display total monthly cost and annual cost. Also, should contain main and calcExpenses functions, calcExpenses should only calculate the expenses and everything else should happen in the main function.
Python Language: The Marietta Country Club has asked you to write a program to gather, then...
Python Language: The Marietta Country Club has asked you to write a program to gather, then display the results of the golf tournament played at the end of March. The Club president Mr. Martin has asked you to write two programs. The first program will input each player's first name, last name, handicap and golf score and then save these records in a file named golf.txt (each record will have a field for the first name, last name, handicap and...
Write a complete and syntactically correct Python program to solve the following problem: You are the...
Write a complete and syntactically correct Python program to solve the following problem: You are the payroll manager for SoftwarePirates Inc. You have been charged with writing a package that calculates the monthly paycheck for the salespeople. Salespeople at SoftwarePirates get paid a base salary of $2000 per month. Beyond the base salary, each salesperson earns commission on the following scale: Sales Commission Rate Bonus <$10000 0% 0 $10000 – $100,000 2% 0 $100,001 - $500,000 15% $1000 $500,001 -...
Write a complete and syntactically correct Python program to solve the following problem: Write a program...
Write a complete and syntactically correct Python program to solve the following problem: Write a program for your professor that allows him to keep a record of the students’ average grade in his class. The program must be written in accordance with the following specs: 1. The input must be interactive from the keyboard. You will take input for 12 students. 2. You will input the students’ name and an average grade. The student cannot enter an average below zero...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
Write a program in Python to simulate a Craps game: 1. When you bet on the...
Write a program in Python to simulate a Craps game: 1. When you bet on the Pass Line, you win (double your money) if the FIRST roll (a pair of dice) is a 7 or 11, you lose if it is ”craps” (2, 3, or 12). Otherwise, if it is x ∈ {4, 5, 6, 8, 9, 10}, then your point x is established, and you win when that number is rolled again before ’7’ comes up. The game is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT