r/programming • • May 09 '15

"Real programmers can do these problems easily"; author posts invalid solution to #4

https://blog.svpino.com/2015/05/08/solution-to-problem-4
3.1k Upvotes

1.3k comments sorted by

View all comments

1

u/[deleted] May 09 '15 edited May 09 '15

Anyone care to evaluate my ideas? I know Java ok.

for number 4: numbers in a higher place will always beat numbers below them. So you want your first sequence of numbers to be as high as possible, then after that it matters much less. Putting a 90210 in front of an 8 should always be worth it, because Of the lead digit.

Declare an int array length [number of digits], populate it with the numbers then sort it by lead digit (find and place all the 9's, etc. After that is done, stick the numbers with a tens place or above in their proper place (after the smaller numbers mind you. If you're comparing 9 and 92, 9 wins. 9009 and 92, 92 wins. .hasnext() is my friend) using more loops that sort them by iterating across the length of each variable (representing the integers) comparing digits of each number by modding for that tens place. After that is done it should be ready to print with a loop.

For 5:

brute force time, cause time limit. make a version for each variant, every combination of the operations and numbers. Compare them to Parallel universes, to make them sound cool. Number1 (+, -," ") number2 ... etc. That branches too, so

have one huge ass 2d string array declared, [ length of the number lis, plus that-1 3 ] [length of number list * 2 -1]

evaluate each combination (iterate through each of the first index) of num1 (+,-," ") num2 etc... by switching over the value of the char at the proper index in the string array, evaluating either a + b, a - b, Or a * 10 + b based on the appropriate character in a loop, summing up results. If it's the correct number move on, if it's the wrong one set the index to null and move on. At the end print all the nonnulls and you're done.

I forget to type things when drunk. I typed out my attempted solution method in words beforehand this time, instead of half assing it from memory

1

u/[deleted] May 09 '15

I don't like your answers sorry. They don't work, but that's not the important thing. It's fine to get the wrong answer.

Pick one answer, and pick a simple example, then work through that example manually using your idea, and see what you get.

1

u/[deleted] May 09 '15

I forgot to write down a lot of stuff. My methods are much sounder when I write them all down. Still feel like I could be missing something.

1

u/[deleted] May 09 '15

What an interviewer is after, is for you to do it the way I said. Write out an example, then work it through. See what happens.