The jolly jumper problem (uva 10038) is a sequence problem, stated as (from onlinejudge)
A sequence of n > 0 integers (n is number of 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.
But don't get confuse with this example, many students get confused and end up thinking jolly jumper difference must be in decreasing order. This is totally wrong!!.
Any sequence with N as length of that sequence, then if all the absolute differences are in range of 1 to N-1 then it is said to jolly jumper. Example,
4 3 4 is a jolly jumper, since 3 is length of sequence and absolute differences 1 (|4-3|), 1 (|3-4|) are lies between 1 to N-1 i.e 1 to 2 (since N = 3).
3 -1 5 is not a jolly jumper, since 3 is length of sequence (N=3) and absolute differences are 4 (| 3-(-1) |), 6 (|-1-5|) . Here difference is 4 and 6 which are greater than N (i.e 3).
Hope the idea is clear, now lets see the code.
Sample Input
4 1 4 2 3
5 1 4 2 -1 6
Sample Output
Jolly
Not jolly
Program Explanation
Here, we just need to do substraction of nth number with n-1 number and return absolute difference. Check if any obtained difference is greater than N (i.e length of sequence) if 'yes' print("Not Jolly").
Code
l = input().split()
n = len(l)
m = n - 1
for i in range(0, n):
l[i] = int(l[i])
def diff(a, b):
if(a>b):
return a-b
else:
return b-a
for i in range(0, n-1):
if (diff(l[i], l[i+1]) <= m):
continue
else:
print("Not Jolly")
break
else:
print("Jolly")
Function diff(), return absolute difference between given input a and b. After calculating length of sequence (i.e. n ) we check if every difference in less than n. If no then then else block will execute printing "Not Jolly" and skip else block below for loop.
If all differences satisfies condition then else block below for loop will be executed by printing "Jolly".
Related keywords: jolly jumper, jolly jumper uva, online judge jolly jumper, jolly jumper online judge, uva 10038, uva 10038 solution, jolly jumper solution online judge, jolly jumper source code, jolly jumper solution python, jolly jumper python, jolly jumper problem explanation, jolly jumper problem.
Comments
Post a Comment