[Baidu one or two sides] test and Development Engineer October 9, 2021

2021.10.09 is hung on both sides, and the code capacity needs to be improved

one side:

1. Understanding of test development position
2. Internship experience
3. Hand tearing algorithm:

	**Find the position of the first character in a string that appears only once** 
class Soltion :
    def maxLengthMax(self,str):
        for i in str:
            if str.count(i) == 1:

		**Implements the longest substring of a non repeating string**
def lengthLong(s):
    if len(s) ==0:
        return 0
    start = 0
    end  = 0
    ans = end -start +1
    while True:
        end +=1
        print('end= ',end)
        if s[end] in s[start:end]:
            k = s[start:end].find(s[end])
            start += k+1
        ans = max(end -start +1,ans)
        if end == len(s)-1:


Two sides

1. Internship related projects
2. Algorithm: find the maximum number and sub maximum number in the array
(no handwriting, but I want you to say your thoughts)
A for loop, assign values and compare them in turn
3.python decorator:

The python decorator is essentially a function. It allows other functions to add additional functions without any code changes. The return value of the decorator is also a function object (function pointer)
, application scenarios: log insertion, performance test, transaction processing, permission verification

2.1 function decorator

import time

def decorator(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        end_time = time.time()
        print(end_time - start_time)

    return wrapper

def func():

func() # function call# Output: 0.8006443977595

In the above code, func is the function I want to decorate. I want to use the decorator to display the running time of func function@ Decorator this syntax is equivalent to executing func=
decorator(func), decorate and return the func function. Let's take a look at our decorator function - decorator. The passed in parameter of this function is func
(decorated function), the return parameter is the inner function. The inner function wrapper here is actually equivalent to a closure function. It plays the role of decorating a given function. The wrapper parameters are * args and * * kwargs* The parameters represented by args are passed in the form of list** The parameters represented by kwargs are passed in the form of Dictionary:
From the figure, we can see that all parameters in the form of key=value exist in kwargs, and all remaining parameters are stored in args in the form of list. Note here: in order not to destroy the logic of the original function, we must ensure that the incoming parameters and return value types of the inner function wrapper and the decorated function func must be consistent.

Function decorator for class 2.2 methods

import time

def decorator(func):
    def wrapper(me_instance):
        start_time = time.time()
        end_time = time.time()
        print(end_time - start_time)
    return wrapper

class Method(object):

    def func(self):

p1 = Method()p1.func() # function call

For class methods, there will be a default parameter self, which actually represents an instance of the class, so the wrapper function of the decorator also needs to pass in a parameter-
me_instance means to pass the instance p1 of the class to the wrapper. Other uses are the same as function decorators.

Type 2.3 trimmer

class Decorator(object):
    def __init__(self, f):
        self.f = f
    def __call__(self):
        print("decorator start")
        print("decorator end")

def func():


Note that call() is a special method that can turn a class instance into a callable object:

p = Decorator(func) # p is an instance of the class Decorator. p() # implements__ call__ () method, p can be called
To use a class decorator, you must implement the__ call__ () method is equivalent to turning an instance into a method.

Keywords: Python

Added by thesaleboat on Sun, 10 Oct 2021 04:09:03 +0300