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

Commit 30e69cb

Browse files
committed
recursion continuation
1 parent 62f0953 commit 30e69cb

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

PythonDataStructsAndAlgo/Notes.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@
1818
> Many common behaviors of an OS such as copying a directory or deleting a directory, are implemented using recursive algorithms.
1919
2020
> ### Python OS module
21-
> * *os module*
21+
> * *os module* -> provides robust tools for interacting with the operating system during execution of a program.
22+
>> os.path.getsize(path) - return imemediate disk usage (measure in bytes) for the file or directory that is identified by the string *path* (eg. /user/rt/courses).
23+
24+
>> os.path.isdir(path) - return True if entry designated by string path is a directory; False otherwise.
25+
26+
>> os.listdir(path) -Return a list of strings that are names of all entries within a directory designated by string *path*
27+
28+
>> os.path.join(path, filename) - Compose a path string name and filename string using an appropriate operating system separator between the two ("/" or "\\\"). Returning the string that represents the full path to the file.
29+
30+
> Linear recursion - if a recursive function is designed so that each invocation of the body makes at most one new recursive call, this is known as linear recursion.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
3+
def disk_usage(path):
4+
"""Return the number of bytes used by a file or folder or any descendents."""
5+
total= os.path.getsize(path) #account for direct usage
6+
if os.path.isdir(path):
7+
for filename in os.listdir(path, filename):
8+
childpath= os.path.join(path, filename)
9+
tota+= disk_usage(childpath)
10+
11+
print('{0:7}'.format(total), path)
12+
return total
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# time complexity is O(n)
2+
def reverse(S, start, stop):
3+
"""Reverse elements in implicit slice S[start:stop]"""
4+
if start < stop-1:
5+
S[start] , S[stop-1] = S[stop-1], S[start]
6+
reverse(S, start+1, stop-1)
7+
8+
return S
9+
10+
list=[9,8,7,6,5,4,3,2,1,0]
11+
print(reverse(list, 0 , len(list)))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def linear_sum(S, n):
2+
if n ==0:
3+
return 0
4+
else:
5+
return linear_sum(S, n-1)+ S[n-1]
6+
7+
S=[4,3,6,2,8]
8+
n= len(S)
9+
10+
print(linear_sum(S,n))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def power(x,n):
2+
"""Compute the value x**n for integer n without using power functio"""
3+
if n==0:
4+
return 1
5+
else:
6+
return x* power(x,n-1)
7+
8+
print(power(2,7))

0 commit comments

Comments
 (0)