Assignment 6 
Ryan Gogats 
52 
1. 
1) b 
2) b 
3) a 
4) b 
5) b 
6) c 
7) b 
8) b 
9) b 
10) b 
2. 
"""Program name: randomDuplicates.pyAuthor name: Ryan GogatsGenerates 10 random 
integers from 1 to 5. Displays number of appearancesof each number, and then 
prints the list without duplicates."""import randomdef main(): randList = 
genRandom(1, 5, 10) listAppear = countAppearance(randList) print("1:", 
listAppear[0], "times") print("2:", listAppear[1], "times") print("3:", 
listAppear[2], "times") print("4:", listAppear[3], "times") print("5:", 
listAppear[4], "times") nonDupeList = removeDupes(randList) 
print(nonDupeList) def genRandom(low, high, count): numberList = [] for 
number in range(count): numberList.append(random.randint(low, high)) 
return numberListdef countAppearance(aList): countList = [0, 0, 0, 0, 0] 
for index in range(len(countList) + 1): for item in aList: if 
item == index: countList[index-1] += 1 return countListdef 
removeDupes(aList): cleanList = [] for element in aList: if element 
not in cleanList: cleanList.append(element) return cleanList 
--- 
>>> main()1: 1 times2: 5 times3: 1 times4: 0 times5: 3 times[2, 5, 3, 1] 
>>> main()1: 4 times2: 1 times3: 1 times4: 2 times5: 2 times[2, 1, 5, 3, 4] 
>>> main()1: 1 times2: 1 times3: 3 times4: 3 times5: 2 times[5, 1, 4, 3, 2] 
3. 
"""Program name: roundRobinTournament.pyAuthor name: Ryan GogatsCreates a round 
robin-style schedule given a number of players.Input: number of players 
Computation: generates two lists which cycle to determine what games are 
being playedOutput: schedule displaying who is playing who in each round""" 
import mathdef main(): players = int(input("Enter number of players: ")) 
fullSchedule = printSchedule(players)def getNumRounds(numTeams): numRounds = 
numTeams - 1 return numRounds def printSchedule(numPlayers): cycleA 
= [] cycleB = [] for eachPlayer in range(1, math.ceil(numPlayers / 2)): 
cycleA.append(eachPlayer + 1) for eachPlayer in range(numPlayers, 
math.floor(numPlayers / 2), -1): cycleB.append(eachPlayer) numRounds = 
getNumRounds(numPlayers) cycleA.insert(0, 1) print("-Round", 1, "-") 
for player in cycleA: print("Player", player,"vs. Player", 
 
cycleB[cycleA.index(player)]) print("
n") cycleA.pop(0) 
newCycleA = rotateCycle(cycleA, numPlayers) newCycleB = rotateCycle(cycleB, 
numPlayers) for eachRound in range(2, numRounds + 1): 
newCycleA.insert(0, 1) print("-Round", eachRound, "-") for player 
in newCycleA: print("Player", player, "vs. Player", 
 
newCycleB[newCycleA.index(player)]) newCycleA.pop(0) 
newerA = rotateCycle(newCycleA, numPlayers) newerB = 
rotateCycle(newCycleB, numPlayers) newCycleA = newerA newCycleB = 
newerB print("
n")def rotateCycle(cycle, numPlayers): newCycle = [] 
for item in cycle: if item > 2: newCycle.append(item - 1) 
else: newCycle.append(numPlayers) return newCycle
--- 
Enter number of players: 4
-Round 1 -Player 1 vs. Player 4
Player 2 vs. Player 3


- 
Round 2 -Player 1 vs. Player 3


Player 4
 vs. Player 2-Round 3


 -Player 1 vs. Player 
2Player 3


 vs. Player 4
 
Enter number of players: 5
Please enter a positive even number. 
Enter number of players: 6
-Round 1 -Player 1 vs. Player 6
Player 2 vs. Player 5
 
Player 3


 vs. Player 4
-Round 2 -Player 1 vs. Player 5
Player 6
 vs. Player 4
Player 
2 vs. Player 3


-Round 3


 -Player 1 vs. Player 4
Player 5
 vs. Player 3


Player 6
 vs. 
Player 2-Round 4
 -Player 1 vs. Player 3


Player 4
 vs. Player 2Player 5
 vs. Player 
6
-Round 5
 -Player 1 vs. Player 2Player 3


 vs. Player 6
Player 4
 vs. Player 5
 
4
. 
"""Program name: pigLatinFileConvert.pyAuthor name: Ryan GogatsReads a text file 
and creates a new one with the text converted to Pig Latin."""def main(): 
fileName = input("Enter the name of your file: ") valid = 
validateFile(fileName) if valid == True: f = open(fileName, "r") 
fileName = stripExtension(fileName) newFileName = fileName + 
"PigLatin.txt" pigFile = open(newFileName, "w") for line in f: 
pigLine = translateLine(line) pigFile.write(pigLine) 
pigFile.close()def translateLine(line): #uses pigWord function to translate 
entire line to Pig Latin pigLine = "" words = line.split() for word in 
words: translation = pigWord(word) pigLine += translation + " " 
pigLine += "n" return pigLinedef pigWord(word): #converts single word 
to Pig Latin and accounts for punctuation/capitalization noPuncWord = "" 
punctuation = "" upperCaseWord = False for char in word: if 
char.isalnum(): if char.isupper(): char = char.lower() 
noPuncWord += char upperCaseWord = True else: 
noPuncWord += char else: punctuation += char 
if len(noPuncWord) > 2: noPuncWord = noPuncWord[1:len(noPuncWord)] + 
noPuncWord[0] + 'ay' pigWord = noPuncWord + punctuation else: 
pigWord = noPuncWord + punctuation if upperCaseWord: 
firstLetter = pigWord[0] upperLetter = firstLetter.upper() pigWord 
= upperLetter + pigWord[1:] return pigWord else: return pigWord 
def validateFile(file): #finds out if user-inputted file exists try: 
f = open(file, "r") return True except IOError: print("Please 
enter an existing filen") return Falsedef stripExtension(file): 
#takes file extension out of a file name fileName = str(file) fileNameList 
= fileName.rsplit(".") strippedFileName = str(fileNameList[0]) return 
strippedFileName 
--- 
-testFile.txt- 
Hello there! How are you? 
Pretty good. 
OK, good bye. 
-testFilePigLatin.txt- 
Ellohay heretay! Owhay reaay ouyay? 
Rettypay oodgay. 
Ok, oodgay yebay. 
-test2File.txt- 
The most terrifying fact about the universe is not that it is hostile but that 
it is indifferent; but if we can come to terms with this indifference and accept 
the challenges of life within the boundaries of death, however mutable man may 
be able to make them, our existence as a species can have genuine meaning and 
fulfillment. However vast the darkness, we must supply our own light.
-test2FilePigLatin.txt- 
Hetay ostmay errifyingtay actfay boutaay hetay niverseuay is otnay hattay it is 
ostilehay utbay hattay it is ndifferentiay; utbay if we ancay omecay to ermstay 
ithway histay ndifferenceiay ndaay cceptaay hetay hallengescay of ifelay 
ithinway hetay oundariesbay of eathday, oweverhay utablemay anmay aymay be 
bleaay to akemay hemtay, uroay xistenceeay as a peciessay ancay avehay enuinegay 
eaningmay ndaay ulfillmentfay. Oweverhay astvay hetay arknessday, we ustmay 
upplysay uroay wnoay ightlay.

Assignment6

  • 1.
    Assignment 6 RyanGogats 52 1. 1) b 2) b 3) a 4) b 5) b 6) c 7) b 8) b 9) b 10) b 2. """Program name: randomDuplicates.pyAuthor name: Ryan GogatsGenerates 10 random integers from 1 to 5. Displays number of appearancesof each number, and then prints the list without duplicates."""import randomdef main(): randList = genRandom(1, 5, 10) listAppear = countAppearance(randList) print("1:", listAppear[0], "times") print("2:", listAppear[1], "times") print("3:", listAppear[2], "times") print("4:", listAppear[3], "times") print("5:", listAppear[4], "times") nonDupeList = removeDupes(randList) print(nonDupeList) def genRandom(low, high, count): numberList = [] for number in range(count): numberList.append(random.randint(low, high)) return numberListdef countAppearance(aList): countList = [0, 0, 0, 0, 0] for index in range(len(countList) + 1): for item in aList: if item == index: countList[index-1] += 1 return countListdef removeDupes(aList): cleanList = [] for element in aList: if element not in cleanList: cleanList.append(element) return cleanList --- >>> main()1: 1 times2: 5 times3: 1 times4: 0 times5: 3 times[2, 5, 3, 1] >>> main()1: 4 times2: 1 times3: 1 times4: 2 times5: 2 times[2, 1, 5, 3, 4] >>> main()1: 1 times2: 1 times3: 3 times4: 3 times5: 2 times[5, 1, 4, 3, 2] 3. """Program name: roundRobinTournament.pyAuthor name: Ryan GogatsCreates a round robin-style schedule given a number of players.Input: number of players Computation: generates two lists which cycle to determine what games are being playedOutput: schedule displaying who is playing who in each round""" import mathdef main(): players = int(input("Enter number of players: ")) fullSchedule = printSchedule(players)def getNumRounds(numTeams): numRounds = numTeams - 1 return numRounds def printSchedule(numPlayers): cycleA = [] cycleB = [] for eachPlayer in range(1, math.ceil(numPlayers / 2)): cycleA.append(eachPlayer + 1) for eachPlayer in range(numPlayers, math.floor(numPlayers / 2), -1): cycleB.append(eachPlayer) numRounds = getNumRounds(numPlayers) cycleA.insert(0, 1) print("-Round", 1, "-") for player in cycleA: print("Player", player,"vs. Player", cycleB[cycleA.index(player)]) print(" n") cycleA.pop(0) newCycleA = rotateCycle(cycleA, numPlayers) newCycleB = rotateCycle(cycleB, numPlayers) for eachRound in range(2, numRounds + 1): newCycleA.insert(0, 1) print("-Round", eachRound, "-") for player in newCycleA: print("Player", player, "vs. Player", newCycleB[newCycleA.index(player)]) newCycleA.pop(0) newerA = rotateCycle(newCycleA, numPlayers) newerB = rotateCycle(newCycleB, numPlayers) newCycleA = newerA newCycleB = newerB print(" n")def rotateCycle(cycle, numPlayers): newCycle = [] for item in cycle: if item > 2: newCycle.append(item - 1) else: newCycle.append(numPlayers) return newCycle
  • 2.
    --- Enter numberof players: 4 -Round 1 -Player 1 vs. Player 4 Player 2 vs. Player 3 - Round 2 -Player 1 vs. Player 3 Player 4 vs. Player 2-Round 3 -Player 1 vs. Player 2Player 3 vs. Player 4 Enter number of players: 5 Please enter a positive even number. Enter number of players: 6 -Round 1 -Player 1 vs. Player 6 Player 2 vs. Player 5 Player 3 vs. Player 4 -Round 2 -Player 1 vs. Player 5 Player 6 vs. Player 4 Player 2 vs. Player 3 -Round 3 -Player 1 vs. Player 4 Player 5 vs. Player 3 Player 6 vs. Player 2-Round 4 -Player 1 vs. Player 3 Player 4 vs. Player 2Player 5 vs. Player 6 -Round 5 -Player 1 vs. Player 2Player 3 vs. Player 6 Player 4 vs. Player 5 4 . """Program name: pigLatinFileConvert.pyAuthor name: Ryan GogatsReads a text file and creates a new one with the text converted to Pig Latin."""def main(): fileName = input("Enter the name of your file: ") valid = validateFile(fileName) if valid == True: f = open(fileName, "r") fileName = stripExtension(fileName) newFileName = fileName + "PigLatin.txt" pigFile = open(newFileName, "w") for line in f: pigLine = translateLine(line) pigFile.write(pigLine) pigFile.close()def translateLine(line): #uses pigWord function to translate entire line to Pig Latin pigLine = "" words = line.split() for word in words: translation = pigWord(word) pigLine += translation + " " pigLine += "n" return pigLinedef pigWord(word): #converts single word to Pig Latin and accounts for punctuation/capitalization noPuncWord = "" punctuation = "" upperCaseWord = False for char in word: if char.isalnum(): if char.isupper(): char = char.lower() noPuncWord += char upperCaseWord = True else: noPuncWord += char else: punctuation += char if len(noPuncWord) > 2: noPuncWord = noPuncWord[1:len(noPuncWord)] + noPuncWord[0] + 'ay' pigWord = noPuncWord + punctuation else: pigWord = noPuncWord + punctuation if upperCaseWord: firstLetter = pigWord[0] upperLetter = firstLetter.upper() pigWord = upperLetter + pigWord[1:] return pigWord else: return pigWord def validateFile(file): #finds out if user-inputted file exists try: f = open(file, "r") return True except IOError: print("Please enter an existing filen") return Falsedef stripExtension(file): #takes file extension out of a file name fileName = str(file) fileNameList = fileName.rsplit(".") strippedFileName = str(fileNameList[0]) return strippedFileName --- -testFile.txt- Hello there! How are you? Pretty good. OK, good bye. -testFilePigLatin.txt- Ellohay heretay! Owhay reaay ouyay? Rettypay oodgay. Ok, oodgay yebay. -test2File.txt- The most terrifying fact about the universe is not that it is hostile but that it is indifferent; but if we can come to terms with this indifference and accept the challenges of life within the boundaries of death, however mutable man may be able to make them, our existence as a species can have genuine meaning and fulfillment. However vast the darkness, we must supply our own light.
  • 3.
    -test2FilePigLatin.txt- Hetay ostmayerrifyingtay actfay boutaay hetay niverseuay is otnay hattay it is ostilehay utbay hattay it is ndifferentiay; utbay if we ancay omecay to ermstay ithway histay ndifferenceiay ndaay cceptaay hetay hallengescay of ifelay ithinway hetay oundariesbay of eathday, oweverhay utablemay anmay aymay be bleaay to akemay hemtay, uroay xistenceeay as a peciessay ancay avehay enuinegay eaningmay ndaay ulfillmentfay. Oweverhay astvay hetay arknessday, we ustmay upplysay uroay wnoay ightlay.