Length of the array | Simple search algorithm | Binary search |
---|---|---|
10 | 10 | 3 |
100 | 100 | 6 |
1000 | 1000 | 10 |
1.000.000 | 1.000.000 | 20 |
1.000.000.000 | 1.000.000.000 | 30 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// Object stored in the cache, could be anything | |
class Object (val key: String, val value: Int) | |
class LeastFrequentlyUsed (private val cacheLimit :Int) { | |
// Holds objects | |
private val cache = mutableListOf<Object>() | |
// Keeps track of hashes -> index, used in get(key) => hash(key) => index | |
private val indexLinks = mutableMapOf<Int,Int>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import turtle | |
# Draws a circle from the origin with a given radius | |
def drawCircle ( turtle :turtle.Turtle, radius :int, origin :tuple=(0,0), penSize :int=1, penColor :str="black", writeLabel :bool=False ) : | |
turtle.pensize(penSize) | |
turtle.pencolor(penColor) | |
turtle.up() | |
turtle.goto(origin[0],origin[1]-radius) | |
turtle.down() | |
turtle.circle(radius) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Compare (arrayA, arrayB []int) bool { | |
for i := range arrayA { | |
if arrayA[i] != arrayB[i] { | |
return false | |
} | |
} | |
return true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ToeplitzCheck(matrix): | |
crntRow = 0 | |
maxRow = len(matrix) | |
while crntRow < maxRow -1 : | |
if matrix[crntRow][0:len(matrix)-1] != matrix[crntRow+1][1:len(matrix)]: | |
return False | |
crntRow += 1 | |
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def binarySearch (array, element): | |
a = 1 | |
b = len(array) | |
while array[int((a+b)/2)] != element: | |
m = int((a+b)/2) | |
if (array[m]) > element: | |
b = m - 1 | |
else: | |
a = m + 1 | |
if a > b: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def simpleSort (array, element): | |
for a in array: | |
if a == element: | |
return true | |
return false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def backwardSolution (buildings): | |
haveSight = [buildings[-1]] | |
for b in buildings[::-1]: | |
if b > haveSight[0]: | |
haveSight.insert(0,b) | |
return haveSight |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def forwardSolution (buildings): | |
haveSight = [buildings[0]] | |
for b in buildings[1:]: | |
if b > haveSight[0]: | |
haveSight = [b] | |
continue | |
if b > haveSight[-1]: | |
haveSight[-1] = b | |
else: | |
haveSight.append(b) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MAX_X = 8 | |
MAX_Y = 8 | |
class Pawn: | |
def __init__(self,x,y): | |
self.x = x | |
self.y = y | |
def possibleMoves(self): | |
moves = [] | |
if self.x+1 <= MAX_X and self.y <= MAX_Y: |
NewerOlder