-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.2.1.py
More file actions
39 lines (32 loc) · 1.16 KB
/
Copy path5.2.1.py
File metadata and controls
39 lines (32 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#We've started a recursive function below called
#measure_string that should take in one string parameter,
#myStr, and returns its length. However, you may not use
#Python's built-in len function.
#
#Finish our code. We are missing the base case and the
#recursive call.
#
#HINT: Often when we have recursion involving strings, we
#want to break down the string to be in its simplest form.
#Think about how you could splice a string little by little.
#Then think about what your base case might be - what is
#the most basic, minimal string you can have in python?
#
#Hint 2: How can you establish the base case has been
#reached without the len() function?
#You may not use the built-in 'len()' function.
def measure_string(myStr):
if myStr == '':
return 0
else:
return 1 + measure_string(myStr[:-1])
#def length(s):
# return 0 if s == '' else 1 + length(s[:-1])
# if str == '':
# return 0
# else :
# return 1 + string_length(str[1:])
#print length('hello world') # prints 11
#The line below will test your function. As written, this
#should print 13. You may modify this to test your code.
print(measure_string("13 characters"))