Make the frequent cases fast and the rare case correct

ACM – Jolly Jumpers – Python

Problem taken from here

A sequence of n > 0 integers is called a jolly jumper if the absolute values of the difference between successive elements take on all the values 1 through n-1. For instance,
1 4 2 3
is a jolly jumper, because the absolutes differences are 3, 2, and 1 respectively. The definition implies that any sequence of a single integer is a jolly jumper. You are to write a program to determine whether or not each of a number of sequences is a jolly jumper.

Sample Input

1 4 2 3
1 4 2 -1 6
Sample Output

Not jolly


import math
n = 4
inp = [1,4,2,3]
out = []
for i in range(1,n):
val = 0
for i in range(1,n):
        if out[i-1] == i:
                val = val+1
if val == n-1:
        print "jolly"
        print "not jolly"

$ python

n = 5
inp = [1,4,2,-1,6]

$ python
not jolly

Thanks for reading !

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: