Solved Unscramble UDP Word Jumble

Solved: Jumble! In your job as a network engineer you’ve been asked to write..

Following question that is presented to the university students during their Python course. Most of the students struggles with this programming task despite the make use of ChatGPT to solve the problem. Below is the complete solution of the programming task with explanation so that the students can learn and excel in Python.

The UDP Word Jumble! 🐍
In your job as a network engineer you’ve been asked to write a program that unscrambles a message sent via UDP and puts things in the correct order.

To do this you need to complete the function unscramble which takes a str parameter specifying the filename of the file to read the data from, and returns a str which is the unscrambled message.

The file will contain a series of lines where the first thing on each line is a two digit number, which is the “packet number” (i.e. the order the lines were sent in), followed by a colon, a space and then the string giving the part of the message in that “packet” (just to be clear, there’s no actual packets and no network programming to do).

The function should then put the message back together in the order the lines were sent in (i.e. sort by the packet number) and return the message as a single string (without the packet numbers, the colon and space after the colon).

The UDP World Jumble
The UDP World Jumble

ChatGPT Generated Code for the Problem

Following is the ChatGPT generated code which works fine for the provided example.txt file but the test case mess fails.

def unscramble(filename: str) -> str:
    pass

    with open(filename, 'r') as file:
        lines = file.readlines()

    packets = []
    for line in lines:
        packet_number, message_part = line.split(": ", 1)
        packets.append((int(packet_number), message_part.strip()))

        print("packet_number", packet_number)
        print("packets", packets)

    sorted_packets = sorted(packets, key=lambda x: x[0])

    print("sorted_packets", sorted_packets)

    unscrambled_message = ' '.join(' '.join(packet[1].split()) 
       for packet in sorted_packets)

    return unscrambled_message

if __name__ == "__main__":
    print(unscramble("example.txt"))

Following is the error message for the test case mess

'Hopefully you can unscramble this chaos.' != 'Hopefully you can unscramble this ch aos .'
- Hopefully you can unscramble this chaos.
+ Hopefully you can unscramble this ch aos .
?                                     +   +
 : The output should be 'Hopefully you can unscramble this chaos.', but was 'Hopefully you can unscramble this ch aos .'.
Failed: Already Ordered Test Case
Failed: Already Ordered Test Case
Failed: A Mess Test Case
Failed: A Mess Test Case

Solution to the Problem

Proper use of split function with white space striping using message_part[0:-1] solves the problem and now all the test cases will be passed.

def unscramble(filename: str) -> str:
    with open(filename, 'r') as file:
        lines = file.readlines()

    packets = []
    for line in lines:
        packet_number, message_part = line.split(": ", 1)
        packets.append((int(packet_number), message_part[0:-1]))

        print("Packets: ",packets)

    sorted_packets = sorted(packets, key=lambda x: x[0])

    unscrambled_message = ''.join(packet[1]
     for packet in sorted_packets)

    return unscrambled_message

if _ _name_ _== "_ _main_ _":
    print(unscramble("example.txt"))
Solved - Python Unscramble UDP Problem
Solved – Python Unscramble UDP Problem

Leave a Reply

Your email address will not be published. Required fields are marked *