r/programming • • May 08 '15

Five programming problems every Software Engineer should be able to solve in less than 1 hour

https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour
2.5k Upvotes

2.1k comments sorted by

View all comments

Show parent comments

35

u/Komputer9 May 08 '15

Here's my solution to 5, took about 45 mins to write in C (after looking at some other solutions, though). Runs pretty much instantly and doesn't use string manipulation, recursion, or the heap.

#include <stdio.h>

int main() {
	int digits[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	int digitCount = sizeof(digits) / sizeof(int);
	int operationCount = digitCount - 1;
	
	int total = 1;
	for (int i = 0; i < operationCount; ++i) {
		total *= 3;
	}
	
	for (int i = 0; i < total; ++i) {
		int operations[operationCount];
		
		int sub = i;
		for (int b = 0; b < operationCount; ++b) {
			operations[b] = sub % 3;
			sub /= 3;
		}
		
		int numbers[digitCount];
		numbers[0] = digits[0];
		
		int count = 0;
		for (int b = 1; b < digitCount; ++b) {
			switch (operations[b - 1]) {
			case 0: // ""
				numbers[count] *= 10;
				numbers[count] += digits[b];
				break;
			
			case 1: // "+"
			case 2: // "-"
				++count;
				numbers[count] = digits[b];
				break;
			}
		}
		
		++count;
		
		int numbersTotal = numbers[0];
		int numberIndex = 0;
		for (int b = 1; b < digitCount; ++b) {
			int operation = operations[b - 1];
			
			if (operation == 0) continue;
			
			++numberIndex;
			
			switch (operation) {
				case 1: // "+"
					numbersTotal += numbers[numberIndex];
					break;
				
				case 2: // "-"
					numbersTotal -= numbers[numberIndex];
					break;
			}
		}
		
		if (numbersTotal == 100) {
			printf("%d", numbers[0]);
			
			int numberIndex = 0;
			for (int b = 1; b < digitCount; ++b) {
				int operation = operations[b - 1];
			
				if (operation == 0) continue;
			
				++numberIndex;
			
				switch (operation) {
					case 1: // "+"
						printf(" + %d", numbers[numberIndex]);
						break;
				
					case 2: // "-"
						printf(" - %d", numbers[numberIndex]);
						break;
				}
			}
			
			printf(" = 100\n");
		}
	}
}

Results:

123 - 45 - 67 + 89 = 100
12 - 3 - 4 + 5 - 6 + 7 + 89 = 100
12 + 3 + 4 + 5 - 6 - 7 + 89 = 100
123 + 4 - 5 + 67 - 89 = 100
1 + 2 + 3 - 4 + 5 + 6 + 78 + 9 = 100
12 + 3 - 4 + 5 + 67 + 8 + 9 = 100
1 + 23 - 4 + 56 + 7 + 8 + 9 = 100
1 + 2 + 34 - 5 + 67 - 8 + 9 = 100
1 + 23 - 4 + 5 + 6 + 78 - 9 = 100
123 + 45 - 67 + 8 - 9 = 100
123 - 4 - 5 - 6 - 7 + 8 - 9 = 100

2

u/leeeeeer May 08 '15

Here's my solution in JavaScript, runs in half a minute and uses string manipulation, recursion, the heap, and eval().

function getToWith(getTo, nums) 
{
	var operations = [ "+", "-", "" ];

	var solutions = [];

	(function helper(pos, process) {
		if (pos >= nums.length) {
			if (eval(process) === getTo)
				solutions.push(process);
			return;
		} else {
			operations.forEach(function(a){
				helper(pos + 1, process + a + nums[pos]);
			});
		}
	})(1, ""+nums[0]);

	return solutions;
}

var getTo100 = getToWith.bind(null, 100, [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]);

var solutions = getTo100();
solutions.forEach(function(a){ console.log(a); });

Results:

"1+2+3-4+5+6+78+9"
"1+2+34-5+67-8+9"
"1+23-4+5+6+78-9"
"1+23-4+56+7+8+9"
"12+3+4+5-6-7+89"
"12+3-4+5+67+8+9"
"12-3-4+5-6+7+89"
"123+4-5+67-89"
"123+45-67+8-9"
"123-4-5-6-7+8-9"
"123-45-67+89"

1

u/androbat May 08 '15 edited May 08 '15

This one runs almost instantly on my system:

var five = function (num) {
  var memo = {}; //hack until we have real sets
  (function innerFive(str, pos) {
    if (eval(str) === num) { memo[str] = true; }
    if (pos < str.length) {
      innerFive(str.slice(0, pos) + '+' + str.slice(pos), pos + 2);
      innerFive(str.slice(0, pos) + '-' + str.slice(pos), pos + 2);
    }
    if (pos - 1 < str.length) { innerFive(str.slice(0), pos + 1); }
  }('123456789', 1));
  return Object.keys(memo);
};

five(100).join(', ');

1

u/leeeeeer May 09 '15

Ha, that looks better! Didn't think about filling the string from the start.

Although turns out the 30s time I said is plain wrong, I was actually testing out Twister at the same time and forgot my CPU was trying to mine a block :p

By curiosity I ran the two functions side by side and measured the performance difference and they ran pretty much as fast, although mine was a little ~10ms faster in average. That surprised me since it seems less efficient at first with the greater number of string operations, but I think it comes from the function calls to splice being way more expensive than even a large number of basic string concatenations using +.

→ More replies (1)