The Observed PIN

15th of October


Description

Alright, detective, one of our colleagues successfully observed our target person, Robby the robber. We followed him to a secret warehouse, where we assume to find all the stolen stuff. The door to this warehouse is secured by an electronic combination lock. Unfortunately our spy isn't sure about the PIN he saw, when Robby entered it.


The keypad has the following layout:

            ┌───┬───┬───┐
            │ 1  │ 2 │ 3  │
            ├───┼───┼───┤
            │ 4  │ 5 │ 6  │
            ├───┼───┼───┤
            │ 7  │ 8 │ 9  │
            └───┼───┼───┘
                 │ 0  │
                 └───┘
        

Solution

    def get_pins(observed):

    possibilities = {
        '1': {'1', '2', '4'},
        '2': {'1', '2', '3', '5'},
        '3': {'3', '2', '6'},
        '4': {'1', '4', '5', '7'},
        '5': {'2' ,'4' ,'5' ,'6' ,'8'},
        '6': {'3' ,'5' ,'6' ,'9'},
        '7': {'4' ,'7' ,'8'},
        '8': {'5' ,'7' ,'8' ,'9' ,'0'}, 
        '9': {'6' ,'8' ,'9'},
        '0': {'8' ,'0'}
    }
    
    return_list = ['']
    for number in observed:
        combinations = return_list
        return_list = []
        for possibility in possibilities[number]:
            for combination in combinations:
                return_list.append(combination + possibility)

    return return_list
        

Thanks to my friend Luka again for giving me proper mathematical insight and help.




Moving Zeroes To The End

13th of October


Description

Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements.

moveZeros([false,1,0,1,2,0,1,3,"a"]) // returns[false,1,1,2,1,3,"a",0,0]
        

Solution

    def move_zeroes(array):   
        return_array = []
        zeroes = []
        for index, element in enumerate(array):
        if (element == 0) and element is not False:
                zeroes.append(0)
            else:
                return_array.append(element)
        return_array.extend(zeroes)
        return return_array
        



Snail kata

3rd of October


Description

Given an n x n array, return the array elements arranged from outermost elements to the middle element, traveling clockwise.

                array = [[1,2,3],
                         [4,5,6],
                         [7,8,9]]
                snail(array) #=> [1,2,3,6,9,8,7,4,5]
            

For better understanding, please follow the numbers of the next array consecutively:

                array = [[1,2,3],
                         [8,9,4],
                         [7,6,5]]
                snail(array) #=> [1,2,3,4,5,6,7,8,9]
            

This image will illustrate things more clearly:

NOTE: The idea is not sort the elements from the lowest value to the highest; the idea is to traverse the 2-d array in a clockwise snailshell pattern.
NOTE 2: The 0x0 (empty matrix) is represented as en empty array inside an array [[]].

Solution

    def snail(input_data):
        output_list = []

        try:
            while True:
                # Append all elements of first list
                for element in input_data[0]:
                    output_list.append(element)
                # Remove first list
                input_data.pop(0)

                # Append last values of all lists
                # that remain, but the last one
                for list in input_data:
                    if list != input_data[-1]:
                        output_list.append(list.pop())
                
                # Reverse the last list and append it
                input_data[-1].reverse()
                for element in input_data[-1]:
                    output_list.append(element)
                input_data.remove(input_data[-1])
                
                # Append the first values of lists that 
                # remain, but reverse the lists first
                input_data.reverse()
                for list in input_data:
                    output_list.append(list[0])
                    list.remove(list[0])
                input_data.reverse()
            
                # Repeat untill the index is empty
                # at any step
        
                except:
        return output_list
            

This exercise gave me somethin to think about. I've made a video where I explain the exercise, go through my solution, and then debug it.