codewars闯关玩耍1
-
codewars网址:https://www.codewars.com/dashboard
-
大一时在知乎上看到的网站,然后 点击、收藏、吃灰 一键三连,最近翻收藏夹的时候突然又看见了决定进来玩玩,练练英语,巩固下python
-
以后此系列,没事就写着玩玩
文章目录
- codewars闯关玩耍1
- Convert a String to a Number
- 补基础
- eval
- Stop gninnipS My sdroW!
- 补基础
- [::-1]
- 来实践下
- join()
- split()
- enumerate()
- 一个很大的知识漏洞... 和有些地方的引用搞混了 & pythontutor NB
Convert a String to a Number
-
题目
-
Description
- We need a function that can transform a string into a number. What ways of achieving this do you know? Note: Don’t worry, all inputs will be strings, and every string is a perfectly valid representation of an integral number.
-
Examples
"1234" --> 1234 "605" --> 605 "1405" --> 1405 "-7" --> -7
-
-
AC
def string_to_number(s): return eval(s)
或者
def string_to_number(s): return int(s)
补基础
eval
-
这里记得eval有这种用法,但是忘了具体是干啥的了,去复习一下
eval(expression, globals=None, locals=None)
, eval() 函数用来执行一个字符串表达式,并返回表达式的值- The arguments are a string and optional globals and locals.
- If provided, globals must be a dictionary.
- If provided, locals can be any mapping object(映射对象).
>>>x = 7 >>> eval( '3 * x' ) 21 >>> eval('pow(2,2)') 4 >>> eval('2 + 2') 4 >>> n=81 >>> eval("n + 4") 85
Stop gninnipS My sdroW!
题意好像是:住手!小鬼! 哈哈哈 ~
-
题目
-
DESCRIPTION:
Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.
-
examples
spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw" spinWords( "This is a test") => returns "This is a test" spinWords( "This is another test" )=> returns "This is rehtona test"
-
-
AC, 有好几种写法
def spin_words(sentence): words = sentence.split() for i in range(len(words)): if len(words[i]) >= 5: words[i] = words[i][::-1] return " ".join(words)
使用列表生成式
def spin_words(sentence): return ' '.join([ word[::-1] if len(word) >= 5 else word for word in sentence.split() ])
使用enumerate
def spin_words(sentence): words = sentence.split() for i,word in enumerate(words): if len(word) >= 5: words[i] = word[::-1] return ' '.join(words)
补基础
[::-1]
-
关于[::-1],之前就记得是倒序,这次在StackOverflow看到一个很好的回答
-
Assuming
a
is a string. The slice notation in python has the syntaxlist[<start>:<stop>:<step>]
So, when you do
a[::-1]
, it starts from the end towards the first taking each element. So it reverses a. This is applicable for lists/tuples as well.Example -
>>> a = '1234' >>> a[::-1] '4321'
Then you convert it to int and then back to string (Though not sure why you do that) , that just gives you back the string.
来实践下
a = "strfeddsadafafzxvxbc"
print(a[::-1])
print(a[::2])
print(a[13:6:-1])
print('----------------------------------------------------------------')
print(a[3:6:-1])
print('----------------------------------------------------------------')
print(a[6:3:-1])
print(a[3:5:1])
print(a[1::-1])
print(a[0::-1])
print(a[-1::-1])
print(a[-1:-2:-1])
print(a[-1:0:-1])
cbxvxzfafadasddefrts
sredaaazvb
fafadas
----------------------------------------------------------------
----------------------------------------------------------------
dde
fe
ts
s
cbxvxzfafadasddefrts
c
cbxvxzfafadasddefrt
join()
-
用于将序列中的元素以指定的字符连接生成一个新的字符串
-
str.join(sequence)
, sequence为要连接的元素序列 -
例
symbol = "-"; seq = ("a", "b", "c"); # 字符串序列 print symbol.join( seq ); # a-b-c
split()
-
str.split(str="", num=string.count(str)).
-
str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
-
num – 分割次数。默认为 -1, 即分隔所有。
-
返回值为分割后的字符串列表
-
-
例
str = "Line1-abcdef \nLine2-abc \nLine4-abcd"; print str.split( ); # 以空格为分隔符,包含 \n print str.split(' ', 1 ); # 以空格为分隔符,分隔成两个 ['Line1-abcdef', 'Line2-abc', 'Line4-abcd'] ['Line1-abcdef', '\nLine2-abc \nLine4-abcd']
txt = "Google#Runoob#Taobao#Facebook" # 第二个参数为 1,返回两个参数列表 x = txt.split("#", 1) print(x) ['Google', 'Runoob#Taobao#Facebook']
enumerate()
- enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中
一个很大的知识漏洞… 和有些地方的引用搞混了 & pythontutor NB
-
其实之前自己在之前的一篇刷题博客里面记过笔记,前面犯过错,是直接列表赋值会导致犯错
-
runoob Python可变对象和不可变对象
其实感觉还是没有完全理解
-
然后读了这篇,感觉写的不错,读完之后有点懂了
-
不可变对象
- 不可变指的是什么? 对象内容本身
- python中的变量有一个内存空间,变量保存(指向)的是存储数据(对象)的内存地址,也叫对象引用
- 变的是什么?
i = 73 , i是变量,保存(指向)了对象73, 执行 i += 2, 变量的值此时改变了,创建了新对象75,变量改变了对象引用,指向了新对象75,旧对象会被垃圾回收
- 不可变指的是什么? 对象内容本身
-
可变对象
m = [5, 9] m += [6]
原来对象是[5, 9], 变的是原先对象的内容, 不会创建新对象,
不变的是变量的指向,依旧指向原对象
大概懂啦!
-
-
-
在这题的时候,写了一次这样的写法
这样子是无效的👇
这样并没有改变原来的words 列表里面的内容,原因应该是python中string是immutable的
下面是用pythontutor搞的
-
于是在这里做了些实验,越做发现知识漏洞越多
a = "strfeddsadafafzxvxbc" for char in a: char = chr(ord(char)+2) print(char) print(a) for i in range(len(a)): char = a[i] char = chr(ord(char)+2) print(char) print(a) b = [2,3,5,6] for num in b: num = num + 2 print(b) for i in range(len(b)): num = b[i] num = num + 2 print(b) for i in range(len(b)): b[i] = b[i] + 2 print(b) u v t h g f f u c f c h c h | z x z d e strfeddsadafafzxvxbc u v t h g f f u c f c h c h | z x z d e strfeddsadafafzxvxbc [2, 3, 5, 6] [2, 3, 5, 6] [4, 5, 7, 8]
ates = "strfeddsadafafzxvxbc" for i in range(len(a)): a[i] = ates[i] print(a) TypeError: 'str' object does not support item assignment
这个得注意👆
-
现在仔细想想都能明白了,再不明白的可以去pythontutor
至于下面这个,我觉得学C的时候不会犯错,num改动了并不会影响b[i]
for i in range(len(b)): num = b[i] num = num + 2 print(b)
-
是之前list 赋值给别的,那个会把原先的一起改变,比如下例,再次感叹pythontutor的强大
b = [2,3,5,6] a = b for i in range(len(a)): a[i] += 1
- 没想到一道小题帮我找到这么多的问题
- 另外pythontutor是真的NB, 半年前自己构想过类似的工具还激动半天,没想到N年前 UCB就把这玩意当作CS61B的教学工具了