Cycles of a permutation: Difference between revisions

Content added Content deleted
(New post.)
(Minor code improvement.)
Line 144: Line 144:
class Permutation {
class Permutation {
public:
public:
// Initialise the length of the strings to be permuted.
Permutation(uint32_t letters_size) : letters_count(letters_size) { }


// Return the permutation in one line form that transforms the string 'source' into the string 'destination'.
// Return the permutation in one line form that transforms the string 'source' into the string 'destination'.
Line 188: Line 190:
// Return the one line notation of the permutation given in cycle form.
// Return the one line notation of the permutation given in cycle form.
const One_line cycles_to_one_line(Cycles& cycles) {
const One_line cycles_to_one_line(Cycles& cycles) {
One_line one_line(LETTERS_COUNT);
One_line one_line(letters_count);
std::iota(one_line.begin(), one_line.end(), 1);
std::iota(one_line.begin(), one_line.end(), 1);
for ( uint32_t number = 1; number <= LETTERS_COUNT; ++number ) {
for ( uint32_t number = 1; number <= letters_count; ++number ) {
for ( One_line& cycle : cycles ) {
for ( One_line& cycle : cycles ) {
const int32_t index = index_of(cycle, number);
const int32_t index = index_of(cycle, number);
Line 229: Line 231:
Cycles combined_cycles;
Cycles combined_cycles;
std::unordered_set<uint32_t> used;
std::unordered_set<uint32_t> used;
for ( uint32_t number = 1; used.size() < LETTERS_COUNT; ++number ) {
for ( uint32_t number = 1; used.size() < letters_count; ++number ) {
if ( std::find(used.begin(), used.end(), number) == used.end() ) {
if ( std::find(used.begin(), used.end(), number) == used.end() ) {
uint32_t combined = next(cycles_two, next(cycles_one, number));
uint32_t combined = next(cycles_two, next(cycles_one, number));
Line 317: Line 319:
}
}


const uint32_t LETTERS_COUNT = 15;
const uint32_t letters_count;
};
};


Line 348: Line 350:


auto previous_day = [day_names] (const uint32_t& today) {
auto previous_day = [day_names] (const uint32_t& today) {
return ( day_names.size() + today - 1 ) % day_names.size();
return ( day_names.size() + today - 1 ) % day_names.size();
};
};


Permutation permutation;
Permutation permutation(letters[0].length());


std::cout << "On Thursdays Alf and Betty should rearrange their letters using these cycles:" << std::endl;
std::cout << "On Thursdays Alf and Betty should rearrange their letters using these cycles:" << std::endl;