Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
Get HideMyAss! VPN, PC Mag's Top 10 VPNs of 2016 for 55% off for a Limited Time ×
Programming Government Games

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

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.'
This discussion has been archived. No new comments can be posted.

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

Comments Filter:
  • by Anonymous Coward

    I guess C++ just sounds higher tech.

  • Technically C++ (Score:5, Insightful)

    by Imagix ( 695350 ) on Monday May 04, 2015 @08:21PM (#49617349)
    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.
    • Re:Technically C++ (Score:5, Insightful)

      by Dutch Gun ( 899105 ) on Monday May 04, 2015 @08:35PM (#49617451)

      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.

      • 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.

        Well he might be following Uncle Bob's Clean Code concepts and not filling his code with comments that could become crufty and misleading over time.

        • 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.

          Well he might be following Uncle Bob's Clean Code concepts and not filling his code with comments that could become crufty and misleading over time.

          I take that back after reading all the one letter variable names :)

      • by Threni ( 635302 )

        The comments are definitely c++.

    • Re:Technically C++ (Score:5, Insightful)

      by Bite The Pillow ( 3087109 ) on Monday May 04, 2015 @09:59PM (#49617913)

      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?

    • Given the date he wrote it my guess is it would not have compiled under any standard c compiler. He does most of the foward declarations, but he uses C++ style comments and C++ style void declarations. So, while it might not be OOP it is C++ and likely not valid C.

      I'll grab the code tommorow and see what standards gcc4.8 will compiler it under.

    • 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.

      Extra points then to him for writing code to solve a problem, and not writing code to demonstrate his cleverness. Which is what the worst of C++ programmers usually do.

  • Put in context (Score:5, Informative)

    by Anonymous Coward on Monday May 04, 2015 @08:21PM (#49617351)

    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.

    • Re: Put in context (Score:5, Informative)

      by Anonymous Coward on Monday May 04, 2015 @09:10PM (#49617675)

      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."

      • Re: (Score:2, Informative)

        by Anonymous Coward

        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."

        In other news wiping an adult mans arse [wikipedia.org] was once considered the greatest political achievement attainable not by birth in Britain.

  • The actual code (Score:5, Informative)

    by Anonymous Coward on Monday May 04, 2015 @08:24PM (#49617373)

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

    Here you go:

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

    OR:

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

    • The guy is probably proud of his prog, that's why he published it. Indeed, that's more a C approach than a C++, but anyway for solving a sudoku there is no need to put in place a lot of complex data structures.
      What I like in this prog is that he optimizes the process by simply considering that since any 1-9 digit can only be once in each column, each row, and each of the nine 3×3 sub-grids, he uses a 10 bits int and associate 2^i with a digit (instead of an array, or worse checking all digits each tim
  • Free Amos Yee (Score:2, Interesting)

    by Anonymous Coward

    Hey, The guy released his Sudoku solver code, and that's great!
    Now how about using the same logical prowess to release Amos Yee?

    https://youtu.be/dD4y3U4TfeY [youtu.be]

  • I was able to compile it with both gcc and g++, even though it seems to have been written for a Windoze system. So yes, it is legal as both C and C++ code.

    No idea how to run it. Was expecting "You are standing in an open field west of a white house, with a boarded front door. There is a small mailbox here." Instead, all you get is "Row[1] :"

    • by fisted ( 2295862 )

      I was able to compile it with both gcc and g++ [...] [s]o yes, it is legal as both C and C++ code.

      Hahaha. You're so mistaken, it's not even funny anymore. (No, not even with -ansi -pedantic). The language that gcc accepts is a twisted superset of C, so using gcc to evaluate whether it's correct C code is completely meaningless.

  • by Nutria ( 679911 ) on Monday May 04, 2015 @08:35PM (#49617445)

    after all our President is a "coder" too!!! :eyeroll:

  • Well, we have an ex-Prime-Minister who paints puppies, goats, and feet sticking out of bath-tubs!

  • Well... (Score:4, Informative)

    by EmeraldBot ( 3513925 ) on Monday May 04, 2015 @09:01PM (#49617611)

    While not utilizing any of c++'s features, it is still valid c++. I think he might call it this because he may usually program in c++, but maybe just for this one project he did it in c, and since it's still legal c++ he finds it easier to just label it as c++. I actually quite like it - could use a bit more commenting, but overall very clean and readable. Should compile with a decent c or c++compiler...

    • Yes, technically it compiles with both g++ and gcc for me. If it weren't for using stdio, I'd say that it's more C++ style (not all C++ programs need classes) because of no (void) for functions and main is declared at the bottom (for C++, functions must be declared before use, so it's common to see main at bottom for quick codez like this). Let's just agree that it's C and C++ having sex and call it a day :) My real gripe is that he compiled a .EXE and is clearly using Windows! For shame... for shame... g
  • by cciRRus ( 889392 ) on Monday May 04, 2015 @09:04PM (#49617631)
    He posted this [facebook.com] 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!

  • C version, 67 lines of code ...

    http://rosettacode.org/wiki/Su... [rosettacode.org]

  • by viperidaenz ( 2515578 ) on Monday May 04, 2015 @09:25PM (#49617767)

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

    • Re: (Score:3, Funny)

      Meanwhile, in New Zealand... http://static2.stuff.co.nz/125... [stuff.co.nz]
    • Re:New competition (Score:4, Insightful)

      by NicBenjamin ( 2124018 ) on Monday May 04, 2015 @11:41PM (#49618393)

      Would have to be Heads of Government. Lee's a Prime Minister, so he's not Head of State. Singapore's Head of State is Tony Tan.

      Under Singapore's Constitution President Tan has very few powers, but he still technically outranks the PM who has powers. It's similar to the system in Britain/Canada/etc. where the Queen's Head of State,* but the Prime Minister is the one who actually does everything interesting.

      And yes, this is a pet peeve of mine. I spent the week Netanyahu addressed Congress in constant nerd-rage because the entire US media kept calling him a "Head of State" when he isn't.

      *Altho many Canadians argue the Queen isn't their Head of State [wikipedia.org], her representative in Canada is (the Governor-General). The fact no Court's ruled on this definitively shows how important the title "Head of State" is in a Parliamentary system. Most legal scholars seem to think that the Queen is Head of State, but there is a minority that disagrees and their Constitution is not helpful on this question. But mostly nobody cares.

      • *Altho many Canadians argue the Queen isn't their Head of State [wikipedia.org], her representative in Canada is (the Governor-General). The fact no Court's ruled on this definitively shows how important the title "Head of State" is in a Parliamentary system. Most legal scholars seem to think that the Queen is Head of State, but there is a minority that disagrees and their Constitution is not helpful on this question. But mostly nobody cares.

        Given that she owns the entire country it's kind of a moot point. If they piss her off she'll just kick them out.

        • *Altho many Canadians argue the Queen isn't their Head of State [wikipedia.org], her representative in Canada is (the Governor-General). The fact no Court's ruled on this definitively shows how important the title "Head of State" is in a Parliamentary system. Most legal scholars seem to think that the Queen is Head of State, but there is a minority that disagrees and their Constitution is not helpful on this question. But mostly nobody cares.

          Given that she owns the entire country it's kind of a moot point. If they piss her off she'll just kick them out.

          Heh. I suspect that if she tried the outcome would not be that they would leave.

  • by Anonymous Coward

    Its not C++ code because the code is understandable. For example:

    1. he does not use templates, restricting himself to solving only over the integer space. what about a sudoku puzzle of strings? basics, people, this is basic stuff.
    2. he is using raw loops. what about range based bi-directional iterators using lambdas? scott meyers is going to throw a tantrum.
    3. free functions without namespaces or a class even. more appropriate would have been korea::south::presidental_code::sudoku_solver.
    4. better yet, abst

  • by joss ( 1346 ) on Tuesday May 05, 2015 @04:04AM (#49618979) Homepage

    He was Senior Wrangler while at Cambridge. Plus, his son is one of the most productive scala programmers in the world despite only doing it in his spare time. These guys ain't dumb.

  • I really, really like Singapore. They are doing pretty much everything right, including having the smartest and most educated leaders in the world, by far.

    I am a bit confused by their status as non-democracy though.

  • by gatkinso ( 15975 ) on Tuesday May 05, 2015 @06:44AM (#49619353)

    Not a great example of modern C++.

    • by abies ( 607076 )

      Modern C++ would involve solving sudoku on compile time with template metaprogramming. All the stuff with classes, virtual functions, STL etc is so 90ties...

  • I'm impressed (Score:3, Insightful)

    by Mr Z ( 6791 ) on Tuesday May 05, 2015 @10:50AM (#49620983) Homepage Journal

    There's no shortage of people ragging on the code. "It's not C++ enough to be called C++," "there's not enough comments", "it uses C stdio.h", etc. Get over it.

    This looks like the sort of program I might dash out over an afternoon (maybe two) to satisfy some intellectual curiosity. Programming as play. This isn't production code, it's fun code, written to satisfy oneself.

    Is it perfect? WHO CARES! That's not important. That misses the point. If you doodle in your notepad and it brings you a smile, does anyone care it's not as good as the Mona Lisa? You sure as heck don't. And that's what this code is. A doodle. It just happens to be in simple, straightforward procedural C/C++ code.

    I personally think playing with programming is important. Sure, you'll write a lot of dreck. But, you'll also learn a lot. You learn real lessons when you do write dodgy code, and the dodgy code actually bites you. You also can try new things fearlessly. After all, you're programming a toy for oneself, and you're under no deadline pressure. There's no spec you have to fulfill. You can experiment and enjoy it.

    Programming as play still helps build your programming reflexes. If and when you do sit down to write professional grade software, all of that play will make the basic work of programming natural. Rather than focusing your energy on the basic details of programming, you can instead focus your energy designing maintainable code that meets the business requirements and documenting that design. Writing the code just flows naturally.

    So, yeah, I'm impressed. This Sudoku solver brought a smile to my face. It's incredibly cool that the prime minister shared a code doodle with us.

Garbage In -- Gospel Out.

Working...