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
// 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); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 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): |
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
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)) |
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 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)}.") |
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 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) |
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
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 | |
} |
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
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 | |
} |
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 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 |
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 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 | |
NewerOlder