Python function
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 6PE
Related questions
Question
Please help me solve Problem 2 on induction.
![Problem 2
Motivation
In computer science, we often use induction to prove the correctness of a function - that is, we prove formally that the function does
what it is supposed to do. This is sometimes called "proving a function." For example, consider the Python function below, which
reverses an input string using recursion.
def reverse_string(s):
||||||
reverse the string s. The first letter becomes the last letter, the second letter becomes the second-
args:
s, the string to be reversed
returns: the reversed string
||||||
if s == "":
return
else:
return reverse_string(s[1:]) +s[0]
Here it is in action.
||||
reverse_string("CSCI 0200")
¹0020 ICSC'
We got the right answer for this input! But...would we get the right answer on every possible input? Let's write a proof which will
guarantee that we will.
0
C](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F385eb8fb-6648-4df6-8047-4f830c46ad3b%2Ff106ab72-5d87-4bdb-8fd4-764c8c1f0223%2Fl7sqe6h_processed.png&w=3840&q=75)
Transcribed Image Text:Problem 2
Motivation
In computer science, we often use induction to prove the correctness of a function - that is, we prove formally that the function does
what it is supposed to do. This is sometimes called "proving a function." For example, consider the Python function below, which
reverses an input string using recursion.
def reverse_string(s):
||||||
reverse the string s. The first letter becomes the last letter, the second letter becomes the second-
args:
s, the string to be reversed
returns: the reversed string
||||||
if s == "":
return
else:
return reverse_string(s[1:]) +s[0]
Here it is in action.
||||
reverse_string("CSCI 0200")
¹0020 ICSC'
We got the right answer for this input! But...would we get the right answer on every possible input? Let's write a proof which will
guarantee that we will.
0
C
![Problem Statement
We'll write a string with n characters as $₁8283 · Sn-2Sn-1Sn.
Now we can state the thing we want to prove:
Proposition: For any n E N, the result of calling the function reverse_string on the string $18283
of s, that is, the string SnSn-15n-2 ··· S3S2S1.
Prove this claim using induction.
Note: The empty string € is the string with no elements at all; this string is its own reversal.
Sn-2Sn-1Sn is the reversal](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F385eb8fb-6648-4df6-8047-4f830c46ad3b%2Ff106ab72-5d87-4bdb-8fd4-764c8c1f0223%2Fbd2vtft_processed.png&w=3840&q=75)
Transcribed Image Text:Problem Statement
We'll write a string with n characters as $₁8283 · Sn-2Sn-1Sn.
Now we can state the thing we want to prove:
Proposition: For any n E N, the result of calling the function reverse_string on the string $18283
of s, that is, the string SnSn-15n-2 ··· S3S2S1.
Prove this claim using induction.
Note: The empty string € is the string with no elements at all; this string is its own reversal.
Sn-2Sn-1Sn is the reversal
Expert Solution
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning