Skip to content

Instantly share code, notes, and snippets.

@oieioi
Last active August 29, 2015 14:20
Show Gist options
  • Save oieioi/f0451367f3d3ad7916ea to your computer and use it in GitHub Desktop.
Save oieioi/f0451367f3d3ad7916ea to your computer and use it in GitHub Desktop.
5-programming-problems
http://www.softantenna.com/wp/software/5-programming-problems/
https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour
# Write three functions that compute the sum of the numbers in a given list
# using a for-loop, a while-loop, and recursion.
f1 = (ary) ->
r = 0
for i in ary
r += i
r
f2 = (ary) ->
i = 0
r = 0
while ary.length > i
r += ary[i]
i++
r
f3 = (ary) ->
clone = ary.concat()
f = (sum, ary) ->
if ary.length is 0
return sum
f sum + ary.shift(), ary
f 0, clone
console.assert f1([1,2,3,4,5]) is 15
console.assert f2([1,2,3,4,5]) is 15
console.assert f3([1,2,3,4,5]) is 15
# Write a function that combines two lists by alternatingly taking elements
# For example: given the two lists [a, b, c] and [1, 2, 3],
# the function should return [a, 1, b, 2, c, 3].
f = (ar1, ar2) ->
if ar1.length isnt ar2.length
return null
result = []
for item, index in ar1
result.push ar1[index]
result.push ar2[index]
return result
console.assert f(['a', 'b', 'c'], [1, 2, 3]).join(' ') is 'a 1 b 2 c 3'
# Write a function that computes the list of the first 100 Fibonacci numbers.
# By definition, the first two numbers in the Fibonacci sequence are 0 and 1,
# and each subsequent number is the sum of the previous two. As an example,
# here are the first 10 Fibonnaci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34.
f = (size) ->
result = [0, 1]
while result.length < size
result.push result[result.length - 1] + result[result.length - 2]
result
console.log f(100).join()
# Write a function that given a list of non negative integers,
# arranges them such that they form the largest possible number.
# For example, given [50, 2, 1, 9], the largest formed number is 95021.
f = (ary) ->
ary.sort (f, s) ->
strF = "#{f}"
strS = "#{s}"
# 98 と 9800 は 98 の方が大きい
if new RegExp("^#{strF}").test(strS) or new RegExp("^#{strS}").test(strF)
return strF.length > strS.length
# 片方が片方を含まない場合は辞書順
strF < strS
.join('') * 1
console.assert f([50, 2, 1, 9]) is 95021
console.assert f([1, 10, 11]) is 11110
console.assert f([98, 9800, 99]) is 99989800
console.assert f([999, 90, 9, 99, 98, 9900, 9911]) is 999999991199009890
# Write a program that outputs all possibilities to put + or - or nothing
# between the numbers 1, 2, ..., 9 (in this order) such that the result is
# always 100.
# For example: 1 + 2 + 34 – 5 + 67 – 8 + 9 = 100.
# 順列取得
# [1,2,3], 4 -> [1111, 1112, 1113, 1114, 1121, 1122,...]
getPermutations = (ary, size)->
aryProcessed = ary.map (item) -> [item]
createNewPerms = (a, subsets) ->
result = []
a.forEach (aitem) ->
aItemAdded = subsets.map (subset) ->
aitem.concat subset
result = result.concat aItemAdded
result
f = (perms, subsets, size)->
if perms[0].length is size
return perms
f (createNewPerms perms, subsets), subsets, size
f aryProcessed, ary, size
resolve = (size = 100, operators = ['', '+', '-'], ary = [1, 2, 3, 4, 5, 6, 7, 8, 9]) ->
getPermutations(operators, ary.length - 1)
.map (operators) ->
value = ary.reduce (sum, item, index) ->
sum += "#{operators[index - 1]}#{item}"
.filter (value)-> eval(value) is 100
console.log do resolve
#console.log getPermutations ["a", "i", "u"], 5
{
"name": "f0451367f3d3ad7916ea",
"version": "1.0.0",
"description": "5 programming problems",
"main": "index.js",
"scripts": {
"test": "echo 'problem 1';./node_modules/.bin/coffee p1.coffee; echo 'problem 2';./node_modules/.bin/coffee p2.coffee;echo 'problem 3';./node_modules/.bin/coffee p3.coffee;echo 'problem 4';./node_modules/.bin/coffee p4.coffee;echo 'problem 5';./node_modules/.bin/coffee p5.coffee;"
},
"repository": {
"type": "git",
"url": "https://gist.github.com/f0451367f3d3ad7916ea.git"
},
"author": "",
"license": "ISC",
"dependencies": {
"coffee-script": "^1.9.2"
}
}
@oieioi
Copy link
Author

oieioi commented May 30, 2015

JavaScript does't be able to treat problem 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment