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 the trip problem solution python | uva 10137

The Trip problem (uva 10137) is a very interesting mathematical operation problem, which can be stated as (from onlinejudge)

A number of students are members of a club group agrees in advance to share expenses equally, but it is not practical to have them share every expense as it occurs. So individuals in the group pay for particular things, like meals, hotels, taxi rides, plane tickets, etc. After the trip, each student’s expenses are tallied and money is exchanged so that the net cost to each is the same, to within one cent. Challenge is to find minimum amount required to exchange so that total spend can be equally divided.

Sample Input


10.00 
20.00 
30.00 
0

Sample output

$10.00 
Explanation

For given first lined input, number of students followed by their spends, we have to sum up all and find the minimum amount which can be exchanged in order to equally distribute it among students. Pretty straight forward solution given as

Code

main_list = []
l1 = []
l2 = []
avg = 0
total = 0

n = int(input())

if(n%2==0):
    m = n/2

for i in range(0,n):
    l1.append(float(input()))
    avg+=l1[i]

avg = avg/n

def diff(x,n):
    if(x>n):
        return x-n
    else:
        return n-x

for i in range(0,n):
    difference = diff(l1[i],avg)
    if difference in l2:
        continue
    else:
        l2.append(difference)

for i in l2:
    total = total + i
    
if(n%2==0):
    total = total / m

print("amount of exchange = ",total)
        
    

First, we accept all the inputs from user(i.e spends by each student) then we calculate average and store it in variable 'avg'. Now, for each spend we calculate difference between spend and average and append difference into the list L2. 
If we found difference is already exists in list, then we skip that itteration and move to next. If you observe this ensures that if someone spended extra X amount then someone spended less X amount.

Finally we sum up all differences in list L2 and add them, after divide the result by total number of students to get minimum amount of exchange.

Related keywords: uva the trip, the trip uva solution, the trip problem inpython, the trip problem solution, the trip problem hackerrank, uva 10137 problem explanation, uva 10137 the trip problem source code.

Comments

  1. Hey there but there in online judge shows an wrong answer after submission.Please help

    ReplyDelete
    Replies
    1. It is wrong because it doesn't accurately calculate the decimal numbers

      Delete

Post a Comment

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