Skip to content

Instantly share code, notes, and snippets.

View deepakkumarnd's full-sized avatar
🏠
Working from home

Deepak Kumar deepakkumarnd

🏠
Working from home
View GitHub Profile
@deepakkumarnd
deepakkumarnd / sudoku.py
Created October 26, 2023 09:24
Sudoku solver python
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
@deepakkumarnd
deepakkumarnd / List.ts
Created May 19, 2023 12:45
Functional list in typescript
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;
@deepakkumarnd
deepakkumarnd / ImmutableList.ts
Created September 22, 2022 13:58
ImmutableList in typescript
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) {
@deepakkumarnd
deepakkumarnd / evaluate.ts
Created September 22, 2022 13:04
Expression evaluator
interface Expression {
evaluate(): number
}
type UnaryOperator = "+" | "-"
type BinaryOperator = "+" | "-" | "*" | "/" | "^"
class Value implements Expression {
readonly value: number
@deepakkumarnd
deepakkumarnd / trie.rb
Last active September 22, 2022 13:59
Trie.rb
class Trie
attr_accessor :hash, :value
def initialize
@hash = Array.new(26) { nil }
@value = nil
end
def insert(new_word)
word = new_word.downcase
@deepakkumarnd
deepakkumarnd / Rake.scala
Last active July 9, 2020 18:10
A simple ruby rake like task runner in scala
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
@deepakkumarnd
deepakkumarnd / anorm.scala
Created April 16, 2020 05:51 — forked from davegurnell/anorm.scala
A short guide to Anorm
/*
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
@deepakkumarnd
deepakkumarnd / fibonacci.sc
Created June 25, 2019 08:43
nth fibonacci number
// 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
@deepakkumarnd
deepakkumarnd / factorial.sc
Last active June 25, 2019 07:42
factorial in scala
// 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
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])