Question
Hello, could please help me with my code? i create disck.hpp file it compile but did not give me the right answer, not matching with
Hello, could please help me with my code? i create disck.hpp file it compile but did not give me the right answer, not matching with the discl_test.cpp file, i am sending you my hpp file and cpp file if you make a correction for me to work please?
thank you
// disks.hpp // Definitions for two algorithms that each solve the alternating disks // problem. // As provided, this header has four functions marked with TODO comments. // You need to write in your own implementation of these functions. // ///////////////////////////////////////////////////////////////////////////////
#pragma once
#include #include #include #include #include #include
// State of one disk, either light or dark. enum disk_color { DISK_LIGHT, DISK_DARK };
// Data structure for the state of one row of disks. class disk_state { private: std::vector _colors;
public:
disk_state(size_t light_count) : _colors(light_count * 2, DISK_LIGHT) {
assert(light_count > 0);
for (size_t i = 0; i < _colors.size(); i += 2) { _colors[i] = DISK_DARK; } }
// 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 < total_count()); }
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 << "L"; } else { ss << "D"; }
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 dark, the second disk at index 1 // is light, and so on for the entire row of disks. bool is_alternating() const { for (size_t i = 0; i < total_count(); i += 2) { if (get(i) != DISK_DARK) return false; } for (size_t i = 1; i < total_count(); i += 2) { if (get(i) != DISK_LIGHT) return false; } return true; }
// Return true when this disk_state is fully sorted, with all light disks // on the left (low indices) and all dark disks on the right (high // indices). bool is_sorted() const { for (size_t i = 0; i < total_count() / 2; i++) { if (get(i) != DISK_LIGHT) return false; } for (size_t i = total_count() / 2; i < total_count(); i++) { if (get(i) != DISK_DARK) return false; } return true; } };
// 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) { disk_state after = before; // create a new disk_state object unsigned counter = 0; /* go through the whole disk_state, from the leftmost to the rightmost, swap two adjacent disks only if the left is dark and the right is light, it will repeat until there are no more disks to move. */ for (size_t i = 0; i < after.total_count() / 2; i++) { // for (size_t j = 0; j < after.total_count() - 1; j++) { if (after.get(j) == DISK_DARK && after.get(j + 1) == DISK_LIGHT) { after.swap(j); counter++; // record the number of swap } } } return sorted_disks(after, counter); // return the sorted disk and the counter }
// Algorithm that sorts disks using the lawnmower algorithm. sorted_disks sort_lawnmower(const disk_state& before) { disk_state after = before; // create a new disk_state object size_t total_count = after.total_count(); unsigned swap_count = 0; /* go through the whole disk_state, from the leftmost to the rightmost, then go back, swap two adjacent disks only if the left is dark and the right is light, it will repeat until there are no more disks to move. */ for (size_t i = 0; i < (total_count + 3) / 4; i++) { for (size_t j = 0; j < total_count - 1; j++) { if (after.get(j) == DISK_DARK && after.get(j + 1) == DISK_LIGHT) { after.swap(j); swap_count++; } } for (size_t k = total_count - 1; k > 0; k--) { if (after.get(k - 1) == DISK_DARK && after.get(k) == DISK_LIGHT) { after.swap(k - 1); swap_count++; // record the number of swap } } } return sorted_disks(after, swap_count); // return the sorted disk and the counter }
Step 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