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″]
Max
[spoiler title=”Hint 1″]When given a list of numbers, return the biggest number from that list.[/spoiler]
[spoiler title=”Hint 2″]You could use this in a while loop using the new .length function you’ve been given, but it’s simpler to just use foreach again.[/spoiler]
[spoiler title=”Hint 3″]If you want to try the While loop instead of foreach, you can, but you need know what “zero-based indexing” is. input.length gives you the number of items in that input. You also now have access to square brackets. You can use these to get the nth position in a list. But the first item is always at position 0, not 1 as you’d expect. This is a “zero-based” index. So, if your input was [2, 4, 6, 8] and you wanted the third item (6) in that input, you’d use input[2], and not input[3].[/spoiler]
[spoiler title=”Code”]
[code lang=”hack”]foreach var_a in input {
if var_a > var_b {
var_b = var_a;
}
}
return var_b;[/code]
[/spoiler]
[spoiler title=”Alternative Code”]
[code lang=”hack”]while var_a < input.length {
if input[var_a] > var_b {
var_b = input[var_a];
}
var_a++;
}
return var_b;[/code]
[/spoiler]
Maxxxx
[spoiler title=”Hint 1″]As before, when given a list of numbers, return the biggest number from that list.[/spoiler]
[spoiler title=”Hint 2″]You could use exactly the same script as the last challenge to complete this, but where is the fun in that when “max” will do some of the old logic for you?[/spoiler]
[spoiler title=”Hint 3″]max() takes two parameters/arguments. It expects you to give it 2 numbers. It will then give you back the largest of those two numbers.[/spoiler]
[spoiler title=”Hint 4″]As with the last challenge you can use either foreach or, a while loop…[/spoiler]
[spoiler title=”Code”]
[code lang=”hack”]foreach var_a in input {
var_b = max(var_b, var_a);
}
return var_b;
// explanation:
// Line 1: Loop through each item in the list, using var_a to store the current item in the list
// Line 2: We use var_b to track the current maximum number. It will default to 0 initially.
// Line 2: max(var_b, var_a) compares the current list item (var_a) with the current maximum (var_b) and returns the greater of the two. This will mean var_b either remains the same, or gets set to var_a if it’s higher.
// Line 3: Repeat this process for the next item in the list, comparing again with var_b.
// Line 4: You do this for every item in the list, until you reach the end, by which time var_b will be the highest value from the list.
[/code]
[/spoiler]
[spoiler title=”Alternative Code”]
[code lang=”hack”]while var_a < input.length {
var_b = max(var_b, input[var_a]);
var_a++;
}
return var_b;</pre>
<pre>// explanation:
// Line 1: Input is a list, which has a length. var_a will store the position (index) in that list
// Line 2: We use var_b to track the current maximum number. It will default to 0 initially.
// Line 2: max(var_b, input[var_a]) compares the current list item (input[var_a]) with the current maximum (var_b) and returns the greater of the two. This will mean var_b either remains the same, or gets set to input[var_a] if it’s higher.
// Line 3: Repeat this process for the next item in the list, comparing again with var_b.
// Line 4: You do this for every item in the list, until you reach the end, by which time var_b will be the highest value from the list.
[/code]
[/spoiler]
This Is Odd
[spoiler title=”Hint 1″]You must find a way to check if the input is an odd number[/spoiler]
[spoiler title=”Hint 2″]You could keep taking 2 from the original input until you are left with a value of either 1 (odd) or 0 (even).[/spoiler]
[spoiler title=”Hint 3″]Another way relies on dividing an integer (whole number) by 2 and getting a decimal value, or float. e.g. 3/2 = 1.5. Doubling this value again would equal 3.0, not the original input of 3.[/spoiler]
[spoiler title=”Code”]
[code lang=”hack”]while input > 1 {
input = input – 2;
}
return input;
// explanation:
// If our original input is 0 or 1, then it’s obviously odd or even
// if our original input is more than 1, we can keep taking away two from the input, and it will maintain its odd/even state.
// e.g. original input 7 becomes 5 becomes 3 becomes 1 (odd)
// original input 6 becomes 4 becomes 2 becomes 0 (even)
[/code]
[/spoiler]
[spoiler title=”Alternative Code”]
[code lang=”hack”]if input / 2 * 2 == input {
return 0;
}
return 1;
// explanation:
// Integers are whole numbers, like 4, 1, 7, 6, 2, etc.
// Decimals have a "." in their value, such as 4.1, 3.5, 7.0, etc.
// When you take an odd integer number like 7 and divide by 2, it becomes a decimal value, i.e 3.5.
// For even numbers like 8, dividing this by 2 would give us 4, which means it’s still an "integer" (whole) number.
// Once you have a value in decimal format it will remain a decimal, so multiplying 3.5 by 2 gives you 7.0, not 7.
// Because of the way Hacked datatypes and comparisons work, 7 is not the same as 7.0.
// So we can use this knowledge to compare the values before and after our divide/multiply trick.
// By comparing the original "input" to "input / 2 * 2", if the input is no longer whole, it must have been an odd number
// remember also that we don’t change the value of "input" in the code above, but we could have assigned var_a = input / 2 * 2 and then compared var_a to the original input.
[/code]
[/spoiler]
A man, a plan, a canal, panama
[spoiler title=”Hint 1″]True if it’s a palindrome (spells the same word backwards and forewards)[/spoiler]
[spoiler title=”Hint 2″]You’ll need to loop through using a foreach or while loop.[/spoiler]
[spoiler title=”Hint 3″]You need to compare letters at both ends to see if they match. Or perhaps, don’t match?[/spoiler]
[spoiler title=”Hint 4″]Remember that “pop” will take an item from the end of the list and reduce the list length by 1.[/spoiler]
[spoiler title=”Code”]
[code lang=”hack”]foreach var_a in input {
if var_a != input.pop {
return false;
}
}
return true;
[/code]
[/spoiler]
[spoiler title=”Alternative Code”]
[code lang=”hack”]while var_a < input.length {
if input[var_a] != input[input.length – 1 – var_a] {
return false;
}
var_a++;
}
return true;
[/code]
[/spoiler]
30 thoughts to “Hacked app code solutions – Chapter 3 – Jailbreak”
Hi, can you please explain the logic of “this is odd” for both, the code and the alternative code, because I didn’t get it at all how it works. Thank you
I have no idea, why the alternative code works, but the code works that if input is more than 1 it is subtracted by 2, meaning that until input gets to 0 or 1 it will always substract by 2 ( because 3 and 2 are the last numbers for which while loop work => 2-2=0, and 3-2=1). Let’s say that input =7 (odd number), while loop will substract 3 times (7-2=5,5-2=3,3-2=1, which is the final number). Same works for even numbers, but final number will be 0
I’ve updated both of those with explanations on why they work. Sorry for the long delay in responding!
My idea was to reverse the word and compare to the input. If the input is a palindrome they should match. But for some reason the input is wiped out:
[hack]
Var_a = input. length
Var_b = [ ]
Var_d = input
While var_c < var_a
{
Var_b.push(var_d.pop)
Var_c ++
}
[/hack]
At this point I want to compare var_b and input but input is empty. Why?
I tried the same. My hack looked like this:
[hack]
var_a = input;
var_b = [ ];
while var_a.length > 0 {
var_b.push(var_a.pop);
}
if var_b = input {
return true;
}
else {
return false;
}
[/hack]
But, alas, input was emptied and I never directly operated on it. Bug! I really couldn’t “think outside the box” after that, and came searching for a site like this for a solution. Once seeing the solution I hit myself in the forehead. And it’s more streamlined, too! #duh 😉
Nonetheless, working on a variable set to the value of another (original) variable should not destroy that original!
Lists are passed by reference not copied, to preserve the original make a deep copy.
I love the neatness of the first solution, mine it’s similar to the 2nd one.
In regards to the solution code, how does it solve correctly? If the some of the comparisons don’t match but the final comparison does, wouldn’t that still return a value of true?
How do implement the requirement that it return true every step of the way vs some or just the last value returned?
It compares the character in a calculated position from the beginning of the list to the last character in the list and if it fals then it jumps out of the code and it’s done.
So, the first time through var_a = input[0], which is the first item in the list, and compares it to the last item that is popped from the list (input.pop). If it is not equivalent then it returns false and the program ends. If it is equivalent, then it goes back through the loop to work on the second item in the list, where var_a = input[1] and compared to the last item in the list using input.pop again (which, technically, is the second to the last item in the original list). Later, rinse, repeat. When if you have an odd number of items, the middle item is compared to itself. For an even number of items, the middle pair of items are compared. In either case, assuming both are equivalent, the program continues it’s iterations. In the case of an odd number of items, once it’s popped and no more items remain in the list, the code breaks out of the foreach loop, returns true, and ends. In the case of an even number of items, where the middle pair are equivalent, one item gets popped, and the last iteration through is like working on a list with an odd number of items.
Note that you have to compare if it *is not* the same. If you compare that is *is* the same, a list like [t,i,n,t] will pass the first iteration through and return true and end the program. In order to ensure you iterate through all items in the list, you only want the program to terminate if the comparison is false in any iteration.
I came up with a different solution for “Max” based on my understanding of lists in Python:
[hack]
if input[0] > input[1] {
return input[0];
}
else {
return input[1];
}
[/hack]
I wanted to thank you so very much for your site – this was the first puzzle that I was looking for hints on but in the process came across this awesome resource that you’ve created! Thanks so much for your efforts and I was thrilled to see different solutions than the ones that I came up with so that I can understand different ways of solving it. Thanks again!!
YES! That is actually the simplest solution to solving that challenge for a list with just 2 items in it, which appears to be the case – I think I over-engineered it to make it work with lists of 2 or more.
[hack]
if input[0] > input[1] {
return input[0];
}
return input[1];
[/hack]
I wrote a different code for ” a man, a plan….
And in it I assigned ‘var a= input; ‘ but after using it in a while loop and popping all the elements from var a; I getting empty set for both input and var a can u help me out here…
The original code:
[hack]
var a= input;
var b=[];
Var c=[];
while var a!=[]{
var b= var a.pop;
Var c= var c.push(var b)
}
if input== var c {
return true;
}
else {
return false;
}
[/hack]
Lists are passed by reference, meaning var_a points to the original list. Changes to var_a will also change input.
Hi, I am still a newb on coding. I really dont understand the logic for the solution of Maxxx. Input has more than 2 integers stored, how can you read out the max with just two variables?
Will update the solution with an explanation but both solutions basically loop through each list item in turn, using one variable to store the current item (either the index or the actual item, depending on whether you use a while loop or foreach) and the second variable keeps track of the current maximum.
1) Starting with var_b being the 0, the first item will immediately be higher than 0, so you’ll set var_b to be the value of the first item.
2) You continue onto the next item in the list, and check to see if it’s higher than var_b. If it is, you replace var_b with that new value otherwise you leave var_b as it is.
3) You do this over and over for every item in the list, until you reach the end, by which time var_b will be the highest value from the list.
Hi ? stuck on ” a Man, a Plan..”
I wrote a code which completed the level %90 but I dont know whats wrong with it
Could you explain why it doesnt work? Thank you..
Here the code..
[hack]
While input[var_a] == input.pop {
var_a = var_a+1 ;
Return true ;
}
Return false ;
[/hack]
Sorry for my english.. 🙂
You’ve passed the visible cases, but you’ve failed a hidden case.
A hidden case example is an input of [a, x, y, a] which should return false, but your code would return true.
Using your example above, let’s step through the logic. You’ll see that “a” == “a”, so it adds 1 to var_a, but then immediately returns true. At this stage it hasn’t actually checked if “x” == “y”, which would have returned false.
At this point you may have realised that your code only checks the first and last character, and no others. This means you’ve passed the original cases by luck rather than by correct logic.
1) “cat” – “c” is compared to “t” and immediately fails – correct
2) “kayak” – “k” is compared to “k”, and immediately returns true, when it should have also compared “a” and “a” and then “y”
3) “bob” – “b” is compared to “b”, and immediately returns true.
The trick is to check that input[var_a] is NOT equal to input.pop at which point the loop can end. Keep repeating the loop while the input still has letters to compare. If you reach the end of the word and have no more letters to compare then it ha passed the test.
When I first saw the problem, I had a different view on it than a palindrome, which is to check if the word contains more than 1 instance of a letter.
Not that solving for this would be correct to the problem in the app (as its looking for a palindrome — sort of (just checks through the list to see if current is equal to last)).
How could we iterate through the list to see if there are more than one instance of a character, regardless of the index it is at?
For this is odd, i tried this and it worked!
[hack]
if input/2*2 != input{
return 1;
}
else{
return 0;
}
[/hack]
Very similar to the alternative code example I gave, but with a negative logic check instead of a positive one.
Hey, thanks for the tutorials!
There is something I’m confused about.
I am trying to do the whole game without hints, even though I have little coding experience. I got up to “Missing numbers” by myself, but that one took me nearly 2 hours to complete and my code was pretty big, over 20 lines so I decided to Google and check how other people solve the level concisely and found yours.
I noticed that you’re using the while function and input[var_a] to get it done pretty quickly, but I haven’t encountered input[var_a] inside any of the tutorials or examples within the game. I checked back in your guide to find the first time you used it, found that you offered it in one of your solutions here, with no explanation of what it does.
I still can’t seem to figure out what input[var_a] is supposed to do, do you mind explaining it to me? Thanks!
if input is a list of items, e.g. = [a, b, c, d]
then input[3] means the 4th item in that list – it’s zero based, so the first item “a” is at position 0, not 1, d is at position 3, not position 4.
so var_a would likely be an integer number of some sort, holding the index/position you want to look at in your list.
so if var_a = 2, then return input[var_a] would be the equivalent of return input[2], in my example list, it would return “c”.
Hope that helps!
Yes, prefect! Got it 🙂
Hi! I’m stuck on “a man, a plan…” Neither of the solutions you have here work – the first misses one task and I don’t know enough about coding to check the second letter. The alternative solution only tells me it’s an infinite loop… I only get through about 25% of the tasks by myself, so I would be happy for some help…
var _a = [];
while var_b < input.length / 2 {
var_a.push(input.pop);
var_b++;
}
if input.length != var_a.length {
input.pop;
}
var_c = true;
while var_a.length !=0 {
if var_a.pop != input.pop {
var_c = false;
}
}
return var_c;
For Max I rather use””” if input{0}<input{1} return input{1} else return input{0} "" " as they are 2 item list
Sorry I didn’t read the post above ?
Foreach var_a in input {
max(var_a, var_b)
}
^ this works too
*Read the problem*
“Mmmh… Is there any modulus operator in this game?”
*Check*
“Nope”
*Flip the table (or, in this case, the bed)*
I didn’t think of dividing by two until there was only a 1 or a 0. This is what happens when you are experienced with the C language.
Whoops, I meant substracting by 2, not dividing.
i want hack in jailbreak walk trough