This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sum(*args): | |
return __builtins__.sum(filter(lambda x: type(x) == int, args)) | |
print sum(1, 2, 3) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def unique(integers): | |
int_list = list(set(integers)) | |
int_list.sort(key = integers.index) | |
return int_list | |
print unique([4, 1, 2, 4, 3]) | |
# result [4, 1, 2, 3] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = range(1,11) | |
b = range(1,10) | |
c = sum([item for item in a if item in b]) | |
print c | |
# result: 45 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#吸血鬼数: http://zh.wikipedia.org/wiki/%E5%90%B8%E8%A1%80%E9%AC%BC%E6%95%B8 | |
def vampire_test(x, y): | |
print sorted(str(x) + str(y)) | |
return sorted(str(x * y)) == sorted(str(x) + str(y)) | |
print vampire_test(21, 6) | |
# result: True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 这次终于算是比较满意的符合pythonic的解决方式了 | |
def largest(n, xs): | |
return sorted(xs)[len(xs)-n:] | |
print largest(2, [10,9,8,7,6,5,4,3,2,1]) | |
# 不过看到一个更简洁的写法,好简洁,python真的是魅力无穷 | |
def largest(n, xs): | |
"Find the n highest elements in a list" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 这个是一个很程序化的写法,但是没有利用好python的特性 | |
def number_of_occurrences(s, xs): | |
num = 0 | |
for i in sorted(xs): | |
if i == s: | |
num += 1 | |
return num | |
# 这种写法就很pythonic | |
def number_of_occurrences2(s, xs): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 方法1 | |
def square_sum(numbers): | |
return sum(x**2 for x in numbers) | |
# 方法2 | |
def square_sum2(numbers): | |
return sum(map(lambda x: x**2, numbers)) | |
# 方法3 | |
def square_sum(numbers): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
def isPrime(n): | |
if n <= 1: | |
return False | |
for i in range(2, int(math.sqrt(n)) + 1): | |
if n % i == 0: | |
return False | |
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def validPhoneNumber(phoneNumber): | |
return bool(re.match(r"^(\([0-9]+\))? [0-9]+-[0-9]+$", phoneNumber)) | |
# 使用正则表达式判断电话号码格式正确与否 | |
#validPhoneNumber("(123) 456-7890") => returns true | |
#validPhoneNumber("(1111)555 2345") => returns false | |
#validPhoneNumber("(098) 123 4567") => returns false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def duplicate_count(s): | |
return len([c for c in set(s.lower()) if s.lower().count(c)>1]) | |
#test.assert_equals(duplicate_count("abcde"), 0) | |
#test.assert_equals(duplicate_count(""), 0) | |
#test.assert_equals(duplicate_count("abcdea"), 1) | |
#test.assert_equals(duplicate_count("indivisibility"), 1) | |
#test.assert_equals(duplicate_count("aabbcde"), 2) | |
#test.assert_equals(duplicate_count("aabbcdeB"), 2) | |
#test.assert_equals(duplicate_count("Indivisibilities"), 2) |
OlderNewer