Implement reverseWords function to reverse each word#43
Merged
argondev22 merged 1 commit intomainfrom Dec 31, 2025
Merged
Conversation
Comment on lines
+4
to
+5
| words = [reverseString(word) for word in words] | ||
| return " ".join(words) |
There was a problem hiding this comment.
メソッド内で変数を再代入する際、より明確な変数名を使用するか、再代入を避けることで可読性が向上します。
現在の実装:
words = s.split()
words = [reverseString(word) for word in words]同じ変数名wordsを2回使用していますが、最初は元の単語リスト、2回目は反転された単語リストを保持しています。
より明確にするには:
words = s.split()
reversed_words = [reverseString(word) for word in words]
return " ".join(reversed_words)または、中間変数を省略して1行で記述することも可能です:
return " ".join(reverseString(word) for word in s.split())これにより、コードの意図がより明確になります。
Suggested change
| words = [reverseString(word) for word in words] | |
| return " ".join(words) | |
| reversed_words = [reverseString(word) for word in words] | |
| return " ".join(reversed_words) |
Comment on lines
+9
to
+18
| right = 0 | ||
| left = len(s) - 1 | ||
|
|
||
| while right < left: | ||
| tmp = s[left] | ||
| s[left] = s[right] | ||
| s[right] = tmp | ||
|
|
||
| right += 1 | ||
| left -= 1 |
There was a problem hiding this comment.
変数の初期化が逆になっています。right変数は左端(0)から開始し、left変数は右端(len(s) - 1)から開始していますが、これでは変数名と実際の役割が逆になっています。
現在の実装では:
right = 0(実際には左端を指している)left = len(s) - 1(実際には右端を指している)
この混乱により、コードの可読性が著しく低下しています。変数名と実際の位置を一致させるべきです。
以下のいずれかの修正が必要です:
オプション1:変数名を修正
rightをleftに変更leftをrightに変更
オプション2:初期化を修正
left = 0right = len(s) - 1
どちらの場合も、while条件やインデックスアクセスを適切に調整する必要があります。
Suggested change
| right = 0 | |
| left = len(s) - 1 | |
| while right < left: | |
| tmp = s[left] | |
| s[left] = s[right] | |
| s[right] = tmp | |
| right += 1 | |
| left -= 1 | |
| left = 0 | |
| right = len(s) - 1 | |
| while left < right: | |
| tmp = s[left] | |
| s[left] = s[right] | |
| s[right] = tmp | |
| left += 1 | |
| right -= 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Status
Accepted
Runtime
15 ms
Order
O(n)
Memory
18.06 MB
Order
O(n)
Time Taken
m s