美文网首页
python练习

python练习

作者: 33jubi | 来源:发表于2018-08-12 19:58 被阅读0次

    python练习

    8阶

    FUNDAMENTALS ARRAYS NUMBERS

    You get an array of numbers, return the sum of all of the positives ones.
    Example [1,-4,7,12] => 1 + 7 + 12 = 20
    Note: if there is nothing to sum, the sum is default to 0.

    def positive_sum(arr):
        return sum(x for x in arr if x > 0)
    

    sum(iterable[,start])

    • iterable -- 可迭代对象,如:列表、元组、集合。
    • start -- 指定相加的参数,如果没有设置这个值,默认为0。

    Correct the mistakes of the character recognition software

    Character recognition software is widely used to digitise printed texts. Thus the texts can be edited, searched and stored on a computer.

    When documents (especially pretty old ones written with a typewriter), are digitised character recognition softwares often make mistakes.

    Your task is correct the errors in the digitised text. You only have to handle the following mistakes:

    • S is misinterpreted as 5
    • O is misinterpreted as 0
    • I is misinterpreted as 1

    The test cases contain numbers only by mistake.

    def correct(string):
        return string.translate(str.maketrans("501", "SOI"))
    
    • str.translate(table[, deletechars]);
      _ table -- 翻译表,翻译表是通过maketrans方法转换而来。_
      deletechars -- 字符串中要过滤的字符列表。
    • str.maketrans(intab, outtab)
      _ intab -- 字符串中要替代的字符组成的字符串。_
      _ outtab -- 相应的映射字符的字符串。_
    def correct(string):
        return string.replace('1','I').replace('0','O').replace('5','S')
    
    • str.replace(old, new[, max])
      _ old -- 将被替换的子字符串。_
      _ new -- 新字符串,用于替换old子字符串。_
      _ max -- 可选字符串, 替换不超过 max 次_

    This Kata is intended as a small challenge for my students

    All Star Code Challenge #18

    Create a function called that accepts 2 string arguments and returns an integer of the count of occurrences the 2nd argument is found in the first one.

    If no occurrences can be found, a count of 0 should be returned.

    strCount('Hello', 'o') // => 1
    strCount('Hello', 'l') // => 2
    strCount('', 'z') // => 0

    Notes:

    • The first argument can be an empty string
    • The second string argument will always be of length 1

    7阶

    A History Lesson

    The Pony Express was a mail service operating in the US in 1859-60.
    Pony Express
    It reduced the time for messages to travel between the Atlantic and Pacific coasts to about 10 days, before it was made obsolete by the transcontinental telegraph.

    How it worked
    There were a number of stations, where:

    The rider switched to a fresh horse and carried on, or
    The mail bag was handed over to the next rider
    Kata Task
    stations is a list/array of distances (miles) from one station to the next along the Pony Express route.

    Implement the riders method/function, to return how many riders are necessary to get the mail from one end to the other.

    NOTE: Each rider travels as far as he can, but never more than 100 miles.

    #form me
    def riders(stations):
        count=1
        temp=0
        for x in stations:
            temp+=x
            if temp>100:
                count+=1
                temp=x
        return count
    
    #from others
    def riders(A):
        Z=[0]
        for x in A:
            if Z[-1]+x<=100:Z[-1]+=x#z这里是一个简单的判断语句不是三目运算大姐
            else:Z.append(x)
        return len(Z)
    

    **Z[-1]+x<=100:Z[-1]+=x 判断冒号左边,左边满足做右边

    Beginner Series #3 Sum of Numbers

    Given two integers a and b, which can be positive or negative, find the sum of all the numbers between including them too and return it. If the two numbers are equal return a or b.

    Note: a and b are not ordered!

    Examples

    get_sum(1, 0) == 1 // 1 + 0 = 1
    get_sum(1, 2) == 3 // 1 + 2 = 3
    get_sum(0, 1) == 1 // 0 + 1 = 1
    get_sum(1, 1) == 1 // 1 Since both are same
    get_sum(-1, 0) == -1 // -1 + 0 = -1
    get_sum(-1, 2) == 2 // -1 + 0 + 1 + 2 = 2

    def get_sum(a,b):
        return sum(xrange(min(a,b), max(a,b)+1))
    

    Leonardo numbers

    The Leonardo numbers are a sequence of numbers defined by:

    L(0) = 1 || 0
    L(1) = 1 || 0
    L(x) = L(x - 1) + L(x - 2) + 1

    Generalizing the above a bit more:

    L(x) = L(x - 1) + L(x - 2) + a

    Assume a to be the add number.


    Task

    Return the first n Leonardo numbers as an array.

    Input

    • n : The number of Leonardo numbers to be shown
    • L0 : Whether L(0) is 0 or 1
    • L1 : Whether L(1) is 0 or 1
    • add : The add number

    Examples

    input : n = 5 , L0 = 1 , L1 = 1 , add = 1
    output : [ 1, 1, 3, 5, 9 ]

    input : n = 5 , L0 = 0 , L1 = 0 , add = 2
    output : [ 0, 0, 2, 4, 8 ]

    input : n = 10 , L0 = 0 , L1 = 1 , add = 4
    output : [ 0, 1, 5, 10, 19, 33, 56, 93, 153, 250 ]

    input : n = 5 , L0 = 0 , L1 = 0 , add = 0
    output : [ 0, 0, 0, 0, 0 ]

    def L(n, L0, L1, add) :
        Z=[L0,L1]
        for x in range(1,n-1):
            Z.append(L0+L1+add)
            L0,L1=L1,L0+L1+add
        return Z
    

    Introduction and Warm-up (Highly recommended)

    Task

    Given an array/list [] of integers , Construct a product array Of same size Such That prod[i] is equal to The Product of all the elements of Arr[] except Arr[i].

    Notes

    • Array/list size is at least 2 .

    • Array/list's numbers Will be only Postives

    • Repeatition of numbers in the array/list could occur.


    Input >> Output Examples

    1- productArray ({12,20}) ==>  return {20,12}
    
    

    Explanation**:

    • The first element in prod [] array 12 is the product of all array's elements except the first element

    • The second element 20 is the product of all array's elements except the second element .

    2- productArray ({1,5,2}) ==> return {10,2,5}

    Explanation:

    • The first element 10 is the product of all array's elements except the first element 1

    • The second element 2 is the product of all array's elements except the second element 5

    • The Third element 5 is the product of all array's elements except the Third element 2.

    3- productArray ({10,3,5,6,2}) return ==> {180,600,360,300,900}

    Explanation:

    • The first element 180 is the product of all array's elements except the first element 10

    • The second element 600 is the product of all array's elements except the second element 3

    • The Third element 360 is the product of all array's elements except the third element 5

    • The Fourth element 300 is the product of all array's elements except the fourth element 6

    • Finally ,The Fifth element 900 is the product of all array's elements except the fifth element

    from operator import mul
    from functools import reduce
    
    def product_array(numbers):
        tot = reduce(mul,numbers)
        return [tot//n for n in numbers]
    
    • Python reduce() 函数
      描述 reduce() 函数会对参数序列中元素进行累积。函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果。
      语法 reduce(function, iterable[, initializer])
      参数
    • function -- 函数,有两个参数
    • iterable -- 可迭代对象
    • initializer -- 可选,初始参数
      返回值返回函数计算结果。

    ** 实例**

    以下实例展示了 reduce() 的使用方法:

    def add(x, y) : # 两数相加 ...
    return x + y
    reduce(add, [1,2,3,4,5]) # 计算列表和:1+2+3+4+5
    15 >>>
    reduce(lambda x, y: x+y, [1,2,3,4,5]) # 使用 lambda 匿名函数

    from numpy import prod
    
    def product_array(numbers):
        p = prod(numbers)
        return [p // i for i in numbers]
    

    持续更,未完

    相关文章

      网友评论

          本文标题:python练习

          本文链接:https://www.haomeiwen.com/subject/cthebftx.html