'Jolly Jumper in Python
I'm trying to solve the Jolly Jumper problem on Kattis.
Here is the link to the problem: https://open.kattis.com/contests/ge97jq/problems/jollyjumpers
And here's my answer solution:
values = list(map(int,input().split(" ")))
n = values[0]
x = []
def isJolly(values, n):
for i in range(1, n-1):
d = abs(values[i] - values[i+1])
if (d == 0 or d > n-1 or d in x):
return False
else:
return True
if isJolly(values, n):
print("Jolly")
else:
print("Not Jolly")
I need help because Kattis keeps returning 'Wrong Answer', and I need to figure out why. I think it has something to do with an empty list.
Solution 1:[1]
According to the definition, it's jolly if each difference from 1..n-1 occurs once. This implies each difference should be unique. Whenever we encounter a difference that is out of range, or we've already seen, we conclude it's not jolly.
def isJolly(values, n):
x = {0}
for i in range(1, n-1):
d = abs(values[i] - values[i+1])
if (d > n-1 or d in x):
return False
else:
x.add(d)
return True
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |