Show off your Python chops and compete with others

Chris Angelico rosuav at gmail.com
Thu Nov 7 17:55:57 EST 2013


On Fri, Nov 8, 2013 at 5:38 AM, Nathaniel Sokoll-Ward
<nathanielsokollward at gmail.com> wrote:
> Wow! Thanks for all the feedback everyone. This content is fresh so I appreciate everyone's comments. As opposed to responding to each post individually, I'll just lump everything in here...

Best way, I think :)

> I believe that string literals can be written with single, double, or triple quotes: http://docs.python.org/release/2.5.2/ref/strings.html

Hmm. As a general rule, can you consider aiming your quiz - and any
citations like this - at a current version of Python? I'd prefer to
see this sort of thing aimed at the 3.3 docs, though if you want to
cite 2.7 that would also be of value. But 2.5 is now quite old, and
I'd rather not get the impression that you're writing a quiz based on
an unsupported version of Python. :) Though in this particular
instance it makes no difference.

>> By the way, here's a fairly bad solution to your final question:
>>
>> array666=lambda x:b"\6\6\6" in bytes(x)
>>
>> Works for the given test-cases! Doesn't work with arrays at all,
>> despite the description.
>
> Chris, I actually really like your answer, even if it doesn't satisfy the goal in the question. I'd give it a vote for cleverness!

Heh. Do you know what the limitation of my solution is, though? As I
said, it works for the given test-cases; what sort of input will it
fail on? (And also: What's its algorithmic complexity, and what's the
complexity of a better solution?) That's why I said it's a bad
solution :)

The side comment about arrays, though: Python *does* have arrays, but
they're a different beast from what you're working with, which are
called lists. The version I posted will actually work with any
iterable, but specifying that it be a list might open up some other
options.

BTW, you're going to see a lot of criticism on the list, because
that's the natural state of things. Doesn't mean we didn't enjoy
taking the quiz. :)

In your Intermediate section:
"""Which of the following is false regarding the raw_input() and
input() built-in functions in Python?

The old raw_input() has been renamed to input() in Python 3.x
input() is equivalent to exec(raw_input())
In Python 2.x, raw_input() returns a string.
raw_input() does not exist in Python 3.x"""

Technically one of those is false, but (a) you really need to specify
versions a LOT more clearly here, and (b) the falseness is a minor
technicality; it took me a while to notice that you'd written exec
where it actually uses eval. Is that distinction really worth
highlighting in the quiz?

"""Which of the following statements is false?

Python can be used to generate dynamic web pages.
Python can be used for web development.
Python's syntax is much like PHP.
Python can run on any type of platform."""

What does *any type* of platform mean? Do you mean "any platform", and
if so, do you mean that there is no pocket calculator on which Python
doesn't run? Or is there some other "type" of platform?

>>> type(platform)
<class 'module'>

I get it. Python will run on any module. *dives for cover*

BTW, here's my chosen "bad solution" for the boss question at the end
of the intermediate section. I'm sure someone here can come up with a
worse one. Wasn't sure what should be done if all three numbers are
the same, incidentally.

def indie_three(*numbers):
    seen = {}
    tot = 0
    for n in numbers:
        seen.setdefault(n, 5)
        seen[n] -= 4
        tot += n * seen[n]
    return tot

Note how I've generalized it to any number of input values AND to any
possible number of duplicates!

ChrisA



More information about the Python-list mailing list