Python function

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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.

```python
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.

```python
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.
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. ```python 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. ```python 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.
**Problem Statement**

We’ll write a string with \( n \) characters as \( s_1s_2s_3 \cdots s_{n-2}s_{n-1}s_n \).

Now we can state the thing we want to prove:

**Proposition:** For any \( n \in \mathbb{N} \), the result of calling the function `reverse_string` on the string \( s_1s_2s_3 \cdots s_{n-2}s_{n-1}s_n \) is the reversal of \( s \), that is, the string \( s_ns_{n-1}s_{n-2} \cdots s_3s_2s_1 \).

Prove this claim using induction.

**Note:** The empty string \( \varepsilon \) is the string with no elements at all; this string is its own reversal.
Transcribed Image Text:**Problem Statement** We’ll write a string with \( n \) characters as \( s_1s_2s_3 \cdots s_{n-2}s_{n-1}s_n \). Now we can state the thing we want to prove: **Proposition:** For any \( n \in \mathbb{N} \), the result of calling the function `reverse_string` on the string \( s_1s_2s_3 \cdots s_{n-2}s_{n-1}s_n \) is the reversal of \( s \), that is, the string \( s_ns_{n-1}s_{n-2} \cdots s_3s_2s_1 \). Prove this claim using induction. **Note:** The empty string \( \varepsilon \) is the string with no elements at all; this string is its own 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.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education