Skip to content

Instantly share code, notes, and snippets.

@ldong
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ldong/9426867 to your computer and use it in GitHub Desktop.
Save ldong/9426867 to your computer and use it in GitHub Desktop.
Solution for PythonTip Coding Challenge # http://pythontip.sinaapp.com/coding

PythonTip solutions

# Title: PythonTip Coding Challenge Solutions
# Author: Lin Dong
# Url: http://pythontip.sinaapp.com/coding
# Goal: Trying to achieve solution with minimal effort/ codes
# Feel free to fork it and make improvements
  1. just print a+b print a+b

  2. list排序 print sorted(L)

  3. 字符串逆序 print a[::-1]

  4. 输出字典key print (",".join(map(str, a.keys())))

  5. 输出字符奇数位置的字符串 print "".join([a[i] for i in xrange(len(a)) if i%2==0])

  6. 求解100以内的所有素数

    N = 100
    print " ".join(map(str, reduce((lambda r,x: r-set(range(x**2,N,x)) if (x in r) else r), range(2,N), set(range(2,N)))))
  7. 求矩形面积 print int(a)*int(b), 2*(int(a)+int(b))

  8. 求中位数

    l = sorted(L)
    print ((l[len(L)/2] + l[len(L)/2-1]) /2.0) if len(L)%2 == 0 else l[len(L)/2]
  9. 给你两个正整数a和b, 输出它们的最大公约数.

    from fractions import gcd
    print gcd(a,b)
  10. 最小公倍数

    # Fact: GCD(M, N) × LCM(M, N) = M × N,
    from fractions import gcd
    print a * b // gcd(a, b)
  11. 结尾 0 的个数

    val = 1
    num = 0
    
    for i in L:
        val *= i
        q, r = divmod(val, 10)
        while r == 0:
            val = q
            num += 1
            q, r = divmod(val, 10)
    
    print num
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment