#๐Ÿ”’ How can I make the encoded message using left and right instead of 1's and 0's

45 messages ยท Page 1 of 1 (latest)

restive orioleBOT
#

@restive shore

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

#

Hey @restive shore!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
#

:incoming_envelope: :ok_hand: applied timeout to @restive shore until <t:1711126998:f> (10 minutes) (reason: chars spam - sent 5268 characters).

The <@&831776746206265384> have been alerted for review.

stoic harbor
#

!unmute 159866669626294272

restive orioleBOT
#

:incoming_envelope: :ok_hand: pardoned infraction timeout for @restive shore.

stoic harbor
#

!paste

restive orioleBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

stoic harbor
#

please use our pastebin

restive shore
#

I need to get this: 'HELLO WORLD' will be encoded as:

LLLLLLLRLRLRLLRRLLRLRLLRRRLLRRRR

Instead of Encoded Message: 11100001010110111101111001010001

placid shoal
#

What is you problem currently?

restive shore
#

When I get the encoding table it is 1's and 0's instead of displaying it as L and R. To represent the left and right side of traversing a tree.

So for example LLLL for 'H', LLLR for 'E'

vagrant willow
#

how do you know which letter to map to which sequence of letters?

restive shore
#

Im using Huffman code

placid shoal
restive shore
#

'HELLO WORLD' will be encoded as:

LLLLLLLRLRLRLLRRLLRLRLLRRRLLRRRR

restive shore
vagrant willow
#

can you send the code you have so far?

restive shore
#

even with 1's and 0's when table shows 'H': '1110'. But it would need to be '1111' So idk if i am traversing it wrong

vagrant willow
#

just change "0" and "1" for "L" and "R" on line 45 and 46

#

actually wait

restive shore
#

do you get what im saying about the order part. The letter doesn't matter anymore, i mentioned the wrong thing.

#

it is encoding it weirdly

vagrant willow
#

yeah I just noticed that

restive shore
#

yes ;p

#

so idk if the tree is being built wrong

#

or the traversal part

mint elbow
#

I don't think the output you want doesn't match the encoding table the script generates.

Encoding Table: {'E': '000', 'D': '001', 'R': '010', 'W': '011', 'L': '10', 'O': '110', 'H': '1110', ' ': '1111'}

Or in R/L terms:

Encoding Table: {'E': 'LLL', 'D': 'LLR', 'R': 'LRL', 'W': 'LRR', 'L': 'RL', 'O': 'RRL', 'H': 'RRRL', ' ': 'RRRR'}
vagrant willow
restive shore
#

yea sorry, the main part rn is something is being traversed wrong. In either terms H should be '1111' or 'LLLL' instead of what is being printed

#

the tree I posted above shows how it expects to traverse

mint elbow
#

From what I see, "R" and "W" got paired up first.

mint elbow
#

Got it working.
I reversed the string when calculating frequency, then sorted the queue conventionally. Then, when building the map, I used normal pop instead of the heap pop.

https://paste.pythondiscord.com/PRFQ

restive shore
mint elbow
#

Well, I just simply do some "trial and error" and good ol' "debugging" by just printing values in-between the process.

The first thing I see is that, "R" and "W" got paired up first. In your diagram, "R" should be paired with "D" and "W" with space instead.
So, I started checking the huffman tree build traversal, and check for the order it inserted and when it's merged.
After all got paired up as how shown in your diagram, I see that the length is reversed. So, I tried reversing the input string. Then, it's the same as what you'd expect.

So, yeah. Basically "trial and error" and excessive printing or "debugging".

#

Changes I made is not using heapify (look around the build_huffman_tree function) and reversing the input message to be reversed (look at the bottom of the script, frequencies_dict(message) becomes frequencies_dict(message[::-1]) ([::-1] is to reverse a collection, i.e list and string).

#

Also, the __str___(self) function I added at the end of the Huffman_Node class, just to make the print easier to read.

restive shore
#

and what is the use for normal pop instead of heap pop?

mint elbow
#

Normal pop and heap pop "pop" differently. With normal pop, you can easily see what's popping next. The heap pop will require you to know how heap map and the popping works, and you need to estimate what thing goes where.
In this case, before I do the manual pop, I need to sort the list first. With heap pop, it's not required.

#

The order of the same frequency popping might be different.

restive shore
#

thank you so much for the help ๐Ÿ˜Š

mint elbow
#

No problem ๐Ÿ‘

restive orioleBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.