Bubble sort, recursion, and adorner for Python functions

Source: Internet
Author: User

Bubble sort of function:

The essence is, let the element change position, compare the final sort one by one.

Example 1: Implementing A1,A2 Value Interchange:

A1 = 123a2 = 456temp = A1A1 = A2A2 = tempprint (A1) print (A2) results: 456123

Bubble Sort:

#!/bin/bash/env python#-*-Coding:utf-8-*-li =  [22,1,5,4,3,66,22,12,34,21]for j in Range (1,len (LI)): for    I in R Ange (Len (LI)-1):        if li[i] > li[i+1]:            temp = li[i]            li[i] = li[i+1]            li[i+1] = Tempprint (LI) Results: [1, 3, 4, 5 , 12, 21, 22, 22, 34, 66]

Recursion:

Other functions can be called inside a function. If a function calls itself internally, the function is a recursive function.

Cases:

#!/bin/bash/env pythondef f4 (A1,A2):    If A1 > 10000:        return    print (a1)    a3 = A1 + A2 f4    (A2,A3) F4 ( 0,1) Results: 011235813213455891442333776109871597258441816765

function Adorner:

The purpose of adorners: When you want to modify a piece of code without making internal modifications, you need to add an adorner to the outside to achieve the effect.

Previous actions performed before the original function:

#!/bin/bash/env pythondef Outer (func):    def inner (): Print (        ' hello ') print (' Let's        ')        print (' Moumou ')        r = func ()        return R    return inner@outerdef func1 ():    print (' Yes ') func1 () Result: Hellolet Ismoumouyes

After the function executes:

def outer (func):    def inner ():        r = func () print (' Hello ') print (' Let's ') print        (' Moumou ')        return R    return inner@outerdef func1 ():    print (' Yes ') func1 () Result: Yeshellolet Ismoumou

@ Function: Execute outer function First, then send F1 as parameter to Oute. , the return value of outer is re-assigned to F1, so the function of F1 is equal to inner ()

As long as the function is applied to the adorner, the function is redefined and redefined as: the inner function of the adorner.

Multiple parameter adorners are passed:

#!/bin/bash/env pythondef Outer (func):    def Inner (*args,**kwargs):        print (args)        print (Kwargs)        ret = Func (*args,**kwargs)        return ret    return inner@outerdef func1 (A1,A2,A3):    print ("yes")    return A1 + A2 + A3FUNC1 (11,22,33) Results: (one, one, one) {}yes

Applications for multiple adorners:

#!/bin/bash/env pythondef Outer (func):    def Inner (*args,**kwargs):        print (' Cai ')        ret = func (*args,** Kwargs)        print (' Rui ')        return ret    return innerdef Outer1 (func):    def inner1 (*args,**kwargs):        print (' 123 ')        ret = func (*args,**kwargs)        return ret    return inner1@outer1@outerdef func1 (A1,A2, A3):    print ("Yes") func1 (11,22,33) Results: 123caiyesrui

Analysis:

  

First, outer and func1 as a whole, put inner in Outer1, and then execute the following

Bubble sort, recursion, and adorner for Python functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.