can anyone explain how this works? Specifically what do the slashes, brackets, and gi do here?
nice and clean
when you've been scratching your head for an hour or two and having more than 10 lines of code that actually does what I want it to. Then i see this. Hahaha!
I never knew enumerate() existed but now I do and now i know how to use it.
remark all number for 0,1,2,...,n-1 in the circle.
set f(n, k) is the solution function, so josephusSurvivor(n, k) = f(n, k) + 1
set g(s, n, k) is the solution function but start from number s.
f(n, k) = g(0, n, k).
think the case g(0, n, k), we take out number k-1 at first round, so the first round we will take up number (s + k - 1) % n.
now, let us start the game f(n, k) from n-k, the first round we take out number n-1, then it equels to start the game f(n-1, k).
so g(n-k, n, k) = f(n-1, k)
and g(x, n, k) = (g(0, n, k) + x) % n
so g(0, n, k) = (g(x, n, k) - x) % n
make x = n-k
f(n, k) = g(0, n, k) = (g(n-k, n, k) - (n-k)) % n = (f(n-1, k) + k) % n
last josephusSurvivor(n, k) = f(n, k) + 1
It uses type coersion, forcefully converting the string into an integer.
It's called the unary plus
This comment is hidden because it contains spoiler information about the solution
For anyone reading this 6 years later, this comment is not valid anymore. Please use str, this is much more readable than str.charAt().
It's known as recursion. You're doing the same operation over and over, only with the input of the last operation, until a certain condition has been met. If you remember factorials from basic algebra, that is actually a form of recursion. N * (N-1) * ((N-1)-1)... until you get to 1.
I was under the impression that 0.9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅ == 1, whereas 0.9999999999999999999999999999 != 1.
0.9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅9̅ == 1
0.9999999999999999999999999999 != 1
Yes for extremely large cases. Similar to 0.9999999999999999999999999999 == 1 close to a power of ten
0.9999999999999999999999999999 == 1
let's say some integer n has ?????.00000000000000000000000000000000001 as a square root. this might get truncated when "written" to a float into ???????.00000000000000000000 and produce a false positive
closest non-integer float, sorry
edit : someone found such a number:
9999999999999999 has sqrt 100000000 according to the usual floating point sqrt but that square root, when squared, doesn't work