第三題:
問題:You probably know the "like" system from Facebook and other pages. People can "like" blog posts, pictures or other items. We want to create the text that should be displayed next to such an item.
Implement a function likes :: [String]
-> String
, which must take in input array, containing the names of people who like an item. It must return the display text as shown in the examples:
自己代碼:
def likes(names):
if(len(names)==0):
return "no one likes this"
elif(len(names)==1):
return names[0]+" likes this"
elif(len(names)==2):
return names[0]+" and "+names[1]+" like this"
elif(len(names)==3):
return names[0]+", "+names[1]+" and "+names[2]+" like this"
else:
return names[0]+", "+names[1]+" and "+str(len(names)-2)+" others like this"
評分較高代碼:
- def likes(names):
- n = len(names)
- return {
- 0: 'no one likes this',
- 1: '{} likes this',
- 2: '{} and {} like this',
- 3: '{}, {} and {} like this',
- 4: '{}, {} and {others} others like this'
- }[min(4, n)].format(*names[:3], others=n-2)
第四題:
問題:
Accumul.accum("RqaEzty"); // "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
Accumul.accum("cwAt"); // "C-Ww-Aaa-Tttt"
The parameter of accum is a string which includes only letters from a..z and A..Z.
For example:
[2, 4, 0, 100, 4, 11, 2602, 36]
Should return: 11
[160, 3, 1719, 19, 11, 13, -21]
Should return: 160
問題陳述:目的是一個列表中異類,即列表中大多數爲偶數,則返回奇數,否則反之問題:An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.
is_isogram("Dermatoglyphics" ) == true
is_isogram("aba" ) == false
is_isogram("moOse" ) == false # -- ignore letter case
代碼實現: