Skip to content

125. Valid Palindrome - #6

Merged
t-ooka merged 8 commits into
mainfrom
question/Valid-Palindrome
Aug 6, 2025
Merged

125. Valid Palindrome#6
t-ooka merged 8 commits into
mainfrom
question/Valid-Palindrome

Conversation

@t-ooka

@t-ooka t-ooka commented Jul 2, 2024

Copy link
Copy Markdown
Owner

@t-ooka t-ooka changed the title Question/valid palindrome 125. Valid Palindrome Jul 2, 2024
public:
bool isPalindrome(string s) {
int i = 0;
int j = s.size() - 1;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i,jよりはleft.rightとしたほうが直感的でわかりやすいかと思いました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘ありがとうございます。
おっしゃるとおりですね、、!left, 'right`の方が適切だと思います。

Comment thread Valid Palindrome/step1.py
left_pointer, right_pointer = 0, len(s) - 1

while left_pointer < right_pointer:
while left_pointer < right_pointer and s[left_pointer].isalnum() != True:

@NobukiFukui NobukiFukui Jul 3, 2024

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確認済みでしたらすみませんが,
PEP8だとwhileと同じ行に実行文を置くことは推奨されてないみたいですね
https://peps.python.org/pep-0008/

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参考のリンクありがとうございます。
left_pointer < right_pointer and s[left_pointer].isalnum() != True全体で条件文であるつもりだったのですが、これもこれも分けるべきという認識で合ってますかね、!

@NobukiFukui NobukiFukui Jul 5, 2024

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ごめんなさい.見間違えていました.分けなくて良いかと思います
ただ条件式が長いので,もう少し短くできないかと思いました.

while left_pointer < right_pointer and not s[left_pointer].isalnum():
    left_pointer += 1

以下でodaさんもご指摘されてますね

@@ -0,0 +1,16 @@
class Solution:
def isPalindrome(self, s: str) -> bool:
i, j = 0, len(s) - 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

colorboxさんと同様,left, rightのほうがわかりやすいかなと思いました

i, j = 0, len(s) - 1

while i < j:
while i < j and not s[i].isalnum():

@NobukiFukui NobukiFukui Jul 3, 2024

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的な感想になってしまいますが,whileが連続していて読みにくく感じました.
また,5行目ののwhile文でi < jの条件が効いているので,ここに同じ条件を入れるのも読みにくい原因かもしれません.
ここはs[left or right]がアルファベットor数字でなければi, jを進めるという考えで

if not s[left].isalnum():
 left += 1
   continue
if not s[right].isalnum:
   right -= 1
   continue

のほうがわかりやすいかと思いました

以下の方法も確かめてみていただければと思います.
https://discord.com/channels/1084280443945353267/1200089668901937312/1203011372489777193

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

具体的なコードまでありがとうございます。
おっしゃるとおりですね、こちらで一度コーディングしてみようと思います!

Comment thread Valid Palindrome/step1.py
while left_pointer < right_pointer:
while left_pointer < right_pointer and s[left_pointer].isalnum() != True:
left_pointer += 1
while left_pointer < right_pointer and s[right_pointer].isalnum() != True:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X != True は少し違和感ありますね。まだ、not X ですかねえ。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘ありがとうございます。そこの違和感の部分は認知できてませんでした。

@t-ooka
t-ooka merged commit bb68eb8 into main Aug 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants