Friday 23 August 2019

Dresden is Real!

I've  been a fan of Jim Butcher's Dresden files for nearly a decade and that's more than a few years short of how long they've  been published.

I started reading when Clinton was still president. I have continued reading past the inauguration of the great Cheeto.

It is nearly 2020 now and I have been re-reading and re-listening to the entirety of Butcher's Dresden series at least 5 times over. It's engrossing if not blatantly spot on with its predictions.

Humanity would rather ignore the mistakes and things it can't comprehend then accept them for the truth that they are.

Kincade and the white council both decry: bad shit happens. Good people didn't make it happen but it happens anyway.

But I believe they're wrong.  Which is what I'm certain Butcher is trying to explain.

Only through good deeds and pure thoughts can humanity become enlightened.

And although Butcher presents this theme succeeding time after time; I just can't accept that this will ever happen.

Humans just can't do this. They're too stupid. As a race, we cannot possibly ever, democratically decide to define ourselves as anything other than greedy, and undeserving.

As beautiful as any system may be, we are just not able to agree to anything; without losing something that makes us human, no matter how much I'd like to.

~ Friar Greg ~

Saturday 5 January 2019

COMP200 - Shuffle Left Algorithm

So, I've been learning about Computer Science and my textbook likes to throw these algorithms at me. I thought to myself, "self, you should implement these instructions into something usable." and so I did.

You can download the py file from [here] but I'll post the text anyway.


## utf-8
"""
## shuffle left Algorithm ##
1  Get values for n and the n data items
2  Set the value of legit to n
3  Set the value of left to 1
4  Set the value of right to 2
5  While left is less than or equal to legit do Steps 6 through 14
6    If the item at position left is not 0 then do Steps 7 and 8
7      Increase left by 1
8      Increase right by 1
9    Else (the item at position left is 0) do Steps 10 through 14
10     Reduce legit by 1
11     While right is less than or equal to n do Steps 12 and 13
12       Copy the item at position right into position (right − 1)
13       Increase right by 1
14     Set the value of right to (left + 1)
15  stop

 (Schneider p117)
Schneider, G. M., Judith Gersting. Invitation to Computer Science,  6th Edition.
Cengage/Nelson, 01/2012.
"""

def shuffleLeft(data, verbose=False):
    """ (list of integers) --> list of integers with no 0's
    using the shuffle left Algorithm """

    n = len(data)
    #2 Set the value of legit to n
    legit = n
    #3 Set the value of left to 1
    left = 0 ## positions start at 0 not 1
    #4 Set the value of right to 2
    right = 1 ## positions start at 0 not 1

    #5  While left is less than or equal to legit do Steps 6 through 14
    while left < legit: ## positions start at 0 not 1
        #6 If the item at position left is not 0 then do Steps 7 and 8
        if data[left] != 0:
            #7 Increase left by 1
            left += 1
            #8 Increase right by 1
            right += 1
        #9 Else (the item at position left is 0) do Steps 10 through 14
        else:
            #10 Reduce legit by 1
            legit -= 1
            #11 While right is less than or equal to n do Steps 12 and 13
            while right <= legit:
                #12 Copy the item at position right into position (right − 1)
                data[right-1] = data[right]
                #13 Increase right by 1
                right += 1
            data = data[0 : legit]
        if verbose:
            print("%s - %s - %s" % (legit == len(data), legit, data))
        #14 Set the value of right to (left + 1)
        right = left + 1
    #15 Stop


if __name__ == "__main__":
    dataset = [
               [1, 0, 24, 16, 0, 36, 42, 23, 21, 0],
               [16, 35, 0, 0, 54, 0, 8, 12, 0, 6, 0, 4, 0, 3, 0, 6, 0, 9, 0],
               [0, 24, 1, 2, 32, 98, 56, 45, 21, 0, 0, 21, 35, 35, 0]
               ]
    for testlist in dataset:
        shuffleLeft(testlist, verbose=True)


comments are welcome
~ Friar Greg

Wednesday 4 July 2018

Artificially Intelligent

I've been reading a lot about AI, Neural Networks, Genetic Algorithms, agents, percepts and perceptrons. This subject is Huuuge. I started with AI a Modern Approach and learned about the categories of AI

Dan Shiffman also makes a very extensive and fun tutorial channel on YouTube although he messs with Java and Processing which are not the same as Python. Processing can do Python but I find the error codes insane to follow, almost as crazy as Dan.

So, with the higher understanding of types and categories and with some of the more specific knowledge of neural networks and genetic algorithms, I'm kinda lost trying to figure out how the two meet. It's a vast hole between theory and practice that I am still in the process of sewing together.

Saturday 14 October 2017

My CodeInGame Adventures - Power of Thor - Episode 1

My first CodeInGame experience was running Thor across a grassy field. We had quite the adventure, Thor and I. If it were not for me, he would have died. I'd say he owes me one!

Instructions from CodeInGame :

Thor moves on a map which is 40 wide by 18 high. Note that the coordinates (X and Y) start at the top left! This means the most top left cell has the coordinates "X=0,Y=0" and the most bottom right one has the coordinates "X=39,Y=17".

Once the program starts you are given:
the variable lightX: the X position of the light of power that Thor must reach.
the variable lightY: the Y position of the light of power that Thor must reach.
the variable initialTX: the starting X position of Thor.

the variable initialTY: the starting Y position of Thor.

At the end of the game turn, you must output the direction in which you want Thor to go among:

N (North)
NE (North-East)
E (East)
SE (South-East)
S (South)
SW (South-West)
W (West)
NW (North-West)

Each movement makes Thor move by 1 cell in the chosen direction.



Here's the code I submitted:



import sys
import math

# light_x: the X position of the light of power
# light_y: the Y position of the light of power
# initial_tx: Thor's starting X position
# initial_ty: Thor's starting Y position
light_x, light_y, initial_tx, initial_ty = [int(i) for i in input().split()]
current_x = initial_tx
current_y = initial_ty

# game loop
while True:
    remaining_turns = int(input())  # The remaining amount of turns Thor can move. 
    
    first = ""
    second = ""
    final = ""
    
    EAST = (light_x - current_x)
    WEST = (current_x - light_x)
    NORTH = (current_y - light_y)
    SOUTH = (light_y - current_y)
    
    if NORTH > SOUTH:
        first = "N"
        current_y -=1
        if ( EAST > NORTH or WEST > NORTH ):
            first = ""
            current_y +=1
    elif NORTH < SOUTH:
        first = "S"
        current_y +=1
        if ( EAST > SOUTH or WEST > SOUTH ):
            first = ""
            current_y -=1

    if EAST > WEST:
        second = "E"
        current_x +=1
        if ( NORTH > EAST or SOUTH > EAST ):
            second = ""
            current_x -=1
    elif EAST < WEST:
        second = "W"
        current_x -=1
        if ( NORTH > WEST or SOUTH > WEST ):
            second = ""
            current_x +=1

    final = first + second    
    print(final)

    #print("Debug messages...", file=sys.stderr)
    # Write an action using print
    # To debug: print("Debug messages...", file=sys.stderr)
    # A single line providing the move to be made: N NE E SE S SW W or NW



Drop me a line if you wanna talk programming.

~ Friar Greg

My CodeInGame Adventures - Horse Racing

Another CodeInGame puzzle I solved. This one wasn't too hard, only an afternoon. Don't tease me, I'm still getting back into the swing of things. What bugs me most is learning all the built-ins and special quirks.

Input
Line 1: Number N of horses

The N following lines: the strength Pi of each horse. Pi is an integer.

Output
The difference D between the two closest strengths. D is an integer greater than or equal to 0.
Constraints
1 < N < 100000
0 < Pi ≤ 10000000
Example
Input
9

Output
1
And the code I submitted:

import sys
import math

## Helper functions
def mycompare(horse_a, horse_b):
    if horse_a > horse_b:
        return horse_a - horse_b, horse_b
    elif horse_a < horse_b:
        return horse_b - horse_a, horse_a
    else:
        return 0, horse_b

## get all the data
last_horse, closest = 99999999,99999999
horse_list=[]
horses = int(input())
for horse in range(horses):
    this_horse = int(input())
    horse_list.append(this_horse)

## let's sort the strengths
temp = sorted(horse_list,reverse=True)

## test for differences in strength
for this_horse in temp:
    ## compare the differences
    this_compare, last_horse = mycompare(this_horse,last_horse)
    if this_compare < closest:
        ## put the lessor difference into our flag
        closest = this_compare

print(str(closest))


Again, if you wanna talk code, just drop a comment below.
~ Friar Greg