Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #############################################################
- # FILE : check_wordsearch.py
- # WRITER : Daniel Kerbel , danielkerbel
- # EXERCISE : intro2cs1 ex5 2018-2019
- # DESCRIPTION: Testing wordsearch
- #############################################################
- WEST = "l"
- EAST = "r"
- NORTH = "u"
- SOUTH = "d"
- NORTH_EAST = "w"
- NORTH_WEST = "x"
- SOUTH_EAST = "y"
- SOUTH_WEST = "z"
- DIRECTIONS = {EAST, WEST, NORTH, SOUTH,
- NORTH_EAST, NORTH_WEST, SOUTH_EAST, SOUTH_WEST}
- from wordsearch import *
- mat = [
- list("apple"),
- list("agodo"),
- list("nnert"),
- list("gaTAC"),
- list("micsr"),
- list("PoPoP")
- ]
- """
- a,p,p,l,e
- a,g,o,d,o
- n,n,e,r,t
- g,a,T,A,C
- m,i,c,s,r
- P,o,P,o,P
- """
- def test_find_words_in_matrix():
- words = ["apple", "mic", "nope", "PiT", "", "wut"]
- occurs = find_words_in_matrix(words, mat, EAST+NORTH_EAST)
- assert dict(occurs) == {
- "apple": 1,
- "mic": 1,
- "PiT": 1
- }
- # testing empty inputs
- assert find_words_in_matrix(words, mat, "") == []
- assert find_words_in_matrix([], mat, EAST) == []
- assert find_words_in_matrix(words, [], EAST) == []
- assert find_words_in_matrix([], [], EAST) == []
- assert find_words_in_matrix([], [], "") == []
- def test_tricky_words():
- words = ["pip"]
- matrix = [
- list("pipippip"),
- list("aiaiaiai"),
- list("xxpxpxpx")
- ]
- assert dict(find_words_in_matrix(words, matrix, EAST)) == {
- "pip": 3
- }
- assert dict(find_words_in_matrix(words, matrix, EAST+SOUTH_EAST)) == {
- "pip": 6
- }
- assert dict(find_words_in_matrix(words, matrix, EAST+NORTH_WEST)) == {
- "pip": 6
- }
- assert dict(find_words_in_matrix(words, matrix, EAST+SOUTH_EAST+WEST+NORTH_WEST)) == {
- "pip": 12
- }
- def test_singleletter_matrix():
- matrix = [["h"]]
- words = ["h", "irrelevant"]
- for direction in DIRECTIONS:
- assert dict(find_words_in_matrix(words, matrix, direction)) == {"h": 1}
- def test_singleaxis_matrix():
- matrix = [list("shalom")]
- words = ["shalom", "shal", "lom"]
- assert dict(find_words_in_matrix(words, matrix, EAST)) == {
- "shalom": 1, "shal": 1, "lom": 1
- }
- assert dict(find_words_in_matrix(words, matrix, SOUTH)) == {}
- assert dict(find_words_in_matrix(words, matrix, WEST+SOUTH_WEST + SOUTH_EAST)) == {}
- matrix = [["s"],
- ["h"],
- ["a"],
- ["l"],
- ["o"],
- ["m"]]
- assert dict(find_words_in_matrix(words, matrix, SOUTH)) == {
- "shalom": 1, "shal": 1, "lom": 1
- }
- assert dict(find_words_in_matrix(words, matrix, EAST)) == {}
- assert dict(find_words_in_matrix(words, matrix, WEST+SOUTH_WEST + SOUTH_EAST)) == {}
- def test_antidiagonals_2():
- matrix = [
- list("ABCDEF"),
- list("GHIJKL"),
- list("MNOPQR"),
- list("STUVWX")
- ]
- def test_main_diagonal_range():
- assert list(main_diagonal_iterator(mat, 0, 0)) == list("ageAr")
- assert list(main_diagonal_iterator(mat, 0, 0, invert=True)) == list("rAega")
- assert list(main_diagonal_iterator(mat, 0, 2)) == list("pdt")
- assert list(main_diagonal_iterator(mat, 0, 2, invert=True)) == list("tdp")
- assert list(main_diagonal_iterator(mat, 1, 2)) == list("orC")
- assert list(main_diagonal_iterator(mat, 1, 2, invert=True)) == list("Cro")
- assert list(main_diagonal_iterator(mat, 5, 0)) == ["P"]
- assert list(main_diagonal_iterator(mat, 5, 0, invert=True)) == ["P"]
- assert list(main_diagonal_iterator(mat, 4, 3)) == list("sP")
- assert list(main_diagonal_iterator(mat, 4, 3, invert=True)) == list("Ps")
- def test_antidiagonal_range():
- assert list(antidiagonal_iterator(mat, 0, 0)) == ["a"]
- assert list(antidiagonal_iterator(mat, 0, 0, invert=True)) == ["a"]
- assert list(antidiagonal_iterator(mat, 0, 4)) == list("edeam")
- assert list(antidiagonal_iterator(mat, 0, 4, invert=True)) == list("maede")
- assert list(antidiagonal_iterator(mat, 1, 3)) == list("deam")
- assert list(antidiagonal_iterator(mat, 1, 3, invert=True)) == list("maed")
- assert list(antidiagonal_iterator(mat, 1, 4)) == list("orTiP")
- assert list(antidiagonal_iterator(mat, 1, 4, invert=True)) == list("PiTro")
- assert list(antidiagonal_iterator(mat, 5, 4)) == ["P"]
- assert list(antidiagonal_iterator(mat, 5, 4, invert=True)) == ["P"]
- def test_main_diagonals():
- diagonals = { "".join(diagonal) for diagonal in main_diagonals_iterator(mat)}
- expected = { "ageAr", "porC", "pdt", "lo", "e",
- "anTsP", "naco", "giP", "mo", "P" }
- assert expected == diagonals
- def test_anti_diagonals():
- diagonals = { "".join(diagonal) for diagonal in anti_diagonals_iterator(mat)}
- expected = { "a", "pa", "pgn", "long", "edeam",
- "orTiP", "tAco", "CsP", "ro", "P"}
- assert expected == diagonals
- def test_rows():
- rows_set = { "".join(row) for row in rows_iterator(mat)}
- expected = { "apple", "agodo", "nnert", "gaTAC", "micsr", "PoPoP"}
- assert rows_set == expected
- """
- a,p,p,l,e
- a,g,o,d,o
- n,n,e,r,t
- g,a,T,A,C
- m,i,c,s,r
- P,o,P,o,P
- """
- def test_columns():
- cols_set = { "".join(col) for col in columns_iterator(mat)}
- expected = { "aangmP", "pgnaio", "poeTcP", "ldrAso", "eotCrP"}
- revexpected = { "Pmgnaa", "oiangp", "PcTeop", "osArdl", "PrCtoe"}
- assert cols_set == expected
- rev_col_set = { "".join(col) for col in columns_iterator(mat, invert=True)}
- assert rev_col_set == revexpected
- def filetester(direction):
- matrix = read_matrix_file("mat.txt")
- words = read_wordlist_file("word_list.txt")
- my_output = dict(find_words_in_matrix(words, matrix, direction))
- with open(f"outfile_{direction}.txt", 'r') as file:
- words = {}
- for line in file:
- [word, count] = line.rstrip("\n").split(sep=",")
- words[word] = int(count)
- assert my_output == words
- def test_file_r():
- filetester('r')
- def test_file_l():
- filetester('l')
- def test_file_u():
- filetester('u')
- def test_file_d():
- filetester('d')
- def test_file_w():
- filetester('w')
- def test_file_x():
- filetester('x')
- def test_file_y():
- filetester('y')
- def test_file_z():
- filetester('z')
- def test_file_udlrwxyz():
- filetester('udlrwxyz')
- if __name__ == '__main__':
- print("Please run this via 'pytest check_wordsearch.py'")
Advertisement
Add Comment
Please, Sign In to add comment