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
# Faster XOR | |
def getXor(a, b): | |
return [b, 1, b + 1, 0][b % 4] ^ [(a-1), 1, (a-1) + 1, 0][(a-1) % 4] | |
def solution(start, length): | |
l = length | |
cs = 0 | |
while l > 0: | |
cs ^= getXor(start, start + l - 1) |
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 Node: | |
def __init__(self, value): | |
self.value = value | |
self.left = self.right = None | |
# Build from last elem first. | |
# First decrement with right and then left. | |
def build_tree(height, post): | |
if height == 1: |
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
def find_all(a_str, sub): | |
start = 0 | |
while True: | |
start = a_str.find(sub, start) | |
if start == -1: return | |
yield start | |
start += len(sub) | |
def find_salutes(left, right): | |
accum = 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
def merge(x, y): | |
z = x.copy() # start with x's keys and values | |
z.update(y) # modifies z with y's keys and values & returns None | |
return z | |
# ASCII val of 'a' to shift by | |
a_ord = ord('a') - 1 | |
# braille sums | |
sums = (1, 5, 3, 11, 9, 7, 15, 13, 6, 14) |
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
;; Lovingly stolen from: https://gist.github.com/adambard/2217871 | |
(require '[clojure.java.io :as io]) | |
(defn is-file? [f] (.isFile f)) | |
(defn is-dir? [f] (.isDirectory f)) | |
(defn get-name [f] (.getName f)) | |
(defn get-path [f] (.getPath f)) | |
(def dir-path "/Users/<username>/Downloads/data") | |
(def dir (io/file dir-path)) |
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
;; use filter with an anonymous function | |
(def active-repos (filter (fn [repo] (false? (get repo :archived))) @repos)) | |
;; check how many repos are not archived | |
(println (count active-repos)) | |
; => 202 |
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
;; Create global atom list to hold all repositories | |
(def repos (atom ())) | |
;; Loop through repositories | |
(loop [page 1] | |
(let [resp (get-repos page 100) has_next (get resp :has_next)] | |
(swap! repos concat (get resp :body)) ; Add to global repos list | |
(if has_next | |
(recur (inc page)) | |
page))) |
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
;; Request Repos page=1, per_page=1 | |
;; Demonstrating sync multi-let bindings | |
(let [resp (get-repos 1 1) has_next (get resp :has_next)] | |
(println (str "has_next: " has_next))) | |
; => has_next: true |
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
(def x (loop [i 0] | |
(println (str "i = " i)) | |
(if (< i 5) | |
(recur (inc i)) | |
i))) | |
(println x) ; => 5 |
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
(loop [i 0] | |
(println (str "i = " i)) | |
(if (< i 5) | |
(recur (inc i)) | |
i)) | |
; => | |
; i = 0 | |
; i = 1 | |
; i = 2 | |
; i = 3 |