#6 Learn Python code #learnpython #recursion #algorithm #programming #coding #pythonprogramming
## Slide 1
So, What Does This Python Program Do?
def myst(s):
n = len(s)
match n:
case 0 | 1:
return True
case _ if s[0] == s[n-1]:
return myst(s[1:n-1])
case _:
return False
:hand: Pause the Video, if You Need to.
#Python #Beginner #Algorithm
## Slide 2
Here's an Example Usage:
^^^ myst("madam")
???
What Would be the Output?
Another Example:
^^^ myst('Taco Cat')
???
:hand: Again, Pause the Video Here,
If You Need to.
## Slide 3
Let's Walk through the Code:
def myst(s):
n = len(s)
match n:
case 0 | 1:
return True
...
:small_orange_diamond: First,
Based on `len`, and Slicing,
The Argument `s` is a [Sequence,]
Like a [List] or [String.]
:small_orange_diamond: The `myst` Function Then
Returns a Boolean Value.
:small_orange_diamond: `myst` is [Recursively] Implemented.
:small_orange_diamond: The Second Statement in `myst` is
[A `match` Statement]
## Slide 4
match n:
case 0 | 1:
return True
...
case _:
return False
Python's `match` Statement
- Starts with the Keyword `match` Followed by an Expression, and
- It Consists of `case` Clauses.
- Each `case` Includes a ["Pattern".]
- If the Match Expression Matches One of These Patterns, Then
* Its [Code Block] is Executed, and
* It Exits the `match` Statement.
- The Pattern `_` is Special in That
* [It Matches Any Expression.]{.persimmon-text}
## Slide 5
A `case` Clause Can Include
An `if` ["Match Guard".]
A Pattern with a Match Guard
Can Match the Match Expression
Only if it First Satisfies
The Match Guard.
case _ if s[0] == s[n-1]:
return myst(s[1:n-1])
Now,
Although this `case`
Includes the Catch-All Pattern, `_`,
It Will Not Match the Expression `n`
Unless the Condition `s[0] == s[n-1]`
Is First Satisfied.
## Slide 6
Now, If You Go through
The Entire `match` Statement,
From Top to Bottom,
[Then]
You Will Realize that
It is Checking
If the Given Sequence, `s`,
Is a ["Palindrome".]
If We Run the Example Code,
^^^ myst("madam")
True
^^^ myst('Taco Cat')
False
## Slide 7
Practice Quiz:
[1] First, Make Sure that You Understand the Implementation of the `myst` Function.
[2] Implement [the `match` Statement] in `myst`
Using [an `if` Statement.]
[3] Implement a Function, `is_palindrome`, [Iteratively,]
e.g., without Using Recursion.
Please Like and Subscribe @codingjoy!
Видео #6 Learn Python code #learnpython #recursion #algorithm #programming #coding #pythonprogramming канала Coding Joy
So, What Does This Python Program Do?
def myst(s):
n = len(s)
match n:
case 0 | 1:
return True
case _ if s[0] == s[n-1]:
return myst(s[1:n-1])
case _:
return False
:hand: Pause the Video, if You Need to.
#Python #Beginner #Algorithm
## Slide 2
Here's an Example Usage:
^^^ myst("madam")
???
What Would be the Output?
Another Example:
^^^ myst('Taco Cat')
???
:hand: Again, Pause the Video Here,
If You Need to.
## Slide 3
Let's Walk through the Code:
def myst(s):
n = len(s)
match n:
case 0 | 1:
return True
...
:small_orange_diamond: First,
Based on `len`, and Slicing,
The Argument `s` is a [Sequence,]
Like a [List] or [String.]
:small_orange_diamond: The `myst` Function Then
Returns a Boolean Value.
:small_orange_diamond: `myst` is [Recursively] Implemented.
:small_orange_diamond: The Second Statement in `myst` is
[A `match` Statement]
## Slide 4
match n:
case 0 | 1:
return True
...
case _:
return False
Python's `match` Statement
- Starts with the Keyword `match` Followed by an Expression, and
- It Consists of `case` Clauses.
- Each `case` Includes a ["Pattern".]
- If the Match Expression Matches One of These Patterns, Then
* Its [Code Block] is Executed, and
* It Exits the `match` Statement.
- The Pattern `_` is Special in That
* [It Matches Any Expression.]{.persimmon-text}
## Slide 5
A `case` Clause Can Include
An `if` ["Match Guard".]
A Pattern with a Match Guard
Can Match the Match Expression
Only if it First Satisfies
The Match Guard.
case _ if s[0] == s[n-1]:
return myst(s[1:n-1])
Now,
Although this `case`
Includes the Catch-All Pattern, `_`,
It Will Not Match the Expression `n`
Unless the Condition `s[0] == s[n-1]`
Is First Satisfied.
## Slide 6
Now, If You Go through
The Entire `match` Statement,
From Top to Bottom,
[Then]
You Will Realize that
It is Checking
If the Given Sequence, `s`,
Is a ["Palindrome".]
If We Run the Example Code,
^^^ myst("madam")
True
^^^ myst('Taco Cat')
False
## Slide 7
Practice Quiz:
[1] First, Make Sure that You Understand the Implementation of the `myst` Function.
[2] Implement [the `match` Statement] in `myst`
Using [an `if` Statement.]
[3] Implement a Function, `is_palindrome`, [Iteratively,]
e.g., without Using Recursion.
Please Like and Subscribe @codingjoy!
Видео #6 Learn Python code #learnpython #recursion #algorithm #programming #coding #pythonprogramming канала Coding Joy
Комментарии отсутствуют
Информация о видео
20 мая 2025 г. 1:17:01
00:00:58
Другие видео канала