mirror of
https://github.com/Rushilwiz/AI.git
synced 2025-04-20 02:30:16 -04:00
finished xword 1
This commit is contained in:
parent
b20842e000
commit
5347d0988d
10
Unit 4/Umaretiya_r_U4_C.txt
Normal file
10
Unit 4/Umaretiya_r_U4_C.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
/(\w)+\w*\1\w*/i
|
||||||
|
/(\w)+(\w*\1){3}\w*/i
|
||||||
|
/^1[10]*1$|^0[10]*0$/
|
||||||
|
/\b(?=\w*cat)\w{6}\b/
|
||||||
|
/\b(?=\w*bri)(?=\w*ing)\w{5,9}\b/
|
||||||
|
/\b(?!.*cat)\w{6}\b/
|
||||||
|
/(?:\w)*/
|
||||||
|
/^(?!.*10011)[01]*$/
|
||||||
|
/\w*(a[eiou]|e[aiou]|i[aeou]|o[aeiu]|u[aeio])\w*/i
|
||||||
|
/^(?!.*1.1)[01]*$/
|
10
Unit 4/Umaretiya_r_U4_C_ver2.txt
Normal file
10
Unit 4/Umaretiya_r_U4_C_ver2.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
(\w)+\w*\1\w*
|
||||||
|
(\w)+(\w*\1){3}\w*
|
||||||
|
^1[10]*1$|^0[10]*0$
|
||||||
|
\b(?=\w*cat)\w{6}\b
|
||||||
|
\b(?=\w*bri)(?=\w*ing)\w{5,9}\b
|
||||||
|
\b(?!.*cat)\w{6}\b
|
||||||
|
(?:\w)*
|
||||||
|
^(?!.*10011)[01]*$
|
||||||
|
\w*(a[eiou]|e[aiou]|i[aeou]|o[aeiu]|u[aeio])\w*
|
||||||
|
^(?!.*1.1)[01]*$
|
10
Unit 4/Umaretiya_r_U4_C_ver3.txt
Normal file
10
Unit 4/Umaretiya_r_U4_C_ver3.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
(\w)+\w*\1\w*
|
||||||
|
(\w)+(\w*\1){3}\w*
|
||||||
|
^1[10]*1$|^0[10]*0$
|
||||||
|
\b(?=\w*cat)\w{6}\b
|
||||||
|
\b(?=\w*bri)(?=\w*ing)\w{5,9}\b
|
||||||
|
\b(?!\w*cat)\w{6}\b
|
||||||
|
\b((\w)(?!\w*\2))*\b
|
||||||
|
^(?!.*10011)[01]*$
|
||||||
|
\w*(a[eiou]|e[aiou]|i[aeou]|o[aeiu]|u[aeio])\w*
|
||||||
|
^(?!.*1.1)[01]*$
|
279
Unit 4/crossword/Scavotto_z_U4_L4.py
Normal file
279
Unit 4/crossword/Scavotto_z_U4_L4.py
Normal file
|
@ -0,0 +1,279 @@
|
||||||
|
import sys; args = sys.argv[1:] #gets rid of the .py from the list, not needed to look at
|
||||||
|
import re
|
||||||
|
BLOCKCHAR = "#" # blocked square (black square)
|
||||||
|
OPENCHAR = "-" # open square (not decided yet)
|
||||||
|
PROTECTEDCHAR = "~" #protected
|
||||||
|
|
||||||
|
def initialize(height, width, words ,numBlocks):
|
||||||
|
xword = OPENCHAR * (int(height) * int(width))
|
||||||
|
for word in words:
|
||||||
|
startingIndex = int(int(word[1]) * width + int(word[2]))
|
||||||
|
for letter in word[3]:
|
||||||
|
xword = xword[:startingIndex] + letter + xword[startingIndex + 1:]
|
||||||
|
if letter == BLOCKCHAR:
|
||||||
|
numBlocks -= 1
|
||||||
|
if word[0] == "V":
|
||||||
|
startingIndex += int(width)
|
||||||
|
else:
|
||||||
|
startingIndex += 1
|
||||||
|
return xword, numBlocks
|
||||||
|
|
||||||
|
def initialProtSymmetry(board):
|
||||||
|
length = len(board)
|
||||||
|
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ#"
|
||||||
|
for x in range(length):
|
||||||
|
temp = x + 1
|
||||||
|
if board[x] != OPENCHAR and board[x] != PROTECTEDCHAR and board[x] in alphabet:
|
||||||
|
indexToReplace = length - temp + 1
|
||||||
|
if board[indexToReplace-1] not in alphabet:
|
||||||
|
board = board[:indexToReplace -1] + PROTECTEDCHAR + board[indexToReplace:]
|
||||||
|
if length % 2 != 0:
|
||||||
|
indexToReplace = (length // 2) + 1
|
||||||
|
board = board[:indexToReplace -1] + PROTECTEDCHAR + board[indexToReplace:]
|
||||||
|
return board
|
||||||
|
|
||||||
|
def horizontalProt(board, height, width):
|
||||||
|
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
for y in range(height):
|
||||||
|
letterCount = 0
|
||||||
|
#for x in range(width):
|
||||||
|
# if board[(y * height) + x] in alphabet:
|
||||||
|
# letterCount += 1
|
||||||
|
# else:
|
||||||
|
# break
|
||||||
|
#if letterCount < 3 and letterCount > 0:
|
||||||
|
# for x in range(3):
|
||||||
|
# if board[(y * height) + x] == OPENCHAR:
|
||||||
|
# board = board[:(y * height) + x] + PROTECTEDCHAR + board[(y * height) + x + 1:]
|
||||||
|
for x in range(width):
|
||||||
|
#print(y*width+x,board[y*width+x])
|
||||||
|
if board[(y * width) + x] in alphabet or (board[(y*width)+x] == PROTECTEDCHAR and board[(y*width)+x-1] != PROTECTEDCHAR):
|
||||||
|
letterCount += 1
|
||||||
|
else:
|
||||||
|
|
||||||
|
if letterCount < 3 and letterCount > 0 and x+1 < width:
|
||||||
|
for z in range(3-letterCount):
|
||||||
|
if board[(y * width) + x + z] == OPENCHAR:
|
||||||
|
board = board[:(y * width) + x + z] + PROTECTEDCHAR + board[(y * width) + x + z + 1:]
|
||||||
|
letterCount = 0
|
||||||
|
return board
|
||||||
|
|
||||||
|
def verticalProt(board, height, width):
|
||||||
|
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
for x in range(width):
|
||||||
|
letterCount = 0
|
||||||
|
for y in range(height):
|
||||||
|
if board[(y * height) + x] in alphabet:
|
||||||
|
letterCount += 1
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
if letterCount < 3 and letterCount > 0:
|
||||||
|
for y in range(3):
|
||||||
|
if board[(y * height) + x] == OPENCHAR:
|
||||||
|
board = board[:(y * height) + x] + PROTECTEDCHAR + board[(y * height) + x + 1:]
|
||||||
|
return board
|
||||||
|
|
||||||
|
def symmetricalProt(board):
|
||||||
|
length = len(board)
|
||||||
|
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ#"
|
||||||
|
for x in range(length):
|
||||||
|
temp = x + 1
|
||||||
|
if board[x] == PROTECTEDCHAR and (board[length - temp] not in alphabet):
|
||||||
|
indexToReplace = length - temp + 1
|
||||||
|
board = board[:indexToReplace -1] + PROTECTEDCHAR + board[indexToReplace:]
|
||||||
|
return board
|
||||||
|
|
||||||
|
def addBlocks(board, height, width, numBlocks):
|
||||||
|
pos_list = [x for x in range(len(board)) if board[x] == OPENCHAR and board[len(board)-x-1] == OPENCHAR]
|
||||||
|
return recursiveBacktrack(board,height,width,numBlocks, pos_list)
|
||||||
|
|
||||||
|
def recursiveBacktrack(board,height,width,numBlocks, pos_list):
|
||||||
|
if len(pos_list) == 0 or numBlocks == 0: return board
|
||||||
|
for option in pos_list:
|
||||||
|
pos_list.remove(option)
|
||||||
|
if isValid(board,option,height,width):
|
||||||
|
newBoard = board[:option] + BLOCKCHAR + board[option+1:]
|
||||||
|
newBoard = newBoard[:len(board)-option-1] + BLOCKCHAR + newBoard[len(board)-option:height*width]
|
||||||
|
result = recursiveBacktrack(newBoard,height,width,numBlocks-2, pos_list)
|
||||||
|
if result!= None:
|
||||||
|
return result
|
||||||
|
board = board[:option] + OPENCHAR + board[option+1:]
|
||||||
|
board = board[:len(board)-option-1] + OPENCHAR + board[len(board)-option:]
|
||||||
|
pos_list.insert(0, option)
|
||||||
|
numBlocks += 2
|
||||||
|
return None
|
||||||
|
|
||||||
|
def transpose(board, newWidth):
|
||||||
|
return "".join([board[col:newWidth] for col in range(newWidth)])
|
||||||
|
|
||||||
|
def isValid(board,index,height,width):
|
||||||
|
#checks if block can go there
|
||||||
|
#ALSO KEEP IN MIND IT HAS TO BE SYMMETRIC
|
||||||
|
if board[index] != OPENCHAR:
|
||||||
|
return False
|
||||||
|
tempBoard = board[:index] + BLOCKCHAR + board[index+1:]
|
||||||
|
illegalRegex = "[{}](.?({}|{})|({}|{}).?)[{}]".format(BLOCKCHAR, PROTECTEDCHAR,OPENCHAR, PROTECTEDCHAR,OPENCHAR, BLOCKCHAR)
|
||||||
|
if re.search(illegalRegex, tempBoard) != None or re.search(illegalRegex, transpose(tempBoard, height)) != None:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
if board[len(tempBoard)-index-1] != OPENCHAR:
|
||||||
|
return False
|
||||||
|
tempBoard = tempBoard[:len(tempBoard)-index-1] + BLOCKCHAR + tempBoard[len(tempBoard)-index:]
|
||||||
|
if re.search(illegalRegex, tempBoard) != None or re.search(illegalRegex, transpose(tempBoard, height)) != None:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def selectSpot(board,blockOptions,height,width):
|
||||||
|
for option in blockOptions:
|
||||||
|
if isValid(board,option,height,width):
|
||||||
|
return option
|
||||||
|
return -1
|
||||||
|
|
||||||
|
def subLettersForProctected(board):
|
||||||
|
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
for index in range(len(board)):
|
||||||
|
if board[index] in alphabet:
|
||||||
|
board = board[:index] + PROTECTEDCHAR + board[index+1:]
|
||||||
|
return board
|
||||||
|
|
||||||
|
def display(board, height, width):
|
||||||
|
startInd = 0
|
||||||
|
for y in range(height):
|
||||||
|
print(board[startInd:startInd + width])
|
||||||
|
startInd = startInd + width
|
||||||
|
|
||||||
|
def processInput(args):
|
||||||
|
dimmMatch = r"^(\d+)x(\d+)$"
|
||||||
|
blockMatch = r"^\d+$"
|
||||||
|
wordMatch = r"^(H|V)(\d+)x(\d+)(.+)$"
|
||||||
|
height, width = 0, 0
|
||||||
|
numBlocks = 0
|
||||||
|
prePlacedWords = []
|
||||||
|
for input in args:
|
||||||
|
if re.match(dimmMatch, input) != None:
|
||||||
|
indexOfX = input.index("x")
|
||||||
|
height, width = int(input[:indexOfX]), int(input[indexOfX + 1:])
|
||||||
|
args.remove(input)
|
||||||
|
break
|
||||||
|
for input in args:
|
||||||
|
if re.match(blockMatch, input) != None:
|
||||||
|
numBlocks = int(input)
|
||||||
|
args.remove(input)
|
||||||
|
break
|
||||||
|
unprocessWords = []
|
||||||
|
for input in args:
|
||||||
|
if re.match(wordMatch, input, re.I) != None:
|
||||||
|
unprocessWords.append(input)
|
||||||
|
for word in unprocessWords:
|
||||||
|
args.remove(word)
|
||||||
|
orient = word[0].upper()
|
||||||
|
word = word[1:]
|
||||||
|
digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
|
||||||
|
if "x" in word:
|
||||||
|
indexOfX = word.index("x")
|
||||||
|
else:
|
||||||
|
indexOfX = word.index("X")
|
||||||
|
finalIndex = indexOfX + 1
|
||||||
|
row = word[:indexOfX]
|
||||||
|
while word[finalIndex] in digits:
|
||||||
|
finalIndex += 1
|
||||||
|
col = word[indexOfX + 1:finalIndex]
|
||||||
|
restOfWord = word[finalIndex:]
|
||||||
|
prePlacedWords.append((orient, row, col, restOfWord.upper()))
|
||||||
|
if len(args) > 0:
|
||||||
|
file = args[0]
|
||||||
|
return file,height, width, numBlocks, prePlacedWords
|
||||||
|
|
||||||
|
def removeOuter(board,height,width):
|
||||||
|
toReturn = ""
|
||||||
|
for i in range(len(board)):
|
||||||
|
if (i > width) and ((i+1) % width != 0) and (i % width != 0) and (height*width-width > i):
|
||||||
|
toReturn += board[i]
|
||||||
|
return toReturn
|
||||||
|
|
||||||
|
def finalize(xword, xw):
|
||||||
|
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
toReturn = ""
|
||||||
|
for i in range(len(xword)):
|
||||||
|
if xword[i] in alphabet or xword[i] == BLOCKCHAR:
|
||||||
|
toReturn += xword[i]
|
||||||
|
elif xword[i] == PROTECTEDCHAR:
|
||||||
|
toReturn += OPENCHAR
|
||||||
|
elif xw[i] == BLOCKCHAR:
|
||||||
|
toReturn += BLOCKCHAR
|
||||||
|
else:
|
||||||
|
toReturn += OPENCHAR
|
||||||
|
return toReturn
|
||||||
|
|
||||||
|
def inputtedBlocksSym(board, numBlocks):
|
||||||
|
length = len(board)
|
||||||
|
for i in range(length):
|
||||||
|
if board[i] == BLOCKCHAR and board[len(board)-i-1] != BLOCKCHAR:
|
||||||
|
temp = i + 1
|
||||||
|
indexToReplace = length - temp + 1
|
||||||
|
board = board[:indexToReplace -1] + BLOCKCHAR + board[indexToReplace:]
|
||||||
|
numBlocks -=1
|
||||||
|
return board, numBlocks
|
||||||
|
def main():
|
||||||
|
#USE ONLY THE BELOW LINE IN VSCODE, IF USING JGRASP COMMENT THE BELOW LINE!!!!!!!!!!!!!!!!!!!
|
||||||
|
#COMMENT BELOW LINE OUT WHEN SUBMITTING!!!!!!!!!!!
|
||||||
|
#GOOD RUNS:
|
||||||
|
#sys.argv = ['Scavotto_z_U4_L4.py',"scrabble.txt","3x3","9"]
|
||||||
|
#sys.argv = ['Scavotto_z_U4_L4.py', "7x7", "6", "scrabble.txt", "v0x0come", "v0x1here", "h0x2ers"]
|
||||||
|
#sys.argv = ['Scavotto_z_U4_L4.py', "9x13", "18", "xwords.txt", "V0x1Who"]
|
||||||
|
#sys.argv = ['Scavotto_z_U4_L4.py',"9x9", "14", "xwords.txt", "V0x4con", "V6x4rum"]
|
||||||
|
#NEED FIX
|
||||||
|
#sys.argv = ['Scavotto_z_U4_L4.py',"15x15", "32", "dct20k.txt", "H0x0Mute", "V0x0mule", "V10x13Risen", "H7x5#", "V3x4#", "H6x7#", "V11x3#"]
|
||||||
|
#sys.argv = ['Scavotto_z_U4_L4.py', "10x14", "32", "dct20k.txt", "V6x0#", "V9x3#", "H3x10#", "V0x6Indulgence"]
|
||||||
|
#13x13 32 dct20k.txt H1x4#Toe# H9x2# V3x6# H10x0Scintillating V0x5stirrup H4x2##Ordained V0x1Pits V0x12Ffi V5x0orb
|
||||||
|
#13x13 32 dct20k.txt V2x4# V1x9# V3x2# h8x2#moo# v5x5#two# h6x4#ten# v3x7#own# h4x6#orb# H12x4Vlan
|
||||||
|
#13x13 25 dct20k.txt H6x4no#on v5x5rot v0x0ankles h0x4Trot H0x9Calf V0x12foot
|
||||||
|
#9x22 36 dct20k.txt h4x8e# h3x5s# h2x5# v2x0pan V5x1#w V8x18c
|
||||||
|
#args = sys.argv #gets the sys args
|
||||||
|
#args = args[1:]
|
||||||
|
#COMMENT ALL ABOVE TO SUBMIT
|
||||||
|
filetext,height, width, numBlocks, words = processInput(args) #processes the inputs
|
||||||
|
if numBlocks == height*width:
|
||||||
|
xword = BLOCKCHAR * numBlocks
|
||||||
|
display(xword, height,width)
|
||||||
|
else:
|
||||||
|
xword, numBlocks = initialize(height, width, words, numBlocks)
|
||||||
|
display(xword, height, width)
|
||||||
|
if numBlocks != 0:
|
||||||
|
print("\nMaking Any Inputted Blocks Symmetrical")
|
||||||
|
xword, numBlocks = inputtedBlocksSym(xword, numBlocks)
|
||||||
|
display(xword, height, width)
|
||||||
|
print("\nAdding Symmetrical Protected Characters")
|
||||||
|
xword = initialProtSymmetry(xword)
|
||||||
|
display(xword, height, width)
|
||||||
|
print("\nHorizontal Protection")
|
||||||
|
xword = horizontalProt(xword, height, width)
|
||||||
|
display(xword, height, width)
|
||||||
|
print("\nVertical Protection")
|
||||||
|
xword = verticalProt(xword, height, width)
|
||||||
|
display(xword, height, width)
|
||||||
|
print("\nMaking Protection Symmetric")
|
||||||
|
xword = symmetricalProt(xword)
|
||||||
|
display(xword, height, width)
|
||||||
|
xw = BLOCKCHAR*(width+3)
|
||||||
|
xw +=(BLOCKCHAR*2).join([xword[p:p+width] for p in range(0,len(xword),width)])
|
||||||
|
xw += BLOCKCHAR*(width+3)
|
||||||
|
print("\nTurning into xw")
|
||||||
|
xwHeight = height+2
|
||||||
|
xwWidth = width+2
|
||||||
|
display(xw,xwHeight,xwWidth)
|
||||||
|
print("\nSubbing Letters")
|
||||||
|
subbedXW = subLettersForProctected(xw)
|
||||||
|
display(subbedXW, xwHeight, xwWidth)
|
||||||
|
print("\nAdding Blocks")
|
||||||
|
xw = addBlocks(subbedXW, xwHeight, xwWidth, numBlocks)
|
||||||
|
display(xw,xwHeight,xwWidth)
|
||||||
|
print("\nRemoving Outer Border")
|
||||||
|
xw = removeOuter(xw, xwHeight,xwWidth)
|
||||||
|
display(xw, height, width)
|
||||||
|
print("\nPutting in Letters and Replacing Protected With Open\n")
|
||||||
|
xword = finalize(xword, xw)
|
||||||
|
print(xword)
|
||||||
|
print("\nThe Displayed Board:")
|
||||||
|
display(xword, height, width)
|
||||||
|
if __name__ == '__main__': main()
|
223
Unit 4/crossword/Umaretiya_r_U4_L4.py
Normal file
223
Unit 4/crossword/Umaretiya_r_U4_L4.py
Normal file
|
@ -0,0 +1,223 @@
|
||||||
|
import sys; args = sys.argv[1:]
|
||||||
|
|
||||||
|
# Name: Rushil Umaretiya
|
||||||
|
# Date: 1-3-2021
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
BLOCKCHAR = '#'
|
||||||
|
OPENCHAR = '-'
|
||||||
|
PROTECTEDCHAR = '~'
|
||||||
|
|
||||||
|
def crossword(args):
|
||||||
|
# parse input
|
||||||
|
# args = ['13x13', '32', 'dct20k.txt', 'H1x4#Toe#', 'H9x2#', 'V3x6#', 'H10x0Scintillating', 'V0x5stirrup', 'H4x2##Ordained', 'V0x1Sums', 'V0x12Mah', 'V5x0pew']
|
||||||
|
height, width, block_count, wordlist, words = parse_input(args)
|
||||||
|
|
||||||
|
# initialize board
|
||||||
|
board, width, height, block_count = initialize(width, height, words, block_count)
|
||||||
|
|
||||||
|
print("init board:")
|
||||||
|
display(board, width, height)
|
||||||
|
|
||||||
|
# add blocking chars
|
||||||
|
print("add blocks:")
|
||||||
|
board = add_blocks(board, width, height, block_count)
|
||||||
|
|
||||||
|
if board == None:
|
||||||
|
raise Exception(f'Board is none.')
|
||||||
|
|
||||||
|
# display(board, width, height)
|
||||||
|
# remove border
|
||||||
|
board, width, height = finish_board(board, width, height, words)
|
||||||
|
|
||||||
|
#print(f'{block_count} {board.count(BLOCKCHAR)}')
|
||||||
|
print(board.count(BLOCKCHAR))
|
||||||
|
display(board, width, height)
|
||||||
|
|
||||||
|
def parse_input(args):
|
||||||
|
tests = [r'^(\d+)x(\d+)$', r'^\d+$', r'^(H|V)(\d+)x(\d+)(.+)$']
|
||||||
|
height, width, block_count, wordlist, words = 0, 0, 0, '', []
|
||||||
|
|
||||||
|
for arg in args:
|
||||||
|
# if os.path.isfile(arg):
|
||||||
|
# wordlist = arg
|
||||||
|
# else:
|
||||||
|
for i in range(len(tests)):
|
||||||
|
match = re.search(tests[i], arg, re.I)
|
||||||
|
if match == None: continue
|
||||||
|
if i == 0:
|
||||||
|
height = int(match.group(1))
|
||||||
|
width = int(match.group(2))
|
||||||
|
elif i == 1:
|
||||||
|
block_count = int(match.group(0))
|
||||||
|
elif i == 2:
|
||||||
|
words.append((match.group(1).upper(), int(match.group(2)), int(match.group(3)), match.group(4).upper()))
|
||||||
|
return height, width, block_count, wordlist, words
|
||||||
|
|
||||||
|
def initialize(width, height, words, block_count):
|
||||||
|
board = OPENCHAR * height * width
|
||||||
|
for word in words:
|
||||||
|
index = word[1] * width + word[2]
|
||||||
|
for letter in word[3]:
|
||||||
|
new_char = BLOCKCHAR if letter == BLOCKCHAR else PROTECTEDCHAR
|
||||||
|
board = board[:index] + new_char + board[index + 1 :]
|
||||||
|
board = board[:(len(board) - 1) - index] + new_char + board[len(board) - index:]
|
||||||
|
if word[0] == 'H':
|
||||||
|
index += 1
|
||||||
|
else:
|
||||||
|
index += width
|
||||||
|
block_count -= board.count(BLOCKCHAR)
|
||||||
|
# display(board, width, height)
|
||||||
|
board = add_border(board, width, height)
|
||||||
|
width += 2
|
||||||
|
height += 2
|
||||||
|
# display(board, width, height)
|
||||||
|
board = protect(board, width, height)
|
||||||
|
# display(board, width, height)
|
||||||
|
return board, width, height, block_count
|
||||||
|
|
||||||
|
def protect(board, width, height):
|
||||||
|
right_test = rf'({BLOCKCHAR}(\w|{PROTECTEDCHAR})(\w|{PROTECTEDCHAR})){OPENCHAR}'
|
||||||
|
left_test = rf'{OPENCHAR}((\w|{PROTECTEDCHAR})(\w|{PROTECTEDCHAR}){BLOCKCHAR})'
|
||||||
|
|
||||||
|
for i in range(2):
|
||||||
|
board = re.sub(left_test, rf'{PROTECTEDCHAR}\1', board)
|
||||||
|
board = re.sub(right_test, rf'\1{PROTECTEDCHAR}', board)
|
||||||
|
board = transpose(board, width)
|
||||||
|
width, height = height, width
|
||||||
|
# display(board, width, height)
|
||||||
|
|
||||||
|
return board
|
||||||
|
|
||||||
|
def transpose(board, width):
|
||||||
|
return ''.join([board[col::width] for col in range(width)])
|
||||||
|
|
||||||
|
def add_border(board, width, height):
|
||||||
|
border_board = BLOCKCHAR*(width+3)
|
||||||
|
border_board +=(BLOCKCHAR*2).join([board[p:p+width] for p in range(0,len(board),width)])
|
||||||
|
border_board += BLOCKCHAR*(width+3)
|
||||||
|
return border_board
|
||||||
|
|
||||||
|
def remove_border(board, width, height):
|
||||||
|
no_border = ''
|
||||||
|
for i in range(len(board)):
|
||||||
|
if (width <= i < width * (height - 1)) and ((i + 1) % width != 0) and (i % width != 0):
|
||||||
|
no_border += board[i]
|
||||||
|
return no_border, width - 2, height - 2
|
||||||
|
|
||||||
|
def add_blocks(board, width, height, block_count):
|
||||||
|
if block_count == 0:
|
||||||
|
return board
|
||||||
|
|
||||||
|
if board.count(OPENCHAR) == block_count:
|
||||||
|
return BLOCKCHAR * len(board)
|
||||||
|
|
||||||
|
if block_count % 2 == 1:
|
||||||
|
if width * height % 2 == 1:
|
||||||
|
board = board[: len(board) // 2] + BLOCKCHAR + board[(len(board) // 2) + 1 :]
|
||||||
|
block_count -= 1
|
||||||
|
else:
|
||||||
|
raise Exception("Cannot place an odd number of blockchars on an even sized board.")
|
||||||
|
|
||||||
|
if re.search('#[~-][~-]#', board) or re.search('#[~-][~-]#', transpose(board, width)):
|
||||||
|
for i in range(2):
|
||||||
|
board, num = re.subn('#[~-][~-]#', '####', board)
|
||||||
|
block_count -= 2 * num
|
||||||
|
board = transpose(board, width)
|
||||||
|
width, height = height, width
|
||||||
|
|
||||||
|
possible = [i for i in range(len(board)) if board[i] != BLOCKCHAR]
|
||||||
|
fills = {}
|
||||||
|
|
||||||
|
for i in possible:
|
||||||
|
fills[i] = area_fill(board, width, i)
|
||||||
|
|
||||||
|
fill_counts = {}
|
||||||
|
for fill in fills.keys():
|
||||||
|
count = fills[fill].count('?')
|
||||||
|
|
||||||
|
if count not in fill_counts.values():
|
||||||
|
fill_counts[fill] = count
|
||||||
|
|
||||||
|
fill_counts = {key: value for key, value in sorted(fill_counts.items(), key=lambda item: item[1])}
|
||||||
|
for fill in fill_counts:
|
||||||
|
if fill_counts[fill] < (width - 2) * (height - 2) - board.count(PROTECTEDCHAR):
|
||||||
|
board = area_fill(board, width, fill, char=BLOCKCHAR)
|
||||||
|
board = area_fill(board, width, len(board) - 1 - fill, char=BLOCKCHAR)
|
||||||
|
block_count -= fill_counts[fill] * 2
|
||||||
|
break
|
||||||
|
|
||||||
|
options = [i for i in range(len(board)) if board[i] == board[(len(board) - 1) - i] == OPENCHAR]
|
||||||
|
return blocks_backtrack(board, width, height, block_count, options)
|
||||||
|
|
||||||
|
def blocks_backtrack(board, width, height, block_count, options):
|
||||||
|
# print(options)
|
||||||
|
# display(board, width, height)
|
||||||
|
|
||||||
|
if block_count == 0 or len(options) == 0:
|
||||||
|
return board
|
||||||
|
|
||||||
|
for option in options:
|
||||||
|
if is_valid_blocking(board, width, height, option):
|
||||||
|
copy = board[:option] + BLOCKCHAR + board[option + 1 :]
|
||||||
|
copy = copy[: (len(copy) - 1) - option] + BLOCKCHAR + copy[len(copy) - option :]
|
||||||
|
updated_options = [i for i in options if i != option]
|
||||||
|
result = blocks_backtrack(copy, width, height, block_count - 2, updated_options)
|
||||||
|
if result != None: return result
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
def is_valid_blocking(board, width, height, option):
|
||||||
|
if board[option] != OPENCHAR: return False
|
||||||
|
temp = board[:option] + BLOCKCHAR + board[option + 1:]
|
||||||
|
temp = temp[:(len(temp) - 1) - option] + BLOCKCHAR + temp[len(temp) - option :]
|
||||||
|
|
||||||
|
illegalRegex = rf"[{BLOCKCHAR}](.?({PROTECTEDCHAR}|{OPENCHAR})|({PROTECTEDCHAR}|{OPENCHAR}).?)[{BLOCKCHAR}]"
|
||||||
|
if re.search(illegalRegex, temp) != None: return False
|
||||||
|
if re.search(illegalRegex, transpose(temp, width)) != None: return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def area_fill(board, width, sp, char='?'):
|
||||||
|
dirs = [-1, width, 1, -1 * width]
|
||||||
|
if sp < 0 or sp >= len(board): return board
|
||||||
|
if board[sp] in (OPENCHAR, PROTECTEDCHAR):
|
||||||
|
board = board[0:sp] + char + board[sp+1:]
|
||||||
|
for d in dirs:
|
||||||
|
if d == -1 and sp % width == 0: continue
|
||||||
|
if d == 1 and sp + 1 % width == 0: continue
|
||||||
|
board = area_fill(board, width, sp + d, char)
|
||||||
|
return board
|
||||||
|
|
||||||
|
def finish_board(board, width, height, words):
|
||||||
|
# remove border
|
||||||
|
board, width, height = remove_border(board, width, height)
|
||||||
|
|
||||||
|
# add words
|
||||||
|
for word in words:
|
||||||
|
index = word[1] * width + word[2]
|
||||||
|
for letter in word[3]:
|
||||||
|
board = board[:index] + letter + board[index + 1 :]
|
||||||
|
if word[0] == 'H':
|
||||||
|
index += 1
|
||||||
|
else:
|
||||||
|
index += width
|
||||||
|
|
||||||
|
# replace protected with open
|
||||||
|
board = re.sub(PROTECTEDCHAR, OPENCHAR, board)
|
||||||
|
|
||||||
|
return board, width, height
|
||||||
|
|
||||||
|
def display(board, width, height):
|
||||||
|
for i in range(height):
|
||||||
|
line = ""
|
||||||
|
for letter in range(width):
|
||||||
|
line += (board[(i * width) + letter] + " ")
|
||||||
|
print(line)
|
||||||
|
print()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
crossword(args)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
234
Unit 4/crossword/Umaretiya_r_U4_L4_ver2.py
Normal file
234
Unit 4/crossword/Umaretiya_r_U4_L4_ver2.py
Normal file
|
@ -0,0 +1,234 @@
|
||||||
|
import sys; args = sys.argv[1:]
|
||||||
|
|
||||||
|
# Name: Rushil Umaretiya
|
||||||
|
# Date: 1-3-2021
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
BLOCKCHAR = '#'
|
||||||
|
OPENCHAR = '-'
|
||||||
|
PROTECTEDCHAR = '~'
|
||||||
|
|
||||||
|
def crossword(args):
|
||||||
|
# parse input
|
||||||
|
# args = ['13x13', '32', 'dct20k.txt', 'H1x4#Toe#', 'H9x2#', 'V3x6#', 'H10x0Scintillating', 'V0x5stirrup', 'H4x2##Ordained', 'V0x1Sums', 'V0x12Mah', 'V5x0pew']
|
||||||
|
height, width, block_count, wordlist, words = parse_input(args)
|
||||||
|
|
||||||
|
# initialize board
|
||||||
|
board, width, height, block_count = initialize(width, height, words, block_count)
|
||||||
|
|
||||||
|
print("init board:")
|
||||||
|
display(board, width, height)
|
||||||
|
|
||||||
|
# add blocking chars
|
||||||
|
print("add blocks:")
|
||||||
|
board = add_blocks(board, width, height, block_count)
|
||||||
|
|
||||||
|
if board == None:
|
||||||
|
print('Board is none.')
|
||||||
|
raise Exception(f'Board is none.')
|
||||||
|
|
||||||
|
# display(board, width, height)
|
||||||
|
# remove border
|
||||||
|
board, width, height = finish_board(board, width, height, words)
|
||||||
|
|
||||||
|
#print(f'{block_count} {board.count(BLOCKCHAR)}')
|
||||||
|
print(board.count(BLOCKCHAR))
|
||||||
|
display(board, width, height)
|
||||||
|
|
||||||
|
def parse_input(args):
|
||||||
|
tests = [r'^(\d+)x(\d+)$', r'^\d+$', r'^(H|V)(\d+)x(\d+)(.+)$']
|
||||||
|
height, width, block_count, wordlist, words = 0, 0, 0, '', []
|
||||||
|
|
||||||
|
for arg in args:
|
||||||
|
# if os.path.isfile(arg):
|
||||||
|
# wordlist = arg
|
||||||
|
# else:
|
||||||
|
for i in range(len(tests)):
|
||||||
|
match = re.search(tests[i], arg, re.I)
|
||||||
|
if match == None: continue
|
||||||
|
if i == 0:
|
||||||
|
height = int(match.group(1))
|
||||||
|
width = int(match.group(2))
|
||||||
|
elif i == 1:
|
||||||
|
block_count = int(match.group(0))
|
||||||
|
elif i == 2:
|
||||||
|
words.append((match.group(1).upper(), int(match.group(2)), int(match.group(3)), match.group(4).upper()))
|
||||||
|
return height, width, block_count, wordlist, words
|
||||||
|
|
||||||
|
def initialize(width, height, words, block_count):
|
||||||
|
board = OPENCHAR * height * width
|
||||||
|
for word in words:
|
||||||
|
index = word[1] * width + word[2]
|
||||||
|
for letter in word[3]:
|
||||||
|
new_char = BLOCKCHAR if letter == BLOCKCHAR else PROTECTEDCHAR
|
||||||
|
board = board[:index] + new_char + board[index + 1 :]
|
||||||
|
board = board[:(len(board) - 1) - index] + new_char + board[len(board) - index:]
|
||||||
|
if word[0] == 'H':
|
||||||
|
index += 1
|
||||||
|
else:
|
||||||
|
index += width
|
||||||
|
block_count -= board.count(BLOCKCHAR)
|
||||||
|
display(board, width, height)
|
||||||
|
board = add_border(board, width, height)
|
||||||
|
width += 2
|
||||||
|
height += 2
|
||||||
|
# display(board, width, height)
|
||||||
|
board = protect(board, width, height)
|
||||||
|
# display(board, width, height)
|
||||||
|
return board, width, height, block_count
|
||||||
|
|
||||||
|
def protect(board, width, height):
|
||||||
|
right_test = rf'({BLOCKCHAR}(\w|{PROTECTEDCHAR})(\w|{PROTECTEDCHAR})){OPENCHAR}'
|
||||||
|
left_test = rf'{OPENCHAR}((\w|{PROTECTEDCHAR})(\w|{PROTECTEDCHAR}){BLOCKCHAR})'
|
||||||
|
|
||||||
|
for i in range(2):
|
||||||
|
board = re.sub(left_test, rf'{PROTECTEDCHAR}\1', board)
|
||||||
|
board = re.sub(right_test, rf'\1{PROTECTEDCHAR}', board)
|
||||||
|
board = transpose(board, width)
|
||||||
|
width, height = height, width
|
||||||
|
# display(board, width, height)
|
||||||
|
|
||||||
|
return board
|
||||||
|
|
||||||
|
def transpose(board, width):
|
||||||
|
return ''.join([board[col::width] for col in range(width)])
|
||||||
|
|
||||||
|
def add_border(board, width, height):
|
||||||
|
border_board = BLOCKCHAR*(width+3)
|
||||||
|
border_board +=(BLOCKCHAR*2).join([board[p:p+width] for p in range(0,len(board),width)])
|
||||||
|
border_board += BLOCKCHAR*(width+3)
|
||||||
|
return border_board
|
||||||
|
|
||||||
|
def remove_border(board, width, height):
|
||||||
|
no_border = ''
|
||||||
|
for i in range(len(board)):
|
||||||
|
if (width <= i < width * (height - 1)) and ((i + 1) % width != 0) and (i % width != 0):
|
||||||
|
no_border += board[i]
|
||||||
|
return no_border, width - 2, height - 2
|
||||||
|
|
||||||
|
def add_blocks(board, width, height, block_count):
|
||||||
|
if block_count == 0:
|
||||||
|
return board
|
||||||
|
|
||||||
|
if board.count(OPENCHAR) == block_count:
|
||||||
|
return BLOCKCHAR * len(board)
|
||||||
|
|
||||||
|
if block_count % 2 == 1:
|
||||||
|
if width * height % 2 == 1:
|
||||||
|
board = board[: len(board) // 2] + BLOCKCHAR + board[(len(board) // 2) + 1 :]
|
||||||
|
block_count -= 1
|
||||||
|
else:
|
||||||
|
raise Exception("Cannot place an odd number of blockchars on an even sized board.")
|
||||||
|
|
||||||
|
print(board)
|
||||||
|
if re.search(f'#[{PROTECTEDCHAR+OPENCHAR}]{{1,2}}#', board) or re.search(f'#[{PROTECTEDCHAR+OPENCHAR}]{{1,2}}#', transpose(board, width)):
|
||||||
|
|
||||||
|
display(board, width, height)
|
||||||
|
|
||||||
|
for i in range(2):
|
||||||
|
presub = board.count(BLOCKCHAR)
|
||||||
|
board, num = re.subn(f'#([{PROTECTEDCHAR+OPENCHAR}][{PROTECTEDCHAR+OPENCHAR}]#)*', lambda x: '#' * len(x.group()), board)
|
||||||
|
board, num = re.subn(f'#([{PROTECTEDCHAR+OPENCHAR}]#)*', lambda x: '#' * len(x.group()), board)
|
||||||
|
block_count -= board.count(BLOCKCHAR) - presub
|
||||||
|
|
||||||
|
board = transpose(board, width)
|
||||||
|
width, height = height, width
|
||||||
|
|
||||||
|
# print("yes.")
|
||||||
|
display(board, width, height)
|
||||||
|
# print("yes")
|
||||||
|
possible = [i for i in range(len(board)) if board[i] != BLOCKCHAR]
|
||||||
|
fills = {}
|
||||||
|
|
||||||
|
for i in possible:
|
||||||
|
fills[i] = area_fill(board, width, i)
|
||||||
|
|
||||||
|
fill_counts = {}
|
||||||
|
for fill in fills.keys():
|
||||||
|
count = fills[fill].count('?')
|
||||||
|
|
||||||
|
if count not in fill_counts.values():
|
||||||
|
fill_counts[fill] = count
|
||||||
|
|
||||||
|
fill_counts = {key: value for key, value in sorted(fill_counts.items(), key=lambda item: item[1])}
|
||||||
|
for fill in fill_counts:
|
||||||
|
if fill_counts[fill] < (width - 2) * (height - 2) - (board.count(PROTECTEDCHAR) + board.count(OPENCHAR)):
|
||||||
|
board = area_fill(board, width, fill, char=BLOCKCHAR)
|
||||||
|
board = area_fill(board, width, len(board) - 1 - fill, char=BLOCKCHAR)
|
||||||
|
block_count -= fill_counts[fill] * 2
|
||||||
|
break
|
||||||
|
|
||||||
|
options = [i for i in range(len(board)) if board[i] == board[(len(board) - 1) - i] == OPENCHAR]
|
||||||
|
return blocks_backtrack(board, width, height, block_count, options)
|
||||||
|
|
||||||
|
def blocks_backtrack(board, width, height, block_count, options):
|
||||||
|
# print(options)
|
||||||
|
# display(board, width, height)
|
||||||
|
|
||||||
|
if block_count == 0 or len(options) == 0:
|
||||||
|
return board
|
||||||
|
|
||||||
|
for option in options:
|
||||||
|
if is_valid_blocking(board, width, height, option):
|
||||||
|
copy = board[:option] + BLOCKCHAR + board[option + 1 :]
|
||||||
|
copy = copy[: (len(copy) - 1) - option] + BLOCKCHAR + copy[len(copy) - option :]
|
||||||
|
updated_options = [i for i in options if i != option]
|
||||||
|
result = blocks_backtrack(copy, width, height, block_count - 2, updated_options)
|
||||||
|
if result != None: return result
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
def is_valid_blocking(board, width, height, option):
|
||||||
|
if board[option] != OPENCHAR: return False
|
||||||
|
temp = board[:option] + BLOCKCHAR + board[option + 1:]
|
||||||
|
temp = temp[:(len(temp) - 1) - option] + BLOCKCHAR + temp[len(temp) - option :]
|
||||||
|
|
||||||
|
illegalRegex = rf"[{BLOCKCHAR}](.?({PROTECTEDCHAR}|{OPENCHAR})|({PROTECTEDCHAR}|{OPENCHAR}).?)[{BLOCKCHAR}]"
|
||||||
|
if re.search(illegalRegex, temp) != None: return False
|
||||||
|
if re.search(illegalRegex, transpose(temp, width)) != None: return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def area_fill(board, width, sp, char='?'):
|
||||||
|
dirs = [-1, width, 1, -1 * width]
|
||||||
|
if sp < 0 or sp >= len(board): return board
|
||||||
|
if board[sp] in (OPENCHAR, PROTECTEDCHAR):
|
||||||
|
board = board[0:sp] + char + board[sp+1:]
|
||||||
|
for d in dirs:
|
||||||
|
if d == -1 and sp % width == 0: continue
|
||||||
|
if d == 1 and sp + 1 % width == 0: continue
|
||||||
|
board = area_fill(board, width, sp + d, char)
|
||||||
|
return board
|
||||||
|
|
||||||
|
def finish_board(board, width, height, words):
|
||||||
|
# remove border
|
||||||
|
board, width, height = remove_border(board, width, height)
|
||||||
|
|
||||||
|
# add words
|
||||||
|
for word in words:
|
||||||
|
index = word[1] * width + word[2]
|
||||||
|
for letter in word[3]:
|
||||||
|
board = board[:index] + letter + board[index + 1 :]
|
||||||
|
if word[0] == 'H':
|
||||||
|
index += 1
|
||||||
|
else:
|
||||||
|
index += width
|
||||||
|
|
||||||
|
# replace protected with open
|
||||||
|
board = re.sub(PROTECTEDCHAR, OPENCHAR, board)
|
||||||
|
|
||||||
|
return board, width, height
|
||||||
|
|
||||||
|
def display(board, width, height):
|
||||||
|
for i in range(height):
|
||||||
|
line = ""
|
||||||
|
for letter in range(width):
|
||||||
|
line += (board[(i * width) + letter] + " ")
|
||||||
|
print(line)
|
||||||
|
print()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
crossword(args)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
276642
Unit 4/crossword/scrabble.txt
Normal file
276642
Unit 4/crossword/scrabble.txt
Normal file
File diff suppressed because it is too large
Load Diff
19999
Unit 4/crossword/twentyk.txt
Normal file
19999
Unit 4/crossword/twentyk.txt
Normal file
File diff suppressed because it is too large
Load Diff
99162
Unit 4/crossword/wordss.txt
Normal file
99162
Unit 4/crossword/wordss.txt
Normal file
File diff suppressed because it is too large
Load Diff
|
@ -7,15 +7,11 @@ def num_40(subject):
|
||||||
# Write a regular expression that will match on an Othello board represented as a string.
|
# Write a regular expression that will match on an Othello board represented as a string.
|
||||||
pattern = "/^[xo.]{64}$/i" #notice that python does not want / /
|
pattern = "/^[xo.]{64}$/i" #notice that python does not want / /
|
||||||
return pattern
|
return pattern
|
||||||
match = re.match(pattern, subject, re.I)
|
|
||||||
print (match.group(0)) if match != None else print (match)
|
|
||||||
|
|
||||||
def num_41(subject):
|
def num_41(subject):
|
||||||
# Given a string of length 8, determine whether it could represent an Othello edge with exactly one hole.
|
# Given a string of length 8, determine whether it could represent an Othello edge with exactly one hole.
|
||||||
pattern = "/^[xo]*\.[xo]*$/i"
|
pattern = "/^[xo]*\.[xo]*$/i"
|
||||||
return pattern
|
return pattern
|
||||||
match = re.match(pattern, subject, re.I)
|
|
||||||
print (match.group(0)) if match != None else print (match)
|
|
||||||
|
|
||||||
def num_42(subject):
|
def num_42(subject):
|
||||||
# Given an Othello edge as a string, determine whether there is a hole such that if X plays to the hole (assuming it could),
|
# Given an Othello edge as a string, determine whether there is a hole such that if X plays to the hole (assuming it could),
|
||||||
|
@ -24,57 +20,41 @@ def num_42(subject):
|
||||||
# immediately followed by a hole.
|
# immediately followed by a hole.
|
||||||
pattern = "/^\.o*x|xo*\.$/i"
|
pattern = "/^\.o*x|xo*\.$/i"
|
||||||
return pattern
|
return pattern
|
||||||
match = re.search(pattern, subject, re.I)
|
|
||||||
print (match.group(0)) if match != None else print (match)
|
|
||||||
|
|
||||||
def num_43(subject):
|
def num_43(subject):
|
||||||
# Match on all strings of odd length.
|
# Match on all strings of odd length.
|
||||||
pattern = "/^(..)*.$/"
|
pattern = "/^(..)*.$/"
|
||||||
return pattern
|
return pattern
|
||||||
match = re.match(pattern, subject, re.DOTALL)
|
|
||||||
print (match.group(0)) if match != None else print (match)
|
|
||||||
|
|
||||||
def num_44(subject):
|
def num_44(subject):
|
||||||
# Match on all odd length binary strings starting with 0, and on even length binary strings starting with 1.
|
# Match on all odd length binary strings starting with 0, and on even length binary strings starting with 1.
|
||||||
pattern = "/^(0|1[01])([01]{2})*$/"
|
pattern = "/^(0|1[01])([01]{2})*$/"
|
||||||
return pattern
|
return pattern
|
||||||
match = re.match(pattern, subject)
|
|
||||||
print (match.group(0)) if match != None else print (match)
|
|
||||||
|
|
||||||
def num_45(subject):
|
def num_45(subject):
|
||||||
# Match all words having two adjacent vowels that differ.
|
# Match all words having two adjacent vowels that differ.
|
||||||
pattern = "/\w*(a[eiou]|e[aiou]|i[aeou]|o[aeiu]|u[aeio])\w*/i"
|
pattern = "/\w*(a[eiou]|e[aiou]|i[aeou]|o[aeiu]|u[aeio])\w*/i"
|
||||||
return pattern
|
return pattern
|
||||||
matches = re.finditer(pattern, subject, re.I)
|
|
||||||
print ([m.group() for m in matches])
|
|
||||||
|
|
||||||
def num_46(subject):
|
def num_46(subject):
|
||||||
# Match on all binary strings which DO NOT contain the substring 110.
|
# Match on all binary strings which DO NOT contain the substring 110.
|
||||||
pattern = "/^(1?0)*1*$/"
|
pattern = "/^(0*10)*0*1*$/"
|
||||||
return pattern
|
return pattern
|
||||||
match = re.match(pattern, subject)
|
|
||||||
print (match.group(0)) if match != None else print (match)
|
|
||||||
|
|
||||||
def num_47(subject):
|
def num_47(subject):
|
||||||
# Match on all non-empty strings over the alphabet {a, b, c} that contain at most one a.
|
# Match on all non-empty strings over the alphabet {a, b, c} that contain at most one a.
|
||||||
pattern = "/^(a[bc]*|[bc]+a?[bc]*)$/"
|
pattern = "/^(a[bc]*|[bc]+a?[bc]*)$/"
|
||||||
return pattern
|
return pattern
|
||||||
match = re.match(pattern, subject)
|
|
||||||
print (match.group(0)) if match != None else print (match)
|
|
||||||
|
|
||||||
def num_48(subject):
|
def num_48(subject):
|
||||||
# Match on all non-empty strings over the alphabet {a, b, c} that contain an even number of a's.
|
# Match on all non-empty strings over the alphabet {a, b, c} that contain an even number of a's.
|
||||||
pattern = "/^[bc]*(a[bc]*a[bc]*)*$/"
|
pattern = "/^[bc]*(a[bc]*a[bc]*)*$/"
|
||||||
return pattern
|
return pattern
|
||||||
match = re.match(pattern, subject)
|
|
||||||
print (match.group(0)) if match != None else print (match)
|
|
||||||
|
|
||||||
def num_49(subject):
|
def num_49(subject):
|
||||||
# Match on all positive, even, base 3 integer strings.
|
# Match on all positive, even, base 3 integer strings.
|
||||||
pattern = "/^[02]*(1[02]*1[02]*)*$/"
|
pattern = "/^[02]*(1[02]*1[02]*)*$/"
|
||||||
return pattern
|
return pattern
|
||||||
match = re.match(pattern, subject)
|
|
||||||
print (match.group(0)) if match != None else print (match)
|
|
||||||
|
|
||||||
import sys;
|
import sys;
|
||||||
args = sys.argv[1:]
|
args = sys.argv[1:]
|
43
Unit 4/regex/Lab_C_AI_Grader.py
Normal file
43
Unit 4/regex/Lab_C_AI_Grader.py
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
import sys; args = sys.argv[1:]
|
||||||
|
|
||||||
|
PATTERNS = [
|
||||||
|
|
||||||
|
# Q50: Match all words where some letter appears twice in the same word.
|
||||||
|
r'/(\w)+\w*\1\w*/i',
|
||||||
|
|
||||||
|
# Q51: Match all words where some letter appears four times in the same word.
|
||||||
|
r'/(\w)+(\w*\1){3}\w*/i',
|
||||||
|
|
||||||
|
# Q52: Match all non-empty binary strings with the same number of 01 substrings as 10 substrings.
|
||||||
|
r'/^1[10]*1$|^0[10]*0$/',
|
||||||
|
|
||||||
|
# Q53: Match all six letter words containing the substring cat.
|
||||||
|
r'/\b(?=\w*cat)\w{6}\b/',
|
||||||
|
|
||||||
|
# Q54: Match all 5 to 9 letter words containing both the substrings bri and ing.
|
||||||
|
r'/\b(?=\w*bri)(?=\w*ing)\w{5,9}\b/',
|
||||||
|
|
||||||
|
# Q55: Match all six letter words not containing the substring cat.
|
||||||
|
r'/\b(?!\w*cat)\w{6}\b/',
|
||||||
|
|
||||||
|
# Q56: Match all words with no repeated characters.
|
||||||
|
r'/\b((\w)(?!\w*\2))*\b/',
|
||||||
|
|
||||||
|
# Q57: Match all binary strings not containing the forbidden substring 10011.
|
||||||
|
r'/^(?!.*10011)[01]*$/',
|
||||||
|
|
||||||
|
# Q58: Match all words having two different adjacent vowels.
|
||||||
|
r'/\w*(a[eiou]|e[aiou]|i[aeou]|o[aeiu]|u[aeio])\w*/i',
|
||||||
|
|
||||||
|
# Q59: Match all binary strings containing neither 101 nor 111 as substrings.
|
||||||
|
r'/^(?!.*1.1)[01]*$/'
|
||||||
|
|
||||||
|
]
|
||||||
|
|
||||||
|
if len(args) != 0:
|
||||||
|
idx = int(args[0])-50
|
||||||
|
print(PATTERNS[idx])
|
||||||
|
else:
|
||||||
|
with open('Umaretiya_r_U4_C.txt', 'w') as file:
|
||||||
|
for pattern in PATTERNS:
|
||||||
|
file.write(pattern + '\n')
|
38
Unit 4/regex/Lab_C_self_check.py
Normal file
38
Unit 4/regex/Lab_C_self_check.py
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
import sys; args = sys.argv[1:]
|
||||||
|
|
||||||
|
PATTERNS = [
|
||||||
|
|
||||||
|
# Q50: Match all words where some letter appears twice in the same word.
|
||||||
|
r'(\w)+\w*\1\w*',
|
||||||
|
|
||||||
|
# Q51: Match all words where some letter appears four times in the same word.
|
||||||
|
r'(\w)+(\w*\1){3}\w*',
|
||||||
|
|
||||||
|
# Q52: Match all non-empty binary strings with the same number of 01 substrings as 10 substrings.
|
||||||
|
r'^1[10]*1$|^0[10]*0$',
|
||||||
|
|
||||||
|
# Q53: Match all six letter words containing the substring cat.
|
||||||
|
r'\b(?=\w*cat)\w{6}\b',
|
||||||
|
|
||||||
|
# Q54: Match all 5 to 9 letter words containing both the substrings bri and ing.
|
||||||
|
r'\b(?=\w*bri)(?=\w*ing)\w{5,9}\b',
|
||||||
|
|
||||||
|
# Q55: Match all six letter words not containing the substring cat.
|
||||||
|
r'\b(?!\w*cat)\w{6}\b',
|
||||||
|
|
||||||
|
# Q56: Match all words with no repeated characters.
|
||||||
|
r'\b((\w)(?!\w*\2))+\b',
|
||||||
|
|
||||||
|
# Q57: Match all binary strings not containing the forbidden substring 10011.
|
||||||
|
r'^(?!.*10011)[01]*$',
|
||||||
|
|
||||||
|
# Q58: Match all words having two different adjacent vowels.
|
||||||
|
r'\w*(a[eiou]|e[aiou]|i[aeou]|o[aeiu]|u[aeio])\w*',
|
||||||
|
|
||||||
|
# Q59: Match all binary strings containing neither 101 nor 111 as substrings.
|
||||||
|
r'^(?!.*1.1)[01]*$'
|
||||||
|
|
||||||
|
]
|
||||||
|
|
||||||
|
idx = int(args[0])-50
|
||||||
|
print(PATTERNS[idx])
|
10
Unit 4/regex/Umaretiya_r_U4_B_ver2.txt
Normal file
10
Unit 4/regex/Umaretiya_r_U4_B_ver2.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
^[xo.]{64}$
|
||||||
|
^[xo]*\.[xo]*$
|
||||||
|
\.o*x|xo*\.$
|
||||||
|
^(..)*.$
|
||||||
|
^(0|1[01])([01]{2})*$
|
||||||
|
\w*(a[eiou]|e[aiou]|i[aeou]|o[aeiu]|u[aeio])\w*
|
||||||
|
(1?0)*1*$
|
||||||
|
^[bc]+a?[bc]*$
|
||||||
|
^[bc]*(a[bc]*a[bc]*)*$
|
||||||
|
^[02]*(1[02]*1[02]*)*$
|
10
Unit 4/regex/Umaretiya_r_U4_B_ver3.txt
Normal file
10
Unit 4/regex/Umaretiya_r_U4_B_ver3.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
^[xo.]{64}$
|
||||||
|
^[xo]*\.[xo]*$
|
||||||
|
^\.o*x|xo*\.$
|
||||||
|
^(..)*.$
|
||||||
|
^(0|1[01])([01]{2})*$
|
||||||
|
\w*(a[eiou]|e[aiou]|i[aeou]|o[aeiu]|u[aeio])\w*
|
||||||
|
(1?0)*1*$
|
||||||
|
^[bc]+a?[bc]*$
|
||||||
|
^[bc]*(a[bc]*a[bc]*)*$
|
||||||
|
^[02]*(1[02]*1[02]*)*$
|
Loading…
Reference in New Issue
Block a user