Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion advanced listcomprehenions?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Duncan Booth  
View profile  
 More options Jun 19 2008, 8:54 am
Newsgroups: comp.lang.python
From: Duncan Booth <duncan.bo...@invalid.invalid>
Date: 19 Jun 2008 12:54:31 GMT
Local: Thurs, Jun 19 2008 8:54 am
Subject: Re: advanced listcomprehenions?

Mark Wooding <m...@distorted.org.uk> wrote:
> This is still inelegant, though.  We can glue the results mod 3 and 5
> together using the Chinese Remainder Theorem and working mod 15
> instead.  For example,

>   [['Fizz', 'FizzBuzz', False, None, 'Buzz'][(pow(i, 4, 15) + 1)%7] or
>    str(i) for i in xrange(1, 101)]

> (A less mathematical approach would just use i%15 to index a table.  But
> that's not interesting. ;-) )

Ooh. Doesn't having 5 elements make you shudder? (Even though you did
change one to avoid a repeated value.) You have 4 options for output, so
for elegance that list should also have 4 elements:

[[str(i), 'FizzBuzz', 'Fizz', 'Buzz'][25/(pow(i, 4, 15) + 1)%4] for i in
xrange(1, 101)]

I feel it is even more elegant with the lookup table in its natural order:

[['Fizz', 'Buzz', 'FizzBuzz', str(i)][62/(pow(i, 4, 15) + 1)%4] for i in
xrange(1, 101)]

:)

--
Duncan Booth http://kupuguy.blogspot.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google