Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

46. Permutations #50

wants to merge 1 commit into from

Conversation

fhiyo
Copy link
Owner

@fhiyo fhiyo commented Jul 22, 2024

yield [nums[i] for i in indices]
yield from perm(indices)

return [nums[:]] + list(perm(list(range(len(nums)))))
Copy link

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)))))はかっこが多くて追うのにちょっと時間かかりました

Copy link
Owner Author

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))

なら良さそうですかね?

Copy link

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

全体的に命名をサボりがちかも。ただこのくらいなら読めるんじゃないか?という気もしてこうなっている (が、後で読んだら読めないとかあるのかもしれない)

Copy link

Choose a reason for hiding this comment

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

個人的には読めるんですが、(Step1のラストのやつのようなややこしいやつは変数名よりむしろ、書かれているようなコメントの方が欲しい気分)他の人の意見も気になります。

Choose a reason for hiding this comment

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

自分も読めると思いました。
関数名、perm, permsだけ少し気になりました。(丁寧に書くと、permutation_generatorとかでしょうか?)

Copy link
Owner Author

Choose a reason for hiding this comment

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

generatorだとクラス名っぽい気がするので、自分が書くならgenerate_permutationsとかですかね。
ただ名前の長さの割に得られる情報が少ない気がしまして (permが良いというわけではなく、妥協に近いんですが)。

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を探す
Copy link

Choose a reason for hiding this comment

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

これ降順になってるからmin_valueとか保存せずに大きくなった時点でbreakすればいいんですね。
自分ややこしいことやってました(勉強になりました)

Copy link
Owner Author

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)を思い出しながら書いた。ギリギリ何とか書けたという感じ。

Copy link

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.

https://docs.python.org/3/library/itertools.html#itertools.permutations
ここにもありますね。
Generator など、やはり言語の珍し目の機能を使うの楽しいですね。

@rihib rihib mentioned this pull request Aug 13, 2024
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