Question: Write a function that takes a string with letters and characters. You have to write The function receives as parameters the pattern as a string
Write a function that takes a string with letters and characters. You have to write The function receives as parameters the pattern as a string and words as a list of words whose length is between minlen and maxlen , inclusive, and a scoring function scoring_f that will tell you how many points its argument word will score. From this information, the function returns a new string for the completed row.
def fill_words(pattern, words, scoring_f, minlen, maxlen):
The above automated tester script scrabblerun.py that you can use to automatically test and evaluate your submission, along with the wordlist file words_sorted.txt . The row patterns given to your function to fill are generated pseudorandomly using the seed value defined in the beginning of the tester script. The game is played for the given number of rounds for patterns of length patlen , with the overall wordlist restricted to use the subset of words in words_sorted.txt whose length is between minlen and maxlen , inclusive.
import scrabblerow as sr from random import Random
version = 'August 22, 2020'
# Separator character denoting a blank space.
sep = '-'
# Seed for the random number generator that produces the patterns. seed = 4444
# Length of the random patterns.
patlen = 40
# Minimum and maximum length of words accepted in solutions.
minlen, maxlen = 4, 30
# How many rounds to play this game.
rounds = 10
# Maximum possible consecutive run of blanks in the pattern.
mb = 5
# Percentage probability of the current run of blanks to continue.
bp = 30
# Scrabble letter values in the standard American English version.
letter_values = { 'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10 }
# Dictionary used to count the frequency of each letter.
letter_counts = {c: 0 for c in letter_values}
# Word scoring function for scrabble.
def scrabble_value(word): if minlen <= len(word) <= maxlen: return sum(letter_values.get(c, 0) for c in word) else:
return 0
def length_squared(word): if minlen <= len(word) <= maxlen: return len(word) ** 2 else: return 0
# Create a random pattern of the given length.
def create_pattern(n, rng): prev, result, blanks = '', '', 0 letters = "".join(sorted([c for c in letter_counts])) letter_freqs = [letter_counts[c] for c in letters] for i in range(n): if blanks < mb and (prev != sep or rng.randint(0, 99) < bp): prev = sep blanks += 1 else: prev = rng.choices(letters, letter_freqs, k=1)[0] blanks = 0 result += prev return result
def score_answer(result, pattern, scoring_f): curr, score = '', 0 for (pos, (c1, c2)) in enumerate(zip(result + sep, pattern + sep)): if c2 != sep and c1 != c2: print(f" PATTERN MISMATCH AT POSITION {pos}!!!") return 0 if c1 == sep: if curr in wordset: score += scoring_f(curr) elif len(curr) > 1: print(f" UNKNOWN WORD {curr} AT POSITION {pos}!!!") return 0 curr = '' else: curr += c1 return score
def play_one_round(pattern, scoring_f): score = 0 print(f" Pattern: {pattern}") try: result = sr.fill_words(pattern, words, scoring_f, minlen, maxlen) except Exception as e: print(f"CRASH!!! {e}") return 0 print(f"Result : {result} ", end='') if len(result) == len(pattern): score += score_answer(result, pattern, scoring_f) else: print(f" RESULT AND PATTERN LENGTHS DIFFERENT!!!") print(f"({score})") return score
def play(): print(f"Scrabblerun tester by Ilkka Kokkarinen, {version}.") print(f"Settings seed={seed}, patlen={patlen}, rounds={rounds}.")
def scoring_f(w): return length_squared(w) + scrabble_value(w)
# Just in case you want to try out some test cases of your own. testcases = [] # Fill in your testcase strings inside this list. for pattern in testcases: play_one_round(pattern, scoring_f)
total, rng = 0, Random(seed) for r in range(rounds): pattern = create_pattern(patlen, rng) total += play_one_round(pattern, scoring_f) print(f"{total} {sr.author()} {sr.student_id()}")
with open('words_sorted.txt', 'r', encoding='utf-8') as f: words = [x.strip() for x in f] words = [x for x in words if minlen <= len(x) <= maxlen] wordset = set(words) for word in words: for c in word: letter_counts[c] += 1
if __name__ == '__main__': play()
Output: Should look like this....
An example run with settings seed=7777 , patlen=50 and rounds=10 with the instructor's private model solution at the moment of writing this produced the following output. The score for each returned result is displayed in parentheses after the result. scrabblerun with seed=7777, patlen=50 and rounds=10.
Pattern: --h-a-n--u-q--s--u-d-i-e----i-s-i-e--l-e-l-c-t-w-o Result : mahwa-nabu-qats-quodlibetz-disseize-elve-licht-w-o (360) Pattern: --s--i--o-u---e--o-t---n--e-s-p-u--u--a-x-e-d-e-g- Result : assyriologue-teapottykin-zeks-piupiu-falx-exdie-g- (448) Pattern: -c-h--r-r---e---d--o-n-s---y--h-r-o-i---a--u-p-r-o Result : scfh-frary-fezzed-cognisably-thyreolingual-upper-o (469) Pattern: -t---i--e-d--m-i-a---u-u-s-k-n-i---t-a--o---t-o--- Result : athyridae-duumviral-juju-soken-izzat-alloeostropha (496) Pattern: -r-l-t-l-u-i-n--t-r--s-a-i-e-l----o-c-p-n--e--e--- Result : kral-tellurian-athrepsia-idealize-occupancies-exrx (465) Pattern: -r-a-s-e-e-p--t-t-b--t-i-e-s-i--d-o-----t-m-e-e--i Result : krna-skene-phototube-twice-shieldboard-stampeded-i (425) Pattern: -o-a-i--e-r-e-c---l-v-n-r-a----s-i---y--o-s-n-y-a- Result : colazione-rheic-folkvangr-aggressively-jobson-yday (479) Pattern: -i---a-i---o--l-u-o-l-i-i-e-i-m-n--e--u-b-n-a--r-a Result : piazzalike-oxyl-unoil-isize-immunized-urban-agyria (415) Pattern: ---u-e-e-n---g-e-i-p-m-p-a-r-k-u--o-i-t-a-u--t-e-- Result : piquiere-nuraghe-i-p-mopla-r-khud-ovist-adulatress (349) Pattern: -o---a-w--b-e-i-o--i-i-d-n----b-y--o-l-o--h--p--b- Result : kozuka-womb-erizo-liriodendra-bayamo-loofahs-phoby (410)
The returned result string must be the same length as pattern , and in every position where pattern has a non-blank letter, the result must contain that same letter. Furthermore, each complete word inside the string must be a legal word from words_sorted.txt (English dictionary of sorted word per line) whose length is between minlen and maxlen , inclusive. Result strings that do not obey these constraints receive zero points. (The rest of the evaluation will still continue, though.)
words_sorted.txt (below are just a few words which can be used, otherwise feel free to put words in the raw txt file, its a big file so couldnt upload)
a aa aaa aah aahed aahing aahs aal aalii aaliis aals aam aani aardvark aardvarks aardwolf aardwolves aargh aaron aaronic aaronical aaronite aaronitic aarrgh aarrghh aaru aas aasvogel aasvogels ab aba ababdeh ababua abac abaca abacas abacate abacaxi abacay abaci abacinate abacination abacisci abaciscus abacist aback abacli abacot abacterial abactinal abactinally abaction abactor abaculi abaculus abacus abacuses abada abaddon abadejo abadengo abadia abadite abaff abaft abaisance abaised abaiser abaisse abaissed abaka abakas abalation abalienate abalienated abalienating abalienation abalone abalones abama abamp abampere abamperes abamps aband abandon abandonable abandoned abandonedly abandonee abandoner abandoners abandoning abandonment abandonments
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
