Python core programming version 2 Chapter 6 Exercise continued 4-answers to Python core programming-self-developed-

Source: Internet
Author: User

6-11.
Conversion.
(A) Create a conversion from an integer to an IP address in the following format: www. xxx. yyy. zzz.
(B) Update your program to enable reverse conversion.
[Answer]
(A) The Code is as follows:
Input_number = abs (int (raw_input ('Please input a number ...')))
X3 = x2 = x1 = x0 = 0
X3 = Input_number/(256 ** 3)
If x3> 255:
Tmp = x3
X3 = 255
Else:
Tmp = x3
X2 = (Input_number-256 ** 3 * tmp)/(256 ** 2)
X1 = (Input_number-256 ** 3 * tmp-256 ** 2 * x2)/(256 ** 1)
X0 = Input_number-256 ** 3 * tmp-256 ** 2 * x2-256 * x1

Print '% d. % d' % (x3, x2, x1, x0)

(B) The Code is as follows:
Input_IP = raw_input ('pleae input an IP address ...')
IP = str. split (Input_IP ,'.')
Data = 256 ** 3 * int (IP [0]) + 256 ** 2 * int (IP [1]) + 256 * int (IP [2]) + int (IP [3])
Print 'the number is % d' % Data

[Comment]
I personally think this question does not clearly describe the algorithm used for conversion. It seems that you only need to calculate a valid IP address. I am using an algorithm similar to 8421 yards. The IP address is a 32-bit binary number, which is usually divided into four eight-bit binary numbers (that is, four bytes ). IP addresses are usually expressed in the form of a. B. c. d ~ A decimal integer between 255. Assume that a, B, c, and d are four digits in a decimal system, and every 256 digits is entered.
In (a) calculation, 256 can be represented as 0.0.1.0257, or 0.0.1.1.
Please input a number... 536870977549
1.0.13
(B) assume that the user inputs a valid IP address.

6-12.
String.
(A) Create a function named findchr (). The function declaration is as follows.
Def findchr (string, char)
Findchr () searches for the character char in the string and returns the value index if it is found. Otherwise,-1 is returned. The string. * find () or string. * index () functions and methods cannot be used.
(B) Create another function named rfindchr () to locate the last occurrence of the character char. It works like findchr (), but it looks forward from the end of the string.
(C) Create a third function named subchr () and declare it as follows.
Def subchr (string, origchar, newchar)
Subchr () is similar to findchr (). The difference is that if a matching character is found, the original character is replaced with a new character. Returns the modified string.
[Answer]
(A) The Code is as follows:
Def findchr (string, char ):
A = string
K = index =-1
For I in:
K = k + 1
If I = char:
Index = k
Print index
If index =-1: print 'index = ', index

A = raw_input ('Please input a string ...')
B = raw_input ('Please input a character to be find in this string ...')
Findchr (a, B)

(B) The Code is as follows:
Def rfindchr (string, char ):
A = string
K = index =-1
For I in:
K = k + 1
If I = char:
Index = k
Print index
If index =-1: print 'index = ', index

A = raw_input ('Please input a string ...')
B = raw_input ('Please input a character to be find in this string ...')
Rfindchr (a, B)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.