The Second Drunken Coding Contest - Win 10 FTC (Python) \[FINISHED\]
-
[quote name=“Kevlar” post=“49091” timestamp=“1388664303”]
I’m hammmered, and I can’t finger out how this code werks. If you can help me, I’ll give you a beer in FTC (10).You must MUST! tell me:
1. What does this pretty lettil code do? Give examples of input and output.
2. How does it do it?[code]
def inately(self, doge):
return zip(*[iter(doge)]*3)
[/code]An extra 5 FTC if you can tell me it’s performance in bed in O(n) notation.
[/quote]1. You used the def statement to create a function object called [i]inately[/i]? which does something with doge?
2. Then you’re returning a list to make possible an idiom for clustering a data series into n-length groups?lol I have no clue what that means.
I did run it in Python … no clue what it did or what it was supposed to do.
-
It tells you that Doge coin is innately self centred or introverted and returns zip (nothing, as in worthless) after a number of iterations (time?).
Am I close :D ;D :D
-
Is it the code you use to auto-update your [url=http://blockchain-link.com/#future]http://blockchain-link.com/#future[/url] page?
-
I hope you are THAT drunken next time that you code like me ‘newbie’! Would be great cause in that case I had a chance to get that bounty :D
-
Could you rephrase the question in the form of a story problem?
-
Yay another challenge.
The first line defines a function called ‘inately’ with the parameters ‘self’ and ‘doge’.
Next line is a bit tricky.
I think it returns a list of tuples.
For example if;
doge = [1,2,3,4,5,6,7,8,9]
you code will return [(1,2,3),(4,5,6),(7,8,9)] as n = 3I’m not sure what the purpose of ‘self’ is as you arn’t using it in the zip() are you?
-
[quote name=“delimitri” post=“49432” timestamp=“1388735147”]
Yay another challenge.The first line defines a function called ‘inately’ with the parameters ‘self’ and ‘doge’.
Next line is a bit tricky.
I think it returns a list of tuples.
For example if;
doge = [1,2,3,4,5,6,7,8,9]
you code will return [(1,2,3),(4,5,6),(7,8,9)] as n = 3I’m not sure what the purpose of ‘self’ is as you arn’t using it in the zip() are you?
[/quote]That’s twice now that you came through with the right answer… for 1. What about 2? You’re right, self isn’t used. But what is zip doing and what is the * doing?
-
I AM THE SMARTEST MAN ALIVE. LOL
Perhaps not…Hmmm. I’m not sure exactly how the zip works but it lets you iterate over multiple iterables (don’t know if that’s a word) simultaneously, so in other words allows you to process multiple sequences in parallel rather than sequentially.
As for the * in not too sure. I remember the words idiom and something regarding unpacking or something along those lines.
-
[quote name=“delimitri” post=“49450” timestamp=“1388740536”]
I AM THE SMARTEST MAN ALIVE. LOLPerhaps not…Hmmm. I’m not sure exactly how the zip works but it lets you iterate over multiple iterables (don’t know if that’s a word) simultaneously, so in other words allows you to process multiple sequences in parallel rather than sequentially.
As for the * in not too sure. I remember the words idiom and something regarding unpacking or something along those lines.
[/quote]Close enough for the win.
Since the function returns an iterable of 3-tuples, zip must accept three iterables in its command line, like so:
[code]
zip((x1, x2, x3, …), (y1, y2, y3, …), (z1, z2, z3, …))# So that must be what this is:
>>> [iter(doge)] * 3
(x1, x2, x3, …), (y1, y2, y3, …), (z1, z2, z3, …)
[/code]The asterisk in the function call unpacks the iterable (the list, in this case), so we’re pretty much at the meat of this curious function. However, doge, our input, is just an iterable of (x, y, z) points, so how can it be transformed to three iterables of one coordinate each?
Well, the magic is here:
[code]
[iter(doge)] * 3
[/code]What does this produce? One’s first thought would be that it produces a list of three iterators, which, when evaluated, would return something like:
[code]
(x1, y1, z1, x2, …), (x1, y1, z1, x2, …), (x1, y1, z1, x2, …)
[/code]i.e. the original sequence three times, which is nothing like what we need. The keen eye, however, will notice that this is not three iterators, but it is the same iterator, three times:
[code]
>>> print repr([iter(doge)] * 3)
[, , ]
[/code]As you can see, all the iterators have the same address, which means they are the same object. Thus, when zip tries to iterate over one array each time, the iterator gets advanced and returns the next element in a row, so what actually gets returned is what we needed (i.e. the tuple of three tuples).
In short, you would have to be drunk to write code like this. Congrats to delimitri, who has now won two in a row! You’re coins are on the way.
Stay tuned for the next drinking binge!
-
Cool, thanks for explaining.
And thanks again for the coins :D Much appreciated. Ive made far more coins with these two contests than actually mining.
I think I’ll give someone else a shot at the next one. Don’t want to be too greedy :)