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

Bringing Some Order

[spoiler title=”Hint 1″]Put the letters into Alphabetical order[/spoiler]
[spoiler title=”Hint 2″]You don’t necessarily need to create a new list, you can just manipulate the input.[/spoiler]
[spoiler title=”Hint 3″]There are a number of different sort algorithms, one of which is a “bubble sort”. So named because you can use it to “bubble up” the largest values to the top.[/spoiler]
[spoiler title=”Hint 4″]You’ll need a “nested” while loop for this one. The outer loop will determine which letter you are currently moving. The inner loop is used to compare that letter to everything that comes after it. Swap them if your current letter is greater than the one you are comparing it to. Keep going until you’ve compared it with each letter, then increment the outer loop and start with the next character.[/spoiler]
[spoiler title=”Code”]

[code lang=”hack”]while var_a < input.length {
var_b = var_a + 1;
while var_b < input.length {
if input[var_b] < input [var_a] {
var_c = input[var_b];
input[var_b] = input[var_a];
input[var_a] = var_c;
}
var_b++;
}
var_a++;
}
return input;
[/code]

[/spoiler]

Missing numbers

[spoiler title=”Hint 1″]Return a list of numbers missing from the original input[/spoiler]
[spoiler title=”Hint 2″]You’ll want to sort it so that the largest number is at the end[/spoiler]
[spoiler title=”Hint 3″]You need to loop your sorted list and check if the number is what you expected it to be[/spoiler]
[spoiler title=”Hint 4″]If it’s not, add the expected (i.e. missing) number to a new list[/spoiler]
[spoiler title=”Code”]

[code lang=”hack”]var_a = [];
foreach var_b in input.sort {
while var_b != var_c {
var_a.push(var_c++);
}
var_c++;
}
return var_a;
// Explanation:
// Assuming we started with an array of [0, 3, 2]
// Set up an empty array (var_a) to hold our missing numbers
// Sort the initial list into numerical order (input.sort) becomes [0, 2, 3]
// Begin a loop through each item in the original input
// A complete sequence should be 0, 1, 2, 3, 4, etc. For our example, it would stop at 3.
// So we have a counter (var_c) which defaults to 0.
// We check to see if the first item in the sorted list (0) is equal to var_c (0)
// It is, so we just increment var_c by 1 and move onto the next item in the list.
// We now check that 2nd item in the list (2) is equal to var_c (1)
// Because it’s not, we add the value of var_c to our missing numbers array (var_a) and increment var_c by 1.
// We check again to see if the 2nd item in the list (2) is equal to var_c (2)
// Because they now match, we increment by 1 and move onto the third item in the list.
// Again, because var_c = 3 and the third item is also 3, we don’t need to add anything to our array.
// At the end of the loop, var_a will contain our missing numbers.

[/code]

[/spoiler]

Anagrams

[spoiler title=”Hint 1″]Return true if every item in the list is an anagram of the other[/spoiler]
[spoiler title=”Hint 2″]An easy way to do this is sort them and compare to each another[/spoiler]
[spoiler title=”Hint 3″]if you find one doesn’t match another, you know it’s false.[/spoiler]
[spoiler title=”Hint 4″]If you get to the end, then it’s true that they are all anagrams of each other.[/spoiler]
[spoiler title=”Code”]

[code lang=”hack”]var_a = "";
foreach var_b in input {
if var_a != var_b.sort && var_a != "" {
return false;
}
var_a = var_b;
}
return true;
[/code]

[/spoiler]

39 thoughts to “Hacked app code solutions – Chapter 4 – Cheatcode

  • Niels

    Please explain brining some order.

    Reply
  • Tiago Ferretti

    Hi! Congrats for your site!

    I’m from Brazil and I’d like to see the explanation.

    Tks!

    Reply
  • Alex

    I am new to coding. Thank you for providing hints and solutions for Hacked. Could you please explain the logic of “Bringing Some Order”? I have been trying to work it out on paper.

    Reply
    • Sei

      https://en.wikipedia.org/wiki/Bubble_sort

      Reply
    • Direnc

      sorry, answering a bit late (1 year) but the thought behind this is bubble sorting, comparing two objects and if one of them is bigger (in that case the alphabet), then you swap them by using a temp variable (var_c). this combined in a loop will sort the list.

      Reply
  • Julio

    In Bringing some order, the lane
    if input[var_b] < input [var_a] {
    The game tells that cannot compare those items

    Reply
    • J

      Getting the a similar error saying it’s expecting integers in the if condition

      Reply
    • J

      I fixed that by making sure input[var_a] is typed instead of just var_a by accident

      Reply
  • Facetti

    [hack]
    var_a = input.pop;
    foreach var_b in input {
    if var_a.sort != var_b.sort {
    return false;
    }
    return true;
    [/hack]
    Do you think this is easier to understand?

    Reply
    • Andrew

      Yes! Fewer lines, and more elegant. I definitely prefer yours to my solution.

      Reply
      • Arthur Dent

        Hello and Thank you for this app! I understand Facetti’s solution, but I don’t understand your solution for Chapter 4-3 (Cheatcode – Anagrams) and I’d like to understand it.

        var_a= ” ” (empty space I read in this comment section) – But what is stored here? Isn’t this an empty char-variable or string value with nothing in it?
        Thus I also don’t understand the comparisson between var_a and var_b.sort. neither do I understand why var_a may not be ” “.

        Reply
    • Someone

      I do not have the .sort thing. Please help!

      Reply
  • Naufal Fathoni

    Can you explain the code in “Missing Numbers”?

    Reply
    • Andrew

      I’ve given it a go, but it might not be very clear. It needs a nice little animation to show the steps as it works through the loops.

      Reply
  • Dduct1

    I created a less efficient solution using a while and if loop. For some reason it’s only passing the first case. Can you help me understand where I went wrong?

    [hack]
    var_c = [];
    Input = input.sort;
    While var_a < input.length {
    If input[var_a] != var_a {
    Var_c.push(var_a++);
    }
    Var_a++;
    }
    Return var_c;
    [/hack]

    Reply
    • Andrew

      You’re going to need a second loop. Your logic only works if the gap between numbers is no more than 1 missing number. The simplest demonstration of a failed case is if input is [0, 3]:

      1) line 4: if 0 != 0 is false, so skip line 5 and add 1 to var_a.
      2) next iteration, line 4, second item in the array. if var_a (1) != input[var_a] (3) then add to array. var_c is now [1]. add 1 to var_a
      3) but we’ve now reached the end of the loop, so return var_c.

      var_c was [1] but expected [1, 2]

      Does this help?

      Reply
  • marple

    Please explain the solution of bringing some order

    Reply
  • Quintin

    Please explain bringing some order

    Reply
  • Xander

    Is there a number associated to the letters in “bringing some order”?

    Reply
    • Andrew

      The lists in the test cases contains characters rather than numbers, so basically [“c”, “b”, “a”]

      When comparing strings, less than (<) means "comes before", and greater than (">“) means “comes after”, so comparing “a” > “b” is asking does “a” come after “b” in the alphabet? The answer would be false. (“a” < "b" == true)

      Reply
  • Pratz

    In the anagram challenge when I am trying to store ” ” in var_a what gets stored is “/”/”” why is that happening what to do??

    Reply
    • Josef

      Pratz, you should write ” ” (a space).

      Reply
  • abraham

    Shit your the best

    Reply
  • Will

    Andrew John’s, please explain how the code in “Bringing Some Order” works. Thank you.

    Reply
  • Emphyrio

    For ‘Bring in order’, here is my solution, which will be easier to understand than bubble sorting for beginners (but obviously less efficient). Also I didn t use input[x] since it s not shown in previous examples that you can extract a specific element from the array.
    Only thing I used is the fact ‘Z’ is smaller than ‘a’ (which isn’t obvious to a beginner). The loops just pick the smallest letter in input that hasn’t been picked yet. Also this sort wouldn’t work if 2 of the letters in input are the same, so it’s not very neat. But it s very simple ;).
    var_c stocks the reordered output, var_d stores the last letter added to var_c. The foreach loop looks for the smallest letter in input that is bigger than var_d(which is the last letter added to the output).

    [hack]
    var_d = ‘Z’;
    var_c = [ ];
    while var_c.length > var_a
    {
    if var_a > var_d
    {
    var_b = var_a;
    }
    }
    }
    var_c.push(var_b);
    var_d=var_b;
    }
    return var_c;
    [/hack]

    Reply
    • Emphyrio

      Oops the code got all messed up when publinshing it, sorry 🙁

      Reply
      • Andrew

        Looks like the it went a bit wrong and is missing some code. I’d be interested to see the full solution, you can add code in [ hack ] [ /hack ] brackets to format it nicely.

        Reply
  • Emphyrio

    [hack]
    var_d=’Z’;
    var_c=[];
    while var_c.length<input.length{
    var_b=’z’;
    foreach var_a in input{
    if var_a<var_b && var_a>var_d{
    var_b=var_a;
    }
    }
    var_d=var_b;
    var_c.push(var_b);
    }
    return var_c;
    [/hack]

    Obv it’s not very efficient (square of input length loop iterations), and also doesn’t work if input includes several occurences of the same character, but I guess the algo is easier to understand for beginners

    Reply
    • Kaz

      This was my original solution but didn’t think to use a temp var to store the last added element and couldnt use c.pop ” on empty list” to compare (&& a> c.pop )even when I was sure to check using an if that i pushed before popping (which seemed buggy to me)….. I actually ended up bubbling the ‘a’ to the front rather then z to the end like in Andrews solution, by decrementing in my inner loop from input.length-1 down to the outer loop counter. This actually saves passes making it slightly more efficient for lists with many elements, though perhaps not as clean. It it probably could have been achieved the same way using Andrew’s algo by just looping the inner to input.length-a ,as you know that after var_a many passes the last number of var_a elements will be correctly ordered.

      Reply
  • Mariano

    Hi Andrew, could you explain to me why on line 4 of your code for missing numbers you’re pushing (var_c++) to your empty list instead of just (var_c)? Thanks!

    Reply
    • Andrew

      Just a trick to save characters, it pushes var_c, and then immediately increments var_c by 1. It’s the same as doing this, which would also be more readable:

      [hack]
      var_a.push(var_c);
      var_c++;
      [/hack]

      Reply
      • Mariano

        Oh! What was confusing me was that I thought you were pushing the following value to the one you really needed. Thank you so much for replaying so fast.

        Reply
  • Francisco

    In ‘bringing in order ‘ I can’t compare letters, it returns an error. I’ve made a full alphabet list and a number list. Then I’ve made a burble up with numbers and changed it by the letters… a headache!! But hacked doesn’t let me use with letters. However, java doesn’t either.

    Reply
    • Francisco

      I mean use “”

      Reply
  • Raul Armando Bustamante

    foreach var_a in input{

    }

    Reply
  • Raul Armando Bustamante

    I think this way is easier to understand.

    foreach var_a in input{
    if input[0].sort != var_a.sort {
    return false;
    }
    }
    return true;

    Regards.

    Reply
  • Xavier

    Hey andrew,

    Could you explain why in “anagrams” you compare to var_a = ” “?
    Doesn’t that mean that var_a is equal to nothing and therfore always false?

    Im pretty new to this so my guess is that im completely wrong

    Thanks for all the useful hints and explanations so for!

    Reply
  • Simon

    Hi Andrew,

    Could you explain for the anagram example why a null check is required for var a

    My code is as follow

    For each var a in input
    {
    If var a != input [0].sort{
    Return false;
    }
    Var ==input.sort
    }
    Return true

    Aint sure why it isn’t working. Will you be able to advise

    Reply

Leave a comment

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