Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submission: Python 3 #11

Open
the-fanan opened this issue Oct 8, 2019 · 0 comments
Open

Submission: Python 3 #11

the-fanan opened this issue Oct 8, 2019 · 0 comments

Comments

@the-fanan
Copy link

the-fanan commented Oct 8, 2019

  • Hardware
Processor Name: Intel® Core™ i5-2540M CPU @ 2.60GHz × 4 
Memory: 8 GB
  • Time ~= 7s

  • Memory ~= 82000 kilobytes

  • Program

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())

solution hosted on: https://github.com/the-fanan/sum-files-challenge

  • Command
    pypy python-solution/main.py
  • Output
    49947871404
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant