uva online judge self describing sequence problem solution python | uva 10049

The self describing sequence problem  (uva 10049) is also very straight forward and easy programming challenge in competitive programming once understood well. Which can be stated as (from online judge) Solomon Golomb’s self–describing sequence ⟨f(1),f(2),f(3),...⟩ is the only nondecreasing sequence of positive integers with the property that it contains exactly f(k) occurrences of k for each k. A few moments thought reveals that the sequence must begin as follows: n 1 2 3 4 5 6 7 8 9 10 11   12 f(n) 1 2 2 3 3 4 4 4 5 5 5 6 In this problem you are expected to write a program that calculates the value of f(n) given the value of n. Sample Input 100  9999  123456  1000000000  0 Sample Output 21  356  1684  438744 Programming Explanation This is very simple problem states that for given range of n as input (as shown in above image), we have

uva online judge jolly jumper problem solution python | uva 10038

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". 

To know more about for else statement in python visit our youtube video Cybo Socks - "For loop in python | For else statement"
.

Hope, this article found helpful for you, thank you.

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

Popular posts from this blog

uva online judge vito's family problem solution python | uva 10041

uva online judge australian voting problem solution python | uva 10142

uva online judge common permutation problem solution python | uva 10252