Skip to content

Instantly share code, notes, and snippets.

@deepcube
Created May 11, 2015 15:52
Show Gist options
  • Save deepcube/375ac3d8964452381709 to your computer and use it in GitHub Desktop.
Save deepcube/375ac3d8964452381709 to your computer and use it in GitHub Desktop.
#!/bin/sh
# https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour
# problem 5
# usage: ./p5_adding beg end goal
# e.g. : ./p5_adding 1 10 100
awk -v "beg=$1" -v "end=$2" -v "goal=$3" '
function f(cur, last, out, sum, op) {
if (cur > end) {
if (sum == goal)
printf("%s\n", out)
return
}
f(cur + 1, cur , out " + " cur, sum + cur , 1)
f(cur + 1, cur , out " - " cur, sum - cur , -1)
f(cur + 1, last cur, out cur, sum - op * last + op * (last cur), op)
}
BEGIN {
f(beg + 1, beg, beg, beg, 1)
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment