Calvin (Deutschbein)
W5Wed: 25 Sep
def print_first_vowel(s:str):
for c in s:
if c in "aeiou":
print(c)
return
def print_all_vowels(s:str):
for c in s:
if c in "aeiou":
print(c)
return
def vowel_print(s:str):
for c in s:
if c in "aeiou":
print(c)
return
for c in s: # line with a def, if, for, while ending with a :
print(c) # line at the same indentation level
Method | Description |
---|---|
string.find(pattern) |
Returns the first index of 'pattern' in 'string', or '-1' if it does not appear |
string.find(pattern, k) |
Same as the one-argument version, but starts searching at index 'k |
string.rfind(pattern) |
Returns the last index of 'pattern' is 'string', or '-1' if missing |
string.rfind(pattern, k) |
Same as the one-argument version, but searches backwards from index 'k |
string.startswith(prefix) |
Returns 'True' if the string starts with 'prefix |
string.endswith(suffix) |
Returns 'True' if the string ends with 'suffix |
>>> str = "hello world"
>>>
>>> str.rfind('o')
7
>>> str.rfind('lo')
3
>>> str.find('o')
4
>>> str.find('l')
2
>>> str.rfind('l')
9
>>> str.startswith("he")
True
>>> str.startswith("hl")
False
>>> str.endswith("lo")
False
>>> str.endswith("ld")
True
>>>
import random
>>> import random
>>> random.random() # will be between 0 and 1
0.14444921034146263
>>> import random
>>> random.random() # will be between 0 and 1
0.14444921034146263
>>> from random import *
>>> random() # don't need prefix with 'from _ import *'
>>> random() # don't need prefix with 'from _ import *'
0.1254407334152583
import random
random.randint(minv, maxv) |
Returns an integer between minv and maxv, inclusive |
random.randrange(limit) |
Returns an integer from 0 up to but not including limit |
random.randrange(start,limit) |
Returns an integer from start up to but not including limit |
random.random() |
Returns a random float between 0 and 1 |
random.uniform(minv, maxv) |
Returns a random float between minv and maxv |
random.choice(str) |
Returns a random character from the string |
random.shuffle(str) |
Randomly reorders the collection of characters |
import random
from english import *
def random_five_letter_word():
random.shuffle(ENGLISH_WORDS)
for word in ENGLISH_WORDS:
if len(word) == 5:
return word
def random_no_repeated():
random.shuffle(ENGLISH_WORDS)
for word in ENGLISH_WORDS:
if not contains_repeated_letters(word):
return word
import pgl
gw = GWindow(400,400) # similar to: gw = WordleGWindow()
from pgl import *
gw = GWindow(500, 200) # like WordleGWindow
rect = GRect(150,50,0,0) # answer = "blimp"
rect.set_color("Blue") # answer.upper()
rect.set_filled(True)
gw.add(rect) # like word_to_row(answer)
Functions to create simple geometric objects:
GRect( x, y, width, height )
GOval( x, y, width, height )
GLine( x1, y1, x2, y2 )
(0,0) | (0,1) | (0,2) | (0,3) | (0,4) |
(1,0) | (1,1) | (1,2) | (1,3) | (1,4) |
(2,0) | (2,1) | (2,2) | (2,3) | (2,4) |
(3,0) | (3,1) | (3,2) | (3,3) | (3,4) |
(4,0) | (4,1) | (4,2) | (4,3) | (4,4) |
(5,0) | (5,1) | (5,2) | (5,3) | (5,4) |
gw.set_square_letter(1, 3, '$') # example
$ | ||||
def redact(word:str, secret:str) -> str:
for i in range(len(word)):
if word[i] == secret[i]:
word = word[:i] + '*' + word[i+1:]
DEBUG and print('Redact '+secret[i]+' at '+str(i))
return word