Core Python Programming . . .
I'm working my way thru the book "Core Python Programming" 2d Edition - Wesley Chun. . . Trying to figure out what he's looking for on Page 248, Exercise 6-11 (a). Is it supposed to be: 1) convert a 4-digit Integer (WXYZ) to an IP address (WWW.XXX.YYY.ZZZ) or 2) convert an 8-digit Integer (WWWXXXYYYZZZ) to (WWW.XXX.YYY.ZZZ) Thanks for anyone with the clue!!! -- http://mail.python.org/mailman/listinfo/python-list
Re: Core Python Programming . . .
On Jan 18, 6:00 pm, Yu-Xi Lim <[EMAIL PROTECTED]> wrote: > Mike Driscoll wrote: > > > 6-11 Conversion. > > (a) Create a program that will convert from an integer to an > > Internet Protocol (IP) address in the four-octet format of WWW.XXX.YYY.ZZZ > > (b) Update your program to be able to do the vice verse of the > > above. > > I think it's is asking to convert a 32-bit int to the dotted form. > > It's a little known fact, but IP addresses are valid in non-dotted > long-int form. Spammers commonly use this trick to disguise their IP > addresses in emails from scanners. I guess I'm not fully up to speed on what constitutes an IP address. Does the term 'octet' refer to an 8-bit (xFF) number? -- http://mail.python.org/mailman/listinfo/python-list
Re: Core Python Programming . . .
On Jan 22, 5:00 pm, wesley chun <[EMAIL PROTECTED]> wrote: > > > 6-11 Conversion. > > > (a) Create a program that will convert from an integer to an > > > Internet Protocol (IP) address in the four-octet format of WWW.XXX.YYY.ZZZ > > > (b) Update your program to be able to do the vice verse of the above. > > > I think it's is asking to convert a 32-bit int to the dotted form. > > > It's a little known fact, but IP addresses are valid in non-dotted > > long-int form. Spammers commonly use this trick to disguise their IP > > addresses in emails from scanners. > > that is correct. don't read too much into it. i'm not trying to > validate anything or any format, use old or new technology. it is > simply to exercise your skills with numbers (specifically 32-bit/4- > byte integers), string manipulation, and bitwise operations. if you > wish to use different sizes of numbers, forms of addressing, IPv6, > etc., that's up to you. don't forget about part (b), which is to take > an IP address and turn it into a 32-bit integer. > > enjoy! > -- wesley > > ps. since you're on p. 248, there is also a typo in the piece of code > right above this exercise, Example 6.4, which is tied to exercise > 6-7. "'fac_list'" should really be "`fac_list`", or even better, > "repr(fac_list)". see the Errata at the book's websitehttp://corepython.com > for more details. > > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > "Core Python Programming", Prentice Hall, (c)2007,2001 >http://corepython.com > > wesley.j.chun :: wescpy-at-gmail.com > python training and technical consulting > cyberweb.consulting : silicon valley, cahttp://cyberwebconsulting.com Well, I think I may be way off on this one. . . here's what I came up with. . . . def int_to_IP(int): '''Function accepts an integer and returns a string in the format WWW.XXX.YYY.ZZZ''' string = str(int) return (string[0]*3 + '.' + string[1]*3 + '.' + string[2]*3 + '.' + string[3]*3) number = int(raw_input('Enter the number(4 digits): ')) print (int_to_IP(number)) -- http://mail.python.org/mailman/listinfo/python-list
Dictionary Keys question
I'm curious why the different outputs of this code. If I make the dictionary with letters as the keys, they are not listed in the dictionary in alphabetical order, but if I use the integers then the keys are in numerical order. I know that the order of the keys is not important in a dictionary, but I was just curious about what causes the differences. Thanks!! list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] list2 = [1,2,3,4,5,6,7,8] Dictionary = dict(zip(list1, list2)) print Dictionary Dictionary1 = dict(zip(list2, list1)) print Dictionary1 -- http://mail.python.org/mailman/listinfo/python-list
Re: Dictionary Keys question
On Jan 30, 3:09 pm, Berteun Damman <[EMAIL PROTECTED]> wrote: > On Wed, 30 Jan 2008 14:47:36 -0800 (PST), FireNWater <[EMAIL PROTECTED]> > wrote: > > I'm curious why the different outputs of this code. If I make the > > dictionary with letters as the keys, they are not listed in the > > dictionary in alphabetical order, but if I use the integers then the > > keys are in numerical order. > > > I know that the order of the keys is not important in a dictionary, > > but I was just curious about what causes the differences. Thanks!! > > I don't know the exact way Python's hash function works, but I can take > a guess. I'm sorry if I explain something you already know. > > A hash is for quickly looking up data. Yet, you don't want to waste too > much memory. So there is a limit number of spaces allocated, in which to > store objects. This number of spaces can be thought of as a list. Then, > if you put something into the dict, Python computes the 'hash' of this > object, which basically forms the index in the list where to store it. > So every object should be mapped onto some index within the list. (If > you retrieve it, the hash is computed again, and the value on that index > is looked up, like list indexing, these are fast operations.) > > Say, if you have 100 spaces, and someone puts in integer in the list, > the hashfunction used might be % 100. So the first 100 integers would > always be placed at consecutive places. For strings however, a more > complicated hash-function would be used, which takes into account more > characters, so strings don't end up in order. > > For integers, if you put in integers that are spread very widely apart, > they won't end up in order either (see the mod 100 example, 104 will > come before 10). > > If you replace the list2 in your example by: > list2 = [1 * x for x in range(1,9)] > > You will see that this one doesn't end up in order either. So, there's > no exception for integers when it comes to the order, yet the particular > properties of the hash function will cause sequential integers to end up > in order under some circumstances. > > Berteun > > PS: > What happens if two values map onto the same space is of course an > obvious question, and the trick is choosing your hashfunction so this > occurs not very often on average. If it happens there are several > strategies. Wikipedia probably has an explanation of how hash-functions > can work in such a case. Thank you for the explanation. . . I think I now have a (foggy) understanding of hash tables. It seems to be a way to create order (an index) out of disorder (random numbers or characters) behind the scenes. . -- http://mail.python.org/mailman/listinfo/python-list
Re: Dictionary Keys question
On Jan 31, 4:39 am, Dustan <[EMAIL PROTECTED]> wrote: > On Jan 30, 7:02 pm, FireNWater <[EMAIL PROTECTED]> wrote: > > > Thank you for the explanation. . . I think I now have a (foggy) > > understanding of hash tables. It seems to be a way to create order > > The 'order' that your speaking of is not implemented by the hash > *table*, per se, but rather by the hash function, which returns an > integer (the hash code). > Dustan, Yes, I think I understand it now. . . the order I was referring to is provided "behind the scenes" by the return values of the hash function. Thanks to everyone with their explanations!! -- http://mail.python.org/mailman/listinfo/python-list