-
Notifications
You must be signed in to change notification settings - Fork 0
/
Part2d.py
executable file
·50 lines (33 loc) · 1.17 KB
/
Part2d.py
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
39
40
41
42
43
44
45
46
47
48
49
50
from typing import Optional
def pickMessage(value: int) -> None:
print(f"Pick a number, like {value}." if value is not None else "Pick any number you like")
def findAfterStarRec(str: str) -> Optional[str]:
if len(str) <= 1:
return None
return str[1] if str[0] == '*' else findAfterStarRec(str[1:])
print(findAfterStarRec("*hello"))
def findAfterStarIter(str: str) -> Optional[str]:
if len(str) <= 1:
return None
last = None
for c in str: # Cleaner than i in range(len(str) - 1)
if last is '*':
return c
last = c
return None
print(findAfterStarIter("h*ell*o"))
def findAfterCharRecr(ch: str, str: str) -> Optional[str]:
if len(str) <= 1:
return None
return str[1] if str[0] == ch else findAfterCharRecr(ch, str[1:])
print(findAfterCharRecr('e', "hello"))
def findAfterCharIter(ch: str, str: str) -> Optional[str]:
if len(str) <= 1:
return None
last = None
for c in str: # Cleaner than i in range(len(str) - 1)
if last is ch:
return c
last = c
return None
print(findAfterCharIter('z', "hell*o"))