Code Challenge: Dynamic Nested Loops

While working on a little side project, I came across the need execute a dynamic number of loops. It’s something I’d never done before, and solving was a bit of work. But it was also a fun challenge, so I’m posting here for the fun of it.

If the idea is a little vague, here’s what static loops would look like (in JavaScript):

var arr1 = [0, 1, 2, 3, 4, 5];
var arr2 = ["a", "b", "c"];
var arr3 = ["black", "red", "green", "yellow"];

function do_stuff(data)
{
    console.log(data.join(" "));
}

function static(func, arr1, arr2, arr3)
{
    /// Assuming you knew there were always three arrays, you could just do this.
    /// But what if the number or arrays was dynamic?
    for (var i = 0; i < arr1.length; i += 1) {
        for (var j = 0; j < arr2.length; j += 1) {
            for (var k = 0; k < arr3.length; k += 1) {
                func([arr1[i], arr2[j], arr3[k]]);
            }
        }
    }
}

static(do_stuff, arr1, arr2, arr3);

 

So, here’s a template to get started to make a dynamic loop:

var arr1 = [0, 1, 2, 3, 4, 5];
var arr2 = ["a", "b", "c"];
var arr3 = ["black", "red", "green", "yellow"];

function do_stuff(data)
{
    console.log(data.join(" "));
}

function dynamic(func, arrs)
{
    /// <--- Your code goes here.
}

dynamic(do_stuff, [arr1, arr2, arr3]);

I’ll post my solution later.

Advertisements

There is a Book…

I recently watched the Bill Nye and Ken Ham debate. It was basically what I expected. Ken explains that evolutionists base their theories on faulty and unprovable assumptions, and then Bill goes on to use faulty and unprovable assumptions to try to prove evolution. The thing that stuck out to me was that Bill refused to acknowledge that there is a difference between historical sciences and observational sciences. Apparently that’s a common canard used to bolster evolutionists’ claims to the uninformed public, but it is an obvious fact that evolutionists readily acknowledge in their own company. Consider this quote from Ernst Mayr (a famous evolutionist):

Evolutionary biology, in contrast with physics and chemistry, is a historical science—the evolutionist attempts to explain events and processes that have already taken place. Laws and experiments are inappropriate techniques for the explication of such events and processes. Instead one constructs a historical narrative, consisting of a tentative reconstruction of the particular scenario that led to the events one is trying to explain.
(emphasis mine)

It’s too bad Bill Nye was not that honest.

I also thought it was humorous that Bill tried to act like it was OK to be a Christian and believe in God…just as long as God doesn’t do anything. The sad thing is that there are people that believe that. But does that make any sense? Think about some of the primary Christian tenants, like the virgin birth and Christ’s death and resurrection. What’s harder: creating a star (which is just a ball of gas) or creating a human in the womb? What’s harder: creating a single-celled organism or bringing a human back to life? If you can’t believe in Genesis, how could you ever believe anything else in the Bible?

This got me thinking: Where did this idea come from? Coincidentally, the answer’s in Genesis. This idea that God is nothing special goes all the way back to the first deceiver: Satan. He used this logic on Eve. “[Y]e shall be as gods,” Satan claims in Genesis 3:5. Now, stop and think about that claim. If a created human being can be like God by simply adding knowledge, what is Satan’s view of God?

Evidently, Satan merely views God as a slightly more educated being than he. If we stop and ponder Satan’s actions, it’s clear that he does not acknowledge God to be the infinite, all powerful, all knowing creator God. Think about it. There was a day when Satan woke up. He did not always exist. As far as he knows, God is just one day older than he. Why else would Satan have rebelled against God. Fighting against an all powerful, all knowing God is ludicrous. No one would do that. Victory is obviously impossible. Satan must believe that he can win. I love the way Milton put it in Paradise Lost (which Aaron and I are reading together). Writing insightfully in the sixteen hundreds, this is how Satan and his demons try to explain their defeat:

Whether upheld by strength, or Chance, or Fate,
Too well I see and rue the dire event

In other words: “Maybe God was just lucky. Maybe we can beat him next time.” I actually had a dear friend tell me something similar. He said that if Jesus ever did come back, he would fight against Him. Well, you can try, but God will always win. And it’s not by chance.

Bill Nye never had a chance either. Why? Because he never acknowledged his presuppositions, i.e., his religion. And that is the real debate.

When confronted with straightforward questions, like “where did matter come from,” Bill was at a total loss. Ken Ham, on the other hand, had an answer: “There is a book….” Bill Nye might not like the idea of God creating, but it’s the only answer that makes any scientific sense.

The King of Quines

I was reading a speech by Ken Thompson yesterday, and I was reminded about quines. I heard of quines before, but I had never attempted to write one myself. I liked the way he put it:

In college, before video games, we would amuse ourselves by posing programming exercises. One of the favorites was to write the shortest self-reproducing program. […] If you have never done this, I urge you to try it on your own. The discovery of how to do it is a revelation that far surpasses any benefit obtained by being told how to do it.

I thought it would be a fun learning experience, so I set out to write the shortest quine in JavaScript (and node.js in particular). And I think I succeeded, so thought I would post it here for hackish amusement.

If you’ve never written a quine before, you owe it to yourself to stop now and try. Like Ken said, it’s better to discover how yourself.

I quickly came up with this simple quine:

(function a()
{
    console.log("(" + a.toString() + "())");
}())

Obviously, this can be improved upon to this:

(function a(){console.log("("+a.toString()+"())")}())

After some searching, I discovered it could be shortened slightly to something like this:

+function a(){console.log("+"+a.toString()+"()")}()

However, I wasn’t satisfied. That’s a whole 52 bytes, and look at all those long keywords like function and console. Clearly, I’m going about this the wrong way.[1]

Then I had an idea, but I couldn’t quite get it to work. But in bed late last night, it came to me (sometimes I think God programs for me in my sleep). It took some time to get it just right, but it works…with some caveats.

First, it only works in node.js on *nix systems, like Linux and OS X. Second, it has to have a specific, and rather odd, name and path. Third, by default, it prints it’s source code to stderr instead of stdout, so you may have to run it just right.

So, here it is, my 21 byte JavaScript quine:


/**/a:3 throw 0 ^ 0

It’s so ugly it’s beautiful. And yes, there’s a line break before and after.

Like I said, it needs to be in a specific path. For this to work, you have to create a folder in the root directory called **. The file then needs to be inside /** and named a. (Did I mention you need root privileges?)

Simply running the program from the command line will display the source code, but if you want to get it to send the source to stdout, you need to run it like this:

node /**/a 2>&1

So if you want to confirm that this quine really works, you can just run the following:

node /**/a > /**/out 2>&1
diff -s /**/a /**/out

It should reply with Files /**/a and /**/out are identical.

And if you’re wondering why a:3 is valid JavaScript code, it’s because a: is a label, and 3 is merely a number primitive that JavaScript simply evaluates to it’s value. The caret (^) and final 0 are also valid; since line breaks are ignored, the code becomes 0^0, which is simply an XOR bitwise operation that does absolutely nothing.

So, what do you think? Is throwing an error cheating?

Can you shorten it further?

I was first going for //a:3 but couldn’t get that to work. Could you do something with a regular expression? /a/;a:3 works too but doesn’t save you anything.

By the way, I setup a github repository just for the fun of it.

[1] ^ I realized later the introspective quine can be shortened further, but that’s beside the point.

Audio Exercises for Comprehensive Articulatory Phonetics Online

I am very pleased on announce that the audio exercises for Comprehensive Articulatory Phonetics can now be downloaded for free.  Individual exercises can be downloaded from this Google Drive folder; furthermore, the entire set of MP3’s can be downloaded be found in this zip file. Feel free to send any feedback you may have.