We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Thank you for this explanation. I'm new to programming and still struggle with coming up with short and simple solutions like this. Now I know there's a method for converting numbers to binary in Python!
My solution is embarrassingly long at 41 lines of code, may not be "Pythonic" or elegant, and may even be considered brute force, but it seems to have passed all of the test cases. I know that Python has a lot of extremely useful things already built into it, but as a beginner, I'm finding it more useful to break down the problem and figure out how to get from A to B using what I already know.
Then, once I've come up with my own solution (inefficient though it may be), I take a look at what others have done and end up discovering new and powerful ways to do things in Python. It's one of my favorite things so far about learning how to program.
If anyone has any advice on how to clean up my code or make it more efficient, I'm all ears. I'm sure I won't be able to produce any of these amazing one-liners anytime soon, but I hope to get there someday.
#!/bin/python3importsysimportmathn=int(input().strip())# create an empty list to hold our binary numbersbinary_list=[]# fill the list with 1s and 0s from left to right using Base-Conversion Method (remainders of dividing by 2)whileTrue:ifn>1:ifn%2==0:binary_list.append(0)elifn%2!=0:binary_list.append(1)elifn==1:binary_list.append(1)breakelifn==0:binary_list.append(0)breakn=math.floor(n/2)# set a counter and a max variableconsec=0maximum=0# iterate through our list of 1s and 0s to find our max consecutive set of 1sforiinbinary_list:ifi==1:consec+=1ifmaximum<=consec:maximum=consecelse:passelifi==0:consec=0print(maximum)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 10: Binary Numbers
You are viewing a single comment's thread. Return to all comments →
Thank you for this explanation. I'm new to programming and still struggle with coming up with short and simple solutions like this. Now I know there's a method for converting numbers to binary in Python!
My solution is embarrassingly long at 41 lines of code, may not be "Pythonic" or elegant, and may even be considered brute force, but it seems to have passed all of the test cases. I know that Python has a lot of extremely useful things already built into it, but as a beginner, I'm finding it more useful to break down the problem and figure out how to get from A to B using what I already know.
Then, once I've come up with my own solution (inefficient though it may be), I take a look at what others have done and end up discovering new and powerful ways to do things in Python. It's one of my favorite things so far about learning how to program.
If anyone has any advice on how to clean up my code or make it more efficient, I'm all ears. I'm sure I won't be able to produce any of these amazing one-liners anytime soon, but I hope to get there someday.