comment 0

Day Knight, Fighter of the Night Knight

Welcome to the Island of Knights, Knaves, and Sir Mix-a-lot! There are a few things you should know about this island:

  • Knights always tell the truth
  • Knaves always lie
  • There is exactly one Knight named “Sir Mix-a-lot”. Like every other knight, he cannot lie.
  • This island is one of a chain of islands containing knights and knaves, each one having its own quirk.

Today we are on a very interesting island, the Island of Day Knights and Night Knights. Day knights always tell the truth during the day, but lie at night. Night knights tell the truth at night, but lie during the day. Annoyingly, they all live in structures built underground, so it is impossible for visitors at any given moment to know whether it is daytime or nighttime on the surface.

You are walking along an underground street when you happen upon a resident of the island. You have no idea what time it is, being underground and all, but you’d like to know if it’s daytime or nighttime. How many questions do you need to ask the resident to figure it out?

 

 

 

Solution:

You can do it with one question, “Are you a day knight?” A “Yes” response means that it is daytime, and a “No” response means it is night. See if you can figure out why. I’ll demonstrate why in my next post, which I will link to here later on.

comment 0

Map it

Let’s say we have a ruby array with some numbers in it!

We truly know how to live! But I’m not done yet. Let us also suppose that we want to multiply every number in the array by 2 and save the result in a new array. We could do something like this:

It’s doable with  each , but  Array has all sorts of iterative instance methods that take a block for a reason. It turns out that one of them does exactly what we want — it invokes the given block once for each element of an array, and returns a new array containing the values returned by the block, like so:

Over the coming weeks I’m going to try to blog about many of the instance methods in  Array . It’s a class I should know better.

comment 0

To the left? Now Slide!

Welcome to the Island of Knights, Knaves, and Sir Mix-a-lot! There are a few things you should know about this island:

  • Knights always tell the truth
  • Knaves always lie
  • There is exactly one Knight named “Sir Mix-a-lot”. Like every other knight, he cannot lie.
  • This island is one of a chain of islands containing knights and knaves, each one having its own quirk.

Today we are on the basic island of Knights and Knaves. We are driving along, trying to find the way back to our hotel when we come to a fork in the road. There is a resident of the island standing alongside the road, so I get out out to ask him for directions. I ask one question before returning to the car, to which the resident nods “Yes.” Upon returning to the car, I say, “We have to go left at this fork in the road to get back to our hotel.” You ask, “Did he tell you that? Do you even know if he is a knight or a knave?” I respond, “I have no idea if he is a knight or a knave, but I do know we need to go left.” What did I ask him?

 

 

Solution:

If I had just asked, “Is the hotel to the left?” I would have no way of knowing if his answer was the truth or not. The resident could have been lying and I would have no way of knowing. Instead, I asked, “Are you the type of person who could claim that the hotel is to the left?” Assuming that the hotel is actually to the left, a knight would answer “Yes.” Surprisingly, a knave would also answer “Yes.” If asked directly if the hotel were to the left, he would lie and say “No,” but since he is a knave, he will also lie about lying.

Perhaps it would be easier to think about it another way. What would a knave say to, “Are you the type of person who could claim 2 + 2 = 4?” That question could be changed to “Are you the type of person who tells the truth?” Even more simply, you could ask, “Are you a knight?” A knave would certainly say yes to all of those questions.

Conversely, a “No” answer would have meant the hotel was to the right, no matter what.

comment 0

The Leader

By default the leader key in vim is  \ . (For more on the leader key and mapping in general, see this.) You’ll find that many people remap the leader key to something easier to reach. A common choice is to use the comma as the leader.

But wait! The comma already has a use in normal mode, and it’s a key I actually use now and again. After searching high and low for a new leader key, I finally settled on the spacebar. I thought it was worthy of a blog post because it was more difficult to set up than most other choices would be.

The following two lines in my vimrc are responsible for mapping my leader key to the spacebar:

So there it is. Next time you’re feeling unproductive, remember that it took me an hour or so to figure this out.

comment 0

Pattern Recognition

I’m not sure if I’m getting better at pattern recognition in this area or if this is actually a relatively simple pattern to figure out, but I thought the pattern embedded in the following set of integers was fun to discover:

I found this set on page 73 of Gödel, Escher, Bach: an Eternal Golden Braid. If it hadn’t been presented in the context of positive and negative space, I think it would have taken me much longer to figure it out.

comment 0

Guilty or Not?

Welcome to the Island of Knights, Knaves, and Sir Mix-a-lot! There are a few things you should know about this island:

  • Knights always tell the truth
  • Knaves always lie
  • There is exactly one Knight named “Sir Mix-a-lot”. Like every other knight, he cannot lie.
  • This island is one of a chain of islands containing knights and knaves, each one having its own quirk.

Today we are in a police station on the regular Island of Knights and Knaves. A detective friend of yours brought a resident of the island in for questioning, and suspects him of committing a crime. Before the questioning could begin, the suspect said one sentence to the detective. The detective immediately said he was free to go. You walk up to the detective and he says, “Well, I don’t know if he is a knight or a knave, but I do know he is innocent!” What did the suspect say to the detective?

 

 

Solution:

One possibility is “A knave committed this crime.” If this sentence is true, the suspect is a knight and could not have committed the crime. If the sentence is false, then the suspect is a knave and once again could not have committed the crime, because if he did, our false statement would also be a true statement, and we would have a contradiction.

comment 0

The Little Schemer

I finished reading and working through The Little Schemer a few days ago. I’m still recovering from having my mind blown again and again.

It presented several programming concepts that were relatively new to me in a very unique format. I can see why some people would dislike it, but I thought it was wonderful. If I have one critique it would be that the last three chapters were significantly more challenging than the first seven. I probably spent twice as long on those chapters as I did on the rest of the book.

All the information required to succeed is present, however. The book is completely self contained. Several times I looked to sources outside of the book to better understand the concepts it was trying to teach, only to discover later on that the information I was looking for was in the book the entire time.

I give this book this many stars:

 

comment 0

Awake or Asleep? Knight or Knave?

Welcome to the Island of Knights, Knaves, and Sir Mix-a-lot! There are a few things you should know about this island:

  • Knights always tell the truth
  • Knaves always lie
  • There is exactly one Knight named “Sir Mix-a-lot”. Like every other knight, he cannot lie.

Today, we are on the island of sleepwalking knights and knaves. At night, half of the population sleepwalks around the island. When a resident sleepwalks, they are living in a dream world where everything is the opposite of reality. In other words, every belief held by a sleepwalking resident will be wrong. For example, if you ask an awake knight if he is a knight, he will say “yes”. If you ask a sleepwalking knight if he is a knight, he will say “no”! He is sleepwalking, so he believes he is a knave. Since he is actually a knight, he answers all questions truthfully, so he will tell you that he is actually a knave!

It’s nighttime on the island, and you come across a resident who says, “I am not an awake knight.” What is she?

 

 

Solution

First, our resident can’t be an awake knight, because it would be impossible for an awake knight to claim such a thing. A sleepwalking knight would believe himself to be awake, and would truthfully tell you so, so a sleepwalking knight can’t claim such a thing, either. An awake knave correctly believes he is what he is, but he will lie to you about that. To make the claim our resident is making would be a true statement for a knave to make, so this is impossible for an awake knave to make as well. This leaves only one possibility, so our resident is a sleepwalking knave. Can you figure out why a sleepwalking knave can make such a claim, but no other type of resident can?

comment 0

Netrw

Although it can seem like it’s a part of vim itself, netrw is actually a plugin that’s activated every time you use the  :e  command. There is extensive help to be found with  :help netrw , including a large list of ways you can customize it. I have the following two lines in my .vimrc:

The first line removes the large text banner that appears by default at the top of every netwr buffer. The second increases the amount of information listed about each file in a netrw buffer, showing the date last modified and the file size. The netrw help file is a few thousand lines long, and these two options barely scratch the surface of what’s available.