Beispiel #1
0
int main(void){
	int n;
	while(scanf("%i", &n)==1){
		perms(n);
	}
	return 0;
}
/*---------------------------------------------------------------
 Routine : perms
 Purpose : Generate permutations, r objects from n.
 (objects are integers in array a, length n)
 f is called for each permutation.
 The arguments for f are as for vec_print above.
 
 This routine is recursive.
---------------------------------------------------------------*/
void
  perms(
    int *a,     /* IN  - input array                              */
    int  n,     /* IN  - length of a                              */
    int  r,     /* IN  - length of permutations to take from a    */
    int *z,     /* OUT - array to put permutation into            */
    int  zi,    /* IN  - index into z                             */
    void f( int *, int, void * ),   /* ?     function to call for each perm generated */
    void *p)    /* ?     user data for f()                        */
{
    int i;

    if ( r == 0 ) {
        f(z, zi, p);
        return;
    } else {
        for(i = 0; i < n; i++) {
            z[zi] = a[i];
            swap(a, i, 0);
            perms(a+1, n-1, r-1, z, zi+1, f, p);
            swap(a, i, 0);
        }
    }

} /* perms */
Beispiel #3
0
std::unique_ptr<TorchStage> Transpose::loadFromFile(std::ifstream& file) {
  int32_t num_permutations;
  file.read((char*)(&num_permutations), sizeof(num_permutations));
  std::unique_ptr<int32_t[]> perms(new int32_t[num_permutations * 2]);
  file.read((char*)(perms.get()), sizeof(perms[0]) * num_permutations * 2);
  // But we don't really use it...
  return std::unique_ptr<TorchStage>(new Transpose());
}
// I'm certain this function could be cleaned up, but it will require some
// serious brain storming, so it is a work in progress....
//
// The idea is I want to generate all possible permutations of RPN stacks given
// a set of 4 input integers.
void perms(std::set<int> possible, std::set<int>& all, std::stack<rpn_item> e, std::vector<rpn_item> const& ops) {
    // Once the stack is finished evaluate and add to the set
    if (e.size() == 7) {
        auto test = eval_rpn_stack(e);
        if (test > 0 && test != BAD_DIVIDE)
            all.insert(test);
        return;
    }

    // The very bottom of the stack MUST be an operator
    if (e.size() == 0) {
        for (auto i : ops) {
            auto tmp = e;
            tmp.push(i);
            perms(possible, all, tmp, ops);
        }
    }

    // the TOP 2 items in the stack MUST be integers
    if (e.size() >= 5) {
        for (auto i : possible) {
            auto tmp = possible;
            auto tmp_e = e;
            tmp.erase(i);
            tmp_e.push(i);
            perms(tmp, all, tmp_e, ops);
        }
    }

    // This is where things get messy...
    // I enumerate all possible stack configurations
    // There is no visible pattern that I can see and easily program so for now
    // this will remain a mess
    for (auto n1 = possible.begin(); n1 != possible.end(); ++n1) {
        for (auto n2 = std::next(n1); n2 != possible.end(); ++n2) {
            auto tmp_p = possible;
            tmp_p.erase(*n1); tmp_p.erase(*n2);
            for (auto op1 = ops.begin(); op1 != ops.end(); ++op1) {
                for (auto op2 = ops.begin(); op2 != ops.end(); ++op2) {
                    // op op n n
                    auto n = push_helper(e, {*op1, *op2, *n1, *n2});
                    perms(tmp_p, all, n, ops);
                    // op n op n
                    n = push_helper(e, {*op1, *n1, *op2, *n2});
                    perms(tmp_p, all, n, ops);
                    // n op n op
                    n = push_helper(e, {*n1, *op1, *n2, *op2});
                    perms(tmp_p, all, n, ops);
                    // op n n op
                    n = push_helper(e, {*op1, *n1, *n2, *op2});
                    perms(tmp_p, all, n, ops);
                    // n op op n
                    n = push_helper(e, {*n1, *op1, *op2, *n2});
                    perms(tmp_p, all, n, ops);
                }
            }
        }
    }
}
Beispiel #5
0
		vector< vector<T> > permutations(vector<T> &v) {

			unsigned int next = 0;
			vector< vector<T> > perms( factorial(v.size()) );

			sort(v.begin(), v.end());

			do {
				perms[next] = v;
				next++;
			} while(next_permutation(v.begin(), v.end()));

			return perms;
		}
Beispiel #6
0
std::string DirectoryList::Permissions(const fs::Status& status)
{
  std::string perms(10, '-');
  
  if (status.IsDirectory()) perms[0] = 'd';
  else if (status.IsSymLink()) perms[0] = 'l';
  
  const struct stat& native = status.Native();
  
  if (native.st_mode & S_IRUSR) perms[1] = 'r';
  if (native.st_mode & S_IWUSR) perms[2] = 'w';
  if (native.st_mode & S_IXUSR) perms[3] = 'x';
  if (native.st_mode & S_IRGRP) perms[4] = 'r';
  if (native.st_mode & S_IWGRP) perms[5] = 'w';
  if (native.st_mode & S_IXGRP) perms[6] = 'x';
  if (native.st_mode & S_IROTH) perms[7] = 'r';
  if (native.st_mode & S_IWOTH) perms[8] = 'w';
  if (native.st_mode & S_IXOTH) perms[9] = 'x';
  
  return perms;
}
int main() {
    rpn_item add([](const int x, const int y) { return x + y; });
    rpn_item sub([](const int x, const int y) { return x - y; });
    rpn_item mult([](const int x, const int y) { return x * y; });
    rpn_item div([](const int x, const int y) {
            if (y == 0 || x % y != 0)
                return BAD_DIVIDE;
            return x / y;
        }
    );
    std::vector<rpn_item> ops {add, sub, mult, div};
    int max = 0;
    std::set<int> best;

    for (int i = 1; i <= 9; ++i) {
        for (int j = i + 1; j <= 9; ++j) {
            for (int k = j + 1; k <= 9; ++k) {
                for (int l = k + 1; l <= 9; ++l) {
                    std::set<int> all;
                    std::set<int> poss{i, j, k, l};
                    std::stack<rpn_item> equ;
                    perms(poss, all, equ, ops);
                    auto test = run_length(all);
                    if (test >= max) {
                        max = test;
                        best = std::move(poss);
                    }
                }
            }
        }
    }

    std::cout << max << std::endl;
    for (auto i : best)
        std::cout << i;
    std::cout << std::endl;
    return 0;
}
Beispiel #8
0
ATF_TC_BODY(stat_perms, tc)
{
    atf_fs_path_t p;
    atf_fs_stat_t st;

    create_file("reg", 0);

    RE(atf_fs_path_init_fmt(&p, "reg"));

#define perms(ur, uw, ux, gr, gw, gx, othr, othw, othx) \
    { \
        RE(atf_fs_stat_init(&st, &p)); \
        ATF_REQUIRE(atf_fs_stat_is_owner_readable(&st) == ur); \
        ATF_REQUIRE(atf_fs_stat_is_owner_writable(&st) == uw); \
        ATF_REQUIRE(atf_fs_stat_is_owner_executable(&st) == ux); \
        ATF_REQUIRE(atf_fs_stat_is_group_readable(&st) == gr); \
        ATF_REQUIRE(atf_fs_stat_is_group_writable(&st) == gw); \
        ATF_REQUIRE(atf_fs_stat_is_group_executable(&st) == gx); \
        ATF_REQUIRE(atf_fs_stat_is_other_readable(&st) == othr); \
        ATF_REQUIRE(atf_fs_stat_is_other_writable(&st) == othw); \
        ATF_REQUIRE(atf_fs_stat_is_other_executable(&st) == othx); \
        atf_fs_stat_fini(&st); \
    }

    chmod("reg", 0000);
    perms(false, false, false, false, false, false, false, false, false);

    chmod("reg", 0001);
    perms(false, false, false, false, false, false, false, false, true);

    chmod("reg", 0010);
    perms(false, false, false, false, false, true, false, false, false);

    chmod("reg", 0100);
    perms(false, false, true, false, false, false, false, false, false);

    chmod("reg", 0002);
    perms(false, false, false, false, false, false, false, true, false);

    chmod("reg", 0020);
    perms(false, false, false, false, true, false, false, false, false);

    chmod("reg", 0200);
    perms(false, true, false, false, false, false, false, false, false);

    chmod("reg", 0004);
    perms(false, false, false, false, false, false, true, false, false);

    chmod("reg", 0040);
    perms(false, false, false, true, false, false, false, false, false);

    chmod("reg", 0400);
    perms(true, false, false, false, false, false, false, false, false);

    chmod("reg", 0644);
    perms(true, true, false, true, false, false, true, false, false);

    chmod("reg", 0755);
    perms(true, true, true, true, false, true, true, false, true);

    chmod("reg", 0777);
    perms(true, true, true, true, true, true, true, true, true);

#undef perms

    atf_fs_path_fini(&p);
}