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

Jolly

Not jolly

CODE:

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

OUTPUT:

$ python jolly.py

jolly

For:

n = 5

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

$ python jolly.py

not jolly

Thanks for reading !

### Like this:

Like Loading...

*Related*