Skip to content

Commit b4ec369

Browse files
solves messages order
1 parent e4971a5 commit b4ec369

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ The C++ Domain is further Divided into the following sub-domains.
7777
| [Hotel Prices](https://www.hackerrank.com/challenges/hotel-prices) | Medium | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)](debugging/hotel_prices.cpp)
7878
| [Cpp Exception handling](https://www.hackerrank.com/challenges/cpp-exception-handling) | Medium | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)](debugging/cpp-exception-handling.cpp)
7979
| [Overloading Ostream Operator](https://www.hackerrank.com/challenges/overloading-ostream-operator) | Medium | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)](debugging/overloading_ostream_operator.cpp)
80-
| [Messages Order](https://www.hackerrank.com/challenges/messages-order) | Medium | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)](debugging/)
80+
| [Messages Order](https://www.hackerrank.com/challenges/messages-order) | Medium | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)](debugging/messages_order.cpp)
8181

8282
### Other Concepts ![problems-solved](https://img.shields.io/badge/Problems%20Solved-8/8-1abc9c.svg)
8383

debugging/messages_order.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
class Message {
8+
private:
9+
string text;
10+
static int id;
11+
int current_id;
12+
public:
13+
Message() { current_id = ++id; }
14+
Message(string t){ current_id = ++id; text=t; }
15+
const string& get_text() {
16+
return text;
17+
}
18+
// overloaded < operator
19+
bool operator < (const Message& M2) {
20+
if(current_id < M2.current_id)
21+
return true;
22+
else
23+
return false;
24+
}
25+
};
26+
int Message::id = 0;
27+
28+
class MessageFactory {
29+
public:
30+
MessageFactory() {}
31+
Message create_message(const string& text) {
32+
Message m = Message(text);
33+
return m;
34+
}
35+
};
36+
37+
class Recipient {
38+
public:
39+
Recipient() {}
40+
void receive(const Message& msg) {
41+
messages_.push_back(msg);
42+
}
43+
void print_messages() {
44+
fix_order();
45+
for (auto& msg : messages_) {
46+
cout << msg.get_text() << endl;
47+
}
48+
messages_.clear();
49+
}
50+
private:
51+
void fix_order() {
52+
sort(messages_.begin(), messages_.end());
53+
}
54+
vector<Message> messages_;
55+
};
56+
57+
class Network {
58+
public:
59+
static void send_messages(vector<Message> messages, Recipient& recipient) {
60+
// simulates the unpredictable network, where sent messages might arrive in unspecified order
61+
random_shuffle(messages.begin(), messages.end());
62+
for (auto msg : messages) {
63+
recipient.receive(msg);
64+
}
65+
}
66+
};
67+
68+
int main() {
69+
MessageFactory message_factory;
70+
Recipient recipient;
71+
vector<Message> messages;
72+
string text;
73+
while (getline(cin, text)) {
74+
messages.push_back(message_factory.create_message(text));
75+
}
76+
Network::send_messages(messages, recipient);
77+
recipient.print_messages();
78+
}

0 commit comments

Comments
 (0)