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 codeBe Pythonic
Friday, January 14, 2011
24. Small Factorials
code:
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 codeBe Pythonic
Labels:
challenge
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 codeBe 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 codeBe 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 CodeBe 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
Labels:
classical,
data structure
11. Factorial
Code:
tc = int(raw_input()) while tc: num = int(raw_input()) count = 0 while num >= 5: count += num / 5 num /= 5 tc -= 1 print countBe Pythonic
Labels:
classical,
number theory,
simple math
4. Transform the Expression
Code:
tc = int(raw_input()) while tc: q = str(raw_input()) q = list(q) stack = [] post = [] stack.append('(') q.append(')') length = len(q) i = 0 while i < length: if q[i] == '(': stack.append(q[i]) elif q[i] == ')': while 1 and len(stack) != 0: temp = stack.pop() if temp == '(': break else: post.append(temp) elif q[i] == '+' or q[i] == '-' or q[i] == '*' or q[i] == '/' or q[i] == '^': if q[i] == '^': stack.append(q[i]) elif q[i] == '*' or q[i] == '/': while 1 and len(stack) != 0: temp = stack.pop() if temp == '^' or temp == '*' or temp == '/': post.append(temp) else: stack.append(temp) stack.append(q[i]) break elif q[i] == '+' or q[i] == '-': while 1 and len(stack) != 0: temp = stack.pop() if temp == '^' or temp == '*' or temp == '/' or temp == '+' or temp == '-': post.append(temp) else: stack.append(temp) stack.append(q[i]) break elif q[i] >= 'a' and q[i] <= 'z': post.append(q[i]) i = i + 1 tc = tc - 1 print "".join(post)Be Pythonic
Labels:
classical,
infix-to-postfix
1. Life, the Universe, and Everything
code:
while 1: num = int(raw_input()) if num == 42: break else: print numBe Pythonic
Labels:
classical
Subscribe to:
Posts (Atom)