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

22

u/howdoidoit123 May 08 '15

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.

I can't figure this one out

29

u/itsSparkky May 08 '15

Think like a tree, at each digit you can do the following Add next number Subtract next number Times current number by 10 and add next number

3 outcomes each time, you can either depth first or breadth first search and then evaluate the solution at the end and keep the paths that result in 100.

17

u/billatq May 08 '15

Yeah, I was thinking about doing it with a BFS and a tree. Seems like an okay problem, but I feel like that particular one is more like a math trick than anything else.

0

u/[deleted] May 08 '15

[deleted]

4

u/[deleted] May 08 '15

The real software world is NOT a bunch of math riddles.

No, it's google.

→ More replies (1)
→ More replies (8)

1

u/Brandon23z May 08 '15

Backtracking?

1

u/everysinglelastname May 08 '15

Good answer ! Worth noting here is that within each tree there are "overlapping sub-problems". For example, in the node that has +1, -1 and 1 as its children nodes there is the tree that starts with +2 under each one of those nodes. There are n number of final results that start with +2 but those all do not need to be recalculated each time you descend the tree for each of the children nodes (+1, -1 and 1). So, an optimization would be to save the results in a global object that can be reused. (i.e. memoization).

→ More replies (1)

1

u/gliph May 08 '15

This algorithm fails when subtracting a combined number e.g. 1 - 23

→ More replies (3)

13

u/cretan_bull May 08 '15 edited May 08 '15

Just brute-force it. Between each number, there a 3 possibilities: plus, minus, or concatenated digits. In total, this is 39 =19683 38 = 6561 cases to check.

EDIT: off-by-one error

12

u/matthieum May 08 '15

38 actually, the old intervals/sticks thing.

3

u/matthieum May 09 '15

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors.

1

u/HaMMeReD May 08 '15

You can eliminate a lot of cases. If you ever find 100, you can eliminate every neighbour from needing to be tested.

I'm sure there are more optimizations you could easily do.

2

u/Veedrac May 08 '15

If you ever find 100, you can eliminate every neighbour from needing to be tested.

Only on the leaf node, though, which is largely useless.

The only obvious speedup is to memoize (eg. dynamic programming.)

→ More replies (2)
→ More replies (1)

4

u/carnetarian May 08 '15

Here's my solution in perl. I'm sure a better programmer than me could make something much more efficient, but this gets the job done. Basically I generate every possible combination of strings like "1-2+3+4-56-89" then I evaluate each of those strings. This problem took me much longer than all the other problems combined.

#!/usr/bin/perl

use strict;

sub interpolate($) {
    my $str = shift;

    my @results = ();
    push(@results, $str);

    if (length($str) > 1) {
        for (my $i = 1; $i < length($str); $i++) {
            my $start = substr($str, 0, $i);
            my $end = substr($str, $i);

            my @temp = @{interpolate($end)};
            foreach my $t (@temp) {
                push (@results, "$start+$t");
                push (@results, "$start-$t");
            }
        }
    }

    return \@results;
}

sub evaluate($) {
    my $equation = shift;

    my $lastPlus = rindex($equation, "+");
    if ($lastPlus != -1) {
        my $a = substr($equation, 0, $lastPlus);
        my $b = substr($equation, $lastPlus + 1);

        return evaluate($a) + evaluate($b);
    }

    my $lastMinus = rindex($equation, "-");
    if ($lastMinus != -1) {
        my $a = substr($equation, 0, $lastMinus);
        my $b = substr($equation, $lastMinus + 1);

        return evaluate($a) - evaluate($b);
    }

    return $equation;
}

my @results = @{interpolate("123456789")};

foreach my $result (@results) {
  my $sum = evaluate($result);

  if ($sum == 100) {
    print "$result\n";
  }
}

5

u/[deleted] May 08 '15

Evaling every string will lead to a lot of operations duplicated.

Consider every order that starts with 1+2+3, you could create a tree that stores this order and its result of 6, then do the next operation with 4.

1+2+3+4, 1+2+3-4, & 1+2+34 is 8 operations

(10)+4, (10)-4, and (3)+34. Is 3 operations and 3 reads

This quickly scales as you build longer strings.

2

u/carnetarian May 08 '15

You're right, that's a big improvement. Have an upvote :)

2

u/zarazek May 08 '15

And here is mine in Haskell:

data Op = Plus | Minus | Concat
  deriving Show

execOp :: Op -> Int -> Int -> Int
execOp Plus x y = x + y
execOp Minus x y = x - y

step :: (Int, Op, Int) -> (Op, Int) -> (Int, Op, Int)
step (res, op1, acc) (Concat, x) = (res,                op1, 10*acc+x) 
step (res, op1, acc) (op2,    x) = (execOp op1 res acc, op2, x       )

finish :: (Int, Op, Int) -> Int
finish (res, op, acc) = execOp op res acc

interpret :: [Op] -> [Int] -> Int
interpret ops digits = finish $ foldl step (0, Plus, 0) $ zip (Plus:ops) digits

variationsWithRepetition :: Int -> [a] -> [[a]]
variationsWithRepetition 0 xs = [[]]
variationsWithRepetition n xs = [ y:ys | y <- xs, ys <- variationsWithRepetition (n-1) xs ]

validOps = [ variation | variation <- variationsWithRepetition 8 [Plus, Minus, Concat], interpret variation [1..9] == 100 ]

List comprehensions FTW!

4

u/Tysonzero May 08 '15

Brute force + eval is a really easy way to do it.

3

u/abeliangrape May 08 '15 edited May 10 '15

It really easy to do once you realize you can brute force it. As others have pointed out, there are about 20k different cases to try. If your language has an eval function, the problem reduces to just generating all the possible strings. When you combine this observation with some python abuse, you can actually golf it down to 155 bytes:

from itertools import *
l=['']*17
l[::2]=map(str,range(1,10))
for o in product(['','+','-'],repeat=8):
 l[1::2]=o
 s=''.join(l)+'==100'
 if eval(s):print s

1

u/Zequez May 08 '15

Here is a possible solution with Ruby:

['+', '-', ''].repeated_permutation(8).each do |perm|
  sumText = ('1'..'9').zip(perm).flatten.compact.join
  sum = eval(sumText)
  if sum == 100
    puts "#{sumText} = 100"
  end
end

1

u/TynanSylvester May 09 '15

I managed to solve it in 52 minutes, brute force. I guess I can't call myself a software engineer :( Not that I ever did anyway :D Warning, some awful code below.

class Program
{
	enum Op
	{
		Plus,
		Minus,
		Combine
	}

	static void Main(string[] args)
	{
		List<Op> curSolution = new List<Op>();
		for( int i=0; i<8; i++ )
		{
			curSolution.Add( Op.Plus );
		}

		int solIndex = 0;
		while( true )
		{
			if( Total( curSolution ) == 100 )
				Console.WriteLine( SolString( curSolution ) );
			//Console.ReadKey();

			if( !curSolution.Any(o=>o != Op.Combine ) )
				break;

                             //Move to next solution
			curSolution.Clear();
			int siLocal = solIndex;
			for( int i=0; i<8; i++ )
			{
				curSolution.Add( (Op)(siLocal%3) );
				siLocal /= 3;
			}

			solIndex++;

		}	

		Console.ReadKey();

	}

	static int Total( List<Op> ops )
	{
		int total = 0;

		for( int i=-1; i<ops.Count; )
		{
			if( i < 0 || ops[i] == Op.Plus )
			{
				total += SubTotalFrom(ops, ref i);
				i++;
			}
			else if( ops[i] == Op.Minus )
			{
				total -= SubTotalFrom(ops, ref i);
				i++;
			}
			else
			{
				throw new InvalidOperationException();
			}
		}

		return total;
	}

	static int SubTotalFrom( List<Op> ops, ref int i )
	{
		int subTotal = i+2;

		while( i<7 && ops[i+1] == Op.Combine )
		{
			subTotal *= 10; 

			i++;
			subTotal += i+2;
		}

		return subTotal;
	}

	static string SolString( List<Op> ops )
	{
		string s = "";
		for( int i=1; i<=9; i++ )
		{
			s += i.ToString();

			if( i == 9 )
				break;

			switch( ops[i-1] )
			{
				case Op.Plus: s += "+"; break;
				case Op.Minus: s += "-"; break;
				case Op.Combine: s += ""; break;
			}
		}

		s += " = " + Total(ops);

		return s;
	}
}
→ More replies (5)