Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 62f0953

Browse files
committed
recursion update
1 parent 3b48ae7 commit 62f0953

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

PythonDataStructsAndAlgo/Notes.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# RECURSION
2+
3+
## Recursion
4+
> - technique by which a function makes one or more calls to itself during execution
5+
> - or by which a data structure relies upon smaller instances of the very same type of structure in its representation.
6+
7+
## Recursion Trace
8+
> In Python, each time a function (recursive or not) is called, a structure known as *activation record or frame* is created to store information about the progress of that invocation of the function.
9+
> - This activation record includes a name space for storing the function's call parameters and local variables, and information about which command in the body of the function is currently executing.
10+
>> When the exection of a function leads to a nested function call (recursion) the execution of the former call is suspended and its activation record stores the place in the source code at which the flow control should continue upon return of the nested call.
11+
12+
## File system for a computer:
13+
> has a recursive structure in which directories can be nested arbitrarly deeply within other directories.
14+
> Recursive algorithms are widely used to explore and manage this file systems.
15+
16+
> Modern OS define file-system directories in a recursive way. A file systems consists of a top-level directory (example: "root directory") and the contents of this directory consist of files and other directories, which in turn can contain files and other directories.
17+
18+
> Many common behaviors of an OS such as copying a directory or deleting a directory, are implemented using recursive algorithms.
19+
20+
> ### Python OS module
21+
> * *os module*
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def draw_line(tick_length, tick_lable=' '):
2+
"""Draw one line with given tick length(followed by optional label)"""
3+
line= '-' *tick_length
4+
5+
if tick_lable:
6+
line+= ' ' + tick_lable
7+
8+
print(line)
9+
10+
def draw_interval(center_length ):
11+
"""Draw tick interval based upon a central tick length."""
12+
if center_length >0: #stop when length drops to 0
13+
draw_interval(center_length-1) # recursively draws top ticks
14+
draw_line(center_length) #draw center tick
15+
draw_interval(center_length-1) #recursively draw bottom ticks
16+
17+
def draw_ruler(num_inches, major_length):
18+
"""Draw the englishe ruler wirh given number of inches, major tick length."""
19+
draw_line(major_length, '0')# draw inch line 0
20+
for j in range(1, 1+num_inches):
21+
draw_interval(major_length-1) #draw interior ticks
22+
draw_line(major_length, str(j)) #draw inch line with value j and label
23+
24+
draw_ruler(3,3)
25+
26+
"""Major tick length is the # of "-" before the number of inches """
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def factorial(n):
2+
if n ==0:
3+
return 1
4+
else:
5+
return n * factorial(n-1)
6+
7+
print(factorial(3))
8+
print(factorial(6))
9+
print(factorial(13))

0 commit comments

Comments
 (0)