20190916 Kuaishou, NVIDIA

Kuaishou20190916

1.

arr = list(map(str, input().strip().split()))
if arr[-1]=='.':
    arr.pop()
else:
    arr[-1]=arr[-1][:-1]
arr=arr[::-1]
print(' '.join(arr),end='.\n')

2.

stra = str(input().strip())
strb = str(input().strip())
n = len(stra)
m = len(strb)
if n * m == 0:
    print(n+m)
else:
    d = [ [0] * (m + 1) for x in range(n + 1)]
    for i in range(n + 1):
        d[i][0] = i
    for j in range(m + 1):
        d[0][j] = j
    for i in range(1, n + 1):
        for j in range(1, m + 1):
            left = d[i - 1][j] + 1
            down = d[i][j - 1] + 1
            left_down = d[i - 1][j - 1] 
            if stra[i - 1] != strb[j - 1]:
                left_down += 1
            d[i][j] = min(left, down, left_down)
    print(d[n][m])

3.

Sieve Prime Number Method 

n = int(input().strip())
shuzu=[i for i in range(n+1)]
sum1=0
for i in range(2,n+1):
    if shuzu[i]!=1:
        t=shuzu[i]
        shuzu[i]=shuzu[i]//shuzu[i]
        sum1=sum1+1
        for j in range(2*t,n+1,t):
            while shuzu[j]%t==0:
                shuzu[j]=shuzu[j]//t
                sum1=sum1+1
    #print(shuzu)
print(sum1)

4.

Leetcode original title 

boar=[]
for i in range(9):
    stra = str(input().strip())
    boar.append(stra)
ans='true'
row = [{} for i in range(9)]
column = [{} for i in range(9)]
box = [{} for i in range(9)]
for i in range(9):
    for j in range(9):
        num = boar[i][j]
        if num != 'X':
            num = int(num)
            box_index = (i // 3 ) * 3 + j // 3
            row[i][num] = row[i].get(num, 0) + 1
            column[j][num] = column[j].get(num, 0) + 1
            box[box_index][num] = box[box_index].get(num, 0) + 1
            if row[i][num] > 1 or column[j][num] > 1 or box[box_index][num] > 1:
                ans='false'
print(ans)

NVIDIA 20190916

1. Implement matrix multiplication

If A is a matrix with x rows and y columns and B is a matrix with y rows and z columns, multiply A and B, and the result will be another matrix C with x rows and z columns. Each element of this matrix is ​​determined by the following formula:

C_ {ij} = \ sum_ {k = 1} ^ y A_ {ik} B_ {kj}

Enter a description:

The input contains multiple sets of data, and each set of data contains:

The first row contains a positive integer x, which represents the number of rows in the first matrix,

The second row contains a positive integer y, which represents the number of columns in the first matrix and the number of rows in the second matrix.

The third row contains a positive integer z, which represents the number of columns in the second matrix

After x rows, y integers in each row represent the value of the first matrix

After y rows, z integers in each row represent the value of the second matrix

Output description:

For each set of input data, output x rows, each row of z integers, representing the result of multiplication of two matrices

analysis:

There is a problem with the topic, there are several examples, and a while needs to be added, which leads to no passing and pitfalls. . .

Don't import numpy for this kind of problem, because it will be directly wrong.

Python implements matrix multiplication:

def matxMultiply(A, B):
    multiply = []
    result = [list(row) for row in zip(*B)]
    for Al in range(len(A)):
        row =[]
        for Bl in range(len(result)):
            num = 0   
            for Br in range(len(result[0])):
                num +=  A[Al][Br] * result[Bl][Br]
            row.append(num)
        multiply.append(row)
    return multiply
x = int(input().strip())
y = int(input().strip())
z = int(input().strip())
a=[]
b=[]
for i in range(x):
    arr = list(map(int, input().strip().split()))
    a.append(arr)
for i in range(y):
    arr = list(map(int, input().strip().split()))
    b.append(arr)
c = matxMultiply(a, b)
for sub in c:
    print(' '.join(map(str, sub)))

 

Guess you like

Origin blog.csdn.net/LXQ1071717521/article/details/101793757