Skip to content

Instantly share code, notes, and snippets.

@willmcgugan
Created August 2, 2022 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willmcgugan/11fd2f0b6e88168d1c7247621a7344ee to your computer and use it in GitHub Desktop.
Save willmcgugan/11fd2f0b6e88168d1c7247621a7344ee to your computer and use it in GitHub Desktop.
Fractions are accurate
def segments_float(size, parts):
end = 0.0
part = size / parts
for n in range(parts):
start = int(end)
end += part
print(str(n) * int(end - start), end="")
print()
from fractions import Fraction
def segments_fraction(size, parts):
end = Fraction(0)
part = Fraction(size, parts)
for n in range(parts):
start = int(end)
end += part
print(str(n) * int(end - start), end="")
print()
SIZE = 24
PARTS = 7
print("-" * SIZE)
segments_float(SIZE, PARTS)
segments_fraction(SIZE, PARTS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment