Question

A file named "numbers.txt" contains an unknown number of lines, each consisting of a single positive...

A file named "numbers.txt" contains an unknown number of lines, each consisting of a single positive integer. Write some code that reads through the file and stores the largest number read in a variable named "maxvalue". Use Python

0 0
Add a comment Improve this question Transcribed image text
Answer #1

## ================= maxnumber.py ==============

maxvalue = -1

if __name__ == "__main__":

    with open("numbers.txt") as input_file:
        # This **will not** read whole file into memory but one line at a time. So no memory overhead as number of
        # lines in the file is unknown. When the next line is read, the previous one will be garbage collected

        for line in input_file:
            try:
                number = int(line.strip())
                if number > maxvalue:
                    maxvalue = number
            except Exception as e:
                print("Skipping line because of Exception : {}".format(e.message))

        if maxvalue != -1:
            print("Largest Number found in the file : {}".format(maxvalue))
        else:
            print("Not found !")

## =================== numbers.txt =======================

1
10
10
4
5
8
9
11
12
13
67
7

## =================== output ===================

Largest Number found in the file : 67

Add a comment
Know the answer?
Add Answer to:
A file named "numbers.txt" contains an unknown number of lines, each consisting of a single positive...
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
  • The file ‘classes.txt’ contains an unknown number of lines. Each line contains two words. The first...

    The file ‘classes.txt’ contains an unknown number of lines. Each line contains two words. The first word is the course number (‘CS021’, for example) and the second word is the instructor’s last name (Eddy). There are no duplicate courses in the file. On the other hand, an instructor may teach more than one course. Write a code segment that opens the file, loads the key/value pairs into a dictionary and then closes the file. No documentation or exception handling is...

  • Create a python code named LetterCount with a text file named words.txt that contains at least...

    Create a python code named LetterCount with a text file named words.txt that contains at least 100 words in any format - some words on the same line, some alone (this program involves no writing so you should not be erasing this file). Then create a program in the main.py that prompts the user for a file name then iterates through all the letters in words.txt, counting the frequency of each letter, and then printing those numbers at the end...

  • Write a line of code that will declare a file variable named data. Assuming that the...

    Write a line of code that will declare a file variable named data. Assuming that the variable data was correctly created in the previous problem, write a line of code that will associate the variable with a file named grades.txt that is being opened for reading. Assuming that grades.txt only contains integers, write a line of code that will use the variable data to read in the first integer and store in a variable named test1 that has already been...

  • 1.Write a python program that writes a series of random numbers to a file named random.txt....

    1.Write a python program that writes a series of random numbers to a file named random.txt. Each random number should be in the range of 1 through 300. The application should let the user specify how many random numbers the file will hold. 2. Write another program that reads the random numbers from the random.txt file, displays the numbers, then displays the following data: I. The total of the numbers II. The number of random numbers read from the file

  • In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a seri...

    In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a series of characters separated by a single space - The second line contains the number of states as an integer k 2 1; states will be numbered 0,1,..., k -1. The start state is always state O The third line contains a series...

  • In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a seri...

    In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a series of characters separated by a single space - The second line contains the number of states as an integer k 2 1; states will be numbered 0,1,..., k -1. The start state is always state O The third line contains a series...

  • 2. Read in an unknown number of real values from a file called “data.txt”. Calculate the...

    2. Read in an unknown number of real values from a file called “data.txt”. Calculate the average of the real numbers. Output the average of the real numbers to a file called “output.txt”. Some skeleton code is provided for you below. #include    int main (void) {                         FILE *infile = NULL;                         FILE *outfile = NULL;                         /* Fill in the code to open a file. Make sure you check that the file was open successfully. */                         while (!feof (infile))                         {                                     /*...

  • Question 3 (2 points) Suppose, you have a file called 'numbers.txt' where each line of the...

    Question 3 (2 points) Suppose, you have a file called 'numbers.txt' where each line of the file contains a number. The number in each line can be a positive or a negative number. Write a program that will find the average of all the numbers between 0 to 1000 (including 1000). Your file may look like the following: 10 25 -5 1001 10 Sample output: The average is 15 o Нt

  • Submit a single file named hw0.py that contains the solutions to the problems below. When you...

    Submit a single file named hw0.py that contains the solutions to the problems below. When you are finished, test your solutions using the doctest: copy the file hw0TEST.py from d2l into your working folder. include the following code at the bottom of the module hw0.py, and then run your module.   Fix all failures before you submit your solutions. if __name__=='__main__':     import doctest     print( doctest.testfile( 'hw0TEST.py')) Write a function moreOdds that accepts one argument, a list of integers.   The function then...

  • We have a file that contains a list of an unknown amount of integer values. The end of the list i...

    We have a file that contains a list of an unknown amount of integer values. The end of the list is marked by a sentinel number, -987. The file is located at "extracredit\progdata.txt." Write a program that will read each value from the file, then display it using cout. It should not display the final sentinel value. After it displays the last value, it should display the maximum of the values, saying "The largest number was ____." The program should...

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