SPOJ #1: Accepted!

I blogged a while back about SPOJ, a.k.a. the Sphere Online Judge. Basically, SPOJ is a massive community based around programming challenges, and I was quite excited to have found it at the time because SPOJ would help me look for problems to solve as I was learning Python. However, I haven’t actually solved (or worked on solving) any of the problems since I found SPOJ. This is because I found that most trivial and nearly all non-trivial tasks required loops, which I didn’t learn until recently… and then time constraints kept me from pursuing SPOJ challenges in addition to learning Python from Hetland (great book, by the way).

But now:

#!/usr/bin/env python

# spoj1.py v1.0.0
# SPOJ #1: Life, the Universe, and Everything
#
# Changelog:
# v1.0.0 (10/18/2010) - Initial release
#
# Copyright 2010 Benjamin Braithwaite
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

spoj = input()
while spoj != 42:
    print spoj
    spoj = input()

Above is the source code of spoj1.v1-0-0.py (which you can download from my Dropbox, if you so desire). Result: accepted! According to SPOJ, this solution weighed in at 0.03s of runtime and 3.6MB of memory, well within limits.

Basically, Life, the Universe, and Everything requires you to return the inputted one- or two-digit number unless that number is 42, in which case you end the program. I had some trouble with it — either the program would work but only return the first number before exiting, or it would go into an infinite print loop. After some rumination on how while works, I realized that while I couldn’t use the while not foo: that I love so much, I could duplicate the initial state of the program (so it wouldn’t exit the while loop if the answer wasn’t 42) by resetting the spoj variable to a blank input() after the program returned a piece of non-42 input. If that didn’t make sense, just read the source code and that should clear things up 😛 .

Hopefully this is the first of many successful SPOJ submissions; if you would like to see how I’m doing on SPOJ, my username is (predictably) bgbraithwaite. In the meantime, happy hacking!

Advertisements

One thought on “SPOJ #1: Accepted!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s