Slashdot Mirror


Singapore's Prime Minister Shares His C++ Sudoku Solver Code

itwbennett writes: Several weeks ago, during a speech at the Founders Forum Smart Nation Singapore Reception, Singapore's prime minister Lee Hsien Loong said that he used to enjoy programming, and that the last program he wrote was a Sudoku solver in C++. To back that up, earlier today he announced (on Facebook and Twitter) that his code is available to download. He wrote on Facebook that he wrote the program 'several years ago' and that the code does 'a backtrack search, choosing the next cell to guess which minimises the fanout.'

15 of 230 comments (clear)

  1. Technically C++ by Imagix · · Score: 4, Insightful

    Well... probably more accurate to call that C code. It's compilable under a C++ complier, but offhand I didn't spot anything that really made it C++-specific. Not a knock on the Prime Minister, but it might even be a little more geek cred to call it a Sudoku solver in C.

    1. Re:Technically C++ by Dutch+Gun · · Score: 4, Insightful

      Heh, leave it to the tech community to start nitpicking which language was actually used rather than the fact that we're seeing the very rare sight of a computer programmer in political office.

      I took a look at the code - yeah, it's really just C code, but that's fine for a tiny project like this. Nice code, very clean and readable, but not very well commented.

      --
      Irony: Agile development has too much intertia to be abandoned now.
    2. Re:Technically C++ by Bite+The+Pillow · · Score: 4, Insightful

      Since Dice, and really well before, fuck all of that technicality. If the dude can solve sudoku in anything other than a pencil, should we really nitpick?

    3. Re: Technically C++ by Sique · · Score: 4, Informative

      I guess, Angela Merkel knows. At least she wrote a program to simulate electron movements for her PhD thesis.

      --
      .sig: Sique *sigh*
  2. Put in context by Anonymous Coward · · Score: 5, Informative

    The guy was a Senior Wrangler (top math undergrad) at Cambridge before going to governance. I would hope he'd have picked up some coding skills along the way.

    Now with some other leaders, I'm impressed when they spell their names right.

    1. Re: Put in context by Anonymous Coward · · Score: 5, Informative

      Wiki link to those who aren't across Cambridge lingo

      http://en.m.wikipedia.org/wiki/Senior_Wrangler_%28University_of_Cambridge%29

      The Senior Wrangler is the top mathematics undergraduate at Cambridge University in England, a position once regarded as "the greatest intellectual achievement attainable in Britain."

  3. Re:It may have a .cpp extension but it's all C cod by AuMatar · · Score: 4, Informative

    Its also all legal C++. Just because it doesn't use the advanced features doesn't mean it isn't C++. I'd argue that for something this simple its even better code this way.

    --
    I still have more fans than freaks. WTF is wrong with you people?
  4. The actual code by Anonymous Coward · · Score: 5, Informative

    How's about including a link to the actual code?

    Here you go:

    https://drive.google.com/folde...

    OR:

    https://github.com/Doppp/LHL-S...

  5. From his Facebook post on his Sudoku solver by cciRRus · · Score: 5, Interesting
    He posted this on his Facebook.

    For techies: the program does a backtrack search, choosing the next cell to guess which minimises the fanout.

    Here’s a question for those reading the source code: if x is an (binary) integer, what does (x & -x) compute?

    Hope you have fun playing with this. Please tell me if you find any bugs! – LHL

    ===========

    As several of you noted, (x & –x) returns the least significant ‘1’ bit of x, i.e. the highest power of two that divides x. This assumes two’s complement notation for negative numbers, as some of you also pointed out. e.g. if x=12 (binary 1100), then (x & -x) = 4 (binary 100). I didn’t invent this; it is an old programming trick.

    Nice!

    --
    w00t
    1. Re:From his Facebook post on his Sudoku solver by Anonymous Coward · · Score: 5, Insightful

      Not among prime ministers

  6. Stupid sudoku solver? by Taco+Cowboy · · Score: 4, Insightful

    It might be a 'stupid sudoku solver', like you say, but at least, that is a program that does something, rather than the congress critters who do nothing but producing massive amount of HOT AIR

    --
    Muchas Gracias, Señor Edward Snowden !
    1. Re:Stupid sudoku solver? by antiperimetaparalogo · · Score: 5, Funny

      It might be a 'stupid sudoku solver', like you say, but at least, that is a program that does something, rather than the congress critters who do nothing but producing massive amount of HOT AIR

      Imagine that you are a zombie apocalypse survivor, naked and wet, in a freezing winter Alaskan night, tired to death, and you can share a tent either with this Singapore's prime minister who wants you to stay awake with him and solve stupid Sudoku puzzles or with one of those "congress critters who do nothing but producing massive amount of HOT AIR" - who would be you choice?

      --
      Antisthenes: "Wisdom begins by examining the words/names." - excuse my English, i am (slightly...) better with my Greek!
  7. New competition by viperidaenz · · Score: 5, Funny

    There should be an international coding competition between heads of state.

  8. Re:Lead By Example?! by antiperimetaparalogo · · Score: 4, Informative

    but our former prime-minister had a restaurant... our great Greek dinner with mousaka,

    Which one? You'd have hoped with that experience he might have had some idea how to balance the books. Did his restaurant go broke?

    It was mister Samaras, the one who just lost the elections - (he claims) his restaurant did just fine (he had it years ago, together with a business partner, while studing in USA - yes, one more Greek restaurant in the States!). To be fair he did a good job as a prime minister in the elimination of deficit, he not only balanced the books, but provided a (significant) surplus. Our new prime minister...well...!

    --
    Antisthenes: "Wisdom begins by examining the words/names." - excuse my English, i am (slightly...) better with my Greek!
  9. Re:Not that big of a deal... by gnasher719 · · Score: 4, Insightful

    The code resembles something you expect from a first-year programming student - there's an input buffer overflow bug waiting to happen, the array size is odd (80 byte array? why? scanf() is still called without a field length specifier, and you only use 9 of those 80 bytes in a normal case).

    You seem to be very optimistic in your views of first year programming students.

    Here's what I see: Many years ago, I had a lovely job helping to teach people to program, and I had to review their homework. There were some whose thought processes were all over the place. There was one guy who managed to write the most convoluted code that always worked perfectly well, but I would never have hired him because reviewing it was just too painful. And there was one student who wrote code that I could scan in half a minute and see that it was correct and worked. And that's what this guy's code looks like.

    "Perhaps it was written by him in his spare time". OF COURSE it was written in his spare time. His a prime minister, he doesn't write code on the job. What comments do you want? The code is simple and obvious. What data structures to explain? If you are too stupid to understand them immediately, then you shouldn't be programming. What lack of error checking? What scenario do you suggest where error checking would help?

    Your last sentence is a totally unfounded, vicious attack on the intellectual honesty of the man. It's disgusting. Unless you have any evidence for it, you should apologise.