Python Forum
Need some help with a bit of code for an assignment.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some help with a bit of code for an assignment.
#2
You're on a viable track for the first problem. There are a few things to change.

First, getHexChar() can be greatly improved with a dict:

def getHexChar(dec_digit):
    letters = {
        10: "A",
        11: "B",
        12: "C",
        13: "D",
        14: "E",
        15: "F"
    }
    return letters.get(dec_digit, dec_digit)
The dict.get() method will check for a key matching dec_digit in letters and, if there is no match, it will return the default which is set to str(dec_digit).

Now, decToHex() has a couple significant issues. The number 16 represented in hex is 10. If we run 16 through decToHex() we would have these results at the end of the first loop:

Quote:16 // 16 == 1
16 % 16 == 0

This indicates that 16 is evenly divisible by 16 exactly once; which is also what the hexadecimal 10 indicates as well. To accomplish the task then, it's evident that the quotient needs to be converted to hex. Also, the function must return the hexStr since it stores the end value:

def decToHex(decimal_number):
    hexStr = ''
    
    while decimal_number != 0 :
        decimal_number = decimal_number // 16
        remainder = decimal_number % 16
        hexStr += getHexChar(decimal_number)
        
    return hexStr
Testing this with 155 (hex 9B) returns 90. We're close but there's still more work to do. The problem is that now we are only examining decimal_number and ignoring the remainder. If we take a look at the math again, we see:

Quote:155 // 16 == 9
155 % == 11 <- 11 == 0xB

As noted, the hex for 155 is 9B. Our result is 90 and the remainder that's ignored equals B, which is exactly what we're missing. Looking at the structure of the function again, we need a way to consider the remainder. So, let's add in some logic to check for value.

def decToHex(decimal_number):
    hexStr = ''
    
    while decimal_number != 0 :
        decimal_number = decimal_number // 16
        remainder = decimal_number % 16

        if decimal_number == 0:
            hexStr += getHexChar(remainder)
        else:
            hexStr += getHexChar(decimal_number)
    return hexStr
It still gives us 90 instead of 9B for 155. The issue is with the remainder calculation. Because decimal_number gets reset before the modulus is calculated, it's doing this:

Quote:loop 1:
155 // 16 == 9
9 % 16 == 9

loop2:
9 // 16 == 0
0 % 16 == 0

To fix this, we need to calculate the remainder based on the original decimal_number and not touch it again.

def decToHex(decimal_number):
    hexStr = ''
    remainder = decimal_number % 16
    while decimal_number != 0 :
        decimal_number //= 16

        if decimal_number > 0:
            hexStr += getHexChar(decimal_number)
        else: 
            hexStr += getHexChar(remainder)

    return hexStr 
That gives us the correct value. Try it out for some other values. Now, here's a bigger problem. What's the hex of 15,232? The function only works on numbers up to 16^2 (256).
Reply


Messages In This Thread
RE: Need some help with a bit of code for an assignment. - by stullis - Feb-09-2019, 04:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  morse code assignment raymond2688 11 8,422 Jul-29-2019, 07:43 PM
Last Post: raymond2688
  Write pseudo code for a class assignment Scrimshot 3 3,328 May-07-2019, 05:38 PM
Last Post: Scrimshot
  Need help with lists to continue my assignment code tinabina22 9 10,265 Oct-12-2016, 12:20 AM
Last Post: Yoriz
  [split] Need help with lists to continue my assignment code cylandur 7 9,308 Oct-11-2016, 03:11 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020