As I mentioned elsewhere, the learning curve for Hacked is probably a bit steep for new coders. I didn’t just want to give you the answers outright. I’ve included hints to help where you might get stuck, all wrapped in spoiler tags. Teach a man (or woman) to fish! I’ve also included comments in the code blocks, which you can’t do in the app, but may help those who are just starting out with coding. Ignore them when typing them into the hackpad.

Note that while I tend to use the keyword “return” for better readability/understanding, you don’t always need it when the final action is to output your answer, which will save you some keystrokes and score you more points.

[toc heading_levels=”2″]

Addition

[spoiler title=”Hint 1″][/spoiler]
[spoiler title=”Code”]

[code lang=”hack”]function f1: var_a {
foreach var_b in var_a {
var_c = var_c * 10 + var_b;
}
return var_c;
}
var_b = f1(input[0]) + f1(input[1]);
var_a = [];
var_c = var_b / 10;
if var_c > 0 {
var_a.push(var_c);
}
var_a.push(mod(var_b, 10));
[/code]

[/spoiler]

Match 2

[spoiler title=”Hint 1″][/spoiler]
[spoiler title=”Code”]

[code lang=”hack”]var_a = [];
foreach var_b in input {
if var_b == "(" || var_b == "[" {
var_a.push(var_b);
}
else {
if var_a.length == 0 {
return false;
}
var_c = var_a.pop;
if var_c == "(" && var_b != ")" {
return false;
}
if var_c == "[" && var_b != "]" {
return false;
}
}
}
return var_a.length == 0;
[/code]

[/spoiler]

21 thoughts to “Hacked app code solutions – Chapter 8 – Killer Robot

  • szo

    what is wrong with my code? :

    [hack]
    if mod(input.length, 2) != 0 {
    return false;
    }
    while input.length > 0 {
    if input[0] != "(" && input[0] != "[" {
    return false;
    }
    else {
    if input[0] == "(" && input[input.length – 1] != ")" {
    return false;
    }
    if input[0] == "[" && input[input.length – 1] != "]" {
    return false;
    }
    input.remove(input.length – 1);
    input.remove(0);
    }
    }
    return true;
    [/hack]
    it gives me 93%…

    Reply
    • Andrew

      The easiest way to explain this is to look at one of the hidden tests that should be passing:

      [ ( , ) , [ , ] ]

      Reply
      • szo

        i don’t have twitter, so i cannot see hidden tests. thank you for such quick answer.

        Reply
      • heatx

        that’s a hidden case?
        that sucks, that break the pattern of the 3 cases showed (and the 93? of the cases)
        that must not be a case

        Reply
  • Ali

    Can’t pass match 2. The code given also doesn’t work :/

    Reply
  • Fandalf

    Anyone a hint how the answer of ‘Addition’ is reached. I have no while how to ‘add’ the lists together to reach the goal.
    [[1,2],[1,9]] -> [3,1]. ?
    [[3],[1,2]] -> [1,5]. ?

    Annoying got a hint for me?

    Reply
    • Tom

      12+19=31
      3+12=15

      Reply
    • S3lios

      [[1,2][1,9]] ->[3,1] because:
      [1,2]-12
      [1,9]-19
      [3,1]= 12 + 19 = 31

      Sorry, I don’t speak English. I’m french 🙂

      Reply
      • Leonardo

        Thanks!!!!! I wasn’t understanding the exercise
        This was the Hint that i need it!!

        Reply
    • Filipemtx

      The input is a list containing the digits of a number, so does the outputs. So you have to transform the digits into a number, sum it and then output a list with the digits of the result.

      Reply
  • MArk

    omg man, that psuedo maths.. ew.

    Reply
  • Azim

    Match 2 doesn’t work – u placed wrong code.

    Reply
    • Andrew

      You are the second person to say this, but I just tried it again as shown in my solution and it worked for me? Any chance you could screenshot your code so I can see it?

      Reply
      • john

        there is not yet “else”

        Reply
  • Mariano

    Hi Andrew, here’s my code for ‘match 2’, it works for 100% of the cases. If you have any idea for improvement I’d love to hear it. Also, for some reason, if I write lines 9 through 13 in the same line using ‘||’ like I did on line 3, for some reason it stops working for all cases.
    [hack]
    var_c = input.length -1;
    while var_a < (input.length -1) {
    if input[var_a] == "(" && input[var_a +1] == ")" || input[var_a] == "[" && input[var_a+1] == "]" {
    return true;
    }
    var_a ++;
    }
    while var_b < ((input.length-1)/2+1) {
    if input[var_b] == "(" && input[var_c] == ")" {
    return true;
    }
    if input[var_b] == "[" && input[var_c] == "]"
    {
    return true;
    }
    else {
    return false;
    }
    var_b++;
    var_c–;
    }
    [/hack]

    Reply
    • Rlando

      I believe your code incorrectly returns true on line 4 if the input is ()[)
      Then line 10 incorrectly returns true if the input is ([))
      And line 14 incorrectly returns true if the input is [(]]
      Line 17 in the current code is the only way to get it to return false, which due to the separate if statements is the case if the input does not start with [ and end with ] and none of the earlier if-statements were applicable. I.e. no …()…, no …[]…, no (…) and no […].
      Oh, line 19 and 20 never get executed, as the code always returns either on line 4, 12, 14 or 17. Either the if statement on line 12 is true, or it is not, and both cause the code to return a value and immediately finish.

      I’m surprised your code managed to fool all of the hidden cases as well.

      If anybody is interested in a slightly optimized version of this code:
      [hack]var_c = input.length – 1;
      while var_a < var_c {
      if input[var_a] == "(" && input[var_a +1] == ")" || input[var_a++] == "[" && input[var_a] == "]" {
      return true;
      }
      }
      if input[0] == "(" && input[var_c] == ")" {
      return true;
      }
      return false;
      [/hack]

      Reply
  • Rh

    Wtf is going on in the addition one? How is var_c * 10 + var_b equal to anything besides just var_b?

    Reply
  • paul

    Heyy can you explain the Funktion in Addition?
    I made a code that works but i would like to understand the f1 synthax you use.
    She do I ad which parameter and what parameter is used when?!
    Oh and how do I insert a screenshot here? ^^
    And they should tell they want an array as answer… I tried it with return (var_a) + “, ” + (var_b)
    While they were my tens and once -.-

    Reply
  • Dmitry

    Hi Andrew. How do you think what’s wrong?
    I place code (not screenshots) here cause some lines are too long.

    if input[0] == “)” || input[0]==”]” {
    return false;
    }
    if mod(input.length, 2) == 1 {
    return false;
    }
    else {
    var_b = input;
    }
    while var_b.length > 0 {
    var_c = 0;
    while var_c<var_b.length {
    if var_b[var_c] == "(" && var_b[var_c+1] == "]" {
    return false;
    }
    if var_b[var_c] == "[" && var_b[var_c+1] == ")" {
    return false;
    }
    if ( (var_b[var_c] == "(" ) && (var_b[var_c+1] == ")") ) || ( (var_b[var_c] == "[") && (var_b[var_c + 1] == "]") ){
    var_b.remove(var_c);
    var_b.remove(var_c);
    var_c = 0;
    }
    else {
    c++;
    }
    }
    }
    return true;

    Reply
  • how do i sent my code

    i have a almost wirking code that needs one more change but i dont know how to upload it
    can you tel me?

    Reply
  • Sly1024

    Oh man! You can’t do
    if var_a.length == 0 || var_a.pop() != var_b { return false; }
    because it first executes the .pop() and .length will be incorrect..
    Took me some time to figure it out.

    Reply

Leave a comment

Your email address will not be published. Required fields are marked *