Skip to content

Instantly share code, notes, and snippets.

@yuheiomori
Created August 23, 2014 06:43
Show Gist options
  • Save yuheiomori/0b067b695bd11acf3b89 to your computer and use it in GitHub Desktop.
Save yuheiomori/0b067b695bd11acf3b89 to your computer and use it in GitHub Desktop.
Decimal To Binary (CodeEval) in python 3.x
# coding=utf-8
import sys
def decimal_to_binary(n):
if n == 0:
return '0'
result = ''
while n > 0:
result = str(n % 2) + result
n //= 2
return result
def main():
with open(sys.argv[1], "r") as f:
for line in f:
print(decimal_to_binary(int(line.rstrip())))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment