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/code-master May 09 '15 edited May 09 '15

One of the solutions avoiding string concatenation:

When you've got number with 5 digits 15111 and number 1 [15111 1] you must append 4 missing digits from first number at the end of second number taking them from left to right, so e.g. : 15111 would be compared to 11511 (1xxxx) and then its decided to switch their positions or not.

eg. [50 507] becomes [50x 507] -> [505 507] and because 507 is greater than 505, 507 is placed at the front, so result is 50750

lets test it against some input:

[10 5] -> [10 5x] -> [10 51] (swap)-> 5,10

[51 5] -> [51 5x] -> [51 55] (swap) -> 5,51

[56 5] -> [56 5x] -> [56 55] (no swap) -> 56,5

[55 5] -> [55 5x] -> [55 55] (no swap) -> 55, 5

[95 9] -> [95 9x] -> [95 99] (swap) -> 9,95

[15 1] -> [15 1x] -> [15 11] (no swap) -> 15,1

[11561 1] -> [1151 1xxxx] -> [11561 11156] (no swap)

[9998 9] -> [9998 9xxx] -> [9998 9999] (swap) ...

I don't know if this method is more efficient than plain string concatenation, but it works. Something tells me it is. If digits were converted to BCD (binary coded decimal) it would be easier to do comparisons. Or just convert to readable hex, e.g. 999 -> 0x999 and do the math.

I think nobody came to such original solution like this, so from the whole crowd here I would hire only myself.

Still, this is not a real-life programming problem.

1

u/HobHeartsbane May 09 '15

You could concat mathematically though without the string conversion. Maybe that would be even faster, who knows. :)

0

u/code-master May 09 '15

Yes, the power of the dark side.

1

u/luciusplc May 09 '15

I've done it yestarday: My Solution

I know this could done better (without reversing Acc) but main idea is the same.

Does it mean you would hire me? I've solved all problems in less than an hour (correctly!) and ironically, I'm jobless right now...

2

u/code-master May 09 '15

Maybe I would solve it earlier but the solution has no real value for me.

Hire you? Yes and no. It would depend greatly on your code quality. I'm not an Erlang guy, but judging by you code I'd say that you code is unreadable. Comments also do not help very much.

From my side: I would never give a candidate such task. Because it is clearly a waste of time. Instead showing me your coding skills you are forced to deal with math and if your're unlucky, math will eat whole your time.

Always try to write code that it could be understood and extended even by an intern (what this intern will to is another story).

1

u/luciusplc May 09 '15 edited May 10 '15

But you know that it wasn't production code but a solution for a puzzle...

It was just a rhetorical question... i don't even know on which continent you live.

Added some comments and clarified it a little. If you are still interest here some my code:

-module(problems).

-export([p4/1]).

p4(ListOfIntegers) ->
    ListOfStrings = lists:map(fun integer_to_list/1, ListOfIntegers),
    OrderedListOfStrings = lists:sort(fun before/2, ListOfStrings),
    _OrderedListOfIntegers = lists:map(fun list_to_integer/1, OrderedListOfStrings).

%% @doc comparator function that defines order for problem 4 from
%% https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour
%% true when S1 should be before S2, false otherwise
before(S1, S2) ->
    before(S1, S2, {S1, S2, undefined}). % tuple keeps heads of strings and flag of shorter string (undefined, first, second)

%% both strings are equal or one is a complete repetition of another
before([], [], {_S1, _S2, ShorterFlag}) ->
    case ShorterFlag of
        undefined -> true; % strings are identical
        first -> true; % we prefer shorter string first
        second -> false % we prefer shorter string first
    end;
%% first string appears to be the shorter one, lets start from its beginning and mark it as shorter
before([], L2, {S1, S2, ShorterFlag}) when ShorterFlag == first orelse ShorterFlag == undefined ->
    before(S1, L2, {S1, S2, first});
%% second string ends when first is marked as shorter so we end a recursion. It's not complete repetition
before(_L1, [], {_S1, _S2, first}) ->
    true; % we prefer shorter string first
%% second string appears to be the shorter one, lets start from its beginning and mark it as shorter
before(L1, [], {S1, S2, ShorterFlag}) when ShorterFlag == second orelse ShorterFlag == undefined ->
    before(L1, S2, {S1, S2, second});
%% first string ends when second is marked as shorter so we end a recursion. It's not complete repetition
before([], _L2, {_S1, _S2, second}) ->
    false; % we prefer shorter string first
%% both digits are the same so we keep going
before([H1 | T1], [H2 | T2], HelperTuple) when H1 == H2 -> 
    before(T1, T2, HelperTuple);
%% if digits are not the same we return its logical value comparison as a logical value of comparison of two strings
before([H1 | _T1], [H2 | _T2], _HelperTuple) ->
    H1 > H2.

EDIT: I don't think it was hard to understand for people that knew the idea and have some basics of erlang syntax (or at least prolog). From my personal experience, erlang syntax is ugly and not clear on the first look.

1

u/senatorpjt May 09 '15 edited Dec 18 '24

roll stocking oil pet foolish meeting edge sheet familiar modern

This post was mass deleted and anonymized with Redact

1

u/luciusplc May 09 '15

Yeah, that was pain... but after a week you get used to it and it even looks clean and reasonable.

Quite interesting is Elixir that attepmts to modernize erlang ecosystem. It compiles to the same byte code but authors switched to ruby-like syntax and added a lot of useful stuff and tooling for code composition.

0

u/[deleted] May 09 '15

[deleted]

1

u/luciusplc May 09 '15

I didn't want to sound as whining one here, just wanted to expose the irony that hit me.

I live in capitol city of central (some say eastern) european country and dropped off from university before bachelor's degree. It was a mistake, I know. I worked some time as an erlang developer (I'm really glad i had that opportunity to learn) but it wasn't permanent. During that time I've become really interested in distributed systems (especially in "fogs" and methods of clusterization) and did a lot of research. Erlang is my language of choice (it's very expressive and actor oriented) but I did some stuff in java (swing, spring), javascript, asm, c, c++, python but it's rusted right now.

I just hoped to find a job in distributed systems area but it seems that you need to be am expert senior developer or have some proper degree. In my last technical interview I wasn't asked to answer any programming question. It was a loose chat and you need to know that I lack of "soft skills". I'm not the most charming guy. My english is crappy too...

Don't get me wrong. I don't blame anyone and don't consider myself as an expert. Just wished to become one in my area of interest.

1

u/senatorpjt May 09 '15 edited Dec 18 '24

march alive aback swim tease crawl unite important husky handle

This post was mass deleted and anonymized with Redact

1

u/luciusplc May 09 '15

I forgot to mention, I know scala basics.I even took a course at coursera and there is also akka that is inspired by erlang/otp. But it appears (from jobs offers), that you need to have 5 years of java experience to be hired as scala developer. Weird...

1

u/senatorpjt May 10 '15 edited Dec 18 '24

square deranged enter provide recognise worthless toothbrush dazzling plough relieved

This post was mass deleted and anonymized with Redact

0

u/[deleted] May 10 '15

[deleted]

1

u/luciusplc May 10 '15

Thanks for your advice, I really appreciate it. You are right... Any job is better than none.

Distributed systems are millions dollars buisnesses and its risky to delegate it someone with my status. I completely understand that.