Recursion and Fibonacci sequence

Source: Internet
Author: User

First, recursion

Inside the function, other functions can be called;
If a function calls itself internally, that function is a recursive function.

Case: Traverse all files in the current directory

1. Recursive traversal

1 ImportOS2 defGCI (filepath):3 #traverse all files under FilePath, including subdirectories4Files =Os.listdir (filepath)5    forFiinchFiles:6Fi_d =Os.path.join (FILEPATH,FI)7     ifOs.path.isdir (fi_d):8 GCI (fi_d)9     Else:Ten       Print(Os.path.join (filepath,fi_d)) One  A #recursively traverse all files in the current directory -Gci"./")

2. Other methods

1 Import OS 2  for  in Os.walk ('./'):3for in    FS: 4     Print (Os.path.join (FILEPATH,F))

Second, Fibonacci sequence

Fibonacci numbers refer to such a sequence of 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368

In particular, the No. 0 item is 0, and the 1th item is the first 1. This sequence starts with the 2nd item and each item is equal to the sum of the first two items.
1 the first is to realize the series of the wave by recursion.2 deffunc (arg1,arg2,stop):3Arg3 = Arg1 +arg24     Print(Arg3,"', end="')5     ifArg3 <Stop:6 func (arg2,arg3,stop)7Func (0,1,46368)8 9 the second, implemented through the while LoopTen deffunc (n): OneA, B = 0,1 A      whileb <N: -A, B = b,a+b -         Print(b,"', end="') theFunc (46368)

Recursion and Fibonacci sequence

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.