Sort by

recency

|

2820 Discussions

|

  • + 0 comments

    I started off using the built-in bin() function, but it felt like the point was to do it myself, so I made user_bin to replicate it.

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    def user_bin(n):
        n = int(n)
        out = ""
        while(n > 0):
            remainder = n % 2
            n = n//2
            out += str(remainder)
        return out[::-1]
    
    if __name__ == '__main__':
        n = int(input().strip())
        print(max([len(i) for i in user_bin(n).split("0")]))
    
  • + 0 comments
    if __name__ == '__main__':
    n = int(input().strip())
    a = list(bin(n)[2:])
    c = [-1]
    d = []
    for i in range(len(a)):
    if a[i] == "0":
    c.append(i)
    c.append(len(a)) 
    for i in range(len(c)-1):
    d.append(c[i+1] - (c[i]+1)) 
    print(max(set(d))) 
    
  • + 0 comments

    python 3

    def base2(n):

    mybin = bin(n)[2::]
    
    return max(map(len, mybin.split('0')))
    
  • + 0 comments

    Pyhton

    n = int(input())

    binary_representation = bin(n)[2:]

    consecutive_ones = binary_representation.split('0')

    max_consecutive_ones = max(len(group) for group in consecutive_ones)

    print(max_consecutive_ones)

  • + 0 comments

    My C# solution

    public static void Main(string[] args) { int n = Convert.ToInt32(Console.ReadLine().Trim());

        string binary = Convert.ToString(n ,2);
    
        char[] c = binary.ToCharArray();
    
        int iCount = 1;   
        int left = 0;
        int right = 1;
        // This list will be used to store each count.
        // After each iteration, we add the count to the list.
        // After the loop has finished, we sort the elements in the 
        //list,
        // and finally, we print the last element of the list.
       List<int> lstCounts = new List<int>();
    
        for(int i = 0 ; i < c.Length ; i++)
        {
             if(right <= c.Length -1 )
            {
                 if(c[left] == c[right] && c[right] == '1')
                { 
                    iCount++;
                    lstCounts.Add(iCount);
                }
                 else 
                 { 
                    lstCounts.Add(iCount);
                    iCount = 1;
                 }
            }
    
            left++;
            right++;          
        }
    
    // sort elements from the list  
     lstCounts.Sort();
     //print the highest count from the list
     Console.Write(lstCounts.LastOrDefault()); 
    
    
    }