其他分享
首页 > 其他分享> > Codewars笔记

Codewars笔记

作者:互联网

说明:以下内容均来自codewars网站,列举的试题我都做过且通过,并以此记录来学习python。   1,需求:将大小写互相转换,非字母的字符保留 我的代码:
 1 def to_alternating_case(string):
 2     #your code here
 3     result = ''
 4     for i in range(len(string)):
 5         if string[i].isupper():
 6             result += string[i].lower()
 7         elif string[i].islower():
 8             result += string[i].upper()
 9         else:
10             result += string[i]
11     return result

 

示例代码1:
1 def to_alternating_case(string):
2     return ''.join([c.upper() if c.islower() else c.lower() for c in string])

 

示例代码2:
1 def to_alternating_case(string):
2     return string.swapcase()
  测试代码:
1 Test.assert_equals(to_alternating_case("hello world"), "HELLO WORLD")
2 Test.assert_equals(to_alternating_case("HeLLo WoRLD1234"), "hEllO wOrld1234")
3 Test.assert_equals(to_alternating_case("String.prototype.toAlternatingCase"), "sTRING.PROTOTYPE.TOaLTERNATINGcASE")
4 Test.assert_equals(to_alternating_case(to_alternating_case("Hello World")), "Hello World")
5 Test.it("should work for the title of this Kata")
6 title = "altERnaTIng cAsE"
7 title = to_alternating_case(title)
8 Test.assert_equals(title, "ALTerNAtiNG CaSe")
9 title = "altERnaTIng cAsE <=> ALTerNAtiNG CaSe"
View Code

 

总结:对基础仍不熟练,缺少主动性练习   2、需求:识别字符串中数字化的字符,并将其改正。 例如,处理一下错误 测试方法: Test.assert_equals(correct("L0ND0N"),"LONDON"); Test.assert_equals(correct("DUBL1N"),"DUBLIN"); Test.assert_equals(correct("51NGAP0RE"),"SINGAPORE"); Test.assert_equals(correct("BUDAPE5T"),"BUDAPEST"); Test.assert_equals(correct("PAR15"),"PARIS");   我的代码:
1 def correct(string):
2     for i in range(len(string)):
3         if "5" in string:
4             string = string.replace('5','S')
5         elif "0" in string:
6             string = string.replace('0','O')
7         elif "1" in string:
8             string = string.replace('1','I')
9     return string

 

  示例代码1:
def correct(string):
    return string.translate(str.maketrans("501", "SOI"))
  解析: maketrans 和 translate 函数是进行字符串字符编码的常用方法 maketrans用法 string.maketrans(from, to) #制作翻译表 translate 用法 string.translate(s, table[, deletechars]) str.translate(table[, deletechars]) unicode.translate(table) 参数 返回值 返回翻译后的字符串,若给出了 delete 参数,则将原来的bytes中的属于delete的字符删除,剩下的字符要按照table中给出的映射来进行映射  实例:
1 import string 
2 map = string.maketrans('123', 'abc')  
3 s = "123345" 
4 string.translate(s,map)   #'abcc45' 
5 s.translate(string.maketrans('123', 'aaa'), '5')   #'aaaa4' 
6 s.translate(map)   #'abcc45' 
7 s.translate(string.maketrans('123', 'aaa'))   #'aaaa45'

 

示例代码2:
1 def correct(string):
2     return string.replace('1','I').replace('0','O').replace('5','S')

 

3、需求:得到一组数字,返回所有正数的总和。 Example [1,-4,7,12] => 1 + 7 + 12 = 20 提示:如果数组为空,和默认为0   我的代码;
 1 def positive_sum(arr):
 2     # Your code here
 3     sum = 0
 4     if arr == []:   
 5         return 0
 6     else:
 7         for i in arr:
 8             if i >0:
 9                 sum = sum + i            
10     return sum 

 

 1 #示例代码1:
 2 def positive_sum(arr):
 3     return sum(x for x in arr if x > 0)
 4 
 5 #示例代码2:
 6 def positive_sum(arr):
 7     return sum(filter(lambda x: x > 0,arr))
 8 
 9 #示例代码3:
10 def positive_sum(arr):
11     return sum(map(lambda x: x if x > 0 else 0, arr))

 

总结:因没仔细看需求,瞄了一眼就以为是把列表中的每个数求和,导致因为存在负数使得求和结果和测试的期望值不同,为了解决这个不必要的问题浪费了许多时间。

标签:return,string,sum,equals,笔记,assert,Codewars,Test
来源: https://www.cnblogs.com/chenri/p/10518325.html