
Understanding Algorithms Through Pseudocode
There are many ways to write a piece of code. Sometimes you improvise. On some other days, you try to come up with a really efficient solution. There are even ways to quantify the efficiency of the solution. But when it comes to explaining an algorithm to other people, what would be the most effective way to do so? What even is an “algorithm” in the first place? In this post, we’re going to answer these questions.
What is an algorithm?
According to Cambridge Dictionary, an algorithm is “a set of mathematical instructions or rules that, especially if given to a computer, will help to calculate an answer to a problem.” For example, imagine you’re inserting a letter into the middle of a string. Some people might split the string into two different strings, or some people might start with a completely new string. Here are some Python code for each example:
def insert_letter(existing_string: str, inserting_index: int, inserting_letter: str) -> str:
before = existing_string[0:inserting_index]
after = existing_string[inserting_index:len(existing_string)]
return before + inserting_letter + after
def insert_letter(existing_string: str, inserting_index: int, inserting_letter: str) -> str:
new_string = ""
for index, item in enumerate(existing_string):
if index is inserting_index:
new_string += inserting_letter
new_string += item
return new_string
As you can see from the examples, there is definitely more than just one way to solve this problem. Each solution can be considered an algorithm, which, again, is a set of mathematical instructions.
Different Ways to Visualize Different Algorithms
When we work with other people, we sometimes end up in a situation where we need to explain an algorithm to others. Some algorithms are simple enough to simply write out the code right away. But sometimes algorithms can be really complex. In that case, you might not want to spend all day just to write functionally working code. Because in some languages (like Rust), it can take a lot of time just to make the code compile.
That’s why we often use different means to help people understand. It lets you explain the code without actually writing the code. This includes flow charts, pseudocode, drawing on a whiteboard, step by step walkthroughs, and more.


insert_letter
function implementations.Flowcharts are diagrams that depict a process.
So, what is pseudocode?
Based on the definition on Wikipedia, pseudocode is a description of the steps in an algorithm using a mix of conventions of programming languages. In other words, pseudocode is an explanatory code that is not actual code. It’s also language agnostic, meaning no particular language or paradigm is prioritized. It’s a pretty nice communication tool.
Below is an example pseudocode of a name-spelling program:
function main() {
name_input = input("What is your name : ");
is_loop = true;
while (is_loop) {
agreement_input = input("Can I spell your name\nEnter 'y' or 'n'. : ");
if (!agreement_input.is_empty()) {
is_loop = false;
}
}
if (agreement_input == "y") {
for (item) in (name_input.chars()) {
print(item);
}
print("Here is your name spell {}", name_input);
} else {
print("Exiting the prgram...");
}
}
use std::io::{self, Write};
fn main() {
let mut name_input = String::new();
print!("\nWhat is your name : ");
io::stdout().flush().unwrap();
io::stdin()
.read_line(&mut name_input)
.expect("Failed to read input");
let mut is_loop = true;
let mut agreement_input = String::new();
while is_loop {
print!("\nCan I spell your name?");
io::stdout().flush().unwrap();
print!("\nEnter 'y' or 'n'. : ");
io::stdout().flush().unwrap();
io::stdin()
.read_line(&mut agreement_input)
.expect("Failed to read input");
agreement_input = agreement_input.trim().to_string();
if !agreement_input.is_empty() {
is_loop = false;
}
}
if agreement_input == "y" {
for item in name_input.chars() {
println!("{}", item);
}
println!("Here is your name spell, {}!", name_input.trim());
} else {
println!("Exiting the program...")
}
}
What do you think? Do you see the similarity? It simplifies many language specific parts of the Rust code. But remember, pseudocode per se is not real code. It means you decide the syntax you use. It’s literally up to your preferences!
Leave a Reply