Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this problem, you will create a program from scratch based to demonstrate your abilities to create and manipulate lists in Python. Create a file

In this problem, you will create a program from scratch based to demonstrate your abilities to create and manipulate lists in Python.

Create a file named Lab05P3.py.

For this program:

  • Label each block of code that performs the different steps with an appropriate comment (e.g., # Step a).
  • Your output should also include indications as to what step is being displayed. See the Sample Output for an example.

Write a Python program that performs the following steps:

  1. Ask the user how many numbers they want in the lists. This number will determine the size of BOTH lists.
  2. Use a for loop and a random integer generator to generate random integers from 1 to 15 inclusive. The program should generate the number of integers indicated by the user in step a. Store the random integers in a list.  Display the list.
  3. Create a second list in the same way as was done in step b.  Display the second list.
  4. Use a for loop to compare elements in the two lists in pairs, i.e., compare the first elements in both lists, compare the second elements in the both lists, etc.  Display the larger number in each comparison and indicate if this is from the first or second list. If they are equal, indicate they were equal.
    • HINT: Have your for loop increment through the lists using an index. That will allow you to compare the 0th element, then the 1st element, etc.
  5. Combine the lists into a single list and display the combined list.
  6. Sort the combined list in place and display the sorted combined. Do NOT use the sorted function. Use the method we learned about in class.
  7. Use list slicing to display the first three elements in the list and the last three elements in the list.
  8. Use a for loop and a random integer generator to generate 4 random integers in 1 to 15. Check if each number is in the combined list.
    • If the number is in the combined list, print the number and print the index of the first element that matches.
      • NOTE: There is a list method for finding an element value which returns the index and you should use that function. Do NOT use a for loop or you will lose some points.
    • If the element is found, after printing the index, the element should be deleted or removed from the list.
      • NOTE: There is both a function and a list method that can delete or remove an element from a list. You should use either the function or the list method. Do NOT use a for loop or you will lose some points.
    • If the number is not in the combined list, print the number followed by "not found in list".
  9. Print the combined list now that elements may have been deleted.

 

Sample output:

Step a: How many numbers in each list? 5

Step b: First list: [11, 14, 10, 5, 5]

Step c: Second list: [12, 3, 11, 11, 9]

Step d: 

Larger number in each comparison:

12 : Second list

14 : First list

11 : Second list

11 : Second list

9 : Second list

Step e: Combined list: [11, 14, 10, 5, 5, 12, 3, 11, 11, 9]

Step f: Sorted list: [3, 5, 5, 9, 10, 11, 11, 11, 12, 14]

Step g:

First three elements: [3, 5, 5]

Last three elements: [11, 12, 14]

Step h:

8 not found in list

5 at index 1

10 at index 3

11 at index 3

Step i: Final list: [3, 5, 9, 11, 11, 12, 14]

Run this program using the PyCharm Terminal. Take a screenshot of the Terminal that includes the line showing where you started the program run with the results. Name the screenshot Lab05P3-ouput.jpg.

Step by Step Solution

3.41 Rating (145 Votes )

There are 3 Steps involved in it

Step: 1

code from random import randint for generating random integers step a first for i in range06 valuera... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual C# An Introduction to Object-Oriented Programming

Authors: Joyce Farrell

7th edition

978-1337102100

More Books

Students also viewed these Programming questions

Question

Where do your students find employment?

Answered: 1 week ago