-
Notifications
You must be signed in to change notification settings - Fork 0
49. Group Anagrams #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| ```javascript | ||
| const groupAnagrams = function(strs) { | ||
| const sorted_to_anagrams = new Map() | ||
| for (const original_word of strs) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
original_word, soretd_wordは名前としてごちゃっとしすぎかなと感じます。
変数名が長いので、パッと見た時にロジックを追うよりも、変数名の識別に脳の容量が割かれる感じです。
original_word s or word
soretd_word sorted or t
とかが好きです。
コメント集のこの部分が参考になると思います。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.fcs3httrll4l
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。
パッと見た時にロジックを追うよりも、変数名の識別に脳の容量が割かれる感じです。
こちらの感覚を僕は持てていませんでした。参考になります。
コメント集を見て、短くても正しく伝わる方法を模索しようと思います。
| original_words.push(original_word) | ||
| sorted_to_anagrams.set(sorted_word, original_words) | ||
| } | ||
| const ans = new Array() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
もしpythonだったらconst ans = Array()とreturn ansの直前で改行を入れたくなります。javaにおける改行ってどんな感じなんでしょうか?
また ansという変数名は「問題への答え」という気持ちが前に出過ぎていて、システムに組み込み込まれているとしたら変だなという感じです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
いくつかスタイルガイドを見てみたのですが、改行に関する記載は見つかりませんでした。
const ans = Array()の前の改行に関しては、どちらものケースもあるかなと思います。
return前に改行を入れるのはあまり見ないイメージがあります。
- https://standardjs.com/
- https://google.github.io/styleguide/jsguide.html#features-arrays-ctor
- https://github.com/rwaldron/idiomatic.js
- https://github.com/airbnb/javascript
変数名についてもシステムに組み込むことを意識し、resultという変数を使おうと思います!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改行の入れ方はわりと自由です。慣れるにつれてだんだん改行が減ってくる傾向があるかなとは思います。
|
|
||
| ### 他の人のPRを読んで | ||
|
|
||
| * Pythonのdefaultdictのkeyについて、何を入力としてOKかを調べようとしたが、ドキュメントを探しても見つからず調べ方を教えて欲しいです。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defaultdictのkeyもdictのkeyと同じです。
default は組み込みの dict クラスのサブクラスです。これはメソッドをひとつオーバーライドし、書き込み可能なインスタンス変数をひとつ追加しています。それ以外の機能は dict クラスと同じです
https://docs.python.org/ja/3.13/library/collections.html#collections.defaultdict:~:text=defaultdict%20%E3%81%AF%E7...
辞書のキーには、 ほぼ どんな値も使うことができます。 キーとして使えないのは、 hashable (ハッシュ可能) でない値、すなわちリストや辞書のようなミュータブルな型 (内包する値ではなくオブジェクト自体が同一であるかによって比較が行われるような型)です。https://docs.python.org/ja/3/library/stdtypes.html#dict:~:text=%E8%BE%9E%E6%9B...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます!
tupleが辞書のキーにできると言っていた箇所が原文を確認し、理解できました。
| } | ||
| const original_words = sorted_to_anagrams.get(sorted_word) | ||
| original_words.push(original_word) | ||
| sorted_to_anagrams.set(sorted_word, original_words) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JavaScript 知らないんですが、この set っているんですか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
質問の意図を汲み取れているのか怪しいので、質問をさせてください。
この set っているんですか?
こちらは、どういう意味でしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こういうことです。
const m = new Map();
m.set(1, []);
const a = m.get(1);
a.push("hello");
console.log(m.get(1));There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
意図が理解できました!
コードを試したところ、mapのvalueが正しく置き換わっていることを確認しました。
> const m = new Map();
> m.set(1, []);
> const a = m.get(1);
> a.push("hello");
> console.log(m.get(1));
[ 'hello' ]
また、Mapのget関数のドキュメントを参照したところ、MapのValueがObjectの場合には、更新可能という記載を見つけました。 調べる習慣/量が足りていないので、増やそうと思います。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get
If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ObjectにArrayが含まれていることも確認しました!
Objectの一覧: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
Primitive Value (Objectでないデータ型)の一覧 : https://developer.mozilla.org/en-US/docs/Glossary/Primitive
問題URL
問題文
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
Example 1:
There is no string in strs that can be rearranged to form "bat".
The strings "nat" and "tan" are anagrams as they can be rearranged to form each other.
The strings "ate", "eat", and "tea" are anagrams as they can be rearranged to form each other.
Example 2:
Example 3:
Constraints: