A method for solving the problem of N-Step step walking by Python

Source: Internet
Author: User
This article mainly introduces the method of solving the problem of N-Step step walking by Python, briefly describes the steps problem, and analyzes the relevant operation skills of Python using recursion and recursive algorithm to solve the step problem by the example, and the Friends can refer to the following

In this paper, we describe the method of Python solving the N-Step step-walk problem. Share to everyone for your reference, as follows:

Title: A building has an n-step staircase, the rabbit can jump 1, 2 or 3 steps each time, ask how many ways to go?

Analysis of Afanty:

In the face of this problem of law, self-propelled push is good, 1 order there are several ways to go? How many steps are there in order 2? How many steps are there in order 3? How many steps are there in order 4? How many steps are there in order 5?

Yes, it's the law!

Easy Wrong point: This is not a combinatorial problem, because the 1th time to go 1-order, 2nd-Step 2-step different from the 1th time 2-order, 2nd times to go 1-step

The following is the recursive implementation code for Python:


def allmethods (stairs):  "'  :p Aram stairs:the numbers of Stair  : return:  '  if Isinstance ( stairs,int) and stairs > 0:    basic_num = {1:1,2:2,3:4}    if stairs in Basic_num.keys ():      return basic_num[ Stairs]    Else:      return Allmethods (stairs-1) + allmethods (stairs-2) + allmethods (stairs-3)  else:    print ' The num of stair is wrong '    return False


Of course it can be implemented in a non-recursive way, and here is the code based on the recursive method:


def allmethod (stairs):  "' Recursive implementation  :p Aram stairs:the amount of Stair  : return:  '  if Isinstance ( stairs,int) and stairs > 0:    h1,h2,h3,n = 1,2,4,4    basic_num = {1:1,2:2,3:4}    if stairs in Basic_num.keys (): C8/>return Basic_num[stairs]    else: while      n <= stairs:        temp = h1        h1 = h2        h2 = h3        H3 = Temp + h1 + H2      return h3  else:    print ' The num of stair is wrong '    return False


Well, these are the processes that are implemented using recursion and recursive methods respectively.

Related Article

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.