Python core programming, Version 2, 160th page, Chapter 6 exercises

Source: Internet
Author: User

I will share with you the answers to my core Python programming exercise.
Because it is not from official resources, it is your own exercises. It may be wrong or not the best solution.

[Recommendation] blog: Python module of niub
Http://www.cnblogs.com/chu888chu888/archive/2011/01/09/1931084.html

[Recommendation] recommended blog: Chapter 6 of core Python programming exercises, exercises by others
Http://blog.csdn.net/killua_hzl/archive/2010/05/31/5637828.aspx

[Recommendation] Recommendation blog: lexicographically ordered Algorithm
Http://blog.sina.com.cn/s/blog_4c471b9601000czy.html

[Recommendation] blog: There is no memory for different degrees of parallelism. Learn about Python.
Http://blog.csdn.net/killua_hzl/category/681280.aspx

 

6-1.
String. Is there a string method or function in the string module that can help me identify whether the next string is part of another large string?
[Answer]
The member operator is used to determine whether a character or a string (the character in) appears in another string. If yes, True is returned. If no, False is returned. Note that the member operator is not used to determine whether a string contains another string. Such a function is completed by the find (), index (), rfind (), and rindex () functions.
>>> 'Bc' in 'abc'
True
>>> 'N' in 'abc'
False

 

6-2.
String identifier. Modify the idcheck. py script in the example of 6-1 to detect an identifier and identify the Python keyword. For the next requirement, you can use the keyword module (especially keyword. kwlist.
[Evaluation]
I suspect that the original intention of the question is to modify the script so that it can detect an identifier with a length greater than or equal to one.
[Reference]
Idcheck. py script
Import string

Alphas = string. letters + '_'
Nums = string. digits

Print 'Welcome to the Identifier Checker v1.0'
Print 'testees must be at least 2 chars long .'

MyInput = raw_input ('identifier to test? ')

If len (myInput)> 1:

If myInput [0] not in alphas:
Print ''' invalid: first symbol must be alphabetic '''

Else:
For otherChar in myInput [1:]:

If otherChar not in alphas + nums:
Print ''' invalid: first symbol must be alphabetic '''
Break
Else:
Print 'Okay as an identifier'

[Reference] About keyword. kwlist

>>> Import keyword

>>> Dir (keyword)
['_ All _', '_ builtins _', '_ doc _', '_ file __', '_ name _', 'iskeyword', 'kwlist', 'main']

>>> Keyword. kwlist
['And', 'assert ', 'Break', 'class', 'contine', 'def', 'del', 'elif', 'else ', 'Wait t', 'exec ', 'Finally', 'for ', 'from', 'global', 'if', 'import', 'in', 'is ', 'lambda ', 'not', 'or', 'pass', 'print ', 'raise', 'Return ', 'try', 'while', 'yield ']
>>>
[Answer]
The Code is as follows:
Import string
Import keyword

Alphas = string. letters + '_'
Nums = string. digits

Print 'Welcome to the Identifier Checker v2.0'
Print 'testees must be at least 1 chars long .'

MyInput = raw_input ('identifier to test? ')

IsKeyword = False
IsIdentifier = False

If len (myInput)> = 1:

If myInput [0] not in alphas:
Print ''' invalid: first symbol must be alphabetic '''
IsIdentifier = False
Else:
For otherChar in myInput [1:]:

If otherChar not in alphas + nums:
Print ''' invalid: remaining symbols must be alphabetic '''
IsIdentifier = False
Break
Else:
IsIdentifier = True

If myInput in keyword. kwlist:
Print '"% s" is a keyword of python' % myInput
IsKeyword = True
If isIdentifier and (not isKeyword): print 'Okay as an identifier'

 

6-3.
Sort.
(A) enter a string of numbers in ascending order.
(B) SAME AS. However, it should be arranged in lexicographically ascending order.
[Answer]
(A) The Code is as follows:
AList = raw_input ('Please input numbers, separated by space ...')
BList = []
For I in aList. split (''):
BList. append (int (I ))
BList. sort ()
Print bList [:-1]
(B) The Code is as follows:
AList = raw_input ('Please input numbers, separated by space ...')
BList = []
For I in aList. split (''):
BList. append (I)
BList. sort ()
Print bList [:-1]
[Evaluation]
I checked the meaning of the Lexicographic Order, which seems to be inconsistent with the meaning of the question.

Keywords: Pyhon core programming exercise code unofficial blog

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.