SQLzoo XOR solution

I was feeling good about the speed that I was progressing through the early SQLzoo SQL tutorials. Then I hit question eight in the “SELECT FROM world” tutorial, and couldn’t work out what response the question was seeking. It seems from the various help threads on StackOverflow and Reddit that I am not the only one who has had this issue.

To my mind, the phrasing of the question implies that there is an XOR feature in SQL, as there is in many other languages. So first I tried using the same format as the OR-based answer to the previous question, but changing the operator:

SELECT name, population, area
FROM world
WHERE area > 3000000 XOR population > 250000000

This didn’t work. So then I think “maybe there is no XOR operator in SQL and I have to do this manually”. So I try:

SELECT name, area, population
FROM   world
WHERE  (area > 3000000 AND population <= 250000000)
OR     (area <= 3000000 AND population > 250000000)

This gives the following error:

Wrong answer. Some of the data is incorrect.

But the data are not incorrect. Only after some fretting did I realise that SQLzoo wants you to SELECT the population before you call the area. Hence the correct solution is:

SELECT name, population, area
FROM   world
WHERE  (area > 3000000 AND population <= 250000000)
OR     (area <= 3000000 AND population > 250000000)

This got me the “Correct answer” result, and the weird happy-face emoji. Onwards!

Swapping two variable values without using a third value

I have been cautiously dipping my toe into the Odin Project, an open-source online resource for learning to code. One of the downsides of the project is that it specifically tells you not to get lured down rabbit-holes, before linking to several supplementary articles. These link to others, of course, and then… well, in my case I ended up reading about programmers who can’t program.

Apparently it is not unusual to have candidates with no programming skills apply to programming jobs. The article suggests some screening tests to weed out these optimists. I would fail most of these tests. This is unsurprising, as I don’t consider myself a programmer, despite an amateur interest and some past experience with fairly basic Python, R, and PHP scripts. However, in the comments there was one screening problem that was described as:

swap two variables without using a third variable

This piqued my interest, because it seemed pretty easy. I was intrigued that one comment said:

“We hired the guy who said, well, “if they’re integers, then I’d do it by a=a|b, b=a^b, a=a^b. But I don’t know how to do it if they’re strings.”

I then found a whole plethora of answers on StackOverflow about how to do this in C++, none of which looked especially simple.

So I fired up R Studio Cloud and tried the following:

a <- b <- c(a,b)
a <- a[2]
b <- b[1]

Is this an acceptable solution? It works for and a and b of the same datatype, including integers or strings. Although a vector is created, this is not stored in a third variable. I would have thought this approach could be recreated in most languages. It works identically in Python, for example:

a = b = [a,b]
a = a[1]
b = b[0]