This file contains hidden or 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 texttable import Texttable | |
class Sudoku: | |
__board: list[list[int]] | |
__is_solved: bool | |
def __init__(self, grid: list[list[int]]): | |
self.__board = grid | |
self.__is_solved = False |
This file contains hidden or 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
const identity = <T extends any>(x: T) => x; | |
abstract class List<T> { | |
abstract prepend(el: T): List<T>; | |
abstract map<B>(fn: (e: T) => B): List<B>; | |
abstract flatMap<B>(fn: (e: T) => List<B>): List<B>; | |
abstract filter(fn: (e: T) => boolean): List<T>; | |
abstract merge(other: List<T>): List<T>; | |
abstract foldLeft<B>(fn: (e: T, z: B) => B, z: B): B; | |
abstract foldRight<B>(fn: (e: T, z: B) => B, z: B): B; |
This file contains hidden or 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
abstract class ImmutableList<T> { | |
abstract add(elem: T): ImmutableList<T> | |
abstract forEach(fn: (x: T) => void): void | |
} | |
class Cons<T> implements ImmutableList<T> { | |
readonly el: T | |
readonly next: ImmutableList<T> | |
constructor(elem: T, list: ImmutableList<T> = Nil) { |
This file contains hidden or 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
interface Expression { | |
evaluate(): number | |
} | |
type UnaryOperator = "+" | "-" | |
type BinaryOperator = "+" | "-" | "*" | "/" | "^" | |
class Value implements Expression { | |
readonly value: number |
This file contains hidden or 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
class Trie | |
attr_accessor :hash, :value | |
def initialize | |
@hash = Array.new(26) { nil } | |
@value = nil | |
end | |
def insert(new_word) | |
word = new_word.downcase |
This file contains hidden or 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 rake | |
import scala.concurrent.{Await, Future} | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.duration._ | |
final class Task private ( | |
desc: String, | |
dependencies: Seq[Task] = Seq.empty[Task], | |
body: => Unit |
This file contains hidden or 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
/* | |
Overview | |
-------- | |
To run a query using anorm you need to do three things: | |
1. Connect to the database (with or without a transaction) | |
2. Create an instance of `anorm.SqlQuery` using the `SQL` string interpolator | |
3. Call one of the methods on `SqlQuery` to actually run the query |
This file contains hidden or 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
// find nth fibonacci number | |
def fib(n: Int): Int = { | |
@annotation.tailrec | |
def loop(a: Int, b: Int, acc: Int): Int = | |
if (acc > 0) loop(b, a + b, acc - 1) | |
else b | |
if (n < 1) throw new Exception(s"Can't find ${n}th term") | |
else if(n == 1) 0 |
This file contains hidden or 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
// write a program to print factorial of a given integer | |
def factorial(n: Int): Long = { | |
@annotation.tailrec // asks compiler to raise a compile error if unable to do tail call recurssion optimization | |
def loop(num: Int, acc: Long): Long = | |
if (num > 1) loop(num - 1, acc * (num - 1)) | |
else acc |
This file contains hidden or 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
TOKENS = { '}' => '{', ')' => '(', ']' => '[' }.freeze | |
def valid_brace?(input) | |
stack = [] | |
valid = true | |
input.each_char do |c| | |
case c | |
when '(', '[', '{' then stack.push(c) | |
when ')', ']', '}' then valid = (stack.pop == TOKENS[c]) |
NewerOlder