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

110101110101000101

[spoiler title=”Hint 1″]The input is binary, the output is decimal.[/spoiler]
[spoiler title=”Hint 2″]Add them up: [4, 2, 1], where [1,0,1] = 5, [1,1,0] = 6, etc.[/spoiler]
[spoiler title=”Hint 3″]Remember a list is zero indexed, input positions would be [0, 1, 2][/spoiler]
[spoiler title=”Hint 4″]use the “pow” function (2 to the power of length-index?)[/spoiler]
[spoiler title=”Code”]

[code lang=”hack”]var_a = input.length -1;
while var_a > -1 {
var_b = var_b + input[var_a] * pow(2, input.length – var_a – 1);
var_a–;
}
return var_b;
[/code]

[/spoiler]

Prime

[spoiler title=”Hint 1″]A prime number is one that is greater than one, that can only be divided by itself or by 1[/spoiler]
[spoiler title=”Hint 2″]Remember that “mod()” will return the remainder when dividing one number by another[/spoiler]
[spoiler title=”Hint 3″]In case you forgot, 1 is not a prime number![/spoiler]
[spoiler title=”Code”]

[code lang=”hack”]if input == 1 {
return false;
}
var_a = 2;
while var_a < input {
if mod(input, var_a) == 0 {
return false;
}
var_a++;
}
return true;
[/code]

[/spoiler]

Number In Order

[spoiler title=”Hint 1″]The title is a bit misleading: Are the letters in order?[/spoiler]
[spoiler title=”Hint 2″]Remember: lists are zero indexed, a list of 5 items has a final index of [4][/spoiler]
[spoiler title=”Hint 3″]Compare one letter to the next one, is it true that X comes before Y?[/spoiler]
[spoiler title=”Code”]

[code lang=”hack”]while var_a < input.length – 1 {
if input[var_a] > input[var + 1] {
return false;
}
var_a++;
}
return true;
[/code]

[/spoiler]

26 thoughts to “Hacked app code solutions – Chapter 5 – Corrupted

  • Dimitra

    I used this code and the outputs where all correct but im still 8% false..I used your code but im new at coding and I dont understand wy this code is wrong
    Thank you for your time if you could answer me it would be great! And this is an amazing guide congrats for your work

    [hack]
    if input == 1 {
    return false ;
    }
    var_a = mod ( input, 2) ‘
    if var_a > 0 {
    return true;
    }
    return false;
    [/hack]

    Reply
    • DJ

      Dimitra
      The only problem with your code is that the number “2” would not be considered a prime number, which would be wrong.
      Make an explicite declaration that if input is 2, then true is returned.

      Reply
      • Andrew

        Interestingly, this would pass the challenge, but I think this suggests there’s an issue with poor test coverage on this challenge.

        This seems to pass, which is a simplified version of the above.

        [hack]
        return input == 2 || mod(input, 2) > 0 && input > 1;
        [/hack]

        However, if input was 9, then mod(9, 2) would return 1, which means the logic above would return true.

        However, an input of 9 should really return false, because 9 is not a prime number. And yet the above code passes, so evidently they’ve not put test cases in to prevent this, e.g. 9 or 15.

        Reply
  • Mgn

    In Prime can write only one line:
    return mod(input, 2) == 1;
    And this return values correctly.
    But exists hidden values and have <100% ;/

    Sign with Twitter? Lol.

    Reply
    • Andrew

      it solves the basic cases, but it doesn’t solve for all prime numbers. Remember hint 1: “A prime number is one that is greater than 1, that can only be divided by itself or by 1”

      when input is 1, then mod(1, 2) == 1 is true – but 1 isn’t a prime number. This should fail the hidden test case, but your logic passes it.

      when input is 2, then mod(2, 2) == 1 is false, but 2 IS a prime number. Your logic should pass this hidden test case, but your logic fails it.

      However, this seems to pass:

      [hack]
      return input == 2 || mod(input, 2) > 0 && input > 1;
      [/hack]

      but I think the issue here is poor test coverage – it shouldn’t be allowed to return true when input is 9 or 15 for example, they really should add these as hidden test cases!

      Reply
  • Robbe

    I’m stuck at the Third one.
    I had kinda similar code and came here to see what I did wrong(I’m just trying this out and learning) But I don’t understand the input[var +1] part.
    How do you enter it and what does it mean.

    Reply
    • Andrew

      The input is an array, or list of numbers. Each item in the array has a position (known as an index) which you can use to access the item at that position. The thing to remember is that this index is “zero-based”, meaning the first item starts at position 0, not 1 as you’d expect.

      So if you have an input of [10,20,30,40], and you did input[2] you would get 30 back. the first item 10, is at input[0].

      Does this make sense so far?

      What we do in the code is find out how many items there are in the list (using the .length). In our example above the list length is 4. So var_a will be 0 initially, but will loop repeatedly until var_a is equal to 1 less than the list length (while var_a < input.length - 1) So on the first pass through, var_a is equal to 0, meaning: "if input[var_a] > input[var_a + 1]”

      is the same as writing:

      “if input[0] > input[1]”

      which means on our list we’d be comparing 10 to see if it’s more than 20. If the first item was greater than we’d immediately return false and exit the loop as the list is not in order from smallest to largest. As 10 is less than 20, so we instead increase var_a by 1 (var_a++) and then try the loop again, with var_a now equal to 1.

      We keep doing this until our While condition is no longer true, when var_a is no longer less than the list length – 1, i.e. 3.

      The buttons that appear on the hackpad are context-sensitive, so some options only appear when the context is right. After you’ve hit the “input” button, you should get the option to add a “[ character. Then you can hit

      “var_a” “+” “1”

      and finally “]” to close it.

      Reply
  • Diego

    For sone reason, when i applied the sort function to an variable related to the input,
    The input was sorted too.

    Reply
  • Percy_JW

    I am stuck at the third Level with my own code and I See no problems :

    [hack]
    var_f = input;
    var_a = var_f.sort;
    var_b = var_f.length;
    var_c = var_f.length – 1;
    while var_b > 0 {
    if var_a[var_c] ==var_f[var_c] {
    var_d = var_d + 1;
    }
    var_b = var_b – 1;
    var_c = var_c – 1;
    }
    if var_d == input.length {
    return true
    }
    return false
    [/hack]

    Reply
    • Andrew

      lists are tricky. Two things to be aware of with your code, one is that:

      var_f = input

      results in var_f holding a reference to input. Any change to input or var_f will be reflected to both.

      The second thing is that .sort() actually sorts the original input, so line 2: “var_a = var_f.sort” results in var_a, var_f and input all being exactly the same amount! 🙂

      Debugging in Hacked isn’t fun, but here are some tips:

      1) You can add in return statements to force values to show up in the “out” box where your test results show.

      2) When you click on the cases to show the three tests, you can click on one of the tests to make it show up in the minified view while editing your code. So in your case, click on the “[q,u,a,t,r,e] > ” test, then in your code, at line 3 try these and see what the results are:

      return var_a;
      return input;
      return var_f;

      all 3 of these should result in a,e, q, r, t, u, which means your condition after the while loop will always be returning true.

      You don’t have access to it yet, but there’s a .copy() method that would allow you to make a separate copy of the input list, but in the meantime, you could do something like:
      [hack]
      var_f = []
      foreach var_h in input {
      var_f.push(var_h)
      }
      var_f.sort();
      [/hack]
      Which would result in input and var_f holding two unique copies of the same items, one sorted and one not.

      That should put you on the right track, but following your idea, there’s a 6 line solution that beats my original:

      !!!!!SPOILERS!!!!!! SOLUTION BELOW!!!!!

      [hack]
      var_a = [];
      foreach var_b in input {
      var_a.push(var_b);
      }
      var_a.sort;
      var_a == input;
      [/hack]

      Reply
  • Edo

    regard to “Number in order”, why is not enough check:

    if input.sort == input {
    return true;
    }
    return false

    Reply
  • Shawn M

    For challenge 1,

    While input.length > 0 {
    var_a = var_a + (input.pop * pow(2, var_b++));
    }

    Reply
  • Duriel

    I’m actually on the third level of this stage, and when i saw the case i was like “hey, if i sort the input i can see if it was already sorted !” so i did :

    if(input.sort() == input){
    return true
    }
    else {
    return false
    }

    but the third example fails…. for an unknown reason…
    [q,u,a,t,r,e] isn’t sorted and my program says “true”
    Can anyone explain it to me ?

    Reply
    • Duriel

      var_f = input

      results in var_f holding a reference to input. Any change to input or var_f will be reflected to both.

      Is this meaning my code can only send true ?

      Reply
  • joeguy

    I really found your solution to number 1 to be hard to understand. I found another way I understand and might be more readable to others.

    var_a = input.length-1;
    while var_a > -1 {
    var_b = var_b + input[var_c++]*pow(2, var_a);
    var_a–;
    }
    return var_b;

    this way you don’t have a complicated power function.
    Thanks 🙂

    Reply
    • joeguy

      that didn’t format correctly…
      var_a = input.length-1;
      while var_a > -1 {
      var_b = var_b + input[var_c++]*pow(2, var_a);
      var_a–;
      }
      return var_b;

      Reply
  • Rlando

    Regarding the binary puzzle. A more simple and elegant solution would be the following:
    [hack]
    foreach var_a in input {
    var_b = var_b * 2
    var_b = var_b + var_a
    }
    //Also correct without the following line:
    return var_b
    [/hack]

    Reply
  • Alex

    Mine for 110101110101000101. I think it`s simplier to understand
    [hack]
    var_a = 1;
    foreach var_c in input {
    var_b = var_b + input.pop * var_a;
    var_a = var_a * 2;
    }
    return var_b;
    [/hack]

    Reply
  • Alex

    Or even
    [hack]
    foreach var_c in input {
    var_b = var_b + input.pop * pow(2, var_a);
    var_a ++;
    }
    return var_b;
    [/hack]

    Reply
  • Alex

    Simplified Prime
    [hack]
    var_a = 2;
    while var_a < input {
    if mod(input, var_a) == 0 {
    return false;
    }
    var_a++;
    }
    input != 1;
    [/hack]

    Reply
  • Patroklo imaja

    Hey I’m new at this game and I have 0 knowledge on it. I made an algorithm to solve number in order but I don’t get why it doesn’t work:

    Var_b=input
    Input.sort
    If var_b==input
    |True
    Else
    |false
    And that’s all. It’s really simple but it won’t work. I think somehow the var_b gets sorted too when I sort input, but I don’t get why if the order is to do it after var_b acquires a value

    Reply
    • Patroklo imaja

      Okay so the comment didn’t respect the spaces and “enters” I made, I hope you don’t get trouble while reading the code. If it’s the case I will rewrite it

      Reply
  • Demolins

    Thank you for your helpful website, really instructive.
    You often got more elegant solution than mine, but I guess I’ve an easier way to solve 11011…
    While input.length > 0
    If input.pop == 1 {
    var_a = pow(2, var_b) + var_a;
    }
    var_b++;
    }
    return var_a;

    Reply
  • Dmitry

    Hi, I have another Binary to Decimal solution:
    foreach var_a in input {
    var_b = var_b * 2 + var_a;
    }

    Reply
  • A

    Number in order:
    I wrote it like
    var_a = []
    foreach var_b in input {
    var_a.push(var_b)
    }
    So i created a copy of input that wouldn’t be referenced directly to input.
    Then i sorted var_b(input copy) and compared it with input. It’s 2 lines longer than your solution. But isn’t it a bit more understandable. I feel stupid..
    Final code btw:
    var_a = []
    foreach var_b in input {
    var_a.push(var_b)
    }
    if input == var_b.sort {
    return true
    }
    else {
    return false
    }

    Reply
  • Iliya

    Idk if this is bug or not, but app accepted this solution for *Numbers in order* :
    return Input[0] == Input.Sort[0];

    Reply

Leave a comment

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