Base10ToBase2

Bug: Don't handle 5, 101 -> output 1

#!/bin/python3

import math
import os
import random
import re
import sys

def toBinary(n):
    if n == 0:
        print(0)
        return
    elif n == 1:
        print(1)
        return

    bn = ''
    while n != 0:
        remainder = n % 2
        bn = str(remainder) + bn
        n = int(n / 2)
        # print(n)

    print(bn)
    ct = 0
    max1 = 0
    i = 0
    while i < len(bn) - 1:
        if bn[i] == '0':
            ct = 0
        elif bn[i] == '1':
            if bn[i + 1] == '1':
                ct = ct + 1
                if ct > max1:
                    max1 = ct
            else:
                ct = 0
        i = i + 1

    print(max1)



if __name__ == '__main__':
    n = int(input())
    toBinary(n)

S

#!/bin/python3

import math
import os
import random
import re
import sys

if __name__ == '__main__':
    num = int(input())
    result = 0
    maximum = 0

    while num > 0:
        if num % 2 == 1:
            result += 1
            if result > maximum:
                maximum = result

        else:
            result = 0
        #!Sean 整除
        num //= 2

    print(maximum)

Ref:

https://www.hackerrank.com/challenges/30-binary-numbers/problem

results matching ""

    No results matching ""