Created
August 15, 2014 10:38
-
-
Save yuheiomori/ffe23361938870cc5f3c to your computer and use it in GitHub Desktop.
Juglling with zeros (CodeEval) in Python 3.x
This file contains hidden or 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
# coding=utf-8 | |
import sys | |
def convert1(s): | |
elements = s.split(" ") | |
b = [] | |
for flag, seq in zip(*[iter(elements)] * 2): | |
if flag == "0": | |
b.append(seq) | |
elif flag == "00": | |
for _ in list(seq): | |
b.append("1") | |
return "".join(b) | |
def convert2(s): | |
d = 0 | |
for i, b in enumerate(list(s)): | |
if b == '1': | |
d += 2 ** (len(s) - i - 1) | |
return d | |
def main(): | |
with open(sys.argv[1], "r") as f: | |
for line in f: | |
print(convert2(convert1(line.rstrip()))) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment