Python
/
Applications
- 1 Language 9
-
Hello World S
-
Variables S
-
Functions S
-
Conditional A S
-
Operators S
-
While S
-
Turtle S
-
Script Mode S
-
Debugging S
- 2 Strings 7
-
Slice S
-
Raw Strings S
-
Regex A S
-
Validation S
-
Config S
-
Security S
-
Encrypt A S
- 3 Collections 6
-
Lists S
-
Dictionaries S
-
Efficiency S
-
Tree S
-
Tuples S
-
References S
- 4 Functions 5
-
Recursion S
-
Factorial S
-
Modulus S
-
Reassignment S
-
Approximate S
- 5 Storage 8
-
Files S
-
Databases S
-
Pipes S
-
With open S
-
Shelve A S
-
Zip S
-
Csv S
-
Json S
- 6 Class 4
-
Definition S
-
Attributes S
-
Functional S
-
Methods S
- 7 Goodies 5
-
Conditional Expression S
-
List Comprehension A S
-
Generator S
-
Named Tuple S
-
Modules S
- 8 Applications 5
-
Pythagora A S
-
Palindrome A S
-
Binary Search A S
-
Conway Game A S
-
Coin Flip A S
- 9 Scheduler 4
-
Time S
-
Multithreading A S
-
Subprocess S
-
Logging S
- 10 Packages 6
-
Clipboard A S
-
Ocr A S
-
Socket S
-
Image S
-
Virtualenv S
-
Jupyter S
S
R
Q
Python Applications Binary Search
Start in the middle of the list, then search the second half if keyword < words[k]: words = words[:k] else: words = words[k+1:]Binary-search
Word search
Make a bisection search (or binary search)
# Binary search
#
# You start in the middle of the list, ...
# then you search the second half.
import os
words = []
file = os.path.dirname(__file__) + "/words.txt"
for line in open(file):
word = line.strip()
words.append(word)
def full_search(words, keyword):
i = 0
for word in words:
i = i + 1
if (word == keyword):
return i
return i
def binary_search(words, keyword, i=0):
i = i + 1
key = len(words) // 2
if words[key] == keyword:
return i
if keyword < words[key]: #Look Here
words = words[:key]
else:
words = words[key+1:]
i = binary_search(words, keyword, i)
return i
print(len(words)) # 113783
print(full_search(words, "mother")) # 62889
print(binary_search(words, "mother")) # 16
➥ Caesar cypher
Caesar cypher
Caesar cypher rotates each letter by a number.
# A Caesar cypher is a weak form on encryption:
# It involves "rotating" each letter by a number (shift it through the alphabet)
#
# Example:
# A rotated by 3 is D; Z rotated by 1 is A
# In a SF movie the computer is called HAL, which is IBM rotated by -1
#
# Our function rotate_word() uses:
# ord (char to code_number)
# chr (code to char)
def encrypt(word, no):
rotated = ""
for i in range(len(word)):
j = ord(word[i]) + no
rotated = rotated + chr(j)
return rotated
def decrypt(word, no):
return encrypt(word, -no)
assert encrypt("abc", 3) == "def" # pass
assert encrypt("IBM", -1) == "HAL" # pass
assert decrypt("def", 3) == "abc" # pass
assert decrypt("HAL", -1) == "IBM" # pass
➥ Double letters
Double letters
Get the words with three consecutive double letters
# Get the words with three consecutive double letters
import os
def has_three_doubles(word):
i = 0
jj = 0
while i < len(word) -1:
if word[i] == word[i+1]:
jj = jj + 1
if jj == 3:
return True
i = i + 2
else:
jj = 0
i = i + 1
return False
assert has_three_doubles("aabbcc") == True
assert has_three_doubles("Mississippi") == False
file = os.path.dirname(__file__) + "/words.txt"
for line in open(file):
word = line.strip()
if has_three_doubles(word):
print(word)
# bookkeeper
# bookkeepers
# bookkeeping
# bookkeepings
➥ Questions