So here's a way to define is_palindrome. So we're taking
a single string as an output, I will call it s,
and we're first going to test the base case. So the
base case was to see if the string is empty, we
should return true right away. So we can do that
with an if. We are going to check if s is equal
to the empty string, and if it is, we return true,
that's our base case. For the else, we have the recursive
of case so now we need to do the test of the first and the last characters to
see if they match. And we can do that
using the string indexing operators, as 0 gets us
the first character, s negative 1 gets us the
last character. If they match, then we need to
check the rest of the string. If they didn't
match and let's finish the didn't match case first.
So if they didn't match then we know it is
a palindrome, because we found a place where the first and
the last character did not match, so it should return
false right away. If they did match, well then we have
the harder problem. We need to do the recursive call
to check all the other letters in the strings still form
a palindrome. So this was our starting string. It had
all these characters in it. We checked that this one matches
this one, so now what we need to do is take the rest of the string and check
if this is a palindrome. So that'll be a
recursive call, so we're going to return the result, of calling
is_palindrome. But instead of passing in s, what we
want to do is pass in the string starting from
position 1 of s, so removing the first character,
and ending at position negative 1, removing the last character.
And remember with our indexing, the last value here is
not included. So by having the last index as negative
1, that removes the final letter of the string. So
the first thing to test is the base case. So
we'll pass in the empty string. And the empty string
is a palindrome, so it should give us the result
true, which it does. Let's try the single letter string,
a, that's also a palindrome. It's the same backwards and forwards.
And so we also get the value true, if we try say string ab, which is not a
palindrome. We get false. As a longer test, if we
try a level, we should get true. Which we do. And, let's try
one of the most famous palindromes, amanaplanacanalpanama.
And we should get True which we do.
[BLANK_AUDIO]
これがis_palindrome関数を定義する方法です
入力としてある文字列を選びそれをsとします
まず基本ケースのテストを行います
基本ケースは文字列が空であるかどうかを
見るためでしたので
ただちにTrueを返すはずです
ですからifを使い
sが空の文字列であるかどうかチェックします
もしそうであればTrueを返します
これが基本ケースです
elseの場合は再帰的ケースとなります
最初と最後の文字が同じかどうか
テストする必要があります
これを行うにあたって
文字列のインデックス演算子を使うことができます
s[0]は最初の文字s[-1]は最後の文字を表します
同じであれば
文字列の他の部分もチェックする必要があります
同じでなければまずそのケースを完了させましょう
同じでなければそれは回文でないと分かります
最初と最後の文字が合わない場所を
見つけたからであり
すぐにFalseを返します
同じであればさらに難しい問題となります
再帰呼び出しを行い文字列の他の全文字が
回文を形成しているかチェックします
これが私たちの最初の文字列でした
すべての文字がここに入っており
同じものをチェックしました
今行うべきことは文字列の他の部分を取り出し
回文であるかどうかチェックすることです
これは再帰呼び出しとなり
is_palindromeを呼び出した結果を返します
しかしsを渡す代わりに行いたいことは
最初の文字を除去してs[1]の位置から始まり
最後の文字を除去して
s[-1]で終わる文字列を渡すことです
そしてこの最後の値は含まないという
インデクシングを思い出してください
最後のインデックスを-1とすることで
文字列の最後の文字を取り去ることになります
ですから最初にテストすることは基本ケースです
空の文字列を渡します
空の文字列は回文なのでTrueという結果になります
1文字の文字列であるaを試してみましょう
これもやはり回文です 前からも後ろからも同じです
これもTrueの値を返します
また回文ではない文字列abを試してみると
Falseを返します
長めの文字のテストとしてlevelを入れてみましょう
Trueとなるはずです そうなりますね
そして最も有名な回文である
amanaplanacanalpanamaを試してみましょう
ご覧のようにTrueを返します