Sunday, February 13, 2022

Fake Binary Python Codewars

 

Given a string of digits, you should replace any digit below 5 with '0' and any digit 5 and above with '1'. Return the resulting string.

Note: input will never be an empty string


def fake_bin(x):
    res = ''
    for n in str(x):
        if int(n) < 5:
            res += '0'
        if int(n) >= 5:
            res += '1'
    return res

def fake_bin(x):
    return ''.join('0' if c < '5' else '1' for c in x)

print(fake_bin(509321967506747))

Training on Fake Binary | Codewars



Related Posts


No comments :

Post a Comment