BCD-BINARY-SUMMIT//DAILY-DOSE//upto max limit (input) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

BCD-BINARY-SUMMIT//DAILY-DOSE//upto max limit (input)

bcd of a decimal no.(Binary-Coded-Decimal) eg: 167 bcd eqivalent-> 0001 0110 0111 binary eqivalent-> 10100111 TASK--> if number of 1's in binary is equal to number of 1's in bcd ''upto that length''of binary (from right to left) print the number, print bcd eqiv, print binary eqiv.. eg: 18---bcd 00011000 ( two 1's) 18--binary 10010 ( two 1's) eg: 179---bcd 0001 01111001 ( five 1's) 179--binary 10110011 (five 1's) # REMEMBER...UPTO BINARY LENGTH

24th Aug 2017, 6:49 PM
sayan chandra
sayan chandra - avatar
29 Answers
+ 10
179---bcd 0001 0111 1001 ( six 1's) 179--binary 10110011 (five 1's)
24th Aug 2017, 8:19 PM
Louis
Louis - avatar
25th Aug 2017, 5:34 AM
⚛prudhvi⚛
⚛prudhvi⚛ - avatar
+ 4
this is my code in Python.. It checks every number from 10 to the given input (the numbers from 1 to 9 obviously have this property because bin and bcd representations are equal) https://code.sololearn.com/cKajQ9vjyGg6/?ref=app
24th Aug 2017, 9:19 PM
m abrate
m abrate - avatar
+ 4
SURVEY UPDATE---> @ vari93 @baptiste @ ysraelcon @sam0delk1n @prudhbi raju did everything perfect...with explaination. @seung @m abrate also as i wanted but try doing your function.. not predefined..but its ok
25th Aug 2017, 2:27 AM
sayan chandra
sayan chandra - avatar
+ 3
mine https://code.sololearn.com/WdOjtYzkfhD9/?ref=app still thursday on here :) note: I think 179 isn't, ah, depending of the binary's length it is, I got it, fixed 👌
24th Aug 2017, 7:59 PM
ysraelcon
ysraelcon - avatar
+ 3
Here's my C# implementation ✌ LINQ One-Liner〰 strNumBinary.Count(c => c == '1') == strNumTrimmedBCD.Count(c => c == '1') Unfortunately my program hit the limit of 300+ as it's a naive approach without optimization. I'll try to improve it when got time later. Enjoy! https://code.sololearn.com/ciI5ixbJDa2K/?ref=app
25th Aug 2017, 2:42 AM
Zephyr Koo
Zephyr Koo - avatar
+ 3
I did not had the time to do it earlier ... My input starts at 10 and not 1 as numbers from 1 to 9 will always be printed, they have no interest edit : work with the binary length rule now https://code.sololearn.com/cG5tF5JqdAX7/?ref=app
25th Aug 2017, 8:18 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
predefined? do you mean avoid format() and bin()?
25th Aug 2017, 10:13 AM
hamletmun
hamletmun - avatar
+ 3
rewrote without bin(), format(), join(), range()
25th Aug 2017, 8:55 PM
hamletmun
hamletmun - avatar
+ 2
numbers start from 0 to inputs ? from 9 to max of the data type ? (like unsigned)
24th Aug 2017, 7:34 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
> eg: 167 > bcd eqivalent-> 0001 0110 0111 > binary eqivalent-> 1000111 Something wrong with your binary equivalent. 1000111b = 71d 167d = 10100111b
24th Aug 2017, 8:09 PM
sam0delk1n
+ 2
dec bcd bin / only count 1s in bcd up to len(bin) https://code.sololearn.com/cNPveOvum2ad/?ref=app
24th Aug 2017, 8:47 PM
hamletmun
hamletmun - avatar
+ 2
In the example of 179, bin has 8 digits '1011 0011'. And sayan only counts 1s in the last 8 digits of bcd '0111 1001', ignoring the first 4 digits '0001'. At least, that is how I understood and programmed my code.
24th Aug 2017, 11:29 PM
hamletmun
hamletmun - avatar
+ 2
I guess "upto max limit (input)" in the title means all numbers from 1 to input
25th Aug 2017, 2:04 AM
hamletmun
hamletmun - avatar
+ 2
https://code.sololearn.com/c9wAv9DRPSUn/#cpp @sayan, now it works on all numbers in range.
25th Aug 2017, 7:36 AM
sam0delk1n
+ 1
This sounds fun, I'll work on it when I get out of class
24th Aug 2017, 7:02 PM
Vari93
Vari93 - avatar
24th Aug 2017, 9:31 PM
sam0delk1n
+ 1
Alright, here you go lol. Finally got it to work, I was originally doing comparisons with literal binary representations in bcd, I wasn't adding 0s. Thank you Seung Moo. https://code.sololearn.com/ciL9PO3acX9t/?ref=app
25th Aug 2017, 12:37 AM
Vari93
Vari93 - avatar
+ 1
I think only one number needs print out, not all from 1 to N, see the first post.
25th Aug 2017, 12:46 AM
sam0delk1n
28th Aug 2017, 5:43 PM
Carlos Castro
Carlos Castro - avatar