Created
May 11, 2015 15:52
-
-
Save deepcube/375ac3d8964452381709 to your computer and use it in GitHub Desktop.
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
#!/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