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

19

u/farfaraway May 08 '15

You get the job.

8

u/_teslaTrooper May 08 '15 edited May 08 '15

I'd be more productive though:

// found on reddit
printf("123 - 45 - 67 + 89 = 100\n\
12 - 3 - 4 + 5 - 6 + 7 + 89 = 100\n\
12 + 3 + 4 + 5 - 6 - 7 + 89 = 100\n\
123 + 4 - 5 + 67 - 89 = 100\n\
1 + 2 + 3 - 4 + 5 + 6 + 78 + 9 = 100\n\
12 + 3 - 4 + 5 + 67 + 8 + 9 = 100\n\
1 + 23 - 4 + 56 + 7 + 8 + 9 = 100\n\
1 + 2 + 34 - 5 + 67 - 8 + 9 = 100\n\
1 + 23 - 4 + 5 + 6 + 78 - 9 = 100\n\
123 + 45 - 67 + 8 - 9 = 100\n\
123 - 4 - 5 - 6 - 7 + 8 - 9 = 100\n");

Actually did the first 4 in about 40 minutes, in C, so I'm gonna say if the problem set was constant difficulty I'd be fine.