This is quite hard to explain, but instead of treating each digit as an individual number, I want the compiler to recognise the values together as a long number.
For example, this is my output as a string:
42202
47224
44807
51289
58056
51258
48079
16156
38954
9
(9 because that's what it thinks the greatest number is, when obviously that isn't true here)
And this is my code:
def main():
totalSum = 0
data = open('input.txt', 'r')
elvesList = data.readlines()
for line in elvesList:
line.rstrip()
if (line[0] != '\n'):
totalSum += int(line)
else:
aString = str(totalSum)
print(aString)
totalSum = 0
continue
highestValue(aString)
return str(aString)
def highestValue(aString):
print(max(aString))
main()
Because it is a string, the code is recognising each digit as its own number, so like '4', '2', '2', '0', '2' instead of 42202. How can I get it to identify the number as 42202 without converting it to an int?
I can't convert it to an int because I want to use the max() function to find the greatest number, and I can't use max() on an Int type as the compiler complains that 'int' object is not iterable.
I can't convert it to an int because I want to use the max() function to find the greatest number, and I can't use max() on an Int type as the compiler complains that 'int' object is not iterable
Yes you can do those things, if you do them the right way.
Read the lines in the file, convert each line to an integer, put the integers in a list, and call max()
on the list.
with open('input.txt') as data:
numbers = [int(line) for line in data if line.strip()]
print(max(numbers))