Skip to content

Instantly share code, notes, and snippets.

View NicolaM94's full-sized avatar

Nicola Moro NicolaM94

  • Verona, IT
  • 05:52 (UTC +02:00)
View GitHub Profile
@NicolaM94
NicolaM94 / LFU.kt
Last active June 15, 2024 22:10
Kotlin implementation for the LFU (Least Frequently Used) cache algorithm
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>()
@NicolaM94
NicolaM94 / circle.py
Last active June 7, 2023 19:50
Python script to draw a circle on turtle
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)
@NicolaM94
NicolaM94 / arraycomparison.go
Created March 30, 2023 22:34
Comparison of two int arrays in Go
func Compare (arrayA, arrayB []int) bool {
for i := range arrayA {
if arrayA[i] != arrayB[i] {
return false
}
}
return true
}
@NicolaM94
NicolaM94 / solution.py
Last active March 29, 2023 21:55
Solution to the Toeplitz matrix checker
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
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
@NicolaM94
NicolaM94 / bs.py
Created March 9, 2023 17:16
Binary search implementation
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:
@NicolaM94
NicolaM94 / naive.py
Created March 9, 2023 15:35
Sorted array search
def simpleSort (array, element):
for a in array:
if a == element:
return true
return false
@NicolaM94
NicolaM94 / main.py
Created February 22, 2023 18:54
Backward solution
def backwardSolution (buildings):
haveSight = [buildings[-1]]
for b in buildings[::-1]:
if b > haveSight[0]:
haveSight.insert(0,b)
return haveSight
@NicolaM94
NicolaM94 / main.py
Last active February 24, 2023 14:52
Forward solution
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)
@NicolaM94
NicolaM94 / pawn.py
Last active February 5, 2023 01:29
Pawn class in python
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: