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

11

u/Backfiah May 08 '15

That's 9! runs though.

50

u/anttirt May 08 '15

between the numbers 1, 2, ..., 9 (in this order)

The original problem is only on the order of 38 steps brute-forced.

2

u/AcidDrinker May 08 '15 edited Dec 14 '15

Here's a simple solution in C : {{ LINK REMOVED }}

1

u/scramblor May 08 '15

Good start but I think it has a few problems.

  1. You stop recursion when a solution has been found. I would suspect that there would be more solutions with different permutations in the tree.

  2. It doesn't look like you handle the case of multiple numbers greater than 2 digits.

→ More replies (1)

28

u/lord_braleigh May 08 '15

The digits must stay in order.

0

u/[deleted] May 08 '15

[deleted]

3

u/jlt6666 May 08 '15

A language with eval makes it far easier. Or just call out to bash :)

4

u/yanetut May 08 '15

Bash? Did someone say bash?

#!/bin/env bash

function prob5() {
  if [[ $# -eq 1 ]]; then
    [[ $(($1)) -eq 100 ]] && echo $1
  else
    local exp="$1" ; shift
    local dig="$1" ; shift

    prob5 "$exp + $dig" "$@"
    prob5 "$exp - $dig" "$@"
    prob5 "$exp$dig" "$@"
  fi
}

prob5 1 2 3 4 5 6 7 8 9
→ More replies (4)

1

u/VincentPepper May 08 '15

Or multiply the left side by ten ...

2

u/leeeeeer May 08 '15

What if the last operation was a combination too?

2

u/VincentPepper May 08 '15

If you recurse from left to right you should be able to do it again just with the last result as argument.

So if you have 1 .. 2 .. 3 you can do ((10*1 + 2) * 10 + 3) = 123.

Requires you to evaluate concatenation before +/- though.

But maybe i missed something there.

→ More replies (1)

1

u/Funnnny May 08 '15

carry out a result variable, if you choose a sign, calculate it with previous sign and current number.

It's simple enough with a recursion function, you don't need to use eval

1

u/Paranemec May 08 '15

Commutative property (I believe) makes the order irrelevant.

16

u/Jonyb222 May 08 '15 edited May 08 '15

In this case programmer time is more precious that computing time, get it to work and then make it better.

And while Factorial run-time is horrendous 9! is "only" 362 880 38 is only 6561 runs of a maximal set of 8 addition/subtractions which gives us an upper bound of 2 903 040 52 488 operations.

It's obviously not a good solution, but it's better than not solving it at all, I don't know how long it would take, not long at all for sure and you can work on a better solution while you go along.

5

u/LazinCajun May 08 '15

The numbers have to stay in order, so it's only 38 expressions to check

1

u/Jonyb222 May 08 '15

Even better, thank you for pointing it out.

6

u/sbelljr May 08 '15 edited May 08 '15

9! = 362880

Shouldn't take too long. The point of the question is to get the answer, not to get the answer that works for extremely large cases too.

Edit. There are 38 = 6561 possibilities to check, not 9!. The whole point of the question is to brute force it. My point stands.

4

u/jeffhawke May 08 '15

38 not 9!, it's combination of three elements in eight positions, that's less that 10000.

2

u/nkorslund May 08 '15

If you type 3**8 into google you get 38 = 6561.

1

u/jeffhawke May 08 '15

Yes, well, I was writing from a phone and just did a quick mental math, where 34 is 81 that is less than 100 so 38 would have to be less than 1002, that is 10000, a trivial number of cases to test by brute force.

→ More replies (5)

3

u/Sluisifer May 08 '15

Yup, just brute force that fucker. You can get clever when you need to, and the brute force solution is often a good starting point, if nothing else to get you thinking about the problem clearly.