Conversation
| """ | ||
|
|
||
| count_s = defaultdict(int) | ||
| max_palindrome = 0 |
There was a problem hiding this comment.
max_palindromeよりかは長さという意味でpalindrome_lengthも良いかと思います
| count_s[char] += 1 | ||
| for value in count_s.values(): | ||
| max_palindrome += 2*(value // 2) | ||
| if value%2 == 1: |
There was a problem hiding this comment.
オペランドと演算子間はスペースを空けましょう (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 |
There was a problem hiding this comment.
odd_found = value % 2 == 1 or odd_found
も候補でしょうか
| if value%2 == 1: | ||
| odd_counter += 1 | ||
|
|
||
| if odd_counter > 0: |
| ②アウトプットのmax_palindromeを準備 | ||
| ④文字の出現回数が偶数→出現回数を足す | ||
| ⑤文字の出現回数が奇数→出現回数-1を足す | ||
| ⑥文字列に一つでも奇数があれば、最後に+1を足す |
There was a problem hiding this comment.
正攻法でないものとして、Manacher's algorithm というのがあります。
https://discord.com/channels/1084280443945353267/1199984201521430588/1201656280368807976
| count_to_s[char] += 1 | ||
|
|
||
| for value in count_to_s.values(): | ||
| if value%2 == 0: |
There was a problem hiding this comment.
% の両側にスペースを空けることをお勧めします。
参考までにスタイルガイドへのリンクを貼ります。
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に変更 |
There was a problem hiding this comment.
count には名詞としての使い方がありますので、不自然ではないように思いました。 count_s のほうは、 s の数を数える関数名のように見え、逆に不自然に感じました。
https://leetcode.com/problems/longest-palindrome/