Recursive algorithm examples

Source: Internet
Author: User

Copy the explanation of Baidu Encyclopedia first. The programming technique called by the program itself is called recursion (recursion).

For example, to calculate the product of a 1-9 9-bit number, the intuitive algorithm is 1*2*3*4*5*6*7*8*9, if

To calculate the product of 1-10000, the intuitive algorithm is difficult to achieve, and recursion can be very simple implementation.

Example 1

# Coding:utf-8

def recurision (n): # recurision is recursion

if n = = 1:

return N # place where the operation value is output in the function, if printed, with print type (Recurision (10)), display

else: It is nonetype, and it is clear that only the number can multiply. So we return the value of the function with return.

return n * recurision (n-1)

Recurision (10)

Well, don't you read it. At first I could not touch my head. But since it's a math problem, make a list.

You'll know.

Recurision (n) = n * resurision (n-1)

= n * (n-1) * Recurison (n-2)

Until n = 1, that is, if set, so we use the If...else statement

So easy!

----------------------------------------------------------------------------------

Example 2

We want to delete all text files under the folder where the script file ('. Py ' program) is located (with '. txt ' as the suffix name)

# Coding:utf-8

Import OS

Import Sys

# the directory/path where the script file is located, such as ' c:\python27 ' in front of ' c:\python27\1.py '

Currdir = sys.path[0]

Def removefile (dir, postfix):

# to determine if the folder is True, False. Similarly os.path.isfile to determine whether the file

If Os.path.isdir (dir):

For file in Os.listdir (dir): # OS.LISTDIR Lists all sub-files and subfolders under the Dir directory.

RemoveFile (dir + ' \ \ ' + file, postfix)

Elif Os.path.splitext (dir) [1] = = postfix: # os.path.splitext () separates the prefix of the file name from the suffix. such as 1.txt 1 and. txt

Os.remove (dir) # os.remove () Delete the file

RemoveFile (Currdir, '. txt ')

Ok, let's Digest it. Suppose our script name is 1.py, placed under ' c:\python27 ', and there are 1.txt and 2.txt in this folder.

The file to be deleted.

The execution of RemoveFile (Currdir, '. txt ') must be preceded by an if statement, since Currdir is the directory in which the Py file is located (the directory in addition to the path

a folder). And like RemoveFile (' c:\\python27 ' + ' \ \ ' + 1.py, '. txt ') A look at the suffix name is not the same. Kicked off. So, after all, only txt files are compliant with the Elif case. Smooth removal.

Recursive algorithm examples

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.