说明:以下内容均来自codewars网站,列举的试题我都做过且通过,并以此记录来学习python。
 
1,需求:将大小写互相转换,非字母的字符保留
我的代码:
 def to_alternating_case(string):
#your code here
result = ''
for i in range(len(string)):
if string[i].isupper():
result += string[i].lower()
elif string[i].islower():
result += string[i].upper()
else:
result += string[i]
return result
示例代码1:
 def to_alternating_case(string):
return ''.join([c.upper() if c.islower() else c.lower() for c in string])
示例代码2:
 def to_alternating_case(string):
return string.swapcase()
 
测试代码:
 Test.assert_equals(to_alternating_case("hello world"), "HELLO WORLD")
Test.assert_equals(to_alternating_case("HeLLo WoRLD1234"), "hEllO wOrld1234")
Test.assert_equals(to_alternating_case("String.prototype.toAlternatingCase"), "sTRING.PROTOTYPE.TOaLTERNATINGcASE")
Test.assert_equals(to_alternating_case(to_alternating_case("Hello World")), "Hello World")
Test.it("should work for the title of this Kata")
title = "altERnaTIng cAsE"
title = to_alternating_case(title)
Test.assert_equals(title, "ALTerNAtiNG CaSe")
title = "altERnaTIng cAsE <=> ALTerNAtiNG CaSe"
总结:对基础仍不熟练,缺少主动性练习
 
2、需求:识别字符串中数字化的字符,并将其改正。
例如,处理一下错误
  • S is misinterpreted as 5
  • O is misinterpreted as 0
  • I is misinterpreted as 1
测试方法:
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");
 
我的代码:
 def correct(string):
for i in range(len(string)):
if "" in string:
string = string.replace('','S')
elif "" in string:
string = string.replace('','O')
elif "" in string:
string = string.replace('','I')
return string
 
示例代码1:
def correct(string):
return string.translate(str.maketrans("", "SOI"))
 
解析:
maketrans 和 translate 函数是进行字符串字符编码的常用方法
maketrans用法
string.maketrans(from, to) #制作翻译表
translate 用法
string.translate(s, table[, deletechars]) str.translate(table[, deletechars]) unicode.translate(table)
参数
  • table -- 翻译表,翻译表是通过 maketrans() 方法转换而来。
  • deletechars -- 字符串中要过滤的字符列表。
返回值
返回翻译后的字符串,若给出了 delete 参数,则将原来的bytes中的属于delete的字符删除,剩下的字符要按照table中给出的映射来进行映射 
实例:
 import string
map = string.maketrans('', 'abc')
s = ""
string.translate(s,map) #'abcc45'
s.translate(string.maketrans('', 'aaa'), '') #'aaaa4'
s.translate(map) #'abcc45'
s.translate(string.maketrans('', 'aaa')) #'aaaa45'
示例代码2:
 def correct(string):
return string.replace('','I').replace('','O').replace('','S')
3、需求:得到一组数字,返回所有正数的总和。
Example [1,-4,7,12] => 1 + 7 + 12 = 20
提示:如果数组为空,和默认为0
 
我的代码;
 def positive_sum(arr):
# Your code here
sum = 0
if arr == []:
return 0
else:
for i in arr:
if i >0:
sum = sum + i
return sum
 #示例代码1:
def positive_sum(arr):
return sum(x for x in arr if x > 0) #示例代码2:
def positive_sum(arr):
return sum(filter(lambda x: x > 0,arr)) #示例代码3:
def positive_sum(arr):
return sum(map(lambda x: x if x > 0 else 0, arr))
总结:因没仔细看需求,瞄了一眼就以为是把列表中的每个数求和,导致因为存在负数使得求和结果和测试的期望值不同,为了解决这个不必要的问题浪费了许多时间。

Codewars笔记的更多相关文章

  1. Codewars练习笔记·1 - 6.23

    Codewars地址:https://www.codewars.com/ 笔记资料来源:JavaScript高级程序设计. 欢迎和大家一起来讨论~   基础练习(1):   我的解答为: class ...

  2. codewars 题目笔记

    原题: Description: Bob is preparing to pass IQ test. The most frequent task in this test is to find ou ...

  3. JavaScript练习笔记整理·2 - 6.24

      Codewars地址:https://www.codewars.com/ 欢迎和大家一起来讨论~   基础练习(1):   我的解答为: function isIsogram(str){ if(s ...

  4. JavaScript练习笔记整理·1 - 6.23

    练习平台Codewars地址:https://www.codewars.com/ 欢迎和大家一起来讨论~╭( ・ㅂ・)و ̑̑   基础练习(1):   我的解答为: class SmallestIn ...

  5. git-简单流程(学习笔记)

    这是阅读廖雪峰的官方网站的笔记,用于自己以后回看 1.进入项目文件夹 初始化一个Git仓库,使用git init命令. 添加文件到Git仓库,分两步: 第一步,使用命令git add <file ...

  6. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  7. SQL Server技术内幕笔记合集

    SQL Server技术内幕笔记合集 发这一篇文章主要是方便大家找到我的笔记入口,方便大家o(∩_∩)o Microsoft SQL Server 6.5 技术内幕 笔记http://www.cnbl ...

  8. PHP-自定义模板-学习笔记

    1.  开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2.  整体架构图 ...

  9. PHP-会员登录与注册例子解析-学习笔记

    1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...

随机推荐

  1. 企业大数据之Elasticsearch的搜索类型

    下面的 ES基于版本(V2.3.4) ES之默认 1.默认自动发先同一局域网的所有集群节点 2.默认一个索引库会有5个分片,(分片越多,效率越好) 由于这两个默认,所以统一索引库的分片对分布在不同机器 ...

  2. 0x01 现阶段目标

    现阶段目标: 1.完成前端知识基础的学习. 具体如下: 在目前学习的基础上(html,css,JavaScript+BOM基础已经大致了解).针对DOM进行学习,个人在http://how2j.cn? ...

  3. #001 Emmet的API图片

    这个是一张Emmet的快捷键图片,里面包含了所有的快捷键. 虽然有很多的快捷键,但是常用的也就那么几个   .   样式 #  ID >  上下级节点 +  .col-md-8+.col-md- ...

  4. 汉诺塔问题php解决

    面向过程解决 <?php function hanio($n,$x,$y,$z){//把n个盘子,按照要求从x移到z,y是中介 //递归跳出条件 if($n==1){ move($n, $x, ...

  5. 【转】iOS - SQLite 数据库存储

    本文目录 1.SQLite 数据库 2.iOS 自带 SQLite 的使用 3.fmdb 的使用 4.fmdb 多线程操作 5.其他 SQLite 的第三方封装库 回到顶部 1.SQLite 数据库 ...

  6. virtualbox+vagrant学习-2(command cli)-2-vagrant cloud命令--有问题

    Cloud https://www.vagrantup.com/docs/cli/cloud.html 命令: vagrant cloud 这是用来管理与vagrant相关的任何东西的命令. 该命令的 ...

  7. 多线程之Lock

    Java并发编程:Lock 在上一篇文章中我们讲到了如何使用关键字synchronized来实现同步访问.本文我们继续来探讨这个问题,从Java 5之后,在java.util.concurrent.l ...

  8. Caused by: org.apache.velocity.exception.MethodInvocationException: Invocation of method 'getUser' in class org.uncommons.reportng.ReportMetadata threw exception class java.net.UnknownHostException :

    Running TestSuite [TestNG] [WARN] Ignoring duplicate listener : org.uncommons.reportng.HTMLReporter ...

  9. Verilog基础知识0(`define、parameter、localparam三者的区别及举例)

    1.概述 `define:作用 -> 常用于定义常量可以跨模块.跨文件; 范围 -> 整个工程; parameter:     作用 -> 常用于模块间参数传递; 范围 ->  ...

  10. 如何在C#程序中模拟域帐户进行登录操作 (转载)

    .NET Core .NET Core也支持用PInvoke来调用操作系统底层的Win32函数 首先要在项目中下载Nuget包:System.Security.Principal.Windows 代码 ...