Question

Exercise 6: Program exercise for 2D List Write a complete Python program including minimal comments (file...

Exercise 6: Program exercise for 2D List

Write a complete Python program including minimal comments (file name, your name, and problem description) that solves the following problem with the main function:

Problem Specification:

The following code reads values from the file object infile and stores them in the 2d list table2: (It assumes that each line contains values of elements in each row and the values are whitespace separated.)

  table2 = []
  for line in infile:
        row=line.split()
        intRow = []
        for ele in row:
                intRow.append(int(ele))
        table2.append(intRow)

Write a program that finds how many elements the list (table2) are greater than a value provided by a user.

Read a file name and open for reading

Read the contents in the file and store them in a 2D list using the code above.

Ask the user to enter an integer value. Count and print the number of elements that are greater than the value provided by the user.

Repeat the above process until user enters 0 or nothing.

Create the following data file as shown below and test your program.

Make sure you have a main function and there should not be any global variable or statement other than the main function that is being called.

  Sample RUN #1:
Enter file name: list2d.txt

File list2d.txt converted to 2D list:
[31, 33, 35, 37, 39]
[1, 3, 5, 7, 9]
[11, 13, 15, 17, 19]
[21, 23, 25, 27, 29]

Enter an integer or 0 for quit: 11
there are 14 elements larger than 11

Enter an integer or 0 for quit: 23
there are 8 elements larger than 23

Enter an integer or 0 for quit: 45
there are 0 elements larger than 45

Enter an integer or 0 for quit: 3
there are 18 elements larger than 3

Enter an integer or 0 for quit: 0

   
   Sample input: 
  31 33 35 37 39 
  1 3 5 7 9
  11 13 15 17 19 
  21 23 25 27 29 
  
0 0
Add a comment Improve this question Transcribed image text
Answer #1

PYTHON CODE:

# function to read data from the file
def read_data(infile):

    # empty list
    table2 = []

    # for every line in the file
    for line in infile:    
        # removing the space
        row=line.split()

        # empty list
        intRow = []

        # for every item in the list
        for ele in row:

                # adding the element to the intRow list
                intRow.append(int(ele))

        # adding the intRow list to table2 list
        table2.append(intRow)

    # returning the table2 list
    return table2

# main function
def main():

    # getting file name from the user
    filename=input("Enter file name: ")

    # opening the file
    f=open(filename)

    # reading the data
    r=read_data(f)

    # closing the file
    f.close()

    # printing the 2d list
    print('\nFile %s converted to 2D list:' % filename)
    for l in r:
        print(l)

    # looping
    while True:

        # getting the value of n from the user
        n=int(input("\nEnter an integer or 0 to quit: "))

        # n is zero, break from the loop
        if n == 0:
            break

        # counter variable
        count=0


        # for every list in the 2d list
        for k in r:

            # for every element in the list
            for item in k:

                # checking for element
                if item > n:

                    # incrementing the counter
                    count+=1
                  
        # printing the counting
        print('there are %d elements larger than %d' %(count,n))
                  
# calling the main function
main()

CONTENTS OF list2d.txt:

31 33 35 37 39
1 3 5 7 9
11 13 15 17 19
21 23 25 27 29

SCREENSHOT FOR OUTPUT:

Enter file name: list2d.txt File 1ist2d. txt converted to 2D list: [31, 33, 35, 37, 39] 1, 3, 5, 7, 9] [11, 13, 15, 17, 19] [

Add a comment
Know the answer?
Add Answer to:
Exercise 6: Program exercise for 2D List Write a complete Python program including minimal comments (file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Exercise 7: Program exercise for Basic List operations Write a complete Python program including minimal comments...

    Exercise 7: Program exercise for Basic List operations Write a complete Python program including minimal comments (ile name, your name, and problem description) that solves the following problem with the main function: Problem Specification: Write a program that reads a number of scores from the user and performs the following 1. Read scores from the user until the user enters nothing but Center > key 2. Store scores into a list for processing below 3. Find the average of the...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Python program - Write a Python program, in a file called sortList.py, which, given a list...

    Python program - Write a Python program, in a file called sortList.py, which, given a list of names, sorts the names into alphabetical order. Use a one dimensional array to hold the list of names. To do the sorting use a simple sorting algorithm that repeatedly takes an element from the unsorted list and puts it in alphabetical order within the same list. Initially the entire list is unsorted. As each element is placed in alphabetical order, the elements in...

  • Using the program segment and sample txt file below, write a program that contains a linked...

    Using the program segment and sample txt file below, write a program that contains a linked list of students. The program should allow the user to: 1. initialize list of students 2. add additional student to front of list 3. add additional student to rear of list 4. delete student 5. sort students alphabetically 6. sort students by idNum 7. show number of students in list 8. print students 9. quit program XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX The program should be divided into the...

  • Write a Python program that tests the function main and the functions discussed in parts a...

    Write a Python program that tests the function main and the functions discussed in parts a through g. Create the following lists: inStock - 2D list (row size:10, column size:4) alpha - 1D list with 20 elements. beta - 1D list with 20 elements. gamma = [11, 13, 15, 17] delta = [3, 5, 2, 6, 10, 9, 7, 11, 1, 8] a. Write the definition of the function setZero that initializes any one-dimensional list to 0 (alpha and beta)....

  • Must be done in python and using linux mint Write a program to create a text...

    Must be done in python and using linux mint Write a program to create a text file which contains a sequence of test scores. Each score will be between 0 and 100 inclusive. There will be one value per line, with no additional text in the file. Stop adding information to the file when the user enters -1 The program will first ask for a file name and then the scores. Use a sentinel of -1 to indicate the user...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Use C++ For this week’s lab you will write a program to read a data file...

    Use C++ For this week’s lab you will write a program to read a data file containing numerical values, one per line. The program should compute average of the numbers and also find the smallest and the largest value in the file. You may assume that the data file will have EXACTLY 100 integer values in it. Process all the values out of the data file. Show the average, smallest, largest, and the name of the data file in the...

  • Write a complete Python program with prompts for the user for the main text file (checks...

    Write a complete Python program with prompts for the user for the main text file (checks that it exists, and if not, output an error message and stop), for any possible flags (including none), and for any other input that this program may need from the user: split has an option of naming the smaller files head_tail list the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: -# output #...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT