Berkeley-CS61A | some interesting python projects and tutorials | Learning library
kandi X-RAY | Berkeley-CS61A Summary
kandi X-RAY | Berkeley-CS61A Summary
some interesting python projects and tutorials
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Tokenize a line
- Return the next valid token in the scheme
- Validate a string s
- Runs the game
- Returns the final strategy
- Check if n is prime
- Load user data
- Creates a starter
- Cycle through two functions
- Generate a g function
- Called when the game finishes
- Compute the quadratic distribution
- Returns a dictionary of ratings for a given user
- Make a piecewise piecewise function
- Dispatch a command
- Count amount of amount
- Evaluate expressions
- Main game
- Prompts the user for the given arguments
- Compute the prime strategy
- Build a dictionary from the given tokens
- Decorator to trace the function
- Define the form
- Draw a map of kroids
- Evaluate an expression
- Start a tscheme interpreter
Berkeley-CS61A Key Features
Berkeley-CS61A Examples and Code Snippets
Community Discussions
Trending Discussions on Berkeley-CS61A
QUESTION
So i'm going through the SICP book. I'm in the tree recursion chapter. I googled tree recursion to gain more knowledge about it and i stumbled upon this exercice and i'm having hard times to understand it perfectly.
Exercice :
I want to go up a flight of stairs that has n steps. I can either take 1 or 2 steps each time. How many different ways can I go up this flight of stairs?
The answer was :
For example, in the case where nis 5, there are 8 possible ways:
1 1 1 1 1
2 1 1 1
1 2 1 1
1 1 2 1
1 1 1 2
1 2 2
2 1 2
2 2 1
And this the code block i had trouble understanding it fully :
...ANSWER
Answered 2019-Oct-01 at 18:58The tree diagram is just giving the space of function calls and their arguments that occur starting with (count-stairs 5)
. When we call the function with argument 5
, it will call (count-stairs 4)
due to the expression (count-stairs (- n 1))
and it will call (count-stairs 3)
due to the expression (count-stairs (- n 2))
. Of course, these values get added with +
which becomes the return value of the call. The tree just doesn't show that return value information, just the call arguments.
(count-stairs 5)
doesn't mean "count five stairs", but "call the count-stairs
function with argument 5
to calculate how many different ways there are to go up a flight of 5
stairs".
For (count-stairs 3)
the result will be 3, because (count-stairs 1)
and (count-stairs 2)
just return 1
and 2
, respectively.
However, (count-stairs 4)
adds (count-stairs 3)
and (count-stairs 2)
, therefore (count-stairs 4) -> 5
.
We can use this arrow notation to annotate the expressions in the tree with their result values, starting from the bottom and working upward. At the top of the tree we will end up with (count-stairs 5) -> 8
.
count-stairs
is just a slight variation of the recursive Fibonacci function in disguise.
Why does this calculate the number of ways of ascending the stairs using 1 or 2 sized steps? Firstly, the base cases are clear. If a staircase has one step, there is only one way to traverse it: we take that one step. So (count-stairs 1) -> 1
. If there are two steps, then then there are two ways: take each step, or take both of them in one stride. Thus (count-stairs 2) -> 2
. Then comes the tricky inductive part. If we are faced with three or more stairs, what is the solution?
If we are faced with a staircase with n steps, n > 2, then we have two possibilities about how to begin climbing. Possibility (1): we can take one step, and then climb the remaining staircase of n - 1 steps; or, possibility (2) we can take two steps as a single stride, and then climb the remaining staircase of n - 2 steps. Thus the number of ways of climbing n steps is the sum of the ways from these two possibilities: the number of ways of climbing n - 1 steps, plus the number of ways of climbing n - 2 steps.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Berkeley-CS61A
You can use Berkeley-CS61A like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page