Question: discuss which version of isLower you think will be fastest. Can you th
ID: 3619695 • Letter: Q
Question
Question: discuss which version of isLower you think will be fastest. Can you think of other reasons besides speed to prefer one or the other?-
The reading for the above question is below.
--
Character classification:
It is often helpful to examine a character and test whether it is upper- or lowercase,
or whether it is a character or a digit. The string module provides several
constants that are useful for these purposes.
The string string.lowercase contains all of the letters that the system considers
to be lowercase. Similarly, string.uppercase contains all of the uppercase letters.
Try the following and see what you get:
>>> print string.lowercase
>>> print string.uppercase
>>> print string.digits
We can use these constants and find to classify characters. For example, if
find(lowercase, ch) returns a value other than -1, then ch must be lowercase:
def isLower(ch):
return string.find(string.lowercase, ch) != -1
Alternatively, we can take advantage of the in operator, which determines whether
a character appears in a string:
def isLower(ch):
return ch in string.lowercase
As yet another alternative, we can use the comparison operator:
def isLower(ch):
return ’a’ <= ch <= ’z’
If ch is between a and z, it must be a lowercase letter. - Another constant de?ned in the string module may surprise you when you print
it:
>>> print string.whitespace
Whitespace characters move the cursor without printing anything. They create
the white space between visible characters (at least on white paper). The constant
string.whitespace contains all the whitespace characters, including space, tab
( ), and newline ( ). Question: discuss which version of isLower you think will be fastest. Can you think of other reasons besides speed to prefer one or the other?
-
The reading for the above question is below.
--
Character classification:
It is often helpful to examine a character and test whether it is upper- or lowercase,
or whether it is a character or a digit. The string module provides several
constants that are useful for these purposes.
The string string.lowercase contains all of the letters that the system considers
to be lowercase. Similarly, string.uppercase contains all of the uppercase letters.
Try the following and see what you get:
>>> print string.lowercase
>>> print string.uppercase
>>> print string.digits
We can use these constants and find to classify characters. For example, if
find(lowercase, ch) returns a value other than -1, then ch must be lowercase:
def isLower(ch):
return string.find(string.lowercase, ch) != -1
Alternatively, we can take advantage of the in operator, which determines whether
a character appears in a string:
def isLower(ch):
return ch in string.lowercase
As yet another alternative, we can use the comparison operator:
def isLower(ch):
return ’a’ <= ch <= ’z’
If ch is between a and z, it must be a lowercase letter. - Another constant de?ned in the string module may surprise you when you print
it:
>>> print string.whitespace
Whitespace characters move the cursor without printing anything. They create
the white space between visible characters (at least on white paper). The constant
string.whitespace contains all the whitespace characters, including space, tab
( ), and newline ( ).
Explanation / Answer
The 3rd version of is lower will be faster. def isLower(ch): return 'a'Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.