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″]

Complete

[spoiler title=”Hint 1″]They probably expect you to use the new “insert” method they just gave you to manipulate the input.[/spoiler]
[spoiler title=”Hint 2″]An alternative would be to create a new list, using a while loop and the last number from the original input[/spoiler]
[spoiler title=”Expected Code”]

[code lang=”hack”]while var_a < input.length {
var_b = var_a;
while var_b != input[var_a] {
input.insert(var_a++, var_b++);
}
return input;
}
[/code]

[/spoiler]
[spoiler title=”Alternative Code”]

[code lang=”hack”]var_a = [];
var_b = input.pop + 1;
while var_c < var_b {
var_a.push(var_c);
var_c++;
}
return var_a;
[/code]

[/spoiler]

Match

[spoiler title=”Hint 1″]Does the number of closing brackets match the number of opening brackets?[/spoiler]
[spoiler title=”Hint 2″]We can’t close brackets unless we opened them first.[/spoiler]
[spoiler title=”Code”]

[code lang=”hack”]if input[0] == ")" {
return false;
}
foreach var_a in input {
if var_a == "(" {
var_b++;
}
else {
var_b–;
}
}
return var_b == 0;
[/code]

[/spoiler]

Rotate

[spoiler title=”Hint 1″]Take the first item and make it the last item[/spoiler]
[spoiler title=”Hint 2″]You can do this on one line without any need for vars[/spoilers]
[spoiler title=”Hint 3″]If it’s easier to think of it, you could set var_a to the first item in the list.[/spoiler]
[spoiler title=”Hint 4″].remove(x) and .push(x) are your friends.[/spoiler]
[spoiler title=”Code”]

[code lang=”hack”]return input.push(input.remove(0));
[/code]

[/spoiler]

18 thoughts to “Hacked app code solutions – Chapter 6 – Cyber Attack

  • nicoladc89

    Code of 6.1 (Cyber Attack/Complete)

    [hack]
    while var_a < input[var_a] {
    input.insert(var_a, var_a);
    var_a++;
    }
    return input;
    [/hack]

    Reply
    • Nonono

      6.1
      Can you write
      while var_a < input[var_a] { input.insert(var_a, var_a++);
      }
      return input;

      Reply
  • nicoladc89

    6.2

    [hack]
    foreach var_a in input{
    if(var_a == "("){
    var_b++;
    }
    if(var_a == ")"){
    if(var_b < 1)
    return false;
    }
    var_b–;
    }
    }
    return var_b == 0;
    [/hack]

    Reply
  • Alex Bicksler

    Your code for 6.2 “Match” fails on inputs like “())(()”. Instead of just checking the first element, you have to check for a negative tally on each iteration.

    Reply
    • Filipemtx

      Could you post your answer then?

      Reply
    • Andrew

      Well spotted, seems the test coverage on that challenge could be improved to pick up that kind of logic issue.

      Reply
      • Rlando

        That issue would be resolved by doing the following, and it removes the if statement from the beginning, resulting in shorter code.

        SPOILER:
        [hack]foreach var_a in input {
        if var_a == "(" {
        var_b++;
        }
        else {
        var_b–;
        }
        if var_b<0 {
        return false;
        }
        }
        return var_b == 0;[/hack]

        Reply
    • Alex

      foreach var_a in input {
      if var_a == “(” {
      var_b++;
      }
      else {
      var_b–;
      }
      if var_b < 0 {
      return false;
      }
      }
      return var_b == 0;

      Reply
  • Cleiton

    Code of 6.1 (Cyber Attack/Complete)

    [hack]
    while input[0] > 0 {
    input.insert(0, input[0] – 1);
    }
    return input;
    [/hack]

    Reply
  • BigIndian

    Hi, my solution was :

    [hack]
    input.sort;
    while var_a < input.length {
    if input[var_a] != var_a {
    input.insert(var_a, var_a);
    }
    var_a++;
    }
    return input;
    [/hack]

    Was it a good solution or is it possible to improve ?

    Thanks for your advice !

    Reply
    • Andrew

      You don’t need the input.sort at the beginning because the inputs are already sorted, but it’s a better solution than mine, only bettered by nicoladc89 and Cleiton’s answers above.

      Reply
  • Mariano

    Hi Andrew, I stumbled upon a problem and I don’t know why it happens.
    On the third problem (rotate) I wrote:
    1 var_a = input [0];
    2 input.insert (input.length -1, var_a);
    3 input.remove (0);
    4 return input;

    Line 2 inserts the value on the penultimate position instead of the last one, but if I use input.lenght without substracting 1, it says ‘index out of bounds’
    I know I could use push, but I wanted to know the reason behind the error.
    Thanks!

    Reply
    • Andrew

      Seems to me that .insert() won’t let you insert an item to the final position on the list, it expects you to only use .push() to add an item to the end of the list. This constraint makes sense to me. You’d normally use a .push or .add() in other languages to achieve this.

      insert(-1, item) would add item just before the last item.

      Reply
  • Mat

    6.2
    My solution doesnt work 100%, du u know why?

    Foreach var_a in imput {
    If var_a == “(” {
    Var_b ++
    }
    If var_a == “)” {
    Var_c ++
    }
    }
    Var_b==var_c

    Reply
  • Mat

    Me again, i added && var_b>-1 at the first If and now it worked

    Reply
  • Alex

    My 6.2
    [hack]
    foreach var_a in input {
    var_b++;
    if var_a == input[input.length – var_b] | | input[0] == ")" {
    return false;
    }
    }
    return true;
    [/hack]

    Reply
  • ugur

    Hi Andrew, here is my solution for Code of 6.1 (Cyber Attack/match) :

    var_b = “”;
    var_a = input.length-1;
    while var_a > -1 {
    var_b = var_b + input[var_a];
    var_a –;
    }
    return var_b;

    Reply
    • ugur

      excuse me,
      i made a mistake, it was not the solution ofCode of 6.1 (Cyber Attack/match), it was the solution of the puzzle packs/ easy peasy/ reverse

      Reply

Leave a comment

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