June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,32 +1,29 @@
|
|||
void
|
||||
order(integer m, ...)
|
||||
order(list a, list b)
|
||||
{
|
||||
integer i, j;
|
||||
integer j;
|
||||
record r;
|
||||
text s;
|
||||
|
||||
ocall(o_, 0, 1, m, " ");
|
||||
a.ucall(o_, 0, " ");
|
||||
|
||||
o_("| ");
|
||||
|
||||
i = (j = m) + 1;
|
||||
while (i < count()) {
|
||||
r[s = $i] += 1;
|
||||
for (, s in b) {
|
||||
r[s] += 1;
|
||||
o_(s, " ");
|
||||
i += 1;
|
||||
}
|
||||
|
||||
o_("->");
|
||||
|
||||
i = 0;
|
||||
do {
|
||||
i += 1;
|
||||
if ((r[s = $i] += -1) < 0) {
|
||||
j = -1;
|
||||
for (, s in a) {
|
||||
if ((r[s] -= 1) < 0) {
|
||||
o_(" ", s);
|
||||
} else {
|
||||
o_(" ", $(j += 1));
|
||||
o_(" ", b[j += 1]);
|
||||
}
|
||||
} while (i < m);
|
||||
}
|
||||
|
||||
o_newline();
|
||||
}
|
||||
|
|
@ -34,13 +31,13 @@ order(integer m, ...)
|
|||
integer
|
||||
main(void)
|
||||
{
|
||||
order(6, "the", "cat", "sat", "on", "the", "mat", "mat", "cat");
|
||||
order(6, "the", "cat", "sat", "on", "the", "mat", "cat", "mat");
|
||||
order(9, "A", "B", "C", "A", "B", "C", "A", "B", "C", "C", "A", "C", "A");
|
||||
order(9, "A", "B", "C", "A", "B", "D", "A", "B", "E", "E", "A", "D", "A");
|
||||
order(2, "A", "B", "B");
|
||||
order(2, "A", "B", "B", "A");
|
||||
order(4, "A", "B", "B", "A", "B", "A");
|
||||
order(list("the", "cat", "sat", "on", "the", "mat"), list("mat", "cat"));
|
||||
order(list("the", "cat", "sat", "on", "the", "mat"), list("cat", "mat"));
|
||||
order(list("A", "B", "C", "A", "B", "C", "A", "B", "C"), list("C", "A", "C", "A"));
|
||||
order(list("A", "B", "C", "A", "B", "D", "A", "B", "E"), list("E", "A", "D", "A"));
|
||||
order(list("A", "B"), list("B"));
|
||||
order(list("A", "B"), list("B", "A"));
|
||||
order(list("A", "B", "B", "A"), list("B", "A"));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
Author: Kevin Bacon [haxifix (@gmail.com)]
|
||||
Date: 2018-05-19
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
template <typename T>
|
||||
void print(const std::vector<T> v) {
|
||||
std::cout << "{ ";
|
||||
for (const auto& e : v) {
|
||||
std::cout << e << " ";
|
||||
}
|
||||
std::cout << "}";
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
auto orderDisjointArrayItems(std::vector<T> M, std::vector<T> N) {
|
||||
std::vector<T*> M_p(std::size(M));
|
||||
for (auto i = 0; i < std::size(M_p); ++i) {
|
||||
M_p[i] = &M[i];
|
||||
}
|
||||
for (auto e : N) {
|
||||
auto i = std::find_if(std::begin(M_p), std::end(M_p), [e](auto c) -> bool {
|
||||
if (c != nullptr) {
|
||||
if (*c == e) return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
if (i != std::end(M_p)) {
|
||||
*i = nullptr;
|
||||
}
|
||||
}
|
||||
for (auto i = 0; i < std::size(N); ++i) {
|
||||
auto j = std::find_if(std::begin(M_p), std::end(M_p), [](auto c) -> bool {
|
||||
return c == nullptr;
|
||||
});
|
||||
if (j != std::end(M_p)) {
|
||||
*j = &M[std::distance(std::begin(M_p), j)];
|
||||
**j = N[i];
|
||||
}
|
||||
}
|
||||
return M;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::vector<std::vector<std::vector<std::string>>> l = {
|
||||
{ { "the", "cat", "sat", "on", "the", "mat" }, { "mat", "cat" } },
|
||||
{ { "the", "cat", "sat", "on", "the", "mat" },{ "cat", "mat" } },
|
||||
{ { "A", "B", "C", "A", "B", "C", "A", "B", "C" },{ "C", "A", "C", "A" } },
|
||||
{ { "A", "B", "C", "A", "B", "D", "A", "B", "E" },{ "E", "A", "D", "A" } },
|
||||
{ { "A", "B" },{ "B" } },
|
||||
{ { "A", "B" },{ "B", "A" } },
|
||||
{ { "A", "B", "B", "A" },{ "B", "A" } }
|
||||
};
|
||||
for (const auto& e : l) {
|
||||
std::cout << "M: ";
|
||||
print(e[0]);
|
||||
std::cout << ", N: ";
|
||||
print(e[1]);
|
||||
std::cout << ", M': ";
|
||||
auto res = orderDisjointArrayItems<std::string>(e[0], e[1]);
|
||||
print(res);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
std::cin.ignore();
|
||||
std::cin.get();
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
sub order-disjoint-list-items(\M, \N) {
|
||||
my \bag = N.BagHash;
|
||||
M.map: { bag{$_}-- ?? N.shift !! $_ }
|
||||
}
|
||||
|
||||
# Testing:
|
||||
|
||||
for q:to/---/.comb(/ [\S+]+ % ' ' /).map({[.words]})
|
||||
the cat sat on the mat mat cat
|
||||
the cat sat on the mat cat mat
|
||||
A B C A B C A B C C A C A
|
||||
A B C A B D A B E E A D A
|
||||
A B B
|
||||
A B B A
|
||||
A B B A B A
|
||||
X X Y X
|
||||
A X Y A
|
||||
---
|
||||
-> $m, $n { say "\n$m ==> $n\n", order-disjoint-list-items($m, $n) }
|
||||
Loading…
Add table
Add a link
Reference in a new issue