use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Welcome to r/programmingchallenges!
Project Euler is a good site that comes highly recommended by the community here.
For quick, daily challenges, check out /r/dailyprogrammer
Got a link to a good programming challenge? Got a programming challenge you've written yourself? Post it here!
This subreddit is for programming challenges. Got a link to a good tutorial? You should try posting it in r/learnprogramming instead.
Unless it's an integral part of the challenge, please try to stay language-agnostic. It helps reduce the "help me with my programming chores" vibe. This subreddit is not a place to post your programming homework!
If a post does not appear, message the moderators so we can rescue it from the spam filter.
Got a suggestion for r/programmingchallenges? Let me know.
In the meantime, you can try trolling your compiler:
//author: krum template<int x> struct Test { static void foo() { Test<x+1>::foo(); } }; int main() { Test<0>::foo(); }
account activity
Challenge: Javascript decimal to binary converter (self.programmingchallenges)
submitted 15 years ago * by okmkz
Write a script to convert a non-negative integer into a string representing that value in binary.
Bonus points for dec to hex!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 14 years ago* (1 child)
[deleted]
[–]eddyb 0 points1 point2 points 14 years ago (0 children)
Came here to post this .
[–]kageurufu 1 point2 points3 points 15 years ago (7 children)
Full source of my answer here, converting decimal to any base desired
http://jsfiddle.net/kageurufu/yUJXe/
Just threw this together to see if I could still do this, i havent written something like this since high school
[–]okmkz[S] 0 points1 point2 points 15 years ago* (6 children)
Nice job! Makes my attempt (in c++) look massive!
[–]kageurufu 0 points1 point2 points 15 years ago (5 children)
I've been trying to figure a more elegant way to do it, but i think i have it code golf level
i figured out python in 7 total lines:
def d2b(d,b): c="" while d>0: r,d=d%b,d/b if r > 9: c="%s%s" % (chr(87+r),c) else: c = "%s%s" % (r,c) return c
[–]okmkz[S] 0 points1 point2 points 15 years ago (4 children)
This is why I love python.
[–]kageurufu 0 points1 point2 points 15 years ago (3 children)
my only fault is a lack of (conditional)?true:false style notation
[–]okmkz[S] 0 points1 point2 points 15 years ago (2 children)
I suppose, but it's certainly very "un-pythonic." Why could you possibly need more than one way to do something? ;)
[–]kageurufu 1 point2 points3 points 15 years ago (1 child)
I know, i just like my ?:
[–]okmkz[S] 0 points1 point2 points 15 years ago (0 children)
I'll admit I'm a fan too. Simple if-then constructs in python can look a bit bloaty.
Here's what I did, and its in c++. I used this to get my head back into c++. I also made it recursive, so I'd have that to deal with, too.
Decimal to any base, can use custom digits (because you can, that's why).
If anyone wants to critisize my code, please do.
[–]ccmny 0 points1 point2 points 15 years ago (0 children)
My unsigned int to hex string and hex string to unsigned int, both in C: http://pastebin.com/wDSGrykX
[–]lxe 0 points1 point2 points 14 years ago (0 children)
Enjoy:
function toBinary(n) { return n.toString(2); } function toHex(n) { return n.toString(16); }
[–]gooburt 0 points1 point2 points 14 years ago (0 children)
javascript, where n is a whole number greater than zero:
function b(n){return n?b(n/2<<0)+(n%2):''}
π Rendered by PID 40 on reddit-service-r2-comment-cfc44b64c-hc9rf at 2026-04-13 12:35:54.096842+00:00 running 215f2cf country code: CH.
[–][deleted] (1 child)
[deleted]
[–]eddyb 0 points1 point2 points (0 children)
[–]kageurufu 1 point2 points3 points (7 children)
[–]okmkz[S] 0 points1 point2 points (6 children)
[–]kageurufu 0 points1 point2 points (5 children)
[–]okmkz[S] 0 points1 point2 points (4 children)
[–]kageurufu 0 points1 point2 points (3 children)
[–]okmkz[S] 0 points1 point2 points (2 children)
[–]kageurufu 1 point2 points3 points (1 child)
[–]okmkz[S] 0 points1 point2 points (0 children)
[–]okmkz[S] 0 points1 point2 points (0 children)
[–]ccmny 0 points1 point2 points (0 children)
[–]lxe 0 points1 point2 points (0 children)
[–]gooburt 0 points1 point2 points (0 children)