- Mathspp Insider 🐍🚀
- Posts
- Graphs saved the Ten Queens
Graphs saved the Ten Queens
Hey there, 👋 How is your Python going? In this Mathspp Insider 🐍🚀 email we’ll talk about how I helped the Ten Queens with their conundrum. |
I pressed Enter
and waited nervously for a second.
The number 724
was printed in the REPL below my function call:
>>> queens(10)
724
After a split second Queen #3 shouted “BRILLIANT!”. Her enthusiasm was welcome! But there really was no need to shout so close to my ears…
Queen #7 wasn’t so easily impressed. “How can we know this is correct?”.
I proceeded to explain:
“The problem isn’t that difficult. It may sound difficult… But it is not. I’ve solved many different problems like this one in my bootcamp. See–”
Queen #7 silenced me with her hand.
“If you have solved different problems how can they be like this one?”
I smiled. It was an excellent question. But a simple question. A question coming from someone who hasn’t solved many problems before. A question from someone who doesn’t do programming.
“To solve a problem you need to look beyond the words used to describe the problem.”
“Explain. Please.” Queen #7 was starting to show her impatience.
“You explained what you needed when we talked before. The ten of you wanted to be distributed on a board without clashing with each other.
After our conversation I had to think for a bit... And then the EUREKA moment struck.
Experience told me this was a problem I could solve with a graph searching algorithm. Experience that I obtained from solving problems like the ones from my bootcamp.
I have solved so many graph searching problems that I could have written the code with my eyes closed.
I didn’t do it because I wanted to avoid typos. But I could have.”
Queen #7 raised her eyebrow. My words were rubbing her the wrong way. I decided to correct course:
“I don’t mean that in an arrogant way. Let me put it another way.
You’ve been queen for many years. I am certain there are recurring problems your people need help with. – Queen #7 nodded – Aren’t recurring problems easier to solve?”
Queen #7 nodded again: “Whenever a bishop and a knight waltz in here I know what’s coming. It’s always the bishop that envies the knight or vice-versa.
Sometimes it’s the knight that wanted to move diagonally instead of in its weird way.
Sometimes it’s the bishop that wanted to ride a horse instead of having to walk.
But it’s always about envy.
It’s always the same problem.”
“Precisely.
You’ve been queen longer than I’ve been writing code. But I’ve practiced a lot. And this practice gave me the tools to solve this with 20 lines of code.”
Queen #7 smiled. I think. It’s hard to tell.
She then concluded: “Very well. Now I understand why you say this was like other different problems you’ve solved.
Walk me through your 20 lines of code.”
And that’s what I did.
I started explaining my 20 lines of code.
A couple of minutes later Queen #7 was satisfied.
She waved her hand and four pawns walked in.
They carried a beautiful chest with intricate engravings filled with gold coins.
I was puzzled and turned to Queen #7.
Queen #7 was definitely smiling.
“Payment for your help.”
If you want, you can see the code I showed the queen.
Here’s a link to the 20 lines of code that solves the “ten queens problem”.
⚠️ Today I tried a different email format.
Use the poll below and / or reply to this email to let me know what you thought of it.
🐍🚀 How was this email? |