python3 -m venv venv
source venv/bin/activate
pip install ...
pip freeze > requirements.txt
def max_depth(s): | |
arr = [0] | |
for char in s: | |
if char == "(": | |
arr.append(arr[-1] + 1) | |
if char == ")": | |
arr.append(arr[-1] - 1) | |
return max(arr) | |
s = "( ((X)) (((Y))) )" |
(ns roman-numeral-converter.core | |
(:require [clojure.test :refer [deftest is run-tests]] | |
[clojure.string :refer [replace]])) | |
(def numeral-mapping {1 "I" 5 "V" 10 "X" 50 "L" 100 "C" 500 "D" 1000 "M"}) | |
(defn arabic-to-roman [n] | |
(loop [[x & xs :as all] (-> numeral-mapping keys sort reverse) | |
romanised "" | |
num n] |
a HOF is just a function which takes a function as an argument. Alternatively, it’s a function which returns a new function. In both cases it’s a higher-order function. There is another case when you define a function which returns a function. This could be a middleware in Clojure’s Ring or a Python decorator:
def my_decorator(func):
def wrapper():
print("Something is happening before the function is called.")
func()
(defn binary-search [n coll] | |
(loop [upper (dec (count coll)) | |
lower 0] | |
(let [mid (int (Math/ceil (/ (+ upper lower) 2)))] | |
(if (= upper lower) | |
nil | |
(cond (= n (nth coll mid)) mid | |
(< n (nth coll mid)) (recur (dec mid) lower) | |
(>= n (nth coll mid)) (recur upper mid)))))) |
class Timer { | |
startTime | |
endTime | |
constructor() { | |
this.startTime = Date.now(); | |
} | |
finish() { | |
this.endTime = Date.now(); |
function isAnagram(s: string, t: string): boolean { | |
if (s.length !== t.length) return false | |
let counter = new Array(26).fill(0); | |
for (let i = 0; i < s.length; i++) { | |
counter[s.charCodeAt(i) - 'a'.charCodeAt(0)]++ | |
counter[t.charCodeAt(i) - 'a'.charCodeAt(0)]-- | |
} | |
return !counter.some(i => i < 0) | |
}; |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>404 Not Found - React + Babel + Tailwind HTML only</title> | |
<script src="https://unpkg.com/react@17/umd/react.development.js"></script> | |
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script> | |
<!-- Don't use this in production: --> |
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
/* | |
* There once was a wise servant who saved the life of a prince. The king promised to pay whatever the servant could dream up. | |
* Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. |