The Self-Taught Computer Scientist


Chapter 5 String Algorithms 61


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

Chapter 5 String Algorithms
61
encrypt += uppercase[new]
If the character is lowercase, you do the same thing but use 
lowercase
instead:
elif c in lowercase:
new = (lowercase.index(c) + key) % 26
encrypt += lowercase[new]
If the character isn’t in 
uppercase
or 
lowercase
, it is a special character, so you add it to 
encrypt
without making any changes:
else:
encrypt += c
At the end of your loop, you return your new, encrypted string:
return encrypt
Because your algorithm only has to iterate through every letter in the string to encrypt it, its run 
time is O(
n).
Vocabulary
anagrams: Two strings that contain the same letters, but not necessarily in the same order (case 
does not matter).
palindrome: A word that reads the same backward as forward.
list comprehension: Python syntax to create a new, altered list from an existing iterable (like 
another list).
cipher: An algorithm for encryption or decryption.
modulo arithmetic: A type of arithmetic where numbers wrap around at a specific value.
Challenge
1. 
Use a list comprehension to return a list of all the words in the following list that have more 
than four characters: 
["selftaught", "code", "sit", "eat", "programming", "dinner", 
"one", "two", "coding", "a", "tech"]
.



6
Math
Do not worry too much about your difficulties in mathematics, I can assure you that mine are 
still greater.
Albert Einstein
In this chapter, you will learn some basic math that will help you pass a technical interview and 
improve as a programmer. While searching for prime numbers might not help you in your day- to- day 
programming, understanding the different algorithms you can use to find them will make you a better 
programmer. And while testing your ability to use the modulo operator in your algorithms is one 
of the most common technical interview “gotchas,” the modulo operator can also be handy in real- 
world applications. Finally, this chapter introduces the concept of boundary conditions. If you create 
applications without thinking about boundary conditions, you will likely produce a website or app 
filled with unexpected errors, so it is important to learn what they are and how to prepare for them.
Binary
Computers “think” in binary. A binary number is a number in the base 2 numeral system. A numeral 

Download 1.48 Mb.

Do'stlaringiz bilan baham:
1   ...   51   52   53   54   55   56   57   58   ...   147




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