Small Practice 15

'''

1. achieve a decorator, the function is called the limiting frequency of, e.g., 10 seconds (interview questions)

import time

l1=[]

def wrapper(f):

def inner (* args, ** kwargs): # Step 5

a=time.time()

l1.append(a)

if len(l1)==1:

ret = f (* args, ** kwargs) # Step 7

return right

elif l1[-1]-l1[-2]>10:

ret = f (* args, ** kwargs) # Step 7

return right

else:

print ( 'call frequency is too large')

return inner # Step 3

@wrapper # zz = weapper (zz) Step

def zz (): # 4 The first step is the inner zz

print ( 'I was called')

zz()

time.sleep(11)

zz()

2. Write the output of the following code fragment:

def say_hi(func):

def wrapper(*args,**kwargs):

print("HI")

ret=func(*args,**kwargs)

print("BYE")

return right

return wrapper

def say_yo(func):

def wrapper(*args,**kwargs):

print("Yo")

return func(*args,**kwargs)

return wrapper

@ Say_hi # func = say_hi (func) func --- wrapper outside of the case inside func - func

@ Say_yo # func = say_yo (func) func --- wrapper outside of the case inside func - func

def func():

print("ROCK&ROLL")

func()

3. Write decorator complete the following requirements:

There are two sets of user account password, account password set to Jingdong, a Taobao account password are stored in two files.

Set four functions, representing the home Jingdong, Jingdong supermarkets, home Taobao, Taobao supermarket.

After starting the program, showing the user the option to:

1, Jingdong Home

2, Jingdong supermarkets

3, Taobao Home

4, Taobao supermarket

5, exit the program

Four functions are added authentication function, the user can choose the user selects a supermarket or Jingdong Jingdong homepage, simply enter a username and password and successfully Jingdong, the two functions can access any; the user selects Taobao Taobao supermarket or home, just enter a Taobao account and password, and successful, these two functions can be arbitrarily access.

Related Tip: Use decorators with arguments. Analyzing interior decorator added, different account password verification.

4. to l1 = [1,1,2,2,3,3,6,6,5,5,2,2] to heavy, can not use the set collection (interview questions).

5. Complete Fibonacci number (interview questions) with a recursive function:

Fibonacci number: 1,1,2,3,5,8,13,21 .......... (the third number for the first two numbers, but the beginning of 1, 1 is a special case to be discussed separately)

6. The user input corresponding to the number acquired Fibonacci numbers: 6, such as input, the result returned is 8.

my_input=input('>>>>>')

def func(my_input):

l1=[1,1,]

my_input = int(my_input)

while my_input>=l1[-1]:

if my_input == 1:

print(l1[0:2])

l1.append(l1[-1]+l1[-2])

print(l1[0:-1])

func(my_input)

'''

Guess you like

Origin www.cnblogs.com/saoqiang/p/11403054.html