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.'
Is a politician actually doing it?
Not C++.
I guess C++ just sounds higher tech.
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.
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.
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?
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...
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
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] :"
after all our President is a "coder" too!!! :eyeroll:
"I don't know, therefore Aliens" Wafflebox1
Well, we have an ex-Prime-Minister who paints puppies, goats, and feet sticking out of bath-tubs!
Table-ized A.I.
can he eat a raw onion?
Seems he's into making calculated moves.
This strikes me as being some quite good code and easily readable.
Kudos, Mr. PM!
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...
"Set a man a fire, he'll be warm for the rest of the night. Set a man afire, he'll be warm for the rest of his life."
Nice!
w00t
C version, 67 lines of code ...
http://rosettacode.org/wiki/Su...
Is a politician actually doing it?
How many of our congress critters know how to code?
...
... other than the MS-BASIC goto statements, that is
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 !
There should be an international coding competition between heads of state.
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, abstract it all away in a templated class, be sure to overload all the operators, implement copy-on-write and do the non-public stuff in a pimpl that relies some weird over-engineered templated-to-death library from boost. and make that shit virtual.
5. and be sure to do all of the above multi-threaded, since you need to scale to multiple cores these days.
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.
http://rareformnewmedia.com/
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.
That's not C++!!!
That is NOT C++, it is C!!!
NO he can't.
Not a great example of modern C++.
I am very small, utmostly microscopic.
Most Sudoku's can be solved without back-tracking. There is a rather trivial mapping of Sudoku's to Exact Cover problems (see: Hardest Sudoku). Good solvers, only use back-tracking when needed, and try to apply logic reduction first. When there is a column were A implies B, all rows in which B is true and A false can be eliminated, because a selection of such a row will leave the A column empty. Most of the published Sudoku's can be solved with using this simple reduction rule.
That's right, but that doesn't change that it's all C code as well. Labeling it C++ code is quite a stretch IMO.
CLI paste? paste.pr0.tips!
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.
Program Intellivision!
I made mine general to all dimensions of Sudoku, and arbitrary rules (allows Xdokus, and 16x16s etc.).
Naturally, I had to include the binary sudoku as well https://xkcd.com/74/
You're not making it better by actively promoting horrible, non-portable, implementation-dependent and error-prone coding practices.
What's wrong with making assumptions about implementation-defined behaviors and using static assertions to verify them? For example, would it be poor form to assume (and assert) things like 8-bit bytes or that the character set is ASCII, and if so, why?
This way, the compiler will fail and kick out a diagnostic if the environment doesn't match the assumptions.
No help file. WTF is this supposed to do? A bad example given our age of touch and click operating systems, even the most arcane GNU or BSD program is more comprehensible. Maybe it says a lot about the politics of his country, smart but not very user friendly.
Adonis Georgiadis, a major political figure of the former Greek government (New Democracy) cannot solve this equation: x/2=30/3 but this guy shares his C++ program. My respect. :)
PS: Proof in these videos: https://www.youtube.com/watch?... and https://www.youtube.com/watch?...
Fortune Rota Volvitur
If you had a hard requirement such as "has to be ASCII-based" or "char must be 8 bits wide", then I'd wonder where it comes from.
The fact that Internet protocols use 8-bit bytes and either ASCII or its superset UTF-8.
For requiring char to be of some specific width, there's hardly a reason, unless you're improperly (de)serializing.
Last time I checked, the C standard offered no facility for networking, graphics, or even enumeration of the files in a directory. This means most nontrivial interactive programs will need to use POSIX or Windows functions, which are defined in the POSIX and Win32 specifications but are undefined behavior from the perspective of the C standard, in order to access the data that the program is (de)serializing in the first place. Or is there a portable way to do this that I'm somehow missing?
So following your argument you could say that it's actually Objective-C? You could put a .mm on there and it would still execute.
- Holy crap, I've got MOD points! Who thought that was a good idea.