Created
July 22, 2019 03:00
-
-
Save sourencho/2b6928588a4bdc885c7438940cb4cc72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def crackle_pop_1(): | |
""" Simple initial solution """ | |
for i in range(1, 101): | |
if i % 3 == 0 and i % 5 == 0: | |
print("CracklePop") | |
elif i % 3 == 0: | |
print("Crackle") | |
elif i % 5 == 0: | |
print("Pop") | |
else: | |
print(i) | |
def crackle_pop_2(): | |
""" More time efficient than "crackle_pop_1" (one less mod) """ | |
for i in range(1, 101): | |
divided = False | |
if i % 3 == 0: | |
print("Crackle", end="") | |
divided = True | |
if i % 5 == 0: | |
print("Pop", end="") | |
divided = True | |
if not divided: | |
print(i, end="") | |
print() | |
def crackle_pop_3(): | |
""" More time efficient than "crackle_pop_2" (no ifs or mods) but less space efficient """ | |
start = 1 | |
end = 100 | |
out = list(range(start, end+1)) | |
for i in range(3, end+1, 3): | |
out[i-1] = "Crackle" | |
for i in range(5, end+1, 5): | |
out[i-1] = "Pop" | |
for i in range(15, end+1, 15): | |
out[i-1] = "CracklePop" | |
print(*out, sep="\n") | |
crackle_pop_1() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment