随笔分类 - Python 函数
摘要:Exercise: DESCRIPTION: This time no story, no theory. The examples below show you how to write function accum: Examples: accum("abcd") -> "A-Bb-Ccc-Dd
阅读全文
摘要:>> False >> 'No' >>True >> 'Yes' Solutions: 1 def bool_to_word(boolean): return "Yes" if boolean else "No" 2 def bool_to_word(boolean): return ['No',
阅读全文
摘要:Solution: def rps(p1, p2): dict_wins = {('rock', 'scissors') : 'Player 1 won!', ('rock', 'paper'):'Player 2 won!', ('scissors', 'rock') : 'Player 2 wo
阅读全文
摘要:Solition: def number(bus_stops): return sum([i[0] - i[1] for i in bus_stops]) def number(bus_stops): return sum(on - off for on, off in bus_stops)
阅读全文
摘要:描述: 你的函数有两个参数: 当前父亲的年龄(岁) 儿子现在的年龄(岁) 计算多少年前父亲的年龄是儿子的两倍(或多少年后他将是儿子的两倍)。 Solution: def twice_as_old(f, s): return abs(f - s * 2) :abs(x) 取数值绝对值
阅读全文
摘要:Exercise: Solution: def descending_order(num): return eval(''.join(sorted(str(num), reverse=True))) def descending_order(num): return int(''.join(sort
阅读全文
摘要:In this kata you will create a function that takes a list of non-negative integers and strings and returns a new list with the strings filtered out. E
阅读全文
摘要:Exercise: 单词首字母大写(刷题遇到title()失效) solution: def case(string): return ' '.join(word.capitalize() for word in string.split()) #先用split()函数 切片 字符串 #再capit
阅读全文
摘要:exercis: Solution: def basic_op(operator, value1, value2): return eval(str(value1) + operator + str(value2))#math模块eval()执行字符串表达式
阅读全文
摘要:def double_char(s): s2 = '' for i in s: i += i s2 += i return s2 *replace()函数 *tanslate()-str.maketrans(oldstr, newstr, delstr)
阅读全文
摘要:1.第一个:upper()函数,将所有字母都转换成大写; 2.第二个:lower()函数,将所有字母都转换成小写; 3.第三个:capitalize()函数,将首字母都转换成大写,其余小写; 4.第四个:title()函数,将每个单词的首字母都转换成大写,其余小写;
阅读全文
摘要:strip() 简介: 移除字符串 首尾 指定字符/片段 语法: str.strip('char') 参数: 'char': 指定的字符或者片段
阅读全文
摘要:Write a function to convert a name into initials. This kata strictlytakes two words with one space in between them.The output should be two capital le
阅读全文
摘要:title(): Python title() 方法返回"标题化"的字符串,就是说所有单词都是以大写开始 语法: str.title() 返回值: 所有单词都是以大写开始 例: istitle(): 简介: 检测字符串中的 单词 拼写 首字母是否大写 且 其他字母小写 语法: str.istitle
阅读全文
摘要:reverse() 简介: >reverse()是python中列表的一个内置方法(在字典,字符串或者元组中,没有这个内置方法) >用于列表中数据的反转 >该方法没有返回值,但会改变itertable对象 语法: itertable.reverse() 例: lst = [1, 2, 3,4, 5,
阅读全文
摘要:map() 简介: 根据提供的函数对指定序列做映射。 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。 语法: map(function, itertable) 参数: function: 自定义函数、内置函数
阅读全文