-
Notifications
You must be signed in to change notification settings - Fork 0
46. Permutations #50
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?
46. Permutations #50
Conversation
yield [nums[i] for i in indices] | ||
yield from perm(indices) | ||
|
||
return [nums[:]] + list(perm(list(range(len(nums))))) |
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.
list(perm(list(range(len(nums)))))はかっこが多くて追うのにちょっと時間かかりました
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.
たしかにこれは多いですね。
indices = list(range(nums))
return [nums[:]] + list(perm(indices))
なら良さそうですかね?
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.
そういえば、
#17 (comment)
心理学やマーケティングだと、マジカルナンバーといって、短期記憶の限界は7つか4つかという
みたいな話を前にしましたね。
## 3rd | ||
|
||
全体的に命名をサボりがちかも。ただこのくらいなら読めるんじゃないか?という気もしてこうなっている (が、後で読んだら読めないとかあるのかもしれない) | ||
|
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.
個人的には読めるんですが、(Step1のラストのやつのようなややこしいやつは変数名よりむしろ、書かれているようなコメントの方が欲しい気分)他の人の意見も気になります。
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.
自分も読めると思いました。
関数名、perm, permsだけ少し気になりました。(丁寧に書くと、permutation_generatorとかでしょうか?)
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.
generatorだとクラス名っぽい気がするので、自分が書くならgenerate_permutationsとかですかね。
ただ名前の長さの割に得られる情報が少ない気がしまして (permが良いというわけではなく、妥協に近いんですが)。
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.
上から読んでいった場合に何するものなのかちょっとわかりづらいと感じた位でした。また、右記も同意です。ただ名前の長さの割に得られる情報が少ない気がしまして
### ③ | ||
|
||
1. 後ろから初めて昇順になっているindexを探す | ||
2. 後ろから、1で見つけたindexの要素より初めて大きくなる要素のindexを探す |
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.
これ降順になってるからmin_valueとか保存せずに大きくなった時点でbreakすればいいんですね。
自分ややこしいことやってました(勉強になりました)
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.
そうですね、自分はdiscord内で紹介されてたC++のドキュメント (https://en.cppreference.com/w/cpp/algorithm/next_permutation) を見て覚えました
### ① | ||
|
||
[cpythonのpermutationの内容](https://github.com/python/cpython/blob/bc264eac3ad14dab748e33b3d714c2674872791f/Modules/itertoolsmodule.c#L2603)を思い出しながら書いた。ギリギリ何とか書けたという感じ。 | ||
|
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.
これ思い出しながら自力で書けるのすごいですね
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.
https://docs.python.org/3/library/itertools.html#itertools.permutations
ここにもありますね。
Generator など、やはり言語の珍し目の機能を使うの楽しいですね。
https://leetcode.com/problems/permutations/description/