REMEMBER PYTHON

MINTE9
2REMEMBER




Strings

p137 ! A segment of string is called slice. String are immutable, you can't change it.
 RunCode
str = "Python"
print(str[0:2]) # Py

fruit = "banana"
print(fruit[:3]) # ban
print(fruit[3:]) # ana
... 5 lines
˄˄˄
 RunCode
hello = "Hello World!"
hello[0] = "J"

# TypeError: 'str' object does not support item assignment
... 3 lines
˄˄˄
p139 To search in a string, use a loop through string.
 RunCode
def find(word, letter):
    index = 0
    while index < len(word):
        if word[index] == letter:
            return index
        index = index + 1
    return -1

print(find("Hello World", "o"))  # 4
... 8 lines
˄˄˄
 CodeCopy
def has_no_e(word):
    for letter in word:
        if letter == "e":
            return False
    return True

file = open("/var/www/python/words.txt")
for line in file:
    word = line.strip()
    if has_no_e(word):
        print(word)
... 10 lines
˄˄˄
 CodeCopy
def avoids(word, forbidden):
    for letter in word:
        if letter in forbidden:
            return False
    return True

file = open("/var/www/python/words.txt")
for line in file:
    word = line.strip()
    if avoids(word, 'zym'):
        print(word)
... 10 lines
˄˄˄
 CodeCopy
def requires(word, required):
    for letter in word:
        if letter in required:
            return True
    return False

file = open("/var/www/python/words.txt")
for line in file:
    word = line.strip()
    if requires(word, 'xyz'):
        print(word)
... 10 lines
˄˄˄
 CodeCopy
def is_abecedarian(word):
    previous = word[0]
    for c in word:
        if c < previous:
            return False
        previous = c
    return True

print(is_abecedarian("about")) # False
print(is_abecedarian("abcde")) # True
... 9 lines
˄˄˄
p141 Strings provide methods for various operations.
 RunCode
str = "Hello World"
str = str.upper()
print(str) # HELLO WORLD

str = "banana"
index = str.find("a")
print(index) # 1
... 6 lines
˄˄˄
Task

 CompleteTask
"""
A Caesar cypher is a weak form on encryption
It involves "rotating" each letter by a number (shift it through the alphabet)
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 

Write a function rotate_word()
Use built-in functions ord (char to code_number), chr (codes to char) """

def rotate(word, i):
    ...
    

print(rotate("abc", 1))  # bcd 
print(rotate("abc", 3))  # def 
print(rotate("HAL", -1))  # IBM 
solution code
 CompleteTask
"""
Get the words with three consecutive double letters
https://github.com/AllenDowney/ThinkPython2/blob/master/code/words.txt """

def has3_2consecutive(word):
    ...

for line in open("/var/www/python/words.txt"):
    word = line.strip()
    if has3_2consecutive(word):
        print(word)
solution code
 CompleteTask
"""
Make a Bisection Search (or binary search), 
similar to what you do in dictionary.

You start in the middle of the list, then you search the second half
https://github.com/AllenDowney/ThinkPython2/blob/master/code/words.txt """

# Word List
words = []
for line in open("/var/www/python/words.txt"):
    word = line.strip()
    words.append(word)

# Full Search
def search(keyword, words):
    ...

# Bisect Search
def search_bisect(keyword, words, loops=0):
    ...

search("mother", words) # 62889
search_bisect("mother", words)  # 16
solution code
Questions    
No. 8   Script Mode
CARD
07
32

        A B C D E F
🔔
1/1