The Self-Taught Computer Scientist


Download 1.48 Mb.
Pdf ko'rish
bet52/147
Sana17.06.2023
Hajmi1.48 Mb.
#1540634
1   ...   48   49   50   51   52   53   54   55   ...   147
Bog'liq
books-library.net-11301817Az7X6

prehension is Python syntax for creating a new, altered list from an existing iterable (like another list).
Here is the syntax for a list comprehension:
new_list = [expression(i) for i in iterable if filter(i)]
iterable
is the iterable you are using to create your new list. 
expression(i)
is a variable that holds 
each element from 
iterable
. For example, in this regular expression
c
contains each character in 
the string 
"selftaught"
.
print([c for c in "selftaught"])
>> ['s', 'e', 'l', 'f', 't', 'a', 'u', 'g', 'h', 't']
As you can see, Python returns a list that contains all the letters from your original string 
"selftaught"
.
filter(i)
allows you to make changes to the original iterable. For example, you can create a filter 
that adds items to your iterable only if they meet your filter’s requirements:
print([c for c in "selftaught" if ord(c) > 102])
>> ['s', 'l', 't', 'u', 'g', 'h', 't']
Python’s built- in function 
ord
returns the ASCII code for a letter. In this case, you added a filter 
that adds characters to your iterable only if the character’s ASCII code is greater than 102 (the letter 
f). As you can see, your new list is missing the letters ef, and a.
You can use Python’s 
isdigit
function to filter everything except numbers:
s = "Buy 1 get 2 free" 
nl = [c for c in s if c.isdigit()]
print(nl)
>> ['1', '2']


Introduction to Algorithms
58
Now that you know how to use a list comprehension to find all the digits in a string, there is only one 
more step to find the rightmost digit, namely, using a negative index to get the last digit from your new list:
s = "Buy 1 get 2 free"
nl =[c for c in s if c.isdigit()][- 1]
print(nl)
>> 2
First, your list comprehension returns a list of all the digits in your string. Then, you use a negative 
index to get the last item in your new list of numbers, which is the rightmost digit in your original string.
As you can see, you can use a list comprehension to turn three or four lines of code into one elegant 
line, which will help you write short, readable code when you are programming professionally.
Because your algorithm iterates through every character in the string to check whether it is a digit 
and reverses a list, its run time is O(
n).
Caesar Cipher
cipher is an algorithm for encryption or decryption. Julius Caesar, the famous Roman general and 
politician, protected his confidential messages by encrypting them using an ingenious cipher. First, 
he would pick a number. Then, he would shift every letter by that number. For example, if he chose 
the number 3, the string 
abc would become def.
If the shift took him past the alphabet’s end, he rotated back to the front of the alphabet. For example, 
if he had to shift 
z two places, it would become b.
Modulo arithmetic is the key to coding a Caesar cipher. Modulo arithmetic is a type of arithmetic 
where numbers wrap around at a specific value. You should already be familiar with modulo arithmetic 
because you can tell time (Figure 5.1).

Download 1.48 Mb.

Do'stlaringiz bilan baham:
1   ...   48   49   50   51   52   53   54   55   ...   147




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling