Absolute sorting
http://www.checkio.org/mission/absolute-sorting/
absolute-sorting:
def checkio(numbers_array):
li=sorted(numbers_array,key = lambda x : abs(x))
return li
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
def check_it(array):
if not isinstance(array, (list, tuple)):
raise TypeError("The result should be a list or tuple.")
return list(array)
assert check_it(checkio((-20, -5, 10, 15))) == [-5, 10, 15, -20], "Example" # or (-5, 10, 15, -20)
assert check_it(checkio((1, 2, 3, 0))) == [0, 1, 2, 3], "Positive numbers"
assert check_it(checkio((-1, -2, -3, 0))) == [0, -1, -2, -3], "Negative numbers"
更好的:
def checkio(numbers_array):
return tuple(sorted(numbers_array, key=abs))
Pangram
http://www.checkio.org/mission/pangram/
pangram:
#chr(i) 返回第i個字符
def check_pangram(text):
t = text.lower()
count = 0
for i in range(97,123):
if chr(i) in t:
count += 1
if count == 26 :
return True
else:
return False
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert check_pangram("The quick brown fox jumps over the lazy dog."), "brown fox"
assert not check_pangram("ABCDEF"), "ABC"
assert check_pangram("Bored? Craving a pub quiz fix? Why, just come to the Royal Oak!"), "Bored?"
更好的:
import string
def check_pangram(text):
text = text.lower()
return all(c in text for c in string.ascii_lowercase)
check_pangram = lambda text: len({x for x in text.lower() if x.isalpha()}) == 26
from string import ascii_lowercase
def check_pangram(text):
return set(ascii_lowercase).issubset(set(text.lower()))
Building Base
http://www.checkio.org/mission/building-base/
Building Base:
class Building:
def __init__(self, south, west, width_WE, width_NS, height=10):
self.south = south
self.west = west
self.width_WE = width_WE
self.width_NS = width_NS
self.height = height
def corners(self): #變量不是全局的都要加前綴
north = self.south + self.width_NS
west = self.west
south = self.south
east = self.west + self.width_WE
return {"north-west":[north,west],"north-east":[north,east],"south-west":[south,west],"south-east":[south,east]}
def area(self):
return self.width_WE * self.width_NS
def volume(self):
return self.area() * self.height
def __repr__(self):
return "Building({}, {}, {}, {}, {})".format(self.south,self.west,self.width_WE,self.width_NS,self.height)
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
def json_dict(d):
return dict((k, list(v)) for k, v in d.items())
b = Building(1, 2, 2, 3)
b2 = Building(1, 2, 2, 3, 5)
assert json_dict(b.corners()) == {'north-east': [4, 4], 'south-east': [1, 4],
'south-west': [1, 2], 'north-west': [4, 2]}, "Corners"
assert b.area() == 6, "Area"
assert b.volume() == 60, "Volume"
assert b2.volume() == 30, "Volume2"
assert str(b) == "Building(1, 2, 2, 3, 10)", "String"
更好的:
from itertools import product
class Building(object): # W to E: width, N to S: depth
def __init__(self, south, west, width, depth, height=10):
self.south, self.west = south, west
self.width, self.depth, self.height = width, depth, height
self.north, self.east = south + depth, west + width
def corners(self):
pairs = product(('south', 'north'), ('west', 'east'))
return {'-'.join(p): [getattr(self, d) for d in p] for p in pairs}
def area(self):
return self.width * self.depth
def volume(self):
return self.width * self.depth * self.height
def __repr__(self):
txt = 'Building({0.south}, {0.west}, {0.width}, {0.depth}, {0.height})'
return txt.format(self)