# justify.py
# leen saflo
''' function that'll take a list of strings and a field size then return one equally spaced out string.This deals with
empty lists, unequal space distribution and a field size smaller than the number of characters'''
def justify(stringList, fieldSize):
if len(stringList) == 1: # case 1 - when there's one word in the list, return it with remaining spaces
return stringList[0] + " " * (fieldSize - len(stringList[0]))
if len(stringList) < 1: # case 2 - if theres no characters, return spaces for field size
return fieldSize * " "
used = 0
for i in range(len(stringList)):
used += len(stringList[i])
space = fieldSize - used
numGaps = len(stringList) - 1 # number of gaps needed
numSpaces = space // numGaps # number of spaces that go into each gap
leftover = space % numGaps # spaces that dont fit into gaps
# building the justified string
justified = ""
if fieldSize < used:
for i in range(len(stringList) - 1):
justified += stringList[i] + " "
return justified + stringList[-1] # last word without extra space
else:
for i in range(len(stringList) - 1):
justified += stringList[i] + " " * numSpaces # Add a word followed by evenly distributed spaces.
if leftover > 0:
justified += " " # add space if there are leftovers
leftover -= 1 # take out spaces left to distribute
return justified + stringList[-1]
# grid.py
# leen saflo
# make grid using recursion
def grid(x, y, size):
if size > 5: # base case - if size reaches 5 or less
draw.rect(screen, (0,255,0), (x,y, size, size), 1)
halfSize = size / 2 # smaller version
# draw smaller grids at each corner
grid(x,y, halfSize)
grid(x + halfSize , y, halfSize)
grid(x, y + halfSize, halfSize)
grid(x + halfSize, y + halfSize, halfSize)