Skip to content

Instantly share code, notes, and snippets.

@t-eckert
Created May 11, 2021 17:16
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 t-eckert/618032ac8dd681db1acf481e525c425d to your computer and use it in GitHub Desktop.
Save t-eckert/618032ac8dd681db1acf481e525c425d to your computer and use it in GitHub Desktop.
Solution to Rounder problem
"""
Given a positive or negative real number, round it to the next whole integer
closer to zero. This means if it’s positive, round down, and if it’s negative,
round up. Try to do this in as few characters possible!
"""
def round(number: float) -> int:
return int(str(number).split(".")[0])
def print_and_assert(expected, actual):
print("expected = " + str(expected))
print("actual = " + str(actual) + "\n")
assert expected == actual
if __name__ == "__main__":
print_and_assert(1, round(1.7))
print_and_assert(-2, round(-2.1))
print_and_assert(500, round(500.4))
print_and_assert(-369, round(-369.5))
print_and_assert(150, round(150))
print_and_assert(-350, round(-350))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment