Skip to content

Create 409. Longest Palindrome.md#27

Open
Kitaken0107 wants to merge 1 commit intomainfrom
Kitaken0107-patch-29
Open

Create 409. Longest Palindrome.md#27
Kitaken0107 wants to merge 1 commit intomainfrom
Kitaken0107-patch-29

Conversation

@Kitaken0107
Copy link
Owner

"""

count_s = defaultdict(int)
max_palindrome = 0

Choose a reason for hiding this comment

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

max_palindromeよりかは長さという意味でpalindrome_lengthも良いかと思います

count_s[char] += 1
for value in count_s.values():
max_palindrome += 2*(value // 2)
if value%2 == 1:

Choose a reason for hiding this comment

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

オペランドと演算子間はスペースを空けましょう (cf. PEP8 https://peps.python.org/pep-0008/)

for value in count_s.values():
max_palindrome += 2*(value // 2)
if value%2 == 1:
odd_counter += 1

Choose a reason for hiding this comment

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

odd_found = value % 2 == 1 or odd_found

も候補でしょうか

if value%2 == 1:
odd_counter += 1

if odd_counter > 0:

Choose a reason for hiding this comment

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

上記のコメントに連続して

if odd_found:

も候補です

②アウトプットのmax_palindromeを準備
④文字の出現回数が偶数→出現回数を足す
⑤文字の出現回数が奇数→出現回数-1を足す
⑥文字列に一つでも奇数があれば、最後に+1を足す    

Choose a reason for hiding this comment

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

ロジックについてはこれが正攻法かと思います

Copy link

Choose a reason for hiding this comment

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

正攻法でないものとして、Manacher's algorithm というのがあります。
https://discord.com/channels/1084280443945353267/1199984201521430588/1201656280368807976

Copy link

Choose a reason for hiding this comment

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

これ、違う問題の話ですね。

count_to_s[char] += 1

for value in count_to_s.values():
if value%2 == 0:
Copy link

Choose a reason for hiding this comment

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

% の両側にスペースを空けることをお勧めします。

参考までにスタイルガイドへのリンクを貼ります。

https://peps.python.org/pep-0008/#other-recommendations

Always surround these binary operators with a single space on either side: assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <=, >=, in, not in, is, is not), Booleans (and, or, not).

https://google.github.io/styleguide/pyguide.html#36-whitespace

Surround binary operators with a single space on either side for assignment (=), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), and Booleans (and, or, not). Use your better judgment for the insertion of spaces around arithmetic operators (+, -, *, /, //, %, **, @).

ただし、上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。

# 2nd
### 主な変更点   
①足し合わせる先を一つにまとめる
②count_to_sが文法的に不自然に感じたので、count_sに変更   
Copy link

Choose a reason for hiding this comment

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

count には名詞としての使い方がありますので、不自然ではないように思いました。 count_s のほうは、 s の数を数える関数名のように見え、逆に不自然に感じました。

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