最近发现一个不错的代码练习网站codewar(http://www.codewars.com)。注册了一个账号,花了几天的茶余饭后时间做题,把等级从8级升到了7级。本文的目的主要介绍使用感受及相应题目,可供大家参考。

新人注册为8级,入门题NO.1:

topic: Multiply

instructions:The code does notexecute properly. Try to figure out why.

my solution:

def multiply(a, b):
c = a * b
return c

NO2:

topic: ListFiltering

instruction: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.

example:

my solution:

def filter_list(l):
new_l =[]
for x in l:
if type(x) != str:
new_l.append(x)
return new_l

best practice from others:

def filter_list(l):
'return a new list with the strings filtered out'
return [i for i in l if not isinstance(i, str)]

and

def filter_list(l):
'return a new list with the strings filtered out'
return [x for x in l if type(x) is not str]

No.3

topic:two to one

instruction:Take 2 strings s1 and s2 including only letters from ato z. Return anew sorted string, the longest possible, containing distinct letters,each takenonly once - coming from s1 or
s2.

Examples:

``` a = "xyaabbbccccdefww" b ="xxxxyyyyabklmopq" longest(a, b) -> "abcdefklmopqwxy"

a = "abcdefghijklmnopqrstuvwxyz" longest(a, a) ->"abcdefghijklmnopqrstuvwxyz" ```

my solution:

import string
from pandas import Series
def longest(a,b):
c1 = a + b
c2 = list(c1)
c3 = Series(c2)
c4 = c3.unique().tolist()
c5 = sorted(c4)
c6 = ''.join(c5)
return c6

best solution from others:

def longest(a1, a2):
return "".join(sorted(set(a1 + a2)))

No.4

topic:Categorize New Member

instruction:The Western Suburbs Croquet Club has two categories of membership,Senior and Open. They would like your help with an application form that willtell prospective members which category they will be placed.

To be a senior, a member must be at least 55 years old and have ahandicap greater than 7. In this croquet club, handicaps range from -2 to +26;the better the player the lower the handicap.

example:

my solution:

def openOrSenior(data):
a = []
for i in data:
if i[0]>54 and i[1]>7:
a.append('Senior')
else:
a.append('Open')
return a

best solution from others:

def openOrSenior(data):
return ["Senior" if age >= 55 and handicap >= 8 else "Open" for (age, handicap) in data]

No.5

topic: Array.diff

instruction:your goal in this kata is to implement an difference function, which subtracts one list from another.

example:


my solution:

def array_diff(a, b):
diff = []
for i in a:
if i not in b:
diff.append(i)
return diff

best solution from others:

def array_diff(a, b):
return [x for x in a if x not in b]

No.6

topic:Mumbling

instruction:This time no story, no theory.

example:

my solution:

def accum(s):
b = list(s)
i = 0
for j in b:
b[i] = b[i] * (i+1)
i = i+1
a = list(map(lambda x: x.capitalize(),b))
c = '-'.join(a)
return c

best solution from others:

def accum(s):
return '-'.join((a * i).title() for i, a in enumerate(s, 1))

No.7

topic:Multiples of 3 or 5

instruction:If we list all the natural numbers below 10 that are multiples of 3or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.Finish the solution so that it returns the sum of all the multiplesof 3 or 5 below the number passed in.

note:If the number is a multiple of both 3 and 5, only count it once.

my solution:

def solution(number):
a = []
j = 3
while j<number:
if j%3==0 or j%5==0:
a.append(j)
j = j + 1
return sum(a)

best solution from others:

def solution(number):
return sum(x for x in range(number) if x % 3 == 0 or x % 5 == 0)

在8级的时候,做7级题甚至6级题,能快速升级到7级。知识点总结:

1. type和instance判断数据类型

2. 遍历list的方法,list切片知识,如何读取两个list中的非交集

3. str转list,list转series数据,array转list(array.tolist()),字母如何排序(sorted)

4. set 函数(创建一个无序不重复元素集)

5. 改变字符串的首字母:capital,upper,lower,title

6. join连接字符,split分割字符串

7. 枚举函数enumerate,例如枚举list中的索引和元素

8. 如何跳出嵌套循环(写个def,用return)

9. try和except的使用

10. lambda函数使用

感受:

8级到7级的晋升路上,主要涉及的python的数据结构相关的基础知识点。而python的数据结构知识是在一年前学习的,零零落落已经忘得差不多了,做题目的时候基本靠谷歌知识点。在codewar做题的时候,貌似答案没有通过,是无法看到其他人的答案的。在通过答案后,再看其他人的解法,常常有眼前一亮的感觉。

codewar代码练习1——8级晋升7级的更多相关文章

  1. codewar代码练习2——7级晋升6级

    7级晋升到6级的过程中以做6级题以及以前未完成的题目为主,一般选择算法题或者基础题.相比之前从8级升级7级(参见此博客:http://blog.csdn.net/m0_37324740/article ...

  2. 行为级和RTL级的区别(转)

    转自:http://hi.baidu.com/renmeman/item/5bd83496e3fc816bf14215db RTL级,registertransferlevel,指的是用寄存器这一级别 ...

  3. CSS 各类 块级元素 行级元素 水平 垂直 居中问题

    元素的居中问题是每个初学者碰到的第一个大问题,在此我总结了下各种块级 行级 水平 垂直 的居中方法,并尽量给出代码实例. 首先请先明白块级元素和行级元素的区别 行级元素 一块级元素 1 水平居中: ( ...

  4. 学习总结:CSS(二)块级与行级元素特性、盒模型、层模型、BUG与BFC、浮动模型

    一.元素的块级与行级特性 在CSS属性display控制元素是否及如何显示的特性,常用的值有none.inline.block.inline-block,在CSS3中还有一些新的特性状态,在这里不做讨 ...

  5. 51nod图论题解(4级,5级算法题)

    51nod图论题解(4级,5级算法题) 1805 小树 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 她发现她的树的点上都有一个标号(从1到n),这些树都在空 ...

  6. js input复选框选中父级同时子级也选中

    js实现复选框选中父级元素子级元素也选中,没有子级元素选中父级也不选中的效果 HTML <tr> <td> <label> <input name=" ...

  7. 操作系统学习笔记5 | 用户级线程 && 内核级线程

    在上一部分中,我们了解到操作系统实现多进程图像需要组织.切换.考虑进程之间的影响,组织就是用PCB的队列实现,用到了一些简单的数据结构知识.而本部分重点就是进程之间的切换. 参考资料: 课程:哈工大操 ...

  8. [数据库事务与锁]详解五: MySQL中的行级锁,表级锁,页级锁

    注明: 本文转载自http://www.hollischuang.com/archives/914 在计算机科学中,锁是在执行多线程时用于强行限制资源访问的同步机制,即用于在并发控制中保证对互斥要求的 ...

  9. MySQL行级锁,表级锁,页级锁详解

    页级:引擎 BDB. 表级:引擎 MyISAM , 理解为锁住整个表,可以同时读,写不行 行级:引擎 INNODB , 单独的一行记录加锁 表级,直接锁定整张表,在你锁定期间,其它进程无法对该表进行写 ...

随机推荐

  1. 应用程序之Xib自定义Cell

    效果展示 结构分析 代码实现 一.效果展示 二.结构分析 1⃣️首先我们让我们的控制器不再继承UIViewController,而是继承UITableViewController.这样就直接遵守了de ...

  2. robotframe使用之滚动条

    方法一:Excute JavaScript window.scrollTo(0,document.body.scrollHeight); 方法二:Execute javascript document ...

  3. css 使表格随着内容自动适应宽度

    所谓难而不会,会儿不难.这个问题让我纠结了很长时间,一句css解决了,仅仅靠一个属性 td { white-space: nowrap; }

  4. ExtJs4学习(二):Dom操作

    如今主流的JS框架要数ExtJs和JQuery应用的比較广泛.JQuery属于轻量级的,一般做站点应用比較常见.可见块头小的优势. ExtJs比較庞大,它除了对主要的JS语法和HTML DOM操作方式 ...

  5. oauth学习

    https://www.cnblogs.com/blowing00/p/4524132.html

  6. windows下如何快速优雅的使用python的科学计算库?

    Python是一种强大的编程语言,其提供了很多用于科学计算的模块,常见的包括numpy.scipy.pandas和matplotlib.要利用Python进行科学计算,就需要一一安装所需的模块,而这些 ...

  7. HUAWEI HiAI亮相华为开发者生态大会 助力应用AI开发实现加速度

    6月23日,在2018华为终端·全球合作伙伴及开发者大会AI分论坛体验区的一角,被层层叠叠的人群围得水泄不通.站在最前面的一名体验者,正跟随着“快手短视频”APP上不断出现的小人左右扭动,每完成一个动 ...

  8. 概率图模型(PGM)学习笔记(二)贝叶斯网络-语义学与因子分解

    概率分布(Distributions) 如图1所看到的,这是最简单的联合分布案例,姑且称之为学生模型. 图1 当中包括3个变量.各自是:I(学生智力,有0和1两个状态).D(试卷难度,有0和1两个状态 ...

  9. ubuntu系统安装好后一些基本软件的安装

    preface: 由于某些原因重装了系统,原来在ubuntu下安装的一些软件又要又一次安装.稍微麻烦,整理下须要安装的步骤. #==================================== ...

  10. 输出 pdf

    jar 包 :core-renderer.jar  iText-2.0.8.jar   iTextAsian.jar 方式1: import java.io.FileNotFoundException ...