Skip to content

Instantly share code, notes, and snippets.

View masiht's full-sized avatar

Masih Tabrizi masiht

View GitHub Profile
@masiht
masiht / find_duplicate.python
Created January 12, 2018 15:55
find duplicate in array of n, where each element ranges from 0 to n - 2, inclusive
# File: FindDuplicate.py
# Author: Keith Schwarz (htiek@cs.stanford.edu)
#
# An algorithm for solving the following (classic) hard interview problem:
#
# "You are given an array of integers of length n, where each element ranges
# from 0 to n - 2, inclusive. Prove that at least one duplicate element must
# exist, and give an O(n)-time, O(1)-space algorithm for finding some
# duplicated element. You must not modify the array elements during this
# process."
# bash/zsh completion support for core Git.
#
# Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
# Distributed under the GNU General Public License, version 2.0.
#
# The contained completion routines provide support for completing:
#
# *) local and remote branch names
# *) local and remote tag names
@masiht
masiht / Matrix.swift
Created January 9, 2018 20:51
use of functional dot product in matrix multiplication
struct Matrix {
let rows: Int
let columns: Int
var elements: [[Int]]
// MARK: - Constructors
private init(rows: Int, columns: Int) {
self.rows = rows
self.columns = columns
elements = Array(repeating: Array(repeating: 0, count: columns), count: rows)
@masiht
masiht / fibonacci.swift
Last active August 11, 2018 17:33
fibonacci algorithms in swift
// https://medium.com/swlh/fibonacci-swift-playground-f56d1ff3ea99
// recursive approach
// time: O(n^2)
// space: O(n^2)
func fib1(_ n: Int) -> Int {
guard n > 2 else { return n }
return fib(n-1) + fib(n-2)
}
////////////////////////////////////////////////