GithubHelp home page GithubHelp logo

utkarsh1308 / scrabble-cheater Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 688 KB

Python script that takes a Scrabble rack as a command-line argument and prints all valid Scrabble words that can be constructed from that rack, along with their Scrabble scores, sorted by score

Python 100.00%

scrabble-cheater's Introduction

Project

Write a Scrabble cheater from scratch

Goals

  • practice breaking down a problem and solving itin Python from scratch
  • practice command line argument parsing
  • practice reading from files
  • practice working with dictionaries and for loops

Problem Statement

Write a Python script that takes a Scrabble rack as a command-line argument and prints all valid Scrabble words that can be constructed from that rack, along with their Scrabble scores, sorted by score. 

An example invocation and output:
    $ python scrabble.py ZAEFIEE
    17 feeze
    17 feaze
    16 faze
    15 fiz
    15 fez
    12 zee
    12 zea
    11 za
    6 fie
    6 fee
    6 fae
    5 if
    5 fe
    5 fa
    5 ef
    2 ee
    2 ea
    2 ai
    2 ae

Resources

Breaking down the problem

Step 1: construct a word list

Write the code to open and read the sowpods word file. Create a list, where each element is a word in the sowpods word file. Note that each line in the file ends in a newline, which you'll need to remove from the word.

Step 1 resources:

Step 2: get the rack

Write the code to get the Scrabble rack (the letters available to make words) from the command line argument passed to your script. For example if your script were called `scrabble_cheater.py`, if you ran python scrabble_cheater.py RSTLNEI, RSTLNEI would be the rack.
	
Handle the case where a a user forgets to supply a rack; in this case, print an error message saying they need to supply some letters, and then exit the program using the exit() function. Make sure you are consistent about capitalization

Step 2 resources: `

Step 3: find valid words

Write the code to find all words from the word list that are made of letters that are a subset of the rack letters. There are many ways to do this, but here's one way that is easy to reason about and is fast enough for our purposes: go through every word in the word list, and for every letter in that word, see if that letter is contained in the rack. If it is, save the word in a valid_words list. Make sure you handle repeat letters: once a letter from the rack has been used, it can't be used again.

Step 3 resources:

Step 4: scoring

Write the code to determine the Scrabble scores for each valid word, using the scores dictionary from above.

step 4 resources:

scrabble-cheater's People

Contributors

utkarsh1308 avatar

Watchers

 avatar

Forkers

kolenja721

scrabble-cheater's Issues

Rack Error

usage: finder.py [-h] RACK
finder.py: error: the following arguments are required: RACK

`import argparse
import operator

with open('kelimeler.txt', 'r') as f:
sowpods = f.readlines()
words = [x.strip() for x in sowpods]

parser = argparse.ArgumentParser(description="get the rack")
parser.add_argument('RACK', help="Input rack")
args = parser.parse_args()
RACK = (args.RACK).upper()

valid_words = []

def IsSubset(s1, s2):
alist = list(s2)

pos1 = 0
stillOK = True

while pos1 < len(s1) and stillOK:
    pos2 = 0
    found = False
    while pos2 < len(alist) and not found:
        if s1[pos1] == alist[pos2]:
            found = True
        else:
            pos2 = pos2 + 1

    if found:
        alist[pos2] = None
    else:
        stillOK = False

    pos1 = pos1 + 1

return stillOK

for word in words:
if IsSubset(word, RACK):
valid_words.append(word)

scores = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}

score = 0
result = {}
for word in valid_words:
for letter in list(word):
score = score + scores[letter.lower()]
result[word] = score
score = 0
sorted_x = sorted(result.items(), key=operator.itemgetter(1), reverse=True)
result = dict(sorted_x)
for word in result.keys():
print("{} {}".format(result[word], word))
`

What is this issue?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.