Day 10: Binary Numbers
Day 10: Binary Numbers
+ 0 comments In python3
binary_string = f"{n:b}" binary_segments = binary_string.split("0") segment_lengths = map(len, binary_segments) max_length = max(segment_lengths) print(max_length)
+ 0 comments In python
print(max([len(i) for i in format(int(input().strip()),"b").split("0")]))
+ 0 comments import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.function.; import java.util.regex.; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList;
public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bufferedReader.readLine().trim()); bufferedReader.close(); String s = Integer.toBinaryString(n); String[] str = s.split(""); int maxcount =0; int count =0; for(int i=0;i<str.length;i++){ if(str[i].equalsIgnoreCase("1")){ count++; }else if(str[i].equalsIgnoreCase("0")){ if(count>maxcount){ maxcount = count; } count =0; } } if(count>maxcount){ System.out.println(count); }else{ System.out.println(maxcount); } }
}
+ 1 comment import re def DecToBin(n): binary = [] while n >= 1: n, r = divmod(n, 2) binary.append(str(r)) return ''.join(binary[::-1]) if __name__ == '__main__': n = int(input().strip()) print(len(max(re.findall('(1+)', DecToBin(n)))))
+ 0 comments The Two's complement table in the "procedure" paragraph is helpful on the Wikipedia page, as it shows the most significant digit is negative and the rest of the digits are positive, and when those are all added together, you get the negative value: https://en.wikipedia.org/wiki/Two%27s_complement
Sort 2700 Discussions, By:
Please Login in order to post a comment