codewars练习
1. 如果我们列出所有小于10的自然数中,3或5的倍数,我们可以得到3 5 6 9。这几个数的和为23。
#给一个数,返回所有小于它的自然数,3和5的倍数之和。如果其中有负数,则返回0。
# 样例:solution(4), 3 ,solution(6), 8。
1 def solution(number): 2 sum = 0 3 if number<0: 4 return sum 5 else: 6 for i in range(1,number): 7 if i % 3 ==0 or i% 5 == 0: 8 sum=sum+i 9 return sum 10
2. 重构 不区分大小写 出现一次的字符为"(",出项两次及以上的为")"
# 样例: "din" => "(((" "recede" => "()()()"
1 from collections import Counter 2 def duplicate_encode(word): 3 dic1 = Counter(word.lower()) 4 new_word = list(word.lower()) 5 for i in range(len(new_word)): 6 if dic1[new_word[i]]>1: 7 new_word[i] =')' 8 else: 9 new_word[i] = '(' 10 return ''.join(new_word) 11 12 def duplicate_encode(word): 13 count={} 14 for i in word.lower(): 15 if i not in count: 16 count[i]=1 17 else: 18 count[i]+=1 19 print(count) 20 new_word=word.lower() 21 s="" 22 for i in new_word: 23 if count[i]>1: 24 s+=')' 25 else: 26 s+='(' 27 return s
# 样例[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]), "(123) 456-7890" def create_phone_number2(n): str1 = ''.join(str(x) for x in n[0:3]) str2 = ''.join(str(x) for x in n[3:6]) str3 = ''.join(str(x) for x in n[6:10]) return '({}) {}-{}'.format(str1, str2, str3) def create_phone_number1(n): return "({}{}{}) {}{}{}-{}{}{}{}".format(*n) def create_phone_number(n): #your code here s = "" m = "" for i in n: if len(s)<=2: s += str(i) for i in range(len(n)): if i>=3: m += str(n[i]) if i==5: m += "-" return("("+s+")"+" "+m) create_phone_number2([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
# 判断数组一的平方是否为数组二
# 样例 a1 = [-121, -144, 19, -161, 19, -144, 19, -11] a2 = [121, 14641, 20736, 361, 25921, 361, 20736, 361]
def comp(array1, array2): # your code count = 0 if(array1 == None) or (array2 == None) or(len(array1)!=len(array2)): return False length = len(array1) for i in range(length): if array1[i]<0: array1[i] = -array1[i] data_1 = sorted(array1) data_2 = sorted(array2) print(data_1) print(data_2) for i in range(length): if pow(data_1[i],2) == data_2[i]: count=count+1 if count==length: return True else: return False
# F(0) = 1; M(0) = 0; F(n) = n - M(F(n - 1)); M(n) = n - F(M(n - 1))
def f(n): if n==0: return 1 return n - m(f(n - 1)) def m(n): if n==0: return 0 return n - f(m(n - 1))
6. Count the smiley faces!
1 # face marked as : or ; 2 # face can have a nose but it does not have to. a nose are - or ~ 3 # face must have a smiling mouth,marked with either ) or D 4 # 样例: [':D',':',';~D',':)'], 4 5 def count_smileys(arr): # arr(eyes, nose, mouth) 6 num = 0 7 print(arr) 8 if arr is None: 9 return num 10 else: 11 for i in range(len(arr)): 12 if len(arr[i]) == 3: 13 if arr[i][0] in [":",";"] and arr[i][-1] in [")","D"] and arr[i][1] in ["-","~",""]: 14 num+=1 15 else: 16 num += 0 17 else: 18 if arr[i][0] in [":",";"] and arr[i][-1] in [")","D"] : 19 num+=1 20 else: 21 num += 0 22 return num 23 count_smileys([':oD', ':D', ';oD', ':(', ':(', ';oD', ';-(', ':oD', ':-(', ';(', ':D', ';D'])
7. Consonant value
1 # We shall assign the following values: a = 1, b = 2, c = 3, .... z = 26. 2 # Consonants are any letters of the alphabet except "aeiou". 3 # For example, for the word "zodiacs", let's cross out the vowels. We get: "z d cs" 4 # z = 26, d = 4 and cs = 3 + 19 = 22. The highest is 26. 5 alphabet='abcdefghijklmnopqrstuvwxyz' 6 alphabet_dict={} 7 for i in range(len(alphabet)): 8 alphabet_dict[alphabet[i]]=i+1 9 def solve(s): 10 # for i in s: 11 # if i in ["a","e","i","o","u"]: 12 # s = s.replace(i,"1") 13 s = s.replace('a','1').replace('e','1').replace('i','1').replace('u','1').replace('o','1') 14 s = s.split("1") 15 a = list(filter(None, s)) # 只能过滤空字符和None 16 list_a=[] 17 for i in a: 18 sum = 0 19 for j in range(len(i)): 20 sum+=alphabet_dict[i[j]] 21 j+=1 22 list_a.append(sum) 23 print(max(list_a)) 24 solve("catchphrase")
浙公网安备 33010602011771号