Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
recursion examples
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SOFT Core
SOFT 161 and 162
recursion examples
Commits
ffd634dd
Commit
ffd634dd
authored
Mar 14, 2021
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Initial commit
parents
Branches
main
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
get_max_prefix_sum_original.py
+11
-0
11 additions, 0 deletions
get_max_prefix_sum_original.py
get_max_prefix_with_helper.py
+17
-0
17 additions, 0 deletions
get_max_prefix_with_helper.py
palindrome.py
+26
-0
26 additions, 0 deletions
palindrome.py
with
54 additions
and
0 deletions
get_max_prefix_sum_original.py
0 → 100644
+
11
−
0
View file @
ffd634dd
def
get_maximum_prefix_sum
(
values
):
if
len
(
values
)
>
0
:
candidate
=
get_maximum_prefix_sum
(
values
[:
-
1
])
total
=
sum
(
values
)
# requires a loop, which makes our recursion slow!
if
candidate
>
total
:
return
candidate
return
total
return
0
print
(
get_maximum_prefix_sum
([
5
,
-
4
,
6
]))
This diff is collapsed.
Click to expand it.
get_max_prefix_with_helper.py
0 → 100644
+
17
−
0
View file @
ffd634dd
def
helper
(
values
):
if
len
(
values
)
>
0
:
candidate
,
partial_total
=
helper
(
values
[:
-
1
])
total
=
partial_total
+
values
[
-
1
]
if
candidate
>
total
:
return
candidate
,
total
return
total
,
total
return
0
,
0
def
get_maximum_prefix_sum
(
values
):
result
,
_
=
helper
(
values
)
return
result
print
(
get_maximum_prefix_sum
([
5
,
-
4
,
6
]))
\ No newline at end of file
This diff is collapsed.
Click to expand it.
palindrome.py
0 → 100644
+
26
−
0
View file @
ffd634dd
def
is_palindrome
(
text
):
return
is_palindrome_helper
(
text
,
0
,
len
(
text
)
-
1
)
def
is_palindrome_helper
(
text
,
start
,
end
):
if
start
>=
end
:
return
True
first
=
text
[
start
].
lower
()
last
=
text
[
end
].
lower
()
if
first
.
isalpha
()
and
last
.
isalpha
():
if
first
==
last
:
return
is_palindrome_helper
(
text
,
start
+
1
,
end
-
1
)
else
:
return
False
elif
not
last
.
isalpha
():
return
is_palindrome_helper
(
text
,
start
,
end
-
1
)
else
:
return
is_palindrome_helper
(
text
,
start
+
1
,
end
)
print
(
is_palindrome
(
'
Madam, I
\'
m Adam
'
))
print
(
is_palindrome
(
'
Go hang a salami, I
\'
m a lasagna hog
'
))
print
(
is_palindrome
(
'
A man, a plan, a canal–Panama!
'
))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment