0

I am rather new to python and seeking help regarding a homework prompt of mine.

Here is the exact prompt from the professor:

Both start_ip and end_ip should be 12-digit integer numbers. That is to make the IP lookup easier. First split the ip address at periods, then add leading zeros to each number to make each of them three digits, i.e. the string 102.1.2.0 becomes the integer 102001002000. Hint: use the string zfill() method (zero fill) and do operations as strings to build a string of digits and only convert to an int after the whole number has been built (note that when converted to an int leading zeros of the first number will be lost since an int does not have leading zeros – that is OK).

So basically we remove the dots from the ip string, reformat the string to be 12 characters long, and then convert that to an int.

For example: 0.0.0.0 should go to -> 000.000.000.000 102.1.2.0 should go to -> 102001002000

This is what I came up with, but it doesnt seem to work:

def ip_to_int(ip):
ip = ip.split('.')
ip = ip[0].__str__() + ip[1].__str__() + ip[2].__str__() + ip[3].__str__()
ip = ip.zfill(12)
return int(ip)

For example, when I plug the ip 0.0.0.0 into this code, it returns the int 0. And when I put in 1.0.0.0 , it results in 1,000. I am not able to pinpoint why this is happening.

Like I said I am new to python so some of what I did might not entirely make sense.

Thanks in advance.

7
  • You should state why it doesn't work, i.e. what the result is Commented Oct 30, 2017 at 0:58
  • 1
    what do you think ip.zfill(12) is doing? Commented Oct 30, 2017 at 1:00
  • Also ip[0] is already a string, no need to call: ip[0].__str__() and etc Commented Oct 30, 2017 at 1:00
  • 1
    note that when converted to an int leading zeros of the first number will be lost since an int does not have leading zeros – that is OK) And so 0.0.0.0 should be 0, correct? Commented Oct 30, 2017 at 1:03
  • According to his explanation, 0.0.0.0 should go to 000000000000. Because we want to fill in all 12 characters with 0s. Commented Oct 30, 2017 at 1:10

5 Answers 5

4

What the professor is asking regarding the zfill is to do this:

ip =  ip[0].__str__().zfill(3) 
    + ip[1].__str__().zfill(3) 
    + ip[2].__str__().zfill(3) 
    + ip[3].__str__().zfill(3)

but notice that your list already contains strings so you don't need to convert those again, then your code would be:

ip =   ip[0].zfill(3) 
     + ip[1].zfill(3) 
     + ip[2].zfill(3) 
     + ip[3].zfill(3)

and to make it even more elegant and pythonic you could do:

ip = ''.join([i.zfill(3) for i in ip])
Sign up to request clarification or add additional context in comments.

9 Comments

Why are you calling .__str__()?
@alfasin that was in the original code, I wouldn't do that either.
@JosepValls If you wouldn't do that, then change your answer to reflect it.
This fixed some of the issues. However, some IP addresses still aren't formatting to 12 characters.
@andrewDev15 Remember my comment from above: note that when converted to an int leading zeros of the first number will be lost since an int does not have leading zeros – that is OK) So that's valid.
|
1

The way you are currently solving the problem, you take your string "102.1.2.0", then you split it into an array ["102", "1","2","0"], then put the string together "102120", and then pad with zeros. 000000102120. Do you see the answer now?

Try to solve it yourself first, then see if this is the answer you came up with.

You need to zfill(3) each string before putting them together. You could type this out all the way, str(ip[0]).zfill(3) + str(ip[1]).zfill(3) + ... but it is better coding practice to use a for loop if your professor has gone over those already. Try for i in range(4): result = result + str(ip).zfill(3)

Comments

1
def ip_to_int(ip):
    return int("".join([n.zfill(3) for n in ip.split('.')]))

The explanation:

First we split the ip string into parts:

     ip.split('.')

Then we create a list, filling every part to the string of the length 3:

     [n.zfill(3) for n in ip.split('.')]

Next we join them ("".join()) and convert the resulting string to an integer (int()).

Comments

1

It's late but:

def ip_to_int(ip):
    m = ''
    for i in ip.split('.'):
        m += (3-len(i))*'0' + i
    return int(m)

Comments

0

You can try this:

int(''.join([i.zfill(3) for i in ip.split(".")]))

1 Comment

See stackoverflow.com/a/47006847/4909087, your answer is a dupe of one of their options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.