Karel needs to fill the entire world with beepers regardless of the size of the world. Other than the first column, the rest of the world are segmented by horizontal walls, preventing Karel from getting to the next row above. Our goal is to write code that does the job no matter how large (or small) the world is.
The problem looks pretty daunting at first. How can Karel know when to put beepers or when to move?
Well the solution lies in decomposition.
The Thinking Through
If your dad asks you to lay down the tiles in your porch, how would you do that?
You probably would lay one row of tiles at a time.
Then you get to the next row and lay down tiles there. So on and so forth.
Until you get to the last row, lay down the tiles, and stop.
That is exactly what we are going to do here:
finish one row
move to the next row
1. Finish One Row
Karel will have only finished a row when she:
puts beepers on every spot in that row
turns around, and
moves all the way back to the wall
At this point, Karel will be facing west, ready to move to the next row.
2. Move to the Next Row
Once Karel finishes a row, she will be ready to move up to the next row. The steps that Karel needs to follow are pretty simple:
turn right
move
turn right
At this point, Karel will be, again, facing an empty row, ready to lay down the beepers for that row, so it’s just a matter of repeating the same process.
But when should Karel stop?
3. The Condition to Stop
For every row other the last one, Karel will return to the first spot after laying down all the beepers, which means Karel “knows” when she is in the last row.
How is it possible that Karel knows this?
Well, we need to find some quality that only the last row has, differentiating it from the rest of rows.
Every time Karel starts from the first spot, ready to put down beepers for a certain row, her left is clear other than when she is in the last row. It will be easier if you try visualize like Karel is reaching out with her left hand, and only when she is in the last row that her left is NOT clear.
Writing the Code
1. Finish One Row
Recall that to finish one row, Karel will need to do three things:
We will keep breaking down each of these three functions:
def laying_tiles():
put_beeper()
while front_is_clear():
move()
put_beeper()
def turn_around():
turn_left()
turn_left()
def move_to_wall():
while front_is_clear():
move()
2. Move to the Next Row
def move_to_next_row():
turn_right()
move()
turn_right()
def turn_right():
for i in range(3):
turn_left()
By this point, we are ready to write the whole thing. Remember that while Karel’s left is clear, she should keep finishing one row at a time and move to the next row, and when it’s not clear, she should simply lay down the tiles and call it a day.
def main():
while left_is_clear():
finish_one_row()
move_to_next_row()
laying_tiles()
def finish_one_row():
laying_tiles()
turn_around()
move_to_wall()
def turn_around():
turn_left()
turn_left()
def move_to_wall():
while front_is_clear():
move()
def laying_tiles():
put_beeper()
while front_is_clear():
move()
put_beeper()
def move_to_next_row():
turn_right()
move()
turn_right()
def turn_right():
for i in range(3):
turn_left()
# There is no need to edit code beyond this point
if __name__ == '__main__':
main()
The main function is simple, easy-to-read, and elegant. This is the beauty of decomposition. And by finding the unique quality of the stop condition, we manage to construct a universal solution with the while loop.
Spread beepers is an interesting problem. While it’s easy for a human eye to solve the problem, it actually takes a bit of decomposing skills for Karel to finish the job.
Karel has the ability to find out whether there are beepers present, but she cannot know how many left. In other words, Karel doesn’t count.
The Thinking
Since the pile of beepers is always on the second corner, Karel should move just once to the beepers location. After that, Karel can start doing her work. Since Karel already possesses infinite numbers of beepers in her bag, and Karel doesn’t know how to count, she cannot pick up all the beepers at once. Instead, Karel should pick up a beeper, go to the next empty location, put down the beeper, and return to the starting corner, facing east.
One of the most difficult issue with this problem is that since Karel doesn’t know how to count, she will keep picking up beeper even when there is only one left. Our solution to this issue is to put the beeper back if Karel incorrectly picks up the last beeper from the second corner. To accomplish this, we will have to put in an additional layer of condition.
def spread():
while beepers_present():
pick_beeper() # karel can only pick
if beepers_present():
move_to_next()
put_beeper()
move_to_start()
move()
put_beeper()
def turn_back():
turn_left()
turn_left()
def move_to_start():
turn_back()
while front_is_clear():
move()
turn_back()
def move_to_next():
while beepers_present():
move()
The while loop and the if loop together helps Karel understand when she needs to stop picking up beepers. If you understand this, the solution become natural.
The Coding
from karel.stanfordkarel import *
# let it go to starting point at the end
## helper function
## 1 turn back
def turn_back():
turn_left()
turn_left()
## 2 move to the start
def move_to_start():
turn_back()
while front_is_clear():
move()
turn_back()
## 3 move to next
def move_to_next():
while beepers_present():
move()
## 4 work function find and spread beeper
def spread():
while beepers_present():
pick_beeper() # karel can only pick
if beepers_present():
move_to_next()
put_beeper()
move_to_start()
move()
put_beeper()
## Main
def main():
move()
spread()
move_to_start()
# There is no need to edit code beyond this point
if __name__ == '__main__':
main()
Ever wonder how your favorite email newsletter sends you emails from a personal email addresses such as pat@starterstory.com instead of something like noreply@starterstory.com? With Ghost you can manage your email configuration easily. However, there could be some complications.
“noreply”: The default email configuration in Ghost
If you belong to the not-so-technical camp but still desire to write online, Ghost’s hosting service could save you some trouble. When you set things up right, you will be able to write a post and publish it on the site and send it as an email newsletter to your subscribers at the same time.
This is my first newsletter sent to myself with Ghost. The email was sent from noreply@michaelshoe.com via m.ghost.io. The only thing I did before this was linking my own domain with Ghost, and magically, I am capable of sending out email newsletters in bulk to my subscribers (though I still don’t know the mechanism behind)!
I want to get “personal” with my dear followers.
To be specific, my goal is to accomplish these two things:
Enable readers to reply to the sending domain (i.e. replying to noreply@michaelshoe.com) and have a direct one-on-one email communication with me (i.e. hit reply, write an email to me directly)
Use a custom email sending domain to send out newsletters, i.e. replace noreply@michaelshoe.com with something like hi@michaelshoe.com
The process, as it turned out, was easier said than done.
Enabling readers to reply to the sending domain
Accomplishing #1 is my priority, since it is important to talk to my dear subscribers directly in my initial phase, an embodiment of “doing the things that don’t scale” mantra. I tried to reply to noreply@michaelshoe.com, and then checked my personal email – the one I used to sign up to Ghost – the email was nowhere to be found.
I then went to my domain registrar, Porkbun, to look for some clue. Porkbun actually provides free email forwarding up to 20 for free if you purchased a domain with them, and I swiftly set it up to have emails sent to noreply@michaelshoe.com delivered to my personal email. However, even though I can receive emails sent to noreply@michaelshoe.com, I can only reply via my personal email address. Additionally, I disliked “noreply” as it presents a robotic feeling, and it literally tells people NOT TO REPLY. So I kept on looking for a better solution.
Just type in “noreply” in the first box and type in your own email address in the second
Use a custom email sending domain to send out email newsletters
To accomplish #2, I first activated an email account with my domain registrar, Porkbun, by leveraging their free three-month email domain service. Now I have my own custom email domain, hi@michaelshoe.com, with their email host. I also configured DMARC, an additional security measure that uses SPF and DKIM to help prevent email spoofing, even though I have absolutely no idea what it means (more on this later). After I’ve acquired my own email domain, I also set it up so that I can send and receive emails from hi@michaelshoe.com in my Gmail mailbox. If you are also using Porkbun, you can read “How to Set Up an Email Address in Gmail” here.
If you are using Ghost (Pro) to send email newsletters, there are two places where you need to make further configurations if you want to use your custom email sending domain. The first place is your membership portal, and the second place is your newsletter.
Membership portal
Your membership portal (Membership -> Portal settings) is the place to:
Customize the look & feel of your signup button and your signup modal
Change the support email address, i.e. the email address that sends the welcome email to the first-time subscriber
Membership -> Portal settings -> Account page
Newsletter
“Newsletters” is the place to:
Customize the design of your newsletter, i.e. how it arrives in your subscribers’ mailbox
Set up basic newsletter info, i.e. name, basic descriptions, and sender email address and reply-to email address
Solving one problem created an even bigger one
I changed all email addresses to my own hi@michaelshoe.com, both for the Membership Portal and for the Newsletter, and then gave it a shot. Something horrible happened afterwards: all my email newsletters and signup welcome emails went straight into spam; when I dug them out of the graveyard, they were covered with a warning sign like this:
What made this an even bigger problem was that regardless of how I tried to tell Gmail that this wasn’t spam, Gmail was adamant about its original opinion. I clicked on the “Looks safe” button, and marked it as no spam, and tried sending again. Again the letter ended up in the spam.
At this point, I’ve exhausted all my skills and methods, so I resorted to the Ghost support team for help. They replied within a few hours and gave me a clearcut solution – disable DMARC in my email hosting. It worked.
As it turned out, Ghost (Pro) offers custom sending domains, but this feature is only supported on the Creator Plan or higher on Ghost (Pro). As a humble Starter Plan user, I don’t have the luxury to this feature.
I’ve figured out how email configuration works in Ghost, but decided it’s not worth it to change.
At this point, I was able to accomplish both goals, i.e.:
Enable readers to reply to the sending domain (i.e. replying to hi@michaelshoe.com) and have a direct one-on-one email communication with me
Use a custom email sending domain to send out newsletters (hi@michaelshoe.com, instead of the default “noreply”)
However, I’ve decided to revert back to Ghost’s default sender addresses. I’m not technical enough to figure out the problems, and it’s just not worth it to risk not delivering the newsletters into every subscriber’s mailbox. It doesn’t matter how personal-sounding your name is if all your emails are ending up in the readers’ spam. Ghost has done a great job to ensure deliverability and it’s just too risky for me to change that. Plus DMARC sounds too important to be turned off.
I still want to have that personal connection with my subscribers, and my solution is simply to add my hi@michaelshoe.com email at the bottom of each newsletter so people can just click and start writing to me.
P.S. the question that finally got answered: who created the “noreply” email domain and why I can only send, but not receive, emails from it?
This question finally got answered by the Ghost support team (kudos to them!).
Sharing my learnings about the core skills on how to run life like a company in the Me Inc. Newsletter. Topics covered: Copywriting, Finance, Technology, and Productivity. Read my why here or join directly👇. No spams. Ever. – Michael
Welcome to the Me Inc. Newsletter.
Build yourself into a business, before you start one.
Learn. Grow. Build. Now.
function ml_webform_success_14362356() { var $ = ml_jQuery || jQuery; $(‘.ml-subscribe-form-14362356 .row-success’).show(); $(‘.ml-subscribe-form-14362356 .row-form’).hide(); } fetch(“https://assets.mailerlite.com/jsonp/918262/forms/119754151914637238/takel”)
Piles Karel is a relatively simple problem. In a 7 by 5 world, there are three piles of beepers in the first row, and Karel should pick them up.
The simplest method would be to get Karel to move to each beeper location and pick all the beepers up, and move to the next pile, etc. The code will look at this following the method:
def main():
move()
while beepers_present():
pick_beeper()
move()
move()
while beepers_present():
pick_beeper()
move()
move()
while beepers_present():
pick_beeper()
move()
if __name__ == '__main__':
main()
Incorporating Decomposition
While the above code does the job, it’s hard to read.
Karel really is just doing two things while solving this problem:
move
pick up beepers
Move is just … move, and there is no room for further docomposition. However, we can write a new function to have Karel pick all the beepers on a single spot.
def pick_all_beepers():
While beepers_present():
pick_beeper
So whenever there are beepers present, we can just use pick_all_beepers( ):
What if we don’t know the exact size of world and locations of beepers?
Before we were dealing with a certain world with a set size and beeper locations. We are using our human eyes to spot where beepers are located, and we are actually doing half of the work.
How can we write codes so that Karel can deal with an uncertain world? Would Karel be able to identify where beepers are located, pick them up, and stop right in front of the wall?
This is a very important point. Whenever we are writing codes, we should try to provide a universal solution that is capable of dealing with all types of situations.
Recall that Karel does only two things in solving this problem:
move
pick up beepers
And Karel should be doing exactly those two things while its front is clear. And because we don’t know the exact size of the world – the world could be just ONE single column – meaning that Karel may not be able to move at all, we still need Karel to pick up those beepers in her original spot. After that, Karel should move and pick up beepers until she hits the wall.
The final solution
The code then will look like this:
def main():
pick_all_beeper()
while front_is_clear():
move()
pick_all_beeper()
def pick_all_beeper():
while beepers_present():
pick_beeper()
if __name__ == '__main__':
main()
This solution can work in all sizes of worlds, and it’s much more readable by human eyes. Karel is doing exactly what the names of those functions are telling you: she first picks all the beepers in her position, and while her front is clear, she will keep moving and picking all beepers in each spot, on spot at a time, until she faces a wall, i.e. her front is NOT clear.
Sharing my learnings about the core skills on how to run life like a company in the Me Inc. Newsletter. Topics covered: Copywriting, Finance, Technology, and Productivity. Read my why here or join directly👇. No spams. Ever. – Michael
Welcome to the Me Inc. Newsletter.
Build yourself into a business, before you start one.
One of the most important concept in coding is decomposition, which is the art and skill of dividing a big problem into smaller units. It’s a type of skill because you will need to know how to use different functions to get the results you want to see in each step; it’s a type of art because the basic functions won’t get the results directly, so you will need to figure out your way of getting there, i.e. the art of breaking down a big problem into smaller problem that the functions can work their magic.
Jigsaw Karel, though not a complicated problem per se, illustrates this concept.
The Problem of Jigsaw Karel
The problem is quite straightforward. Karel starts from the bottom left and will pick up the last piece of the puzzle (beeper) and put it into the only empty spot. Karel will then return to its starting position.
Remember, Karel can only do four things:
move( )
pick_beeper( )
put_beeper( )
turn_left( )
The solution without decomposition
I can certainly ask Karel to move and pick up the beeper and put it in the spot by using the four functions in the most basic manner without considering decomposition.
from karel.stanfordkarel import * def main(): move() move() pick_beeper() # Karel now stands on the beeper, and picks it up. move() turn_left() move() move() put_beeper() # Karel now stands on the empty spot, and puts down the beeper. turn_left() turn_left() move() move() turn_left() turn_left() turn_left() move() move() move() turn_left() turn_left()if __name__ == '__main__': main()
The problem, however, is that the next reader of the code will NOT easily understand what I’m trying to do. Not unless I explain to him directly. In fact, I won’t be able to read it just about five minutes after I wrote this.
The solution with decomposition
So decomposition is really for human, not for machine. Machine doesn’t care or discriminate, and will be able put the puzzle back to its place no matter what. Human, on the other hand, might need some help in understanding.
Now, let’s see how we can divvy up this simple problem and try make it more readable.
At its core, the Jigsaw Karel problem is about picking up something, putting it down, and return to Karel’s original position. We can use the status of the beeper to separate the program, i.e. to decompose. Here are the steps:
1. Move and pick up the beeper
To finish this first step, Karel needs to move forward twice and pick up the beeper. We can simply name this function move_and_pick( ).
def move_and_pick(): for i in range(2): move() pick_beeper()
2. Put the beeper in place
After that, Karel will take the beeper in her pocket to the empty spot in the puzzle. We can name this part put_puzzle( ).
def put_puzzle(): move() turn_left() for i in range(2): move() put_beeper() for i in range(2): turn_left()
3. Return to initial position
Now that Karel has finished her job, she needs to return to her initial position, which I call Ground Zero. So we can call this function return_to_zero( ).
def return_to_zero(): for i in range(2): move() for i in range(3): turn_left() for i in range(3): move() for i in range(2): turn_left()
Now that we’ve defined the three decomposed steps, all we need to do is to call these functions in the main function. Here is my solution to Jigsaw Karel:
from karel.stanfordkarel import *def main(): move_and_pick() put_puzzle() return_to_zero()def move_and_pick(): for i in range(2): move() pick_beeper() def put_puzzle(): move() turn_left() for i in range(2): move() put_beeper() for i in range(2): turn_left()def return_to_zero(): for i in range(2): move() for i in range(3): turn_left() for i in range(3): move() for i in range(2): turn_left() # There is no need to edit code beyond this pointif __name__ == '__main__': main()
This solution is decomposed into three functions that are easy to understand. One doesn’t need to spend lots of time trying to figure out the coder’s original thinking.
Final thoughts
Obviously, there is room for improvement. For example, there are several times when Karel has to turn left twice or three times, which can be simplified. If you think about it, turning left twice is actually turning around, and turning left three times is turning right!
def turn_around(): for i in range(2): turn_left()def turn_right(): for i in range(3): turn_left()
In addition, you should pay attention to the continuity of the final status in each decomposed step. What I mean is that you should maintain a level of consistency in how Karel finishes a step. For example, if Karel picks up the beeper in the first step (function), she should also put the beeper down in the second step to stay consistent. In this way, Karel will always be ready to take action to the next step.
Sharing my learnings about the core skills on how to run life like a company in the Me Inc. Newsletter. Topics covered: Copywriting, Finance, Technology, and Productivity. Read my why here or join directly👇. No spams. Ever. – Michael
Welcome to the Me Inc. Newsletter.
Build yourself into a business, before you start one.
Learn. Grow. Build. Now.
function ml_webform_success_14362356() { var $ = ml_jQuery || jQuery; $(‘.ml-subscribe-form-14362356 .row-success’).show(); $(‘.ml-subscribe-form-14362356 .row-form’).hide(); } fetch(“https://assets.mailerlite.com/jsonp/918262/forms/119754151914637238/takel”)