python练习

2018-08-12  本文已影响0人  33jubi

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])

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:

The test cases contain numbers only by mistake.

def correct(string):
    return string.translate(str.maketrans("501", "SOI"))
def correct(string):
    return string.replace('1','I').replace('0','O').replace('5','S')

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:

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

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


Input >> Output Examples

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

Explanation**:

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

Explanation:

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

Explanation:

from operator import mul
from functools import reduce

def product_array(numbers):
    tot = reduce(mul,numbers)
    return [tot//n for n in numbers]

** 实例**

以下实例展示了 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]

持续更,未完

上一篇 下一篇

猜你喜欢

热点阅读