On Wed, Nov 01, 2006 at 11:20:45AM -0700, Josh Coates wrote:
> Sample Question 1
> 

> We are looking for sequences of n > 0 integers where the absolute values of
> the differences of successive elements are included in the set of numbers 1
> through n - 1. For instance,
> 
> 4 1 2 3
> 

I'm still a python novice, but I thouhgt it would be fun to post a
python solution just for discussion.

I'm also hoping someone will post a lisp solution since I'm trying to
get back into that.

!/usr/bin/python

import sys, string;

def main():
        currLine = sys.stdin.readline()
        while (len(currLine) > 0):
                nums = map(int, string.split(string.strip(currLine), ' '))
                numset = set(nums)
                for ind in range(len(nums)-1):
                        diff = abs(nums[ind]-nums[ind+1])
                        if not diff in numset:
                                print "not a match"
                                break
                else:
                        print "match"
                currLine = sys.stdin.readline()

main()

Barry

/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/

Reply via email to