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 ProfitCalculator { | |
| constructor(prices) { | |
| this.prices = prices | |
| } | |
| // implementation 1 uses standard for-loop | |
| getMaxProfit() { | |
| let maxProfit = 0 | |
| let minPrice = this.prices[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
| const returnedDrones = {} | |
| function dispatchDrones(drones) { | |
| drones.forEach(d => { | |
| droneId = d.dispatch() // droneId contains a random but unique id | |
| returnedDrones[droneId] = false | |
| } | |
| } | |
| // run this function at end of day |
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
| // assumes the passed-in array contains a single non-duplicate. Finds the non-duplicate. | |
| // O(3n) = O(n) runtime | |
| findNonduplicate = (numbers) => { | |
| const count = {} | |
| // N | |
| numbers.forEach(n => count[n] = count[n] ? count[n] + 1 : 1) | |
| return (Object | |
| .keys(count) |
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 IntWithPow(val i: Int) { | |
| def pow(n: Int) = Math.pow(i, n).toInt | |
| } | |
| implicit def intToInt(i: Int) = new IntWithPow(i) | |
| // z = ax + by + c | |
| type LinSolution = (Int, Int, Int) | |
| val testRange = 0 to 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
| // given: Array.prototype.forEach, derive: | |
| // 1. reduce | |
| // 2. concat | |
| // 3. map | |
| // 4. filter | |
| // 5. flatten | |
| // 6. flatMap | |
| const reduce = (xs, f, z) => { | |
| var acc = z |
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
| // The "favoriteNumber" key returns a value that Int | String. Normalize the response. | |
| val json1 = Json.parse( | |
| """ | |
| |{ | |
| |"name": "Albert", | |
| |"age": 32, | |
| |"favoriteNumber": 42 | |
| |} | |
| """.stripMargin) |
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
| # add to your ~/.bash_profile or startup script | |
| jazzygif () { ffmpeg -i "$1" -vf scale=-1:-1 -r 10 -f image2pipe -vcodec ppm - | convert -delay 5 -layers Optimize -loop 0 - "$2"; } | |
| # here's how you run it (assumes that pug.mp4 is in current working directory) | |
| jazzygif pug.mp4 pug.gif | |
| # or, specify paths to files | |
| jazzygif ~/Desktop/pug.mp4 ~/Documents/pugs/pug.gif |
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
| #!/usr/bin/env bash | |
| set -ex | |
| PROJECT=$1 | |
| mkdir $PROJECT && cd $PROJECT | |
| yarn init -y | |
| yarn add -D @babel/cli @babel/core @babel/preset-env @babel/preset-flow | |
| yarn add @babel/polyfill | |
| echo "{\"presets\": [\"@babel/preset-env\", \"@babel/preset-flow\"]}" | jq >> .babelrc | |
| mkdir src |
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
| type Methods = 'get' | 'post'; | |
| interface Req { | |
| data: object; | |
| method: Methods; | |
| url: string; | |
| foo?: number | |
| } | |
| class Req implements Req { |
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
| /* | |
| Notes: | |
| DataSource | |
| - a stream of data | |
| - hasNext(), getNext() | |
| - examples: Location, Transactions, Contacts, ... | |
| - an algorithm's output is a DataSource | |
| Transaction |