Skip to content

Instantly share code, notes, and snippets.

View TheNova22's full-sized avatar
:octocat:
Let's just play a game of Pai Sho

Jayant Sogikar TheNova22

:octocat:
Let's just play a game of Pai Sho
View GitHub Profile
@TheNova22
TheNova22 / LRU Cache
Last active October 7, 2020 05:14
LRU Cache Algo
// vim: syntax=swift
class Node{
var key : Int
var val : Int
var pre: Node?
var next : Node?
init(_ key : Int , _ val : Int){
self.key = key
self.val = val
}
@TheNova22
TheNova22 / Island Sinker
Last active October 7, 2020 05:14
Number Of Islands Algo
// vim: syntax=swift
func numIslands(_ grid: [[Character]]) -> Int {
guard grid.count > 0 else { return 0 }
guard grid[0].count > 0 else { return 0 }
var grid = grid
var islandCounter = 0
for i in 0..<grid.count{
for j in 0..<grid[i].count{
if grid[i][j] == "1"{
islandCounter += 1
@TheNova22
TheNova22 / PreOrder
Created May 1, 2020 12:33
PreOrder Traversal
// vim: syntax=swift
public class TreeNode {
public var val: Int
public var left: TreeNode?
public var right: TreeNode?
public init() { self.val = 0; self.left = nil; self.right = nil; }
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
self.val = val
self.left = left
@TheNova22
TheNova22 / BS
Created May 1, 2020 12:43
Binary Search
// vim: syntax=swift
func binarySearch<T : Equatable & Comparable>(_ arr:[T] , _ val : T)->Bool{
var l = 0
var h = arr.count - 1
while(l < h){
let m = l + (h-l)/2
if arr[m] == val{
return true
}
else if arr[m] < val{
@TheNova22
TheNova22 / TreeC
Last active October 7, 2020 05:14
Tree Class
// vim: syntax=swift
public class TreeNode {
public var val: Int
public var left: TreeNode?
public var right: TreeNode?
public init() { self.val = 0; self.left = nil; self.right = nil; }
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
self.val = val
self.left = left
@TheNova22
TheNova22 / Sudoku
Last active October 7, 2020 05:14
Sudoku Solver (Backtracking)
// vim: syntax=swift
func solveSudoku(_ board: inout [[Character]]) {
cansolve(&board)
}
func cansolve(_ board : inout [[Character]])->Bool{
for i in 0..<9{
for j in 0..<9{
if board[i][j] == "."{
for char in "123456789"{
if validPlacement(&board, i, j, char){
@TheNova22
TheNova22 / NQueens
Last active October 7, 2020 05:14
N Queens
// vim: syntax=swift
func solveNQueens(_ n: Int) -> [[String]] {
var arr : [Int] = []
var res : [[Int]] = []
doer(n, 0, &arr, &res)
var ans : [[String]] = []
for i in 0..<res.count{
ans.append([])
for j in 0..<res[i].count{
var st = ""
@TheNova22
TheNova22 / Anagram
Created May 17, 2020 07:35
Anagram Generator
// vim: syntax=swift
func anagram<C: Collection>(items: C) -> [[C.Iterator.Element]] {
var scratch = Array(items)
var result: [[C.Iterator.Element]] = []
func heap(_ n: Int) {
if n == 1 {
result.append(scratch)
return
}
// vim: syntax=swift
func gcd(_ a : Int, _ b : Int)->Int{
if b == 0{
return a
}
return gcd(b, a%b)
}
func lcm(_ a : Int , b : Int)->Int{
return (a*b)/gcd(a, b)
}
@TheNova22
TheNova22 / LetterIndex
Created May 18, 2020 07:53
Ascii value / Letter Index
// vim: syntax=swift
func letterIndex(_ letter: Character) -> Int {
return Int(letter.unicodeScalars.first!.value) - Int(Unicode.Scalar("a").value)
}