I had trouble with the kata sum of a sequence, hard-core version. After looking at the solutions, I saw that the solutions used were more mathematical/ formula based rather than computation based.
I was hoping some of you can help make my, rather simplistic, code prettier without losing efficiency.
def sequence_sum(b, e, s):
sum = 0
if s >= 0:
while b <= e:
sum += b
b += s
else:
while b > e:
sum += b
b += s
return sum
import codewars_test as test
from solution import sequence_sum
@test.describe("Tests")
def test_group():
@test.it("Basic tests")
def test_case():
test.assert_equals(sequence_sum(2, 6, 2), 12)
test.assert_equals(sequence_sum(1, 5, 1), 15)
test.assert_equals(sequence_sum(1, 5, 3), 5)
test.assert_equals(sequence_sum(0, 15, 3), 45)
test.assert_equals(sequence_sum(16, 15, 3), 0)
test.assert_equals(sequence_sum(2, 24, 22), 26)
test.assert_equals(sequence_sum(2, 2, 2), 2)
test.assert_equals(sequence_sum(2, 2, 1), 2)
test.assert_equals(sequence_sum(1, 15, 3), 35)
test.assert_equals(sequence_sum(15, 1, 3), 0)
Through experience I found that hardcoding in file names/ destinations/ quantities, etc. always lead to unknown bugs later on.
So I would really prefer having the folder source and destination as a variable/parameter.
import os, shutil class MoveFiles: def __init__(self): self.imageFolder = "folderA" self.images = [img for img in os.listdir(self.imageFolder) if img.endswith(('.png', '.jpg', '.jpeg'))] def move_image_files(self, source = "folderA", destination = "folderB"): for img in self.images: shutil.move(os.path.join(source, img), os.path.join(destination, img))
- import os, shutil
- class MoveFiles:
def __init__(self):self.images = [img for img in os.listdir('folderA') if img.endswith(('.png', '.jpg', '.jpeg'))]- def __init__(self):
- self.imageFolder = "folderA"
- self.images = [img for img in os.listdir(self.imageFolder) if img.endswith(('.png', '.jpg', '.jpeg'))]
def move_image_files(self):- def move_image_files(self, source = "folderA", destination = "folderB"):
- for img in self.images:
shutil.move(os.path.join('folderA', img), os.path.join('folderB', img))- shutil.move(os.path.join(source, img), os.path.join(destination, img))