Skip to content

Instantly share code, notes, and snippets.

View manvillej's full-sized avatar

Jeff Manville manvillej

View GitHub Profile
>>>transitionMatrix = np.matrix([
... [0, 1, 1, 0],
... [2, 0, 0, 0],
... [2, 0, 0, 1],
... [0, 0, 2, 0]], dtype=object)
def getMatrix(matrix, turns):
"""
returns a matrix represents the number of unique paths starting in one position and ending in another in N turns
"""
# get an array of the binary powers we need to calculate
baseTwoArray = getArrayOfPowersOfTwos(turns)
# start with an identity matrix
resultMatrix = np.identity(matrix.shape[0], dtype=object)
def getArrayOfPowersOfTwos(number):
"""
breaks an integer into an array of numbers that represent the number as a series of 2 to a power in order from lowest to highest.
for example: 9 = 2**0 + 2**3.
>>> getArrayOfPowerssOfTwos(9)
[0, 3]
"""
array = list()
@manvillej
manvillej / FullyRecursiveSolution.py
Last active May 9, 2021 13:10
fully recursive binary decomposition solution to Google's Knight Dialer
import numpy as np
import math
from functools import lru_cache
@lru_cache(maxsize=None) # for memoization
def getMatrix(turns):
"""
returns a matrix represents the number of unique paths starting in one position and ending in another in N turns
"""
startingVector0 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
startingVector1 = [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
startingVector2 = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
startingVector3 = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
startingVector4 = [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
startingVector5 = [0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
startingVector6 = [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
startingVector7 = [0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
startingVector8 = [0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
startingVector9 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
@manvillej
manvillej / NPKnightSolution.py
Created November 18, 2018 16:09
Knight Solution using Numpy's linalg.matrix_power
import numpy as np
def getPossibilities(startingPosition, turns):
"""
Calculates the total number of uniques paths from the starting position in N turns for the transition matrix
"""
# edgecase: 0 turns, return 1
if turns == 0:
return 1
@manvillej
manvillej / ArrayOfPowers.py
Created November 17, 2018 15:28
breaks an integer into an array of numbers that represent the number as a series of 2 to a power in order from lowest to highest.
def getArrayOfMultiplesOfTwos(number):
"""
breaks an integer into an array of numbers that represent the number as a series of 2 to a power in order from lowest to highest.
for example: 9 = 2**0 + 2**3.
>>> getArrayOfMultiplesOfTwos(9)
[0, 3]
"""
array = list()
@manvillej
manvillej / SemiRecursiveGetMatrix.py
Created November 17, 2018 04:57
returns a matrix equal to the original matrix multiplied by itself a number of times equal to the number of turns minus one
def getMatrix(matrix, turns):
"""
returns a matrix equal to the original matrix multiplied by itself a number of times equal to the number of turns minus one
"""
if turns==1:
return matrix
else:
turnsBaseTwo = math.log(turns, 2)
@manvillej
manvillej / BaseTwoTurnsMatrix.py
Created November 17, 2018 04:36
use this function to calculate the resulting matrix from raising matrix to the 2^baseTwoTurns power.
def getBaseTwoTurnsMatrix(matrix, baseTwoTurns):
"""
use this function to calculate the resulting matrix from raising matrix to the 2^baseTwoTurns power.
"""
for _ in range(baseTwoTurns):
matrix = matrix*matrix
return matrix
@manvillej
manvillej / FullyRecursiveGetMatrix.py
Last active November 17, 2018 14:34
fully recursive solution to the getMatrix function of the Knight's Dialer
def getMatrix(matrix, turns):
"""
returns a matrix equal to the original matrix multiplied by itself a number of times euqal to the number of turns minus one
"""
if turns==1:
return matrix
else:
turnsBaseTwo = math.log(turns, 2)