In the previous topic, we started discussing pseudocode and covered some basics concepts, such as variables, arithmetic operations, conditional statements, and some others. However, some algorithms require more complex constructions to be described. In this topic, we will learn more advanced concepts of our pseudocode, such as loops, arrays and functions. Being familiar with them will allow you to express more sophisticated algorithmic ideas in a simple and concise manner.

Loops

Loops are used to perform repeated calculations. We will use two kinds of loops: the while and the for loops. The while loop looks like this:

i = 0
while i < 5:
    print(i)
    i += 1

The syntax is the following: the while keyword followed by a condition, colon and a loop body.

Here is how the for loop looks like:

sum = 0
 
for i in 0..10:
   sum += i
 
print(sum) # 45

The 0..10 construction denotes a range of numbers from 00 to 1010. The last number is not included in the range. In general, the for i in a..b means that the variable i is sequentially assigned to all numbers from the range a..b.

Arrays

Arrays are used to store a collection of objects of the same type. To initialize an array, we will use the following construction:

array = [0] * 10

Here, the variable array denotes an array of 1010 elements equal to 00. We can also initialize an array with some data explicitly:

fib = [0, 1, 1, 2, 3, 5, 8]

The two most commonly used operations for arrays are getting the length and accessing elements. The enumeration of elements starts with 00. Let’s see an example of how it works:

x = fib[4] # x is 3
length = len(fib) # length is 7
 
for i in 0..len(fib):
   print(fib[i])

The last for loop iterates through the numbers in the fib array and prints all of them to a screen.

Another useful operation is getting a subarray of an array. It works as follows:

array = [0, 3, 2, 4, 1]
subarray = array[1..4]
print(subarray) # 3, 2, 4

To get a subarray, we just specify the desired range in square brackets. Remember that the last number is not included in the range.

Functions

Now, let’s learn how to write a function using our pseudocode. Below is a function that calculates the mean value of numbers in an array:

calc_mean(array):
    mean = 0
 
    for i in 0..len(array):
        mean += array[i]
    
    return mean / len(array)

First, we put a function’s name, then arguments in round brackets separated by spaces, after that a colon and a body. If we need to return something from a function, we use the return keyword, like in the above example.

Implementing simple algorithms using pseudocode

Let’s see how some simple algorithms can be implemented using the described pseudocode. The first example is a function that takes an array of number as input and returns either zero if the array is empty or the maximum number in the array:

find_max(array):
    if len(array) == 0:
        return 0
 
    max = array[0]
    
    for i in 1..len(array):
        if array[i] > max:
            max = array[i]
    
    return max

Another example is a function that merges two arrays. It takes two sorted arrays as input and returns one sorted array containing the numbers from both input arrays:

merge(left, right):
    merged = [] * (len(left) + len(right))
    
    i, j, k = 0, 0, 0
    
    while i < len(left) and j < len(right):
        if left[i] < right[j]:
            merged[k] = left[i]
            i += 1
        else:
            merged[k] = right[j]
            j += 1
        k += 1
 
    while i < len(left):
        merged[k] = left[i]
        i += 1
        k += 1
 
    while j < len(right):
        merged[k] = right[j]
        j += 1
        k += 1
 
    return merged

Summary

In this topic, we learned some advanced concepts of our pseudocode: loops, arrays and functions. Along with the concepts covered in the first part, they are enough to express both simple and complex algorithmic ideas in a clear manner. Further, we will use the introduced syntax to describe and learn algorithms.

Leave a Reply