Question
Need help implementing these boolean functions and algorithm problems #pragma once #include #include #include #include #include #include // State of one disk, either light or
Need help implementing these boolean functions and algorithm problems
#pragma once
#include
#include
#include
#include
#include
#include
// State of one disk, either light or dark.
enum disk_color { DISK_DARK, DISK_LIGHT };
// Data structure for the state of one row of disks.
class disk_state {
private:
std::vector
public:
disk_state(size_t light_count)
: _colors(light_count * 2, DISK_DARK) {
assert(light_count > 0);
for (size_t i = 0; i
_colors[i] = DISK_LIGHT;
}
}
// Equality operator for unit tests.
bool operator== (const disk_state& rhs) const {
return std::equal(_colors.begin(), _colors.end(), rhs._colors.begin());
}
size_t total_count() const {
return _colors.size();
}
size_t light_count() const {
return total_count() / 2;
}
size_t dark_count() const {
return light_count();
}
bool is_index(size_t i) const {
return (i
}
disk_color get(size_t index) const {
assert(is_index(index));
return _colors[index];
}
void swap(size_t left_index) {
assert(is_index(left_index));
auto right_index = left_index + 1;
assert(is_index(right_index));
std::swap(_colors[left_index], _colors[right_index]);
}
std::string to_string() const {
std::stringstream ss;
bool first = true;
for (auto color : _colors) {
if (!first) {
ss
}
if (color == DISK_LIGHT) {
ss
} else {
ss
}
first = false;
}
return ss.str();
}
// Return true when this disk_state is in alternating format. That means
// that the first disk at index 0 is light, the second disk at index 1
// is dark, and so on for the entire row of disks.
bool is_alternating() const {
// TODO: Write code for this function, including rewriting the return
// statement, and then delete these comments.
return false;
}
// Return true when this disk_state is fully sorted, with all light disks
// on the right (high indices) and all dark disks on the left (low
// indices).
bool is_sorted() const {
// TODO: Write code for this function, including rewriting the return
// statement, and then delete these comments.
return false;
}
};
// Data structure for the output of the alternating disks problem. That
// includes both the final disk_state, as well as a count of the number
// of swaps performed.
class sorted_disks {
private:
disk_state _after;
unsigned _swap_count;
public:
sorted_disks(const disk_state& after, unsigned swap_count)
: _after(after), _swap_count(swap_count) { }
sorted_disks(disk_state&& after, unsigned swap_count)
: _after(after), _swap_count(swap_count) { }
const disk_state& after() const {
return _after;
}
unsigned swap_count() const {
return _swap_count;
}
};
// Algorithm that sorts disks using the left-to-right algorithm.
sorted_disks sort_left_to_right(const disk_state& before) {
// TODO: Write code for this function, including rewriting the return
// statement, and then delete these comments.
return sorted_disks(before, 0);
}
// Algorithm that sorts disks using the lawnmower algorithm.
sorted_disks sort_lawnmower(const disk_state& before) {
// TODO: Write code for this function, including rewriting the return
// statement, and then delete these comments.
return sorted_disks(before, 0);
}
The left-to-right algorithm It starts with the leftmost disk and proceeds to the right, doing the swaps as necessary. Now we have one dark disk at the left-hand end and the light disk at the right-hand end. Once it reaches the right-hand end, it goes back to the leftmost disk and proceeds to the right, doing the swaps as necessary. It repeats until there are no more disks to move. For the example shown, the exact list of disks changes as follows at the end of each run (left-to-right): The lawnmower algorithm It starts with the leftmost disk and proceeds to the right, doing the swaps as necessary. Now we have one dark disk at the left-hand end and the light disk at the right-hand end. Once it reaches the right-hand end, it starts with the disk before the rightmost disk and procceds to the left, doing the swaps as necessary, until it reaches the disk before the left-hand end. It repeats until there are no more disks to move. For the example shown, the exact list of disks changes as follows at the end of each run, half round-tripStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started