Hacktoberfest and A Free T-Shirt

Digital Ocean and GitHub sponsor Hacktoberfest to support open source projects. Participants must register and complete 4 pull requests to an open source project.

I supported freeCodeCamp with 4 pull requests to their guides.

I later added a 5th pull-request for the null-coalescing operator in C# and somebody beat me to my 6th pull-request, selection sort using Python.

Example of Bubble Sort in Python

def bubble_sort(lst):
    """
    In-place sort using Bubble Sort.

    Implemented performance optimizations
    to limit comparisons on each pass as
    well as halt sort when list sorted (no
    swaps occur during a pass).

    :param lst: list of items
    :return: None
    """
    if lst is None or len(lst) < 2:
        return

    # Reduce comparisons on each pass.
    for i in range(len(lst) - 1, 0, -1):
        swap_occurred = False

        for j in range(i):
            if lst[j] > lst[j+1]:
                lst[j], lst[j+1] = lst[j+1], lst[j]
                swap_occurred = True

        # Halt if list sorted
        if not swap_occurred:
            return


values = [1, 4, 7, 45, 7, 43, 44, 25, 6, 4, 6, 9]
bubble_sort(values)
print(values)

Mutli-dimensional Arrays in C

C also supports multi-dimensional arrays.

datatype name[size1][size2]...[sizeN]

Two-dimensional arrays are common and can be initialized using the following syntax. One can logically think of the first index as rows and the second index as columns. This example has 2 rows and 5 columns.

int arr[2][5] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

It can be difficult to visualize a 2-dimensional array using the above syntax so developers often use optional, nested brackets to clarify the structure of the array. This is also a valid way to initialize a 2-dimensional array.

int arr[2][5] = {
    {0, 1, 2, 3, 4},
    {5, 6, 7, 8, 9}
};

Two nested for loops can be used to print the contents of a 2-dimensional array in tabular format.

#include <stdio.h>


int main() {
    const int rows = 2, cols = 5;

    int arr[rows][cols] = {
            {0, 1, 2, 3, 4},
            {5, 6, 7, 8, 9}
    };

    for (int row = 0; row < rows; row++) {
        for (int col = 0; col < cols; col++) {
            printf("%5d", arr[row][col]);
        }
        puts("");
    }

    return 0;
}

Using Arrays as Stacks

In some programming languages an array has stack functionality, allowing the developer to perform push and pop operations without the need for a custom stack data structure.

For example, an array in JavaScript has push() and pop() methods allowing one to easily implement stack functionality in an application.

stack = [];

let i = 0;
while(i < 5)
  stack.push(i++);

while(stack.length) {
  stack.pop();
}

A List in Python can also perform stack functionality in an application. Instead of push, one can use the append() method.

stack = []

for i in range(5):
    stack.append(i)

while len(stack):

Basic Stack Functionality

The pop() method is often used in conjunction with append() to implement basic stack functionality in a Python application.

stack = []

for i in range(5):
    stack.append(i)

while len(stack):
    print(stack.pop())

Contents