Skip to content

Instantly share code, notes, and snippets.

@rlieberman
Created May 11, 2015 20:31
Show Gist options
  • Save rlieberman/fbd3a77302ae10df01ab to your computer and use it in GitHub Desktop.
Save rlieberman/fbd3a77302ae10df01ab to your computer and use it in GitHub Desktop.
Module for Shapeshift functions (RWET midterm)
#Module for shapeshift functions
import sys
import random
words_by_len = dict() #create an empty dictionary to store words by their length
#Create a dictionary where the key is the # of characters in the word and the value is a list of words
for line in sys.stdin: #load each line from standard in
line = line.strip() #strip the whitespace
words = line.split() #split each line into a list of words
for word in words:
word = word.strip('!.?,;:-') #NOT WORKING - strip the punctuation from each word
charcount = len(word) #assign the length of the word to a variable 'charcount'
if charcount not in words_by_len: #if that length is not yet in the dictionary as a key
words_by_len[charcount] = [] #add it to the dictionary as a key
if word not in words_by_len[charcount]: #if statement to get rid of duplicates
words_by_len[charcount].append(word) #words_by_len[charcount] evaluates to a list - add each word to that list
#-------------------------------------SHAPE 1: SAWTOOTH-----------------------------------
#-------------------------------------ONE WORD PER LINE-----------------------------------
def sawToothLeft(startCharCount, endCharCount):
for x in range(8):
for i in range(startCharCount,endCharCount):
print random.choice(words_by_len[i])
for i in reversed(range(startCharCount,endCharCount-1)):
print random.choice(words_by_len[i])
def sawToothRight(startCharCount, endCharCount):
for x in range(8):
for i in range(startCharCount,endCharCount):
print random.choice(words_by_len[i]).rjust(15)
for i in reversed(range(startCharCount,endCharCount-1)):
print random.choice(words_by_len[i]).rjust(15)
#----------------------------------SHAPE 2: DIAMOND CHAIN---------------------------------
#---------------------MULTIPLE WORDS PER LINE-----LEFT OR RIGHT JUSTIFY-------------------
#-------------------------1 X CHAR WORD, 2 X CHAR WORD, 3 TX CHAR WORD--------------------
def diamondChain(diamondWidth, charCount):
for x in range(10):
for i in range(diamondWidth):
print " ".join(random.sample(words_by_len[charCount], i)).center(50)
for i in range(diamondWidth, 0, -1):
print " ".join(random.sample(words_by_len[charCount], i)).center(50)
#-----------------------------------SHAPE 3: HOURGLASS------------------------------------
#---------------------------------MULTIPLE WORDS PER LINE---------------------------------
def hourGlass(width, charCount):
for i in range(width, 0, -1):
print " ".join(random.sample(words_by_len[charCount], i)).center(50)
for i in range(width+1):
print " ".join(random.sample(words_by_len[charCount], i)).center(50)
#-----------------------------------SHAPE 4: CHECKERBOARD------------------------------------
#---------------------------------MULTIPLE WORDS PER LINE---------------------------------
def checkerBoard (charCount, spacing):
centerOffset = 0
for x in range(8):
for i in range(4):
print random.choice(words_by_len[charCount]).center(centerOffset)
centerOffset = centerOffset + spacing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment