zoukankan      html  css  js  c++  java
  • codewar代码练习1——8级晋升7级

    最近发现一个不错的代码练习网站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做题的时候,貌似答案没有通过,是无法看到其他人的答案的。在通过答案后,再看其他人的解法,常常有眼前一亮的感觉。













  • 相关阅读:
    搭建nginx反向代理用做内网域名转发
    TOMCAT下面发布项目的4种方式
    centos 安装 rabbitmq
    RabbitMQ在windows下的安装
    mysql物理备份
    Linux(CentOS)下,下载安装Nginx并配置
    whereis 命令
    windows下安装以及配置nginx
    CentOS 7安装配置Redis数据库
    CentOS6.5下安装ActiveMQ
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9411631.html
Copyright © 2011-2022 走看看