Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
"0=0" is an edge case added on request.
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
Yes, anything defined outside a function is in a "global scope", and anything defined within the function is (normally) in a "local scope". Scopes are common to most programming languages, but how they function exactly can vary. (For instance, Python tends to just have local and global scopes, but in e.g. C, C++, or Java, a function can have many scopes, and a variable might only exist within the body of a loop or whatever.)
Yes, a dictionary is basically a list, but instead of each element having a "number" index, you can use anything as the index. In other languages, these are sometimes called "maps" (Java, C++) or "hashes" (Perl, Ruby). I've also heard the term "associative array".
Yes, a "lookup" expression like
foo[k]
produces a value, and you can perform more lookups on that result usingfoo[k][k2]
. You often see this with attributes and methods --foo.bar.baz
orfoo.bar().baz()
. For example, you might have donerequest.POST.iteritems()
. From the point of view of "abstract syntax", a "lookup expression" isE1[E2]
where both E1 and E2 can be expressions. The concept of expressions is deeply recursive so you can build up expressions of arbitrary complexity likefoo[k1][bar.baz[k2]]
. This is IMO one of the most beautiful ideas in computer science, but expressions above a certain threshhold of complexity should probably get broken out into separate variables. I thinkreturn dict[choice][outcome]
is fine and good, but if you felt like it wuold aid readability, you might doRe: the
return foo if .....
solution. In other programming languages, there is a special if/then/else expression. In the C family of languages, it's writtencondition ? trueval : falseval
. Soconsole.log(win ? "you won!" : "you lost");
. For a long time, Python didn't have this kind of expression, but eventually it was added with a (IMO) very quirky syntax:trueval if condition else falseval
, soprint("you won!" if win else "you lost")
. The Python community thinks that this emphasizes the "default" case where the condition is true. Anyhow, so you can see that the "if/else" thing here is an expression, not a statement, and you can return any expression. (You can't do e.g.return while ....
.) I'm not actually sure how his solution works -- it seems like it will sometimes produce scissors-scissors ties, which I thought was forbidden?