Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.

def max_gap(N):
    xs = bin(N)[2:].strip('0').split('1')
    return max([len(x) for x in xs])
Comment

A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N. For example, number 9 has binary representation 1001 and contains a binary gap of length 2. Th

public int solution(int n) {
        // write your code in Java SE 8
        String binaryRep = Integer.toBinaryString(n);
        System.out.println("Binary Representation of " + n + " = " + binaryRep);
        List<String> strList = new ArrayList<String>();
        int count = 0;
        for (int i = 0; i < binaryRep.length(); i++) { // Loop through the each number 
            String str = binaryRep.charAt(i) + ""; // getting one by one number
            if(str.equals("0")){
                for(int j = i;j<binaryRep.length();j++){ //Get each next element
                    String str1 = binaryRep.charAt(j) + "";
                    if(!str.equals("1") &&  str.equals(str1)){
                        if(!strList.isEmpty() && count >= strList.size()){
                            strList.add(str1);
                        }else if(strList.isEmpty()){
                            strList.add(str1);
                        }
                        count ++; 
                    }else{
                        count = 0;
                        break;
                    }
                }
           }   
        }
        return strList.size();
    }
Comment

PREVIOUS NEXT
Code Example
Python :: code example of sum of the first n odd numbers using for loop 
Python :: for in loop python 
Python :: correlation with target variable python 
Python :: all python versions 
Python :: open multiple plots python 
Python :: remove figure label 
Python :: discord.py create button 
Python :: add title to tkinter window python 
Python :: remove punctuation from a string 
Python :: astype float across columns pandas 
Python :: get date only from datetimefiel django 
Python :: python codes for counting the occurrence of a letters in dictionary excluding digits 
Python :: python recursion example 
Python :: print python reverse list 
Python :: swapping upper case and lower case string python 
Python :: get n largest values from 2D numpy array matrix 
Python :: topological sort 
Python :: python re.sub() 
Python :: Python Switch case statement by Dictionary Mapping 
Python :: numpy argsort 
Python :: pandas pivot tables 
Python :: How to Remove Items in a Set in Python Using the discard() Method 
Python :: negative slicing in python 
Python :: upload file setup django url 
Python :: giving number of letter in python 
Python :: aws lambda logging with python logging library 
Python :: concatenate string in python 
Python :: List Get a Element 
Python :: numpy linspace function 
Python :: pandas save dataframe with list 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =