A file named numberstxt contains an unknown number of lines

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.

Solution

file = open(\"numbers.txt\",\"r\")
data = file.read()
# convert string array to int array.
lines = map(int,data.split(\"\ \"))
maxvalue = lines[0]
# loop through to find max element.
for i in lines[1:]:
if(i>maxvalue):
maxvalue = i
print maxvalue
file.close()

\"\"\" sample inputfile
10
15
20
1
3
7
30

sample output
30
\"\"\"

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 stor

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site