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″]
Tree
[spoiler title=”Hint 1″][/spoiler]
[spoiler title=”Code”]
[code lang=”hack”]function f1: var_a, var_b {
if var_b.is_list {
while var_b.length > 0 {
f1(var_a, var_b.remove(0));
}
}
else {
var_a.push(var_b);
}
return var_a;
}
f1([], input);
[/code]
[/spoiler]
7 thoughts to “Hacked app code solutions – Chapter 9 – Skynet”
How can you write an else block? My hackpad doesn’t have it!
Hope you figured this out by now, but once you add an “if” condition, and move the caret after the closing } bracket, your hackpad should show “else” below the true/false options.
I also used recursion. Pretty fun challenge 😀
Hi again, I know maybe I’m asking too much of you, but could you show the steps of your recursion with an example? I get lost thinking which values var_b takes during the process. I know it works because I tried it, but I would think that some values are lost when you keep applying ‘.remove’. Thanks!
I’m at 88%, can’t view the hidden cases, and can’t find them online. Can you help a brother out?
Neat site, Andrew!
My code is:
function f1: var_a {
if var_a.is_list == false {
return [var_a]; # special case of no list passed in
}
var_b = []; # gets returned
foreach var_i in var_a {
if var_i.is_list == false {
var_b.push(var_i); # base case
}
else {
var_c = f1(var_i);
foreach var_j in var_c {
var_b.push(var_j);
}
}
}
}
f1(input);
This succeeds in all three given cases, but it fails a hidden case (or 12% of hidden cases). Do you know where my error is?
Ah, I found my problem! My function wasn’t accounting for the special the input being [ ] (an empty list), which should also return [ ].
I meant to write my function so that it returned the list accumulated in var_b, but I forgot to include the “return var_b;” statement. Including that line is what fixed my program.