Skip to content

Instantly share code, notes, and snippets.

View Nicksil's full-sized avatar

Nick Silvester Nicksil

  • FOCIIS
  • North Carolina, USA
View GitHub Profile
@Nicksil
Nicksil / gist:5645926
Created May 24, 2013 19:26
Python - Find Duplicate
# 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."
@Nicksil
Nicksil / gist:5632127
Created May 23, 2013 01:08
Repeat function using threading.Timer()
import threading
def f():
print('Hello, World!')
# call f() again in 30 seconds
threading.Timer(30, f).start()
f()
@Nicksil
Nicksil / gist:5632051
Last active December 17, 2015 15:29
Repeat function using time.sleep()
import time
def execute_something():
# code
time.sleep(60)
while True:
execute_something()
@Nicksil
Nicksil / gist:5605332
Last active December 17, 2015 11:49
Python Pythagorean Theorem - Distance
import math
# Pythagorean theorem
# a² + b² = c²
def distance(p, q):
return math.sqrt((p[0] - q[0])**2 + (p[1] - q[1])**2)
# >>> distance([200, 250], (300, 350))
# 141.4213562373095