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
initialize = (size) -> | |
spirals = [] | |
for i in [0...size] | |
row = [] | |
for j in [0...size] | |
row.push 0 | |
spirals.push row | |
spirals | |
inRange = (row, col, size) -> |
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
solution = (list) -> | |
# TODO: complete solution | |
console.log (list) | |
result = "" | |
# make list of intervals | |
intervals = [[list[0]]] | |
for i in [1...list.length] | |
lastInterval = intervals[intervals.length - 1] | |
if list[i] is lastInterval[lastInterval.length - 1] + 1 then lastInterval.push list[i] |
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
# i cheat | |
# Based on http://stackoverflow.com/questions/4600048/nth-ugly-number | |
hamming = (n) -> | |
if n is 1 then return 1 | |
result = [1] | |
# for jth ugly number, find a ugly number u_a, u_b, u_c such that with u_a * 2, u_b * 3 and u_c x 5 | |
# is greater than (j-1)th ugly number | |
# jth ugly number = min (u_a x 2, u_b x 3, u_c 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
constructMixMap = (mix_map, text, index) -> | |
lowercase = 'abcdefghijklmnopqrstuvwxyz' | |
for s in text | |
if s in lowercase | |
if mix_map[s]? and mix_map[s][index]? | |
mix_map[s][index].count += 1 | |
mix_map[s][index].string += s | |
else | |
if !mix_map[s]? | |
mix_map[s] = {} |
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 Vector | |
constructor: (@components) -> | |
add: (vector) -> | |
if @components.length isnt vector.components.length | |
throw new Error('different length') | |
else | |
result = [] | |
for i in [[email protected]] | |
result.push (@components[i] + vector.components[i]) | |
return new Vector(result) |
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
# I cheat. https://www.nayuki.io/page/next-lexicographical-permutation-algorithm | |
nextBigger = (n) -> | |
strNumber = "#{n}" | |
# find the largest index i such that a[i - 1] < a[i] | |
largestIndex = -1 | |
for i in [strNumber.length - 1...0] | |
if strNumber[i - 1] < strNumber[i] | |
largestIndex = i | |
break |
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
# I cheat again. http://www.geeksforgeeks.org/print-all-prime-factors-of-a-given-number/ | |
sumOfDivided = (lst) -> | |
factorToNumMap = {} | |
for n in lst | |
if n < 0 then num = -n else num = n | |
if num % 2 is 0 | |
if !factorToNumMap[2]? then factorToNumMap[2] = [n] | |
else factorToNumMap[2].push n | |
while num % 2 is 0 then num /= 2 |
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
determinant = (m) -> | |
if m.length is 1 then return m[0][0] | |
if m.length is 2 then return m[0][0] * m[1][1] - m[1][0] * m[0][1] | |
det = 0 | |
for i in [0...m.length] | |
value = m[0][i] | |
# build the submatrix | |
submatrix = [] | |
for j in [1...m.length] | |
row = [] |
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
function nbMonths(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth) { | |
//your code here | |
if (startPriceOld >= startPriceNew) { | |
return [0, Math.floor(startPriceOld - startPriceNew)]; | |
} | |
let months = 0; | |
let totalSaving = 0; | |
let depreciatedPriceNew = startPriceNew; | |
let depreciatedPriceOld = startPriceOld; |
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 UriBuilder | |
constructor: (@url) -> | |
@params = {} | |
idx = @url.indexOf('?') | |
if idx >= 0 | |
lstParams = @url.substring(idx + 1) | |
@url = @url.substring(0, idx) | |
paramPairs = lstParams.split('&') |