Skip to content

Instantly share code, notes, and snippets.

View MishaelRosenthal's full-sized avatar

Mishael Rosenthal MishaelRosenthal

View GitHub Profile
// The following code implements Huffman Coding:
// https://en.wikipedia.org/wiki/Huffman_coding
function start() {
var text = readLine("enter text:\n");
var counterMap = new Map();
for (var i = 0; i < text.length; i++) {
var char = text.charAt(i);
if(!counterMap.has(char)) {
counterMap.set(char, 0);
@MishaelRosenthal
MishaelRosenthal / introduction-to-python.ipynb
Last active February 16, 2023 00:54
introduction-to-python.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
def move_one(source, destination):
towers_dict[destination].append(towers_dict[source].pop())
# print the movement and resulting towers
global number_of_steps
number_of_steps += 1
print("step", number_of_steps, "moved from", source, "to", destination)
print_towers()
def move_upper(number_of_disks, source, destination, helper_tower):
from fractions import Fraction as frac
from collections import defaultdict
class JeepProblem:
def __init__(self, n):
self.location = frac(0)
self.fuel_in_tank = frac(1)
self.n = n
self.fuel_dumps = defaultdict(lambda: frac(0))
import scala.annotation.tailrec
@tailrec
def gcd(a: Int, b: Int): Int = if(b == 0) a else gcd(b, a % b)
val a = 30
val b = 48
println(s"The gcd of $a, $b is ${gcd(a, b)}.")
def divisible_by_7(number):
number = abs(number)
if(number == 7 or number == 0):
return True
elif(number < 10):
return False
else:
last_digit = number % 10
other_digits = number // 10
return divisible_by_7(other_digits-2*last_digit)
object CellState extends Enumeration {
type CellState = Value
val X, O, EMPTY = Value
}
import CellState._
object GameStatus extends Enumeration {
type GameStatus = Value
val X_WON, O_WON, DRAW, NOT_DECIDED = Value
}
type Digit = Int
def toDigits(i: Int) = i.toString.map(_.asDigit).reverse
def fromDigits(digits: Seq[Digit]) = digits.reverse.dropWhile(_ == 0).map(_.toString).reduce(_ + _).toInt
case class FullAdder(inCarry: Digit, i: Digit, j: Digit) {
val asDigits = toDigits(inCarry + i + j)
def sum() = asDigits(0)
def carry() = if(asDigits.length > 1) asDigits(1) else 0
}
@MishaelRosenthal
MishaelRosenthal / FixedSizeQueue.scala
Last active May 15, 2019 07:57
An implementation of a constant size queue, in which enqueue dequeues the oldest enqueued element. * The performance characteristics of all operations are amortized constant time, due to the fact that * tail, append, and apply (random access) are amortized constant time operations for vectors.
package com.twitter.mrosenthal.timelines.adhoc.dataset_transform
/**
* An implementation of a constant size queue, in which enqueue dequeues the oldest enqueued element.
* The performance characteristics of all operations are amortized constant time, due to the fact that
* tail, append, and apply (random access) are amortized constant time operations for vectors.
*/
case class FixedSizeQueue[T](elements: Vector[T]){
def newest: T = elements.last
package com.twitter.mrosenthal.timelines.adhoc.dataset_transform
import scala.collection.mutable
import scala.concurrent.duration._
object MeasureTimeUtil {
def measureTime[T](numRepeats: Int)(f: => T): FiniteDuration = {
f // warm up