#***************************************************************** # FUNCTION LIBRARY Kevin Stys Period 4/5 # FILENAME = "myFunctions.py" #***************************************************************** # # >>>>>>>> INSTRUCTIONS <<<<<<<<<< # # This file contains user defined functions that will be developed # over the remainder of the semester. The functions defined in this # file will include processing that determines primes, searches # strings, searches lists, print lists, etc. and will be used in # forthcoming projects # # NOTE!!! The myFunctions.py file must be in the same directory # as the program that imports it. #***************************************************************** import math import string import random # ***************************************************************** # # The function sumInt(max) sums all integers between 1 and max # inclusive. # # ***************************************************************** def sumInt(max): max = (max * (max + 1)) / 2 return max # ***************************************************************** # # The function isEven(val) determines if an integer value is even. The # function accepts an integer value and returns a boolean of True # if the integer is even. # # ***************************************************************** def isEven(val): remainder = val%2 if remainder == 0: return True else: return False # ***************************************************************** # # The function isPrime(val) determines if an integer value is prime. The # function accepts an integer value and returns a boolean of True # if the integer is prime. # # ***************************************************************** def isPrime(val): if val == 0: return False if val == 1: return False for x in range(2,int(math.sqrt(val)+1)): if (val % x) == 0: return False return True #****************************************************************** # # The function backwardStr(Str) will flip a given string # so that when the user searches a list for a string, the # search can handle forwards and backwards strings. # #****************************************************************** def backwardStr(Str): idx = len(Str) - 1 nStr = "" while idx >= 0: nStr = nStr + Str[idx] idx = idx - 1 return nStr # ***************************************************************** # # The function searchList(Listx,strIn) determines if a strIn exists # in Listx. Return -1 if string not found else return the starating # location of strIn in the Listx. # # ***************************************************************** def searchList(Listx,strIn): idx1 = 0 idx2 = 0 maxIdx = len(Listx) maxIdx2 = len(strIn) Return = [-1] while idx1 < maxIdx and idx2 < maxIdx2: if maxIdx2 > maxIdx: Return = [-1] break IDX=idx1 while Listx[idx1] == strIn[idx2]: idx1 = idx1 + 1 idx2 = idx2 + 1 if idx2==maxIdx2: Return=IDX if idx1 == maxIdx or idx2 == maxIdx2: break idx2=0 idx1 = IDX + 1 return Return # ***************************************************************** # # The function listLower(Listx) change all letters in Listx to # lower case. # # ***************************************************************** def listLower(Listx): List = [] idx = 0 string = "" maxIdx = len(Listx) while idx < maxIdx: string = string + Listx[idx] idx = idx + 1 string = string.lower() idx = 0 while idx < maxIdx: List = List + [string[idx]] idx = idx + 1 return List #***************************************************************** # SIGNATURE BANNER PAGE FUNCTION NAME / PERIOD # #**************************************************************************** # # Create a signature banner display that contains: # 1) Class Name Your Name Period # 2) Proj/Quiz/Guided Code Information # # <<<<<<< EXAMPLES >>>>>>> # # ************************************************************************ # * COMP PROG 1 -- QUIZ: OCTOBER 23, 2013 * # * SUBMITTED BY: Mr. Smith Perod 4/5 * * # ************************************************************************ # # ************************************************************************ # * COMP PROG 1 -- PROJECT: OCTOBER 23, 2013 * # * SUBMITTED BY: Mr. Smith Perod 10 * * # ************************************************************************ # # ************************************************************************ # * COMP PROG 1 -- GUIDED CODE: OCTOBER 23, 2013 * # * SUBMITTED BY: Mr. Smith Perod 4/5 * * # ************************************************************************ # # # # The function will accept a string input (str1) to fill in # the text represented by xxxxxxxxxxxxxxxxxx. # # ************************************************************************ # * COMP PROG 1 -- xxxxxxxxxxxxxxxxxxxxxx * # * SUBMITTED BY: Mr. Smith Perod 4/5 * * # ************************************************************************ # # The 2 lines must be centered in the frame. # Assume a frame width of 70 Characters with a 3 character indent. # # # #**************************************************************************** def mybanner(str1): stars = "*" * 70 string = "COMP PROG 1 -- " + str1 strang = "SUBMITTED BY: Kevin Stys Period 4/5" numOfStars = len(stars) - 1 lenOfString = len(string) lenOfStrang = len(strang) spacesForBottomString = " " * ((numOfStars/2) - (lenOfStrang/2)) indent = " " * 3 remainder = lenOfString%2 if remainder == 0: spacesForTopString = " " * ((numOfStars/2) - (lenOfString/2)) else: spacesForTopString = " " * ((numOfStars/2) - (lenOfString/2) - 1) print indent + stars print indent + "*" + (" " * ((numOfStars/2) - (lenOfString/2))) + string + spacesForTopString + "*" print indent + "*" + (" " * ((numOfStars/2) - (lenOfStrang/2))) + strang + (" " * ((numOfStars/2) - (lenOfStrang/2) - 1)) + "*" print indent + stars #**************************************************************************** # # INTEGER LIST PROCESSOR # # Process a List that contains Lists as elements. Each element of the List # is a subList of integers. # # The function will iterate through a List's subLists and return the # following: # a) A List that contains the original subLists # b) Each original sublist extended to show the number of integers, # the sum of the integers, and the product of the integers. # # The function must work for any size List and subLists. # # # EXAMPLES. # ORIGINAL LIST: [[1,3],[2,3,5],[6,2,2],[6]] # NEW LIST: [[1,3,'->',2,4,3],[2,3,5,'->',3,10,30],[6,2,2,'->',3,10,24], # [6,'->',1,6,6]] # # #**************************************************************************** def listIntSumProd(Lx): innerIdx = 0 outerIdx = 0 newList = [] maxIdxOuter = len(Lx) multi = 1 multanswer = 1 while outerIdx < maxIdxOuter: maxIdxInner = len(Lx[outerIdx]) while multi <= maxIdxInner: multanswer = multanswer * Lx[outerIdx][innerIdx] multi = multi + 1 innerIdx = innerIdx + 1 newList = newList + [Lx[outerIdx] + ["->"] + [maxIdxInner] + [sum(Lx[outerIdx])] + [multanswer]] outerIdx = outerIdx + 1 innerIdx = 0 multi = 1 multanswer = 1 return newList # ***************************************************************** # # The function print2DList(Listx) will print the list in the form of # a n x m matrix. # Examples. # # # GIVEN: L = [['a', 's', 'B', 'e', 'w'], ['n', 'o', 'U', 'a', 'i'], # ['d', 'o', 'S', 'n', 'n'], ['e', 'n', 'a', 't', 'v']] # # Print as: a s B e w # n o U a i # d o S n n # e n a t v # # # !!NOTE!! THIS FUNCTIONS IS ALLOWED TO USE PRINT STATEMENTS # # ***************************************************************** def print2DList(Listx): if Listx == [-1]: return [-1] outerIdx = 0 maxOuterIdx = len(Listx) while outerIdx < maxOuterIdx: string = "" innerIdx = 0 maxInnerIdx = len(Listx[outerIdx]) while innerIdx < maxInnerIdx: string = string + str(Listx[outerIdx][innerIdx]) + " " innerIdx = innerIdx + 1 outerIdx = outerIdx + 1 print string # ***************************************************************** # # The function listLower2D(Listx) change all letters in a two dimensional # list to lower case. # # ***************************************************************** def listLower2D(Listx): List = [] outerIdx = 0 maxOuterIdx = len(Listx) while outerIdx < maxOuterIdx: string = "" innerIdx = 0 maxInnerIdx = len(Listx[outerIdx]) while innerIdx < maxInnerIdx: string = string + Listx[outerIdx][innerIdx] innerIdx = innerIdx + 1 string = string.lower() List = List + [list(string)] outerIdx = outerIdx + 1 return List # ***************************************************************** # # The function list1DCharCap(Listx,strt,length) will capitalize # the letters in a given range of a List. # # # GIVEN: L = ['a', 's', 'b', 'u', 'c', 'k', 'e', 't', 'a', 'i'] # # The folloiwng function call --> list1DCharCap(L,2,6) # will return: ['a', 's', 'B', 'U ,'C', 'K', 'E', 'T', 'a', 'i'] # # # # # ***************************************************************** def list1DCharCap(Listx,strt,length): List = [] idx = 0 endSpot = (strt + length) - 1 maxIdx = len(Listx) while idx < maxIdx: if idx == strt and strt <= endSpot: List = List + [Listx[idx].upper()] strt = strt + 1 else: List = List + [Listx[idx]] idx = idx + 1 return List #************************************************************************** # # FUNCTION NAME: list2DCharCap(List,drx,strtPos,stopPos) # # # The function list2DCharCap(List,drx,strtPos,stopPos) will capitalize # the letters in a given range of a List. The "drx" parameter will accept # values h(horizontal), v(vertical), or d(diagonal). The strtPos and # stopPos are Lists that contain a (row,col) pair. # # The function will return the original list with the requested postions # capitalized. # # # GIVEN: Lx = [['a', 'b', 'c', 'd', 'w'],['e', 'a', 'g', 'r', 'x'], # ['t', 'l', 'k', 'o', 'y'],['m', 'k', 'o', 'p', 'z']] # #the matrix representation of the list is: abcdw # eagrx # tlkoy # mkopz # # # printing the List after calling list2DCharCap(Lx,'d',[0,2],[3,0]) is: # # abCdw # eAgrx # Tlkoy # mkopz # # # printing the List after calling list2DCharCap(Lx,'v',[0,3],[3,3]) is: # # abcDw # eagRx # tlkOy # mkoPz # # # ***************************************************************** def list2DCharCap(Lst,drx,StrtPos,stopPos): if drx == "H" or drx == "h": if StrtPos[1] > stopPos[1]: newStrtPos = stopPos newStopPos = StrtPos else: newStrtPos = StrtPos newStopPos = stopPos elif drx == "V" or drx == "v" or drx == "D" or drx == "d": if StrtPos[0] > stopPos[0]: newStrtPos = stopPos newStopPos = StrtPos else: newStrtPos = StrtPos newStopPos = stopPos upperdIdx = newStrtPos[0] upperdIdx2 = newStrtPos[1] dIdx = 0 nList = [] while dIdx < len(Lst): dIdx2 = 0 piece= [] while dIdx2 < len(Lst[dIdx]): if [dIdx,dIdx2] == [upperdIdx,upperdIdx2]: piece = piece+ [string.upper(Lst[dIdx][dIdx2])] if [dIdx,dIdx2] != [newStopPos[0],newStopPos[1]]: if drx == "H" or drx == "h": upperdIdx2 = upperdIdx2 + 1 elif drx == "V" or drx == "v": upperdIdx = upperdIdx + 1 elif drx == "D" or drx == "d": if newStrtPos[1] > newStopPos[1]: upperdIdx = upperdIdx + 1 upperdIdx2 = upperdIdx2 - 1 elif newStrtPos[1] < newStopPos[1]: upperdIdx = upperdIdx + 1 upperdIdx2 = upperdIdx2 + 1 else: piece = piece+ [Lst[dIdx][dIdx2]] dIdx2 = dIdx2 + 1 nList = nList + [piece] dIdx = dIdx + 1 return nList #************************************************************************** # # FUNCTION NAME: stringtoList(Lst) # # # The function stringtoList(Lst) will convert a list of strings to a 2-D # list of sublists. Each sublist will contain a string converted into # a sublist of individual charaters # # # GIVEN: the list ListStr = ['abcdw','eagrx','tlkoy','mkopz'] # # Calling stringtoList(Liststr) will return # # [['a', 'b', 'c', 'd', 'w'],['e', 'a', 'g', 'r', 'x'], # ['t', 'l', 'k', 'o', 'y'],['m', 'k', 'o', 'p', 'z']] # # # # # # ***************************************************************** def stringtoList(Lst): ListOuter = [] outerIdx = 0 maxOuterIdx = len(Lst) while outerIdx < maxOuterIdx: ListOuter = ListOuter + [list(Lst[outerIdx])] outerIdx = outerIdx + 1 return ListOuter #*********************************************************************** # HORIZONTAL SEARCH FUNCTION # hSearch(Lst,Str) # #*********************************************************************** # # The function receives a 2D list and search string. # If string is found return list with string capitaized. # If string is not found return [-1] # #*********************************************************************** def hSearch(Lst,Str): Lst = listLower2D(Lst) Str = Str.lower() outerIdx = 0 maxOuterIdx = len(Lst) Return = [-1] while outerIdx < maxOuterIdx: innerIdx = 0 maxInnerIdx = len(Lst[outerIdx]) maxStringIdx = len(Str) while innerIdx < maxInnerIdx: if maxStringIdx > maxInnerIdx: return [-1] if Lst[outerIdx][innerIdx:(innerIdx + maxStringIdx)] == list(Str[0:maxStringIdx]) or list(Str[0:maxStringIdx]) == Lst[outerIdx][innerIdx:(innerIdx + maxStringIdx)][::-1]: end = innerIdx + maxStringIdx while innerIdx < end: Lst[outerIdx][innerIdx] = Lst[outerIdx][innerIdx].upper() innerIdx = innerIdx + 1 Return = Lst break if Lst[outerIdx][innerIdx:(innerIdx + maxStringIdx)] != list(Str[0:maxStringIdx]) or list(Str[0:maxStringIdx]) != Lst[outerIdx][innerIdx:(innerIdx + maxStringIdx)][::-1]: innerIdx = innerIdx + 1 outerIdx = outerIdx + 1 return Return #*********************************************************************** # VERTICAL SEARCH FUNCTION # vSearch(Lst,Str) # #*********************************************************************** # # The function re******************************************************* # # The function receives a 2D list and search string. # If the string is found return list with string capitaized. # If the tring is not found return [-1] # #*********************************************************************** def vSearch(Lst,Str): Lst = listLower2D(Lst) Str = Str.lower() idx2 = 0 while idx2 < len(Lst[0]): part = [] idx = 0 while idx < len(Lst): part = part + [Lst[idx][idx2]] idx = idx + 1 if searchList(part,Str) != [-1]: locIdx = searchList(part,Str) startPos = [locIdx,idx2] locIdx2 = locIdx - 1 + len(Str) stopPos = [locIdx2,idx2] drx = "v" return list2DCharCap(Lst,drx,startPos,stopPos) elif searchList(part,backwardStr(Str)) != [-1]: locIdx = searchList(part,backwardStr(Str)) startPos = [locIdx,idx2] locIdx2 = locIdx - 1 + len(Str) stopPos = [locIdx2,idx2] drx = "v" return list2DCharCap(Lst,drx,startPos,stopPos) idx2 = idx2 + 1 return [-1] #*********************************************************************** # DIAGONAL SEARCH FUNCTION # dSearch(Lst,Str) # #*********************************************************************** # # The function receives a 2D list and search string. # If the string is found return list with string capitaized. # If the tring is not found return [-1] # #*********************************************************************** def dSearch(Lst,Str): Lst = listLower2D(Lst) Str = Str.lower() idx = 0 while idx < len(Lst): idx2 = 0 while idx2 < len(Lst[idx]): StrPos = 0 checkStr = "" strIdx = idx strIdx2 = idx2 if Lst[idx][idx2] == Str[StrPos]: checkStr = checkStr + Lst[idx][idx2] StrPos = StrPos + 1 strIdx = strIdx + 1 strIdx2 = strIdx2 + 1 while strIdx < len(Lst) and strIdx2 < len(Lst[idx]) and StrPos < len(Str): if Lst[strIdx][strIdx2] == Str[StrPos]: checkStr = checkStr + Lst[strIdx][strIdx2] if checkStr == Str: stopPos = [strIdx,strIdx2] startPos = [idx,idx2] return list2DCharCap(Lst,'d',startPos,stopPos) strIdx = strIdx + 1 strIdx2 = strIdx2 + 1 StrPos = StrPos + 1 idx2 = idx2 + 1 idx = idx + 1 #upper-right idx = 0 while idx < len(Lst): idx2 = 0 while idx2 < len(Lst[idx]): StrPos = 0 checkStr = "" strIdx = idx strIdx2 = idx2 if Lst[idx][idx2] == Str[StrPos]: checkStr = checkStr + Lst[idx][idx2] StrPos = StrPos + 1 strIdx = strIdx - 1 strIdx2 = strIdx2 + 1 while strIdx >= 0 and strIdx2 < len(Lst[idx]) and StrPos < len(Str): if Lst[strIdx][strIdx2] == Str[StrPos]: checkStr = checkStr + Lst[strIdx][strIdx2] if checkStr == Str: stopPos = [strIdx,strIdx2] startPos = [idx,idx2] return list2DCharCap(Lst,'d',startPos,stopPos) strIdx = strIdx - 1 strIdx2 = strIdx2 + 1 StrPos = StrPos + 1 idx2 = idx2 + 1 idx = idx + 1 #upper-left idx = 0 while idx < len(Lst): idx2 = 0 while idx2 < len(Lst[idx]): strPos = 0 checkStr = "" strIdx = idx strIdx2 = idx2 if Lst[idx][idx2] == Str[strPos]: checkStr = checkStr + Lst[idx][idx2] strPos = strPos + 1 strIdx = strIdx - 1 strIdx2 = strIdx2 - 1 while strIdx >= 0 and strIdx2 >= 0 and strPos < len(Str): if Lst[strIdx][strIdx2] == Str[strPos]: checkStr = checkStr + Lst[strIdx][strIdx2] if checkStr == Str: stopPos = [strIdx,strIdx2] startPos = [idx,idx2] return list2DCharCap(Lst,'d',startPos,stopPos) strIdx = strIdx - 1 strIdx2 = strIdx2 - 1 strPos = strPos + 1 idx2 = idx2 + 1 idx = idx + 1 #lower-left idx = 0 while idx < len(Lst): idx2 = 0 while idx2 < len(Lst[idx]): strPos = 0 checkStr = "" strIdx = idx strIdx2 = idx2 if Lst[idx][idx2] == Str[strPos]: checkStr = checkStr + Lst[idx][idx2] strPos = strPos + 1 strIdx = strIdx + 1 strIdx2 = strIdx2 - 1 while strIdx < len(Lst) and strIdx2 >= 0 and strPos < len(Str): if Lst[strIdx][strIdx2] == Str[strPos]: checkStr = checkStr + Lst[strIdx][strIdx2] if checkStr == Str: stopPos = [strIdx,strIdx2] startPos = [idx,idx2] return list2DCharCap(Lst,'d',startPos,stopPos) strIdx = strIdx + 1 strIdx2 = strIdx2 - 1 strPos = strPos + 1 idx2 = idx2 + 1 idx = idx + 1 return [-1] #*********************************************************************** # WORD SEARCH FUNCTION # wordSearch(Lst,Str) # #*********************************************************************** # # The function receives a 2D list and search string. # If the string is found return list with string capitaized. # If the tring is not found return [-1] # # !!!!!!!!!! DO NOT PRINT THE LIST AS A MATRIX !!!!!!! # !!!! JUST RETURN ORIGINAL 2D LIST WITH FOUND STRING CAPATILIZED !!!! # #*********************************************************************** def wordSearch(Lst,Str): Lst = listLower2D(Lst) Str = Str.lower() if hSearch(Lst,Str) == [-1]: if vSearch(Lst,Str) == [-1]: if dSearch(Lst,Str) == [-1]: return [-1] else: return dSearch(Lst,Str) else: return vSearch(Lst,Str) return [-1] if vSearch(Lst,Str) == [-1]: if hSearch(Lst,Str) == [-1]: if dSearch(Lst,Str) == [-1]: return [-1] else: return dSearch(Lst,Str) else: return hSearch(Lst,Str) return [-1] if dSearch(Lst,Str) == [-1]: if hSearch(Lst,Str) == [-1]: if vSearch(Lst,Str) == [-1]: return [-1] else: return vSearch(Lst,Str) else: return hSearch(Lst,Str) return [-1] #****************************************************************************** # >>>>>>>>>> QUIZ FOR Jan 16 2014 <<<<<<<<<<< # # Function makePuzzle(rows,cols) will generate a two dimensional (M x N) # list of random characters. Put makePuzzle(rows,cols) into myFunctions.py!!! # # # Your test routine for makePuzzle(rows,cols)should Prompt the user for # the number of rows and the number of columns. Then using your functions in # myFunctions.py print the two dimensional list in matrix form after the # user enters the row and col values. # # # # HINT: Python has a built in function "chr(n)" where 0>>>>>>>>> COMPUTER PROGRAMMING 1 MIDTERM FOR Jan 2014 <<<<<<<<<<< #****************************************************************************** # # # makeMagicSquare(size) # # The functon makeMagicSquare(size)will generate a 2-D List that contains # consecutive integers starting from 1 and ending at the value of (size). # # # 1)Place the numbers randomly in the 2-D List. # 2)Each row of the MagicSquare is represented by a subList. # 3)Each number should appear only once in the 2D List. # 4)The number of rows must equal the number of cols in the 2D List # 5)Return a 2D List that contains the randomly generated consective Numbers. # # #****************************************************************************** def makeMagicSquare(size): idx = 0 maxIdx = size Lst = [] maxIdxRan = size * size numList = [] x = 1 idx3 = 0 while x <= maxIdxRan: numList = numList + [x] x = x + 1 random.shuffle(numList) while idx < maxIdx: Lst2 = [] idx2 = 0 maxIdx2 = size while idx2 < maxIdx2: Lst2 = Lst2 + [numList[idx3]] idx2 = idx2 + 1 idx3 = idx3 + 1 Lst = Lst + [Lst2] idx = idx + 1 return Lst #****************************************************************************** # # # isMagicSquare(Lst) # # The functon isMagicSquare(List)will determine if a 2-D List is a MagicSquare. # # A square matrix of consecutive numbers is a a MagicSquare if the sum of # the numbers in each row, each column, and the two longest(main)diagonals # are equal. This value is called the MagicSum. # # The MagicSum For an N x N MagicSquare is equal to: # # # N *(N*N + 1) # ------------- # 2 # # # 1)Return True if the Lst IS a MagicSquare # 2)Return False if the Lst IS NOT a MagicSquare # #****************************************************************************** def isMagicSquare(Lst): idx = 0 maxIdx = len(Lst) N = sum(Lst[0]) magic = (N * (N*N + 1)) / 2 while idx < maxIdx: horizontal = Lst[idx] vertical = [] diagonal = [] diagonal2 = [] idx2 = 0 while idx2 < maxIdx: vertical = vertical + [Lst[idx2][0]] idx2 = idx2 + 1 idx2 = 0 idx3 = 0 while idx2 < maxIdx and idx3 < maxIdx: diagonal = diagonal + [Lst[idx2][idx3]] idx2 = idx2 + 1 idx3 = idx3 + 1 idx2 = len(Lst[0]) - 1 idx3 = 0 while idx2 >= 0 and idx3 < maxIdx: diagonal2 = diagonal2 + [Lst[idx2][idx3]] idx2 = idx2 - 1 idx3 = idx3 + 1 if sum(horizontal) == magic and sum(vertical) == magic and sum(diagonal) == magic and sum(diagonal2) == magic: return True else: return False idx = idx + 1