Calvin (Deutschbein)
W5Fri: 27 Sep
def redact(word:str, secret:str) -> str:
for i in range(len(word)):
if word[i] == secret[i]:
word = word[:i] + '*' + word[i+1:] # or ' ' etc
DEBUG and print('Redact '+secret[i]+' at '+str(i))
return word
>>> DEBUG = True
>>> redact('glass','sassy')
Redact s at 3
'gla*s'
def censor_letter(letter:str, word:str) -> str:
# remove ONLY THE FIRST case of letter in word
for index in range(len(word)):
if word[index] == letter:
return word[:index] + '-' + word[index+1:]
return word # if letter isn't present
>>> censor_letter('s', 'sas*y')
'-as*y'
def censor(word:str, secret:str) -> str:
for letter in secret:
word = censor_letter(letter, word)
return word
>>> censor("sas*y", "claps")
'--s*y'
def censor(word:str, secret:str) -> str:
for letter in secret:
censored = censor_letter(letter, word)
if censored != word:
DEBUG and print("Censored " + letter)
word = censored
return word
Censored a
Censored s
'--s*y'
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
$ | ||||
GWindow
gw = GWindow(width, height)
gw.add(object) |
Adds an object to the window |
gw.add(object, x, y) |
Adds an object to the window after moving it to (x,y) |
gw.remove(object) |
Removes an object from the window |
gw.get_width() |
Returns the width of the graphics window in pixels |
gw.get_height() |
Returns the height of the graphics window in pixels |
GObject
object = GRect(150,50,0,0) # like saying answer = "blimp"
object.get_x() |
Returns the x coordinate of this object |
object.get_y() |
Returns the y coordinate of this object |
object.get_width() |
Returns the width of this object |
object.get_height() |
Returns the height of this object |
object.set_color(color) |
Sets the color of the object to the specified color |
GFillableObject
gw.set_square_color(row, col, color)
object = GRect(150,50,0,0)
object.set_filled(bool) |
Sets the fill state of the object |
object.set_fill_color(color) |
Sets the color to be used to fill the interior, otherwise same as the outer line |
object.get_fill_color() |
Gets the current color used to display the object interior |
object.is_filled() |
Returns True or False depending on whether the object is currently filled |
|
gw = GWindow(400, 400)
head = GOval(20, 20, 360, 360)
head.set_fill_color("yellow")
head.set_filled(True)
gw.add(head)
reye = GOval(110, 100, 40, 40)
reye.set_filled(True)
gw.add(reye)
leye = GOval(250, 100, 40, 40)
leye.set_filled(True)
gw.add(leye)
mouth = GLine(150,250,250,250)
mouth.set_line_width(5)
gw.add(mouth) |