You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class FileNumberSummer:
def __init__(self, rootDir, start, end):
self.rootDir = rootDir
self.start = start
self.end = end
def generateFolderName(self,value):
if value % 10 == 0:
value = value - 1
highest = value + (10 - (value % 10))
lowest = highest - 9
return "{0:0=6}".format(lowest) + "-" + "{0:0=6}".format(highest)
def generateFileName(self,value):
return "{0:0=6}".format(value) + ".csv"
def sumNumbersInFile(self,fileDir):
fileContent = open(fileDir, "r")
sums = 0
try:
line = fileContent.readline()
while line:
sums += sum(map(int,line.split(',')))
line = fileContent.readline()
finally:
fileContent.close()
return sums
def getSum(self):
sums = 0
for i in range(self.start, self.end + 1):
sums += self.sumNumbersInFile(self.rootDir + "/" + self.generateFolderName(i) + "/" + self.generateFileName(i))
return sums
import os
from FileNumberSummer import FileNumberSummer
# 1, 1000 because the files are named from 000001.csv to 001000.csv
FNS = FileNumberSummer(os.getcwd() + "/files", 1, 1000)
print(FNS.getSum())
Time ~= 7s
Memory ~= 82000 kilobytes
Program
solution hosted on: https://github.com/the-fanan/sum-files-challenge
pypy python-solution/main.py
49947871404
The text was updated successfully, but these errors were encountered: