Last active
October 2, 2023 22:40
-
-
Save ChristopherBilg/aceebd0491f55ba7cf56fc018be53d10 to your computer and use it in GitHub Desktop.
r/DailyProgrammer Packet Assembler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!usr/bin/env python3 | |
""" | |
This script is the r/DailyProgrammer Easy Challenge #333 titled | |
Packet Assembler. | |
Author: Christopher Bilger | |
""" | |
class Packet(): | |
""" | |
This class handles the packet identifiers along with the packet contents. | |
@param message_id: Which message this packet is a part of. | |
@param packet_id: Index of this specific packet in the message. | |
@param total_packets: Total number of packets in the message. | |
@param packet_contents: Contents of the packet. (String) | |
""" | |
def __init__(self, message_id, packet_id, total_packets, packet_contents): | |
self.message_id = message_id | |
self.packet_id = packet_id | |
self.total_packets = total_packets | |
self.packet_contents = packet_contents | |
def get_message_id(self): | |
""" | |
Getter method for the Packet.message_id | |
@return ID number for this over-arcing message. | |
""" | |
return int(self.message_id) | |
def get_packet_id(self): | |
""" | |
Getter method for the Packet.packet_id | |
@return ID number for the individual packet of the message. | |
""" | |
return int(self.packet_id) | |
def get_total_packets(self): | |
""" | |
Getter method for the Packet.total_packets | |
@return Total number of individual packets in the message. | |
""" | |
return int(self.total_packets) | |
def get_packet_contents(self): | |
""" | |
Getter method for the Packet.message_id | |
@return String of the contents of the packet itself. | |
""" | |
return self.packet_contents | |
def main(): | |
""" | |
Function that will run the actual code to sort the incoming packets. | |
""" | |
packets = [] | |
with open("packets.txt", "r", encoding='utf-8') as all_packets: | |
for packet in all_packets.readlines(): | |
m_id = int(packet.split(" ")[0]) | |
p_id = int(packet.split(" ")[4]) | |
if p_id < 10: | |
tot_packets = int(packet.split(" ")[7]) | |
else: | |
tot_packets = int(packet.split(" ")[6]) | |
message = str(packet[16:-1]) | |
packets.append(Packet(m_id, p_id, tot_packets, message)) | |
packets.sort(key=lambda x: (x.message_id, x.packet_id)) | |
for packet in packets: | |
m_id = packet.get_message_id() | |
message = packet.get_packet_contents() | |
if packet.get_packet_id() < 10: | |
p_id = str(packet.get_packet_id()) + " " | |
else: | |
p_id = str(packet.get_packet_id()) | |
if packet.get_total_packets() < 10: | |
tot_packets = str(packet.get_total_packets()) + " " | |
else: | |
tot_packets = str(packet.get_total_packets()) | |
print("{} {} {} {}".format(m_id, p_id, tot_packets, message)) | |
if __name__ == '__main__': | |
main() | |
#!usr/bin/env python3 | |
""" | |
This script is the r/DailyProgrammer Easy Challenge #333 titled | |
Packet Assembler. | |
Author: Christopher Bilger | |
""" | |
class Packet(): | |
""" | |
This class handles the packet identifiers along with the packet contents. | |
@param message_id: Which message this packet is a part of. | |
@param packet_id: Index of this specific packet in the message. | |
@param total_packets: Total number of packets in the message. | |
@param packet_contents: Contents of the packet. (String) | |
""" | |
def __init__(self, message_id, packet_id, total_packets, packet_contents): | |
self.message_id = message_id | |
self.packet_id = packet_id | |
self.total_packets = total_packets | |
self.packet_contents = packet_contents | |
def get_message_id(self): | |
""" | |
Getter method for the Packet.message_id | |
@return ID number for this over-arcing message. | |
""" | |
return int(self.message_id) | |
def get_packet_id(self): | |
""" | |
Getter method for the Packet.packet_id | |
@return ID number for the individual packet of the message. | |
""" | |
return int(self.packet_id) | |
def get_total_packets(self): | |
""" | |
Getter method for the Packet.total_packets | |
@return Total number of individual packets in the message. | |
""" | |
return int(self.total_packets) | |
def get_packet_contents(self): | |
""" | |
Getter method for the Packet.message_id | |
@return String of the contents of the packet itself. | |
""" | |
return self.packet_contents | |
def main(): | |
""" | |
Function that will run the actual code to sort the incoming packets. | |
""" | |
packets = [] | |
with open("packets.txt", "r", encoding='utf-8') as all_packets: | |
for packet in all_packets.readlines(): | |
m_id = int(packet.split(" ")[0]) | |
p_id = int(packet.split(" ")[4]) | |
if p_id < 10: | |
tot_packets = int(packet.split(" ")[7]) | |
else: | |
tot_packets = int(packet.split(" ")[6]) | |
message = str(packet[16:-1]) | |
packets.append(Packet(m_id, p_id, tot_packets, message)) | |
packets.sort(key=lambda x: (x.message_id, x.packet_id)) | |
for packet in packets: | |
m_id = packet.get_message_id() | |
message = packet.get_packet_contents() | |
if packet.get_packet_id() < 10: | |
p_id = str(packet.get_packet_id()) + " " | |
else: | |
p_id = str(packet.get_packet_id()) | |
if packet.get_total_packets() < 10: | |
tot_packets = str(packet.get_total_packets()) + " " | |
else: | |
tot_packets = str(packet.get_total_packets()) | |
print("{} {} {} {}".format(m_id, p_id, tot_packets, message)) | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7469 1 7 believe. Attack ships on fire off the | |
9949 6 10 He's a silent guardian. | |
2997 9 19 Force is a pathway to many abilities some | |
6450 2 11 is a vestige of the vox populi, now vacant, vanished. However, this valorous | |
6450 10 11 | |
6450 8 11 veers most verbose, so let me simply add that it's my very good honour to meet | |
6450 5 11 and voracious violation of volition! The only verdict is vengeance; a vendetta | |
9949 1 10 Because he's the hero Gotham deserves, | |
6450 1 11 and villain by the vicissitudes of fate. This visage, no mere veneer of vanity, | |
2997 13 19 he did. Unfortunately, he taught his | |
9949 8 10 A Dark Knight. | |
1938 4 17 by the iniquities of the selfish and the | |
1938 0 17 You read the Bible, Brett? Well there's | |
2997 0 19 Did you ever hear the tragedy of Darth | |
2997 1 19 Plagueis the Wise? I thought not. It's not a | |
1938 8 17 of darkness, for he is truly is brother's | |
2997 14 19 apprentice everything he knew, then his | |
6450 3 11 visitation of a bygone vexation stands vivified, and has vowed to vanquish these | |
1938 12 17 who attempt to poison and destroy my | |
6450 9 11 you and you may call me V. | |
7469 2 7 shoulder of Orion. I watched C-beams | |
2997 10 19 consider to be unnatural. He became so | |
1938 1 17 this passage I got memorized, sorta fits | |
2997 5 19 Force to influence the midichlorians to | |
1938 6 17 in the name of charity and good will, | |
7469 0 7 I've seen things you people wouldn't | |
9949 4 10 Because he can take it. | |
6450 7 11 vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage | |
9949 0 10 We have to chase him. | |
9949 7 10 A watchful protector. | |
2997 3 19 legend. Darth Plagueis was a Dark Lord of the | |
6450 6 11 held as a votive, not in vain, for the value and veracity of such shall one day | |
2997 8 19 cared about from dying. The dark side of the | |
1938 10 17 And I will strike down upon thee with | |
1938 11 17 great vengeance and furious anger those | |
1938 7 17 shepherds the weak through the valley | |
1938 2 17 this occasion. Ezekiel 25:17? "The path | |
2997 18 19 | |
9949 9 10 | |
1938 14 17 the Lord when I lay my vengeance upon | |
1938 15 17 thee." | |
1938 9 17 keeper and the finder of lost children. | |
1938 13 17 brothers. And you will know my name is | |
9949 2 10 but not the one it needs right now. | |
2997 16 19 he could have others from death, but not | |
2997 7 19 dark side that he could even keep the once he | |
1938 5 17 tyranny of evil men. Blessed is he who, | |
2997 17 19 himself. | |
2997 6 19 create life...He had such a knowledge of the | |
2997 12 19 losing his power. Which eventually, of course, | |
7469 4 7 Gate. All those moments will be lost | |
2997 2 19 story the Jedi would tell you. It's a Sith | |
1938 16 17 | |
2997 4 19 Sith so powerful and so wise, he could use the | |
1938 3 17 of the righteous man is beset on all sides | |
2997 11 19 powerful...The only thing he was afraid of was | |
7469 6 7 | |
2997 15 19 apprentice killed him in his sleep. Ironic, | |
7469 5 7 in time, like tears in rain. Time to die. | |
9949 3 10 So we'll hunt him. | |
7469 3 7 glitter in the dark near the Tannhäuser | |
6450 4 11 venal and virulent vermin vanguarding vice and vouchsafing the violently vicious | |
6450 0 11 Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim | |
9949 5 10 Because he's not a hero. | |
7469 1 7 believe. Attack ships on fire off the | |
9949 6 10 He's a silent guardian. | |
2997 9 19 Force is a pathway to many abilities some | |
6450 2 11 is a vestige of the vox populi, now vacant, vanished. However, this valorous | |
6450 10 11 | |
6450 8 11 veers most verbose, so let me simply add that it's my very good honour to meet | |
6450 5 11 and voracious violation of volition! The only verdict is vengeance; a vendetta | |
9949 1 10 Because he's the hero Gotham deserves, | |
6450 1 11 and villain by the vicissitudes of fate. This visage, no mere veneer of vanity, | |
2997 13 19 he did. Unfortunately, he taught his | |
9949 8 10 A Dark Knight. | |
1938 4 17 by the iniquities of the selfish and the | |
1938 0 17 You read the Bible, Brett? Well there's | |
2997 0 19 Did you ever hear the tragedy of Darth | |
2997 1 19 Plagueis the Wise? I thought not. It's not a | |
1938 8 17 of darkness, for he is truly is brother's | |
2997 14 19 apprentice everything he knew, then his | |
6450 3 11 visitation of a bygone vexation stands vivified, and has vowed to vanquish these | |
1938 12 17 who attempt to poison and destroy my | |
6450 9 11 you and you may call me V. | |
7469 2 7 shoulder of Orion. I watched C-beams | |
2997 10 19 consider to be unnatural. He became so | |
1938 1 17 this passage I got memorized, sorta fits | |
2997 5 19 Force to influence the midichlorians to | |
1938 6 17 in the name of charity and good will, | |
7469 0 7 I've seen things you people wouldn't | |
9949 4 10 Because he can take it. | |
6450 7 11 vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage | |
9949 0 10 We have to chase him. | |
9949 7 10 A watchful protector. | |
2997 3 19 legend. Darth Plagueis was a Dark Lord of the | |
6450 6 11 held as a votive, not in vain, for the value and veracity of such shall one day | |
2997 8 19 cared about from dying. The dark side of the | |
1938 10 17 And I will strike down upon thee with | |
1938 11 17 great vengeance and furious anger those | |
1938 7 17 shepherds the weak through the valley | |
1938 2 17 this occasion. Ezekiel 25:17? "The path | |
2997 18 19 | |
9949 9 10 | |
1938 14 17 the Lord when I lay my vengeance upon | |
1938 15 17 thee." | |
1938 9 17 keeper and the finder of lost children. | |
1938 13 17 brothers. And you will know my name is | |
9949 2 10 but not the one it needs right now. | |
2997 16 19 he could have others from death, but not | |
2997 7 19 dark side that he could even keep the once he | |
1938 5 17 tyranny of evil men. Blessed is he who, | |
2997 17 19 himself. | |
2997 6 19 create life...He had such a knowledge of the | |
2997 12 19 losing his power. Which eventually, of course, | |
7469 4 7 Gate. All those moments will be lost | |
2997 2 19 story the Jedi would tell you. It's a Sith | |
1938 16 17 | |
2997 4 19 Sith so powerful and so wise, he could use the | |
1938 3 17 of the righteous man is beset on all sides | |
2997 11 19 powerful...The only thing he was afraid of was | |
7469 6 7 | |
2997 15 19 apprentice killed him in his sleep. Ironic, | |
7469 5 7 in time, like tears in rain. Time to die. | |
9949 3 10 So we'll hunt him. | |
7469 3 7 glitter in the dark near the Tannhäuser | |
6450 4 11 venal and virulent vermin vanguarding vice and vouchsafing the violently vicious | |
6450 0 11 Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim | |
9949 5 10 Because he's not a hero. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment