2D Programming and Resources

There are lots of applications for 2D data. Common terms in 2D are tabular data, row/columns, matrix, etc. Nested iterative loops are often used to find or discover each cell in a 2D array.

Python 2D array

Example of pre-populating 2D array and printing using 3 different styles

  • Candy Challenge:print a christmas tree and trunk
"""
* Creator: Nighthawk Coding Society
2D arrays
"""

# Classic nested loops using ij indexes, this shows 2 dimensions
def print_matrix1(matrix):
    print("Classic nested loops using ij indexes")
    for i in range(len(matrix)):  # outer loop (i), built on length of matrix (rows)
        for j in range(len(matrix[i])):  # inner loop (j), built on length of items (columns)
            print(matrix[i][j], end=" ")  # [i][j] is 2D representation, end changes newline to space
        print()


# Enhanced nested for loops, row and col variables
def print_matrix2(matrix):
    print("Enhanced nested for loops")
    for row in matrix:  # short hand row iterator, index is not required
        for col in row:  # short hand column iterator
            print(col, end=" ")
        print()


# For loop with shortcut (*) row expansion
def print_matrix3(matrix):
    print("For loop with shortcut (*) row expansion")
    for row in matrix:
        print(*row)  # pythons has (*) that is one line expansion of row into columns


def test_matrices():
    # setup some text matrices
    keypad = [[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9],
              [" ", 0, " "]]

    keyboard = [["`", 1, " ", 2, " ",3, " ",  4, " ", 5, " ", 6, " ", 7, " ", 8, " ", 9, " ", 0, " ", "-"," ", "="],
                [" ", " ", "Q", " ", "W", " ", "E", " ", "R", " ", "T", " ", "Y", " ", "U", " ", "I", " ", " ", "O", " ", "P", " ", "[", " ", "]", " ", "\\"],
                [" ", " ", " ", "A", " ", "S", " ", "D", " ", "F", " ", "G", " ", "H", " ", "J", " ", "K", " ", "L", " ", ";", " ", "'"],
                [" ", " ", " ", " ", "Z", " ", "X", " ", "C", " ", "V", " ", "B", " ", "N", " ", "M", " ", ",", " ", ".", " ", "/"]]

    numbers = [
            [0, 1], # binary
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], # decimal
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"]] # hexadecimal
    
    tree = [["   *",],
        ["  /||"],
        [" ////|"],
        ["//////|"],
        ["  ||"]]

    # pack into a list of matrices with titles
    matrices = [
        ["Keypad", keypad], 
        ["Keyboard", keyboard], 
        ["Number Systems", numbers]
        ["Tree", tree]
    ]

    # loop 2D matrix with returning list in [key, value] arrangement
    for title, matrix in matrices:  # unpack title and matrix as variables
        
        # formatted message with concatenation
        print(title, len(matrix), "x", "~" + str(len(matrix[0])))  
        
        # use three different methods
        print_matrix1(matrix)
        print_matrix2(matrix)
        print_matrix3(matrix)
        # blank link in between
        print()


# tester section
if __name__ == "__main__":
    test_matrices()
<>:58: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma?
<>:58: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma?
/tmp/ipykernel_1685/720425594.py:58: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma?
  ["Number Systems", numbers]
/tmp/ipykernel_1685/720425594.py:58: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma?
  ["Number Systems", numbers]
/tmp/ipykernel_1685/720425594.py:58: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma?
  ["Number Systems", numbers]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb Cell 4 in <cell line: 77>()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=75'>76</a> # tester section
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=76'>77</a> if __name__ == "__main__":
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=77'>78</a>     test_matrices()

/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb Cell 4 in test_matrices()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=47'>48</a> tree = [["   *",],
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=48'>49</a>     ["  /||"],
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=49'>50</a>     [" ////|"],
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=50'>51</a>     ["//////|"],
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=51'>52</a>     ["  ||"]]
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=53'>54</a> # pack into a list of matrices with titles
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=54'>55</a> matrices = [
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=55'>56</a>     ["Keypad", keypad], 
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=56'>57</a>     ["Keyboard", keyboard], 
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=57'>58</a>     ["Number Systems", numbers]
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=58'>59</a>     ["Tree", tree]
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=59'>60</a> ]
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=61'>62</a> # loop 2D matrix with returning list in [key, value] arrangement
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=62'>63</a> for title, matrix in matrices:  # unpack title and matrix as variables
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=63'>64</a>     
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#W3sdnNjb2RlLXJlbW90ZQ%3D%3D?line=64'>65</a>     # formatted message with concatenation

TypeError: list indices must be integers or slices, not tuple

JavaScript 2D array

Example below populate a 2D array. Key concepts are ij loop and assignments. Observe the object that is created in console. Learn the basics of iteration through 2D array in JavaScript.

  • Candy challenge:Work of pairs. Create one of the Python examples (christmas tree, keyboard, ...). Use the element.append to output within the notebook. As you work on JavaScript make sure your development enviornment is setup like JavaScript programmer.
%%js

/*
* Creator: Nighthawk Coding Society
Construct a two-dimensional array in JS
*/

var arr2D = [];
var rows = 3;
var cols = 4;
 
// Loop to initialize 2D array elements
for (var i = 0; i < rows; i++) {
    arr2D[i]=[];
    for (var j = 0; j < cols; j++) {
        arr2D[i][j] = "r:" + i + "c:" + j;
    }
}
console.log(arr2D);
element.append(arr2D);
%%js 

var arr = [];
var rows = 4;
var cols = 3;
 
// Loop to initialize 2D array elements
for (var i = 1, 2, 3; i < rows; i) {
    arr[i]=[];
    for (j < cols; j++) {
    }
}
console.log(arr2D);
element.append(arr2D);

Monkey Jumpers Poem

Here are some of the key parts of these arrays

  • Build ASCII monkeys, 5 different monkeys using ASCII Art for the "Monkey Jumpers" countdown poem
  • ANSII Color codes are added to each Monkey
  • Candy Challenge:Print monkeys horizontally versus vertically.
"""
 * Creator: Nighthawk Coding Society
 * Mini Lab Name: Hello Series, featuring Monkey Jumpers Poem
"""

import time # used for delay
from IPython.display import clear_output  # jupyter specific clear

def main():    
    # ANSI Color Codes
    Red = "\u001b[31m"
    Green = "\u001b[32m"
    Yellow = "\u001b[33m"
    Blue = "\u001b[34m"
    Magenta = "\u001b[35m"

    """ 2D array data assignment """
    monkeys = [
        [
            Red,
            "ʕง ͠° ͟ل͜ ͡°)ʔ ",  # [0][0] eyes
            "  \\_⏄_/  ",  # [0][1] chin
            "  --0--   ",  # [0][2] body
            "  ⎛   ⎞   "  # [0][3] legs
        ],
        [
            Green,
            " ʕ༼ ◕_◕ ༽ʔ ",  # [1][0]
            "  \\_⎏_/  ",
            "  ++1++  ",
            "   ⌋ ⌊   "
        ],
        [
            Yellow,
            " ʕ(▀ ⍡ ▀)ʔ",  # [2][0]
            "  \\_⎐_/ ",
            "  <-2->  ",
            "  〈  〉 "
        ],
        [
            Blue,
            "ʕ ͡° ͜ʖ ° ͡ʔ",  # [3][0]
            "  \\_⍾_/  ",
            "  ==3==  ",
            "  _/ \\_  "
        ],
        [
            Magenta,
            "  (◕‿◕✿) ",  # [4][0]
            "  \\_⍾_/ ",  # [4][1]
            "  ==4==  ",  # [4][2]
            "  _/ \\_ "  # [4][3]
        ]
    ]

    """ 2D array program logic """
    # cycles through 2D array backwards
    for i in range(len(monkeys), -1, -1):
        clear_output(wait=True)
        
        print("Nursery Rhyme")  # identification message

        # this print statement shows current count of Monkeys
        # concatenation (+) of the loop variable and string to form a countdown message
        print(str(i) + " little monkeys jumping on the bed...")

        # cycle through monkeys that are left in poem countdown
        for row in range(i - 1, -1, -1):  # cycles through remaining monkeys in countdown

            # cycles through monkey part by part
            for col in range(len(monkeys[row])):
                # prints specific part of the monkey from the 2D cell
                print(monkeys[row][col] + " ")

            # this new line gives separation between stanza of poem
            print("\u001b[0m")  # reset color
            
        time.sleep(5)

    # out of all the loops, prints finishing messages
    clear_output(wait=True)
    print("No more monkeys jumping on the bed")
    print("0000000000000000000000000000000000")
    print("             THE END              ")


if __name__ == "__main__":
    main()
Nursery Rhyme
5 little monkeys jumping on the bed...
 
  (◕‿◕✿)  
  \_⍾_/  
  ==4==   
  _/ \_  

 
ʕ ͡° ͜ʖ ° ͡ʔ 
  \_⍾_/   
  ==3==   
  _/ \_   

 
 ʕ(▀ ⍡ ▀)ʔ 
  \_⎐_/  
  <-2->   
  〈  〉  

 
 ʕ༼ ◕_◕ ༽ʔ  
  \_⎏_/   
  ++1++   
   ⌋ ⌊    

 
ʕง ͠° ͟ل͜ ͡°)ʔ  
  \_⏄_/   
  --0--    
  ⎛   ⎞    

---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb Cell 9 in <cell line: 87>()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=83'>84</a>     print("             THE END              ")
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=86'>87</a> if __name__ == "__main__":
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=87'>88</a>     main()

/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb Cell 9 in main()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=74'>75</a>         # this new line gives separation between stanza of poem
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=75'>76</a>         print("\u001b[0m")  # reset color
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=77'>78</a>     time.sleep(5)
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=79'>80</a> # out of all the loops, prints finishing messages
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=80'>81</a> clear_output(wait=True)

KeyboardInterrupt: 
import time
from IPython.display import clear_output

def main():
    # ANSI Color Codes
    Red = "\u001b[31m"
    Green = "\u001b[32m"
    Yellow = "\u001b[33m"
    Blue = "\u001b[34m"
    Magenta = "\u001b[35m"

    """ 2D array data assignment """
    monkeys = [
        [
            Red + "ʕง ͠° ͟ل͜ ͡°)ʔ ",  # [0][0] eyes
            Red + "  \\_⏄_/  ",  # [0][1] chin
            Red + "  --0--   ",  # [0][2] body
            Red + "  ⎛   ⎞   "  # [0][3] legs
        ],
        [
            Green + " ʕ༼ ◕_◕ ༽ʔ ",  # [1][0]
            Green + "  \\_⎏_/  ",
            Green + "  ++1++  ",
            Green + "   ⌋ ⌊   "
        ],
        [
            Yellow + " ʕ(▀ ⍡ ▀)ʔ",  # [2][0]
            Yellow + "  \\_⎐_/ ",
            Yellow + "  <-2->  ",
            Yellow + "  〈  〉 "
        ],
        [
            Blue + "ʕ ͡° ͜ʖ ° ͡ʔ",  # [3][0]
            Blue + "  \\_⍾_/  ",
            Blue + "  ==3==  ",
            Blue + "  _/ \\_  "
        ],
        [
            Magenta + "  (◕‿◕✿) ",  # [4][0]
            Magenta + "  \\_⍾_/ ",  # [4][1]
            Magenta + "  ==4==  ",  # [4][2]
            Magenta + "  _/ \\_ "  # [4][3]
        ]
    ]

    """ 2D array program logic """
    # iterate through monkeys
    for i in range(len(monkeys), -1, -1):
        clear_output(wait=True)
        
        print("Nursery Rhyme")  # identification message

        # this print statement shows current count of Monkeys
        # concatenation (+) of the loop variable and string to form a countdown message
        print(str(i) + " little monkeys jumping on the bed...")

        # iterate through monkey parts
        for part in range(len(monkeys[0])):
            # iterate through monkeys in countdown
            for row in range(i - 1, -1, -1):
                # prints specific part of the monkey from the 2D cell
                print(monkeys[row][part], end=" ")

            # print a new line after each part of the monkeys
            print()

        print("\u001b[0m")  # reset color
        time.sleep(2)

    # out of all the loops, prints finishing messages
    clear_output(wait=True)
    print("No more monkeys jumping on the bed")
    print("0000000000000000000000000000000000")
    print("             THE END              ")


if __name__ == "__main__":
    main()
Nursery Rhyme
5 little monkeys jumping on the bed...
  (◕‿◕✿)  ʕ ͡° ͜ʖ ° ͡ʔ  ʕ(▀ ⍡ ▀)ʔ  ʕ༼ ◕_◕ ༽ʔ  ʕง ͠° ͟ل͜ ͡°)ʔ  
  \_⍾_/    \_⍾_/     \_⎐_/    \_⎏_/     \_⏄_/   
  ==4==     ==3==     <-2->     ++1++     --0--    
  _/ \_    _/ \_     〈  〉     ⌋ ⌊      ⎛   ⎞    

---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb Cell 10 in <cell line: 77>()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#X16sdnNjb2RlLXJlbW90ZQ%3D%3D?line=73'>74</a>     print("             THE END              ")
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#X16sdnNjb2RlLXJlbW90ZQ%3D%3D?line=76'>77</a> if __name__ == "__main__":
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#X16sdnNjb2RlLXJlbW90ZQ%3D%3D?line=77'>78</a>     main()

/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb Cell 10 in main()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#X16sdnNjb2RlLXJlbW90ZQ%3D%3D?line=64'>65</a>         print()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#X16sdnNjb2RlLXJlbW90ZQ%3D%3D?line=66'>67</a>     print("\u001b[0m")  # reset color
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#X16sdnNjb2RlLXJlbW90ZQ%3D%3D?line=67'>68</a>     time.sleep(2)
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#X16sdnNjb2RlLXJlbW90ZQ%3D%3D?line=69'>70</a> # out of all the loops, prints finishing messages
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jishnus/vscode/CSP-tri1/_notebooks/2023-05-16-DS-arrays_lab.ipynb#X16sdnNjb2RlLXJlbW90ZQ%3D%3D?line=70'>71</a> clear_output(wait=True)

KeyboardInterrupt: 

Animation, the Energetic versus Lazy Programmer methods

Animation is done like the old Disney films, lots of little images put togehter. In these examples we eliminate using a 2D array, but simulate int with a sequence of print statements.

  • This 1st sequence is a lot of lines of code.
  • The 2nd takes the lazy programmer method to do the same.
  • Candy challenge:Make you own ASCII art animation.
"""
* Creator: Nighthawk Coding Society
Sailing Ship Animation (long method)
"""

import time # used for delay
from IPython.display import clear_output  # jupyter specific clear

# ANSI Color Codes
Color34 = "\u001b[34m"
Color37 = "\u001b[37m"


# As you can see, its not very optimal 
def ship1():
    print("    |\ ")
    print("    |/ ")
    print("\__ |__/ ")
    print(" \____/ ")
    print("\u001b[34m -------------------------------------------- \u001b[37m")


def ship2():
    print("      |\ ")
    print("      |/ ")
    print("  \__ |__/ ")
    print("   \____/ ")
    print("\u001b[34m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \u001b[37m")


def ship3():
    print("        |\ ")
    print("        |/ ")
    print("    \__ |__/ ")
    print("     \____/ ")
    print("\u001b[34m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \u001b[37m")


def ship4():
    print("          |\ ")
    print("          |/ ")
    print("      \__ |__/ ")
    print("       \____/ ")
    print("\u001b[34m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \u001b[37m")


def ship5():
    print("            |\ ")
    print("            |/ ")
    print("        \__ |__/ ")
    print("         \____/ ")
    print("\u001b[34m -------------------------------------------- \u001b[37m")


def ship6():
    print("              |\ ")
    print("              |/ ")
    print("          \__ |__/ ")
    print("           \____/ ")
    print("\u001b[34m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \u001b[37m")


def ship7():
    print("                |\ ")
    print("                |/ ")
    print("            \__ |__/ ")
    print("             \____/ ")
    print("\u001b[34m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \u001b[37m")


def ship8():
    print("                  |\ ")
    print("                  |/ ")
    print("              \__ |__/ ")
    print("               \____/ ")
    print("\u001b[34m -------------------------------------------- \u001b[37m")


def ship9():
    print("                    |\ ")
    print("                    |/ ")
    print("                \__ |__/ ")
    print("                 \____/ ")
    print("\u001b[34m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \u001b[37m")


def ship10():
    print("                      |\ ")
    print("                      |/ ")
    print("                  \__ |__/ ")
    print("                   \____/ ")
    print("\u001b[34m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \u001b[37m")


def ship11():
    print("                        |\ ")
    print("                        |/ ")
    print("                    \__ |__/ ")
    print("                     \____/ ")
    print("\u001b[34m -------------------------------------------- \u001b[37m")


def ship12():
    print("                          |\ ")
    print("                          |/ ")
    print("                      \__ |__/ ")
    print("                       \____/ ")
    print("\u001b[34m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \u001b[37m")


def ship13():
    print("                            |\ ")
    print("                            |/ ")
    print("                        \__ |__/ ")
    print("                         \____/ ")
    print("\u001b[34m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \u001b[37m")


def ship14():
    print("                              |\ ")
    print("                              |/ ")
    print("                          \__|__/ ")
    print("                           \____/ ")
    print("\u001b[34m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \u001b[37m")


def ship15():
    print("                                |\ ")
    print("                                |/ ")
    print("                            \___|__/ ")
    print("                             \____/ ")
    print("\u001b[34m -------------------------------------------- \u001b[37m")


def ship16():
    print("                                  |\ ")
    print("                                  |/ ")
    print("                              \___|__/ ")
    print("                               \____/ ")
    print("\u001b[34m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \u001b[37m")


def ship17():
    print("                                    |\ ")
    print("                                    |/ ")
    print("                                \___|__/ ")
    print("                                 \____/ ")
    print("\u001b[34m -------------------------------------------- \u001b[37m")


def ship18():
    print("                                      |\ ")
    print("                                      |/ ")
    print("                                  \___|__/ ")
    print("                                   \____/ ")
    print("\u001b[34m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \u001b[37m")


def ship19():
    print("                                        |\ ")
    print("                                        |/ ")
    print("                                    \___|__/ ")
    print("                                     \____/ ")
    print("\u001b[34m -------------------------------------------- \u001b[37m")


def ship20():
    print("                                          |\ ")
    print("                                          |/ ")
    print("                                      \___|__/ ")
    print("                                       \____/ ")
    print("\u001b[34m -------------------------------------------- \u001b[37m")


clear_output(wait=True)
time.sleep(.01)
ship1()
time.sleep(.01)
clear_output(wait=True)
ship2()
time.sleep(.01)
clear_output(wait=True)
ship3()
time.sleep(.01)
clear_output(wait=True)
ship4()
time.sleep(.01)
clear_output(wait=True)
ship5()
time.sleep(.01)
clear_output(wait=True)
ship6()
time.sleep(.01)
clear_output(wait=True)
ship7()
time.sleep(.01)
clear_output(wait=True)
ship8()
time.sleep(.01)
clear_output(wait=True)
ship9()
time.sleep(.01)
clear_output(wait=True)
ship10()
time.sleep(.01)
clear_output(wait=True)
ship11()
time.sleep(.01)
clear_output(wait=True)
ship12()
time.sleep(.01)
clear_output(wait=True)
ship13()
time.sleep(.01)
clear_output(wait=True)
ship14()
time.sleep(.01)
clear_output(wait=True)
ship15()
time.sleep(.01)
clear_output(wait=True)
ship16()
time.sleep(.01)
clear_output(wait=True)
ship17()
time.sleep(.01)
clear_output(wait=True)
ship18()
time.sleep(.01)
clear_output(wait=True)
ship19()
time.sleep(.01)
clear_output(wait=True)
ship20()
time.sleep(.01)
                                          |\ 
                                          |/ 
                                      \___|__/ 
                                       \____/ 
 -------------------------------------------- 
"""
* Creator: Nighthawk Coding Society
Sailing Ship Animation (programatic method)
"""

import time # used for delay
from IPython.display import clear_output  # jupyter specific clear


# ANSI Color Codes
OCEAN_COLOR = u"\u001B[34m\u001B[2D"
SHIP_COLOR = u"\u001B[35m\u001B[2D"
RESET_COLOR = u"\u001B[0m\u001B[2D"

def ship_print(position):  # print ship with colors and leading spaces according to position
    clear_output(wait=True)
    print(RESET_COLOR)
    
    sp = " " * position
    print(sp + "    |\   ")
    print(sp + "    |/   ")
    print(SHIP_COLOR, end="")
    print(sp + "\___|__/ ")
    print(sp + " \____/  ")
    print(OCEAN_COLOR + "--"*32 + RESET_COLOR)


def ship():  # ship function, loop/controller for animation speed and times
    # loop control variables
    start = 0  # start at zero
    distance = 60  # how many times to repeat
    step = 2  # count by 2

    # loop purpose is to animate ship sailing
    for position in range(start, distance, step):
        ship_print(position)  # call to function with parameter
        time.sleep(.03)

        
ship() # activate/call ship function
                                                              |\   
                                                              |/   
                                                          \___|__/ 
                                                           \____/  
----------------------------------------------------------------