Pages

Bangladeshi Blog

Friday, January 14, 2011

24. Small Factorials

code:
def f(n):
    if n == 0:
        return 1
    else:
        return n * f(n-1)

for t in range(0, input()):
    print "%d" % f(input())

# end of source code
Be Pythonic

378. Size Contest

code:
tc = int(raw_input()) 

total = 0 
while tc: 
    tc -= 1 
    inp = int(raw_input()) 

    if inp > 0: 
        total += inp 

print total

# end of source code
Be Pythonic

8060. Cookies Piles

code:
tc = int(input()) 

while tc: 
    tc -= 1 
    n, a, d = [int(x) for x in raw_input().split()] 
    total = 0 
    while n: 
        n -= 1 
        total += a 
        a += d 

    print total

# end of source code
Be Pythonic

7974. What's Next

code:
while 1: 
    a, b, c = [int(x) for x in raw_input().split()] 
    if a == 0 and b == 0 and c == 0: 
        break 

    elif b - a == c - b: 
        print 'AP', c + (b - a) 
         
    elif b/a == c/b: 
        print 'GP', c * (b/a)

# end of source code
Be Pythonic

7757. Flowers Flourish from France

code:
while True: 
    line = str(raw_input()) 
    if line == '*': 
        break 

    s = [x.lower() for x in line] 

    # Removing leading spaces 
    while s: 
        temp = s.pop(0) 
        if temp != ' ': 
            s.insert(0, temp) 
            break 
         
    # Removing trailing spaces 
    while s: 
        temp = s.pop() 
        if temp != ' ': 
            s.append(temp) 
            break 

     
    check = s.pop(0) 
    ls = len(s) 
    i = 0 
    flag = True 
     
    while i < ls: 
        if s[i] == ' ': 
            if s[i+1] == check: 
                flag = True 
                i += 2 
            else: 
                flag = False 
                break 
        else: 
            i += 1 

    if flag: 
        print 'Y' 
    else: 
        print 'N' 

## End of Source Code
Be Pythonic

302. Count on Cantor

code:
import math 

tc = int(raw_input()) 

while tc: 
    tc -= 1 
    term = int(raw_input()) 
    row_p = float((-1 + (1 + 8 * term) ** 0.5) / 2.0) 
    row = int(math.ceil(row_p)) 

    pos = term - (row * (row - 1)) / 2; 

    a = row - pos + 1 

    if row % 2 == 0: 
        print 'TERM %d IS %d/%d' % (term, pos, a) 

    else: 
        print 'TERM %d IS %d/%d' % (term, a, pos)
Be Pythonic

42. Adding Reversed Numbers

code:
tc = int(input()) 

while tc: 
    tc -= 1 
    s, t = [str(x) for x in raw_input().split()] 

    p = [int(x) for x in s] 
    q = [int(x) for x in t] 

    lp = len(p) 
    lq = len(q) 

    if lp > lq: 
        q[lq : lp] = [0] * (lp - lq) 
    elif lq > lp: 
        p[lp : lq] = [0] * (lq - lp) 

    result = [] 
    carry = 0 
    while p and q: 
        s = p.pop(0) + q.pop(0) + carry 
        carry = s / 10 
        s = s % 10 
        result.append(s) 
    if carry: 
        result.append(carry) 

    while result: 
        temp = result.pop(0) 
        if temp != 0: 
            result.insert(0, temp) 
            break 

    output = [str(x) for x in result] 
    print ''.join(output) 
Be Pythonic