Updates on how we generate permutations.

c-declarations
Joshua Potter 2024-03-05 16:48:53 -07:00
parent 32bd0e51ce
commit ec45aad8c3
5 changed files with 136 additions and 33 deletions

View File

@ -220,7 +220,7 @@
"_journal/2024-02/2024-02-27.md": "f75a0d04a875aeee932343dae0c78768",
"filesystems/index.md": "cbd2b0290a3ba3b32abec4bd8bfefad5",
"filesystems/cas.md": "34906013a2a60fe5ee0e31809b4838aa",
"git/objects.md": "b95228a78744d3f9fe173e575aa0445a",
"git/objects.md": "1c867853b080700a73de55b7be185f8d",
"git/index.md": "83d2d95fc549d9e8436946c7bd058d15",
"encoding/integer.md": "84b2ce080e0e756f265257e57467f0e6",
"_journal/2024-02-29.md": "f610f3caed659c1de3eed5f226cab508",
@ -237,7 +237,7 @@
"_journal/2024-03/2024-03-02.md": "8136792b0ee6e08232e4f60c88d461d2",
"_journal/2024-03-04.md": "9ec052061e7a613ff877a4488576e82f",
"_journal/2024-03/2024-03-03.md": "64e2f17b4d57a6bd42a3d1b7f2851b83",
"_journal/2024-03-05.md": "a285ac3e48e335c50c42ee20b0cb0472",
"_journal/2024-03-05.md": "e9a911c19bb4c0ff451db793248cb4bb",
"_journal/2024-03/2024-03-04.md": "4948d90a08af2cff58c629c9a2e11ee4",
"algebra/sequences/geometric.md": "53936ec392b3b714bd4a9bdb4554b582",
"algebra/sequences/arithmetic.md": "80381ca0f2b3b9a1c155c597a7dea75a"

View File

@ -5,14 +5,24 @@ title: "2024-03-05"
- [x] Anki Flashcards
- [x] KoL
- [ ] Sheet Music (10 min.)
- [ ] Go (1 Life & Death Problem)
- [x] Go (1 Life & Death Problem)
- [ ] Korean (Read 1 Story)
- [ ] Interview Prep (1 Practice Problem)
- [ ] Log Work Hours (Max 3 hours)
- [x] Interview Prep (1 Practice Problem)
- [x] Log Work Hours (Max 3 hours)
* Arithmetic and geometric sequences in "Discrete Mathematics: An Open Introduction".
* Completed chapter exercises 2.2.
* TODO
* Finish up tree objects and commit objects.
* 101weiqi
* 2 interview problems.
* Finished adding flashcards for git tree and commit objects.
* 101weiqi (serial numbers)
* Q-82289
* Q-350205
* Q-77774
* Q-267476
* Q-184824
* Q-102329
* Q-152376
* Q-1358
* Q-114783
* Q-26296
* Leetcode Problems
* [Next Permutation](https://leetcode.com/problems/next-permutation/)

View File

@ -39,7 +39,7 @@ void combinations(const int n, const int k, int A[static n]) {
}
```
The above approach prints out all $k$-combinations of a given array.
The above approach prints out all $k$-combinations of an array.
%%ANKI
Basic

View File

@ -12,38 +12,38 @@ tags:
A **permutation** of some $n$ objects is a (possible) rearrangement of those $n$ objects. The number of permutations is $n!$ since there are $n$ possible ways to pick the first object, $(n - 1)$ possible ways to pick the second, and so on.
```c
void permutations_aux(
const size_t n,
int A[static n],
int res[static n],
uint64_t choices
) {
if (!choices) {
for (size_t i = 0; i < n; ++i) {
printf("%d ", A[res[i]]);
void next(const size_t n, int A[static n]) {
size_t pivot = -1;
for (size_t i = n - 1; i >= 1; --i) {
if (A[i - 1] < A[i]) {
pivot = i - 1;
break;
}
printf("\n");
}
if (pivot == -1) {
reverse(0, n - 1, A);
return;
}
unsigned int weight = n - bit_weight(choices);
for (unsigned int i = 0; i < 64; ++i) {
uint64_t next = 1L << i;
if (choices & next) {
res[weight] = i;
permutations_aux(n, A, res, choices & ~next);
size_t j = pivot;
for (size_t i = pivot + 1; i < n; ++i) {
if (A[pivot] < A[i] && (j == pivot || A[i] < A[j])) {
j = i;
}
}
swap(pivot, j, A);
reverse(pivot + 1, n - 1, A);
}
void permutations(const size_t n, int A[static n]) {
int *res = malloc(sizeof(int) * n);
permutations_aux(n, A, res, (1L << n) - 1);
free(res);
size_t iters = factorial(n);
for (size_t i = 0; i < iters; ++i) {
print_array(n, A);
next(n, A);
}
}
```
The above approach prints out all permutations of a given array, provided the array contains at most `64` digits. It relies on `bit_weight` as defined in [[binary/index|binary]].
The above approach prints out all permutations of an array.
%%ANKI
Basic
@ -254,4 +254,4 @@ END%%
## References
* Gries, David. *The Science of Programming*. Texts and Monographs in Computer Science. New York: Springer-Verlag, 1981.
* Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n.d., [https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf](https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf).
* Oscar Levin, *Discrete Mathematics: An Open Introduction*, 3rd ed., n.d., [https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf](https://discrete.openmathbooks.org/pdfs/dmoi3-tablet.pdf).

View File

@ -44,6 +44,14 @@ Reference: Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Soft
<!--ID: 1709177255589-->
END%%
%%ANKI
Basic
How is the SHA-1 of a git object generated?
Back: By calculating the checksum of the object header + contents.
Reference: Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Software Development (New York, NY: Apress, 2014).
<!--ID: 1709674569885-->
END%%
%%ANKI
Basic
What is "oid" an acronym for?
@ -117,7 +125,7 @@ END%%
%%ANKI
Basic
Where does e.g. `d670460b4b4aece5915caf5c68d12f560a9fe3e4` live in the object database?
Where does `d670460b4b4aece5915caf5c68d12f560a9fe3e4` live in the object database?
Back: At `.git/objects/d6/70460b4b4aece5915caf5c68d12f560a9fe3e4`.
Reference: Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Software Development (New York, NY: Apress, 2014).
<!--ID: 1709177255668-->
@ -182,6 +190,14 @@ Reference: Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Soft
<!--ID: 1709345254716-->
END%%
%%ANKI
Basic
What plumbing command can be used to find the git object type of an oid?
Back: `cat-file`
Reference: Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Software Development (New York, NY: Apress, 2014).
<!--ID: 1709674569890-->
END%%
## Blobs
The **b**inary **l**arge **ob**ject (blob) is used to represent arbitrary files.
@ -315,6 +331,14 @@ Reference: Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Soft
<!--ID: 1709345254740-->
END%%
%%ANKI
Basic
What kind of git objects can a tree contain?
Back: Blobs and trees.
Reference: Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Software Development (New York, NY: Apress, 2014).
<!--ID: 1709674569895-->
END%%
%%ANKI
Cloze
{`hash-object`} is to blobs as {`write-tree`} is to trees.
@ -424,6 +448,75 @@ Reference: Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Soft
<!--ID: 1709349279660-->
END%%
## Commits
Commit objects provide metadata information about particular trees.
> While a tree represents a particular directory state of a working directory, a commit represents that state in "time", and explains how to get there.
%%ANKI
Basic
What distinguishes a tree from a commit?
Back: A commit contains data about a tree (e.g. author, parent commits, etc.).
Reference: Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Software Development (New York, NY: Apress, 2014).
<!--ID: 1709674569898-->
END%%
%%ANKI
Basic
How do git commits maintain a traversable history?
Back: Each commit can have parent commits associated with it.
Reference: Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Software Development (New York, NY: Apress, 2014).
<!--ID: 1709674569902-->
END%%
%%ANKI
Basic
What kind of git object is responsible for maintaining history?
Back: Commits.
Reference: Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Software Development (New York, NY: Apress, 2014).
<!--ID: 1709674569906-->
END%%
%%ANKI
Basic
What plumbing subcommand is used to create commits?
Back: `commit-tree`
Reference: Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Software Development (New York, NY: Apress, 2014).
<!--ID: 1709674569911-->
END%%
%%ANKI
Cloze
{`write-tree`} is to trees as {`commit-tree`} is to commits.
Reference: Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Software Development (New York, NY: Apress, 2014).
<!--ID: 1709674569916-->
END%%
%%ANKI
Basic
What does the decompressed header of a commit object look like?
Back: `commit <size><NUL>`
Reference: Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Software Development (New York, NY: Apress, 2014).
<!--ID: 1709674569920-->
END%%
%%ANKI
Basic
Commits points to what kind of git object?
Back: Trees.
Reference: Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Software Development (New York, NY: Apress, 2014).
<!--ID: 1709674569924-->
END%%
%%ANKI
Basic
What does the `commit-tree` subcommand return?
Back: The oid of the new commit object.
Reference: Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Software Development (New York, NY: Apress, 2014).
<!--ID: 1709674569928-->
END%%
## References
* Scott Chacon, *Pro Git*, Second edition, The Experts Voice in Software Development (New York, NY: Apress, 2014).