Skip to content

Instantly share code, notes, and snippets.

@matklad
Created September 21, 2017 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matklad/534c7c1561262d15cbe7a6a3e4af07da to your computer and use it in GitHub Desktop.
Save matklad/534c7c1561262d15cbe7a6a3e4af07da to your computer and use it in GitHub Desktop.
#include <vector>
#include <cstdint>
#include <iostream>
#include <algorithm>
void perm(std::vector<int32_t> const& xs, std::vector<int32_t> curr) {
if (curr.size() == xs.size()) {
for (int32_t x: curr) {
std::cout << x << ' ';
}
std::cout << '\n';
}
for (int32_t x: xs) {
if (find(begin(curr), end(curr), x) == end(curr)) {
curr.push_back(x);
perm(xs, curr);
curr.pop_back();
}
}
}
int main() {
std::vector<int32_t> xs = {1, 2, 3};
std::vector<int32_t> curr;
perm(xs, curr);
return 0;
}
import itertools
print("permutations")
for p in itertools.permutations([1, 2, 3]):
print(p)
print("caretesian product")
for p in itertools.product([1, 2, 3], repeat=3):
print(p)
def my_permutations(xs):
if not xs:
yield []
return
for i, x in enumerate(xs):
for p in my_permutations(xs[:i] + xs[i + 1:]):
p.append(x)
yield p
print("my_permutations")
for p in my_permutations([1, 2, 3]):
print(p)
sum(x * y for (x, y) in zip(xs, ys))
import sys
_n, *xs = map(int, sys.stdin().read())
import itertools
for (x, y) in itertools.product(range(n), range(m)):
image[x, y]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment