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
icon
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
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
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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Function Arguments
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.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning