473,386 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to get possible words from the given letters?

I have some problem with one task (only this task left from 12 tasks). I am trying to write a scrabble code on python.

I have to write a function that gets all the possible words from the given letters in hand. And there is an asterisk that can be replaced by any letter.
For example:

Given letters = 'crn*'
Words can be = 'cry', 'corn' and etc. I can't get all the possible words when there is an asterisk.

Can you give me some hints for this problem?
May 2 '10 #1
2 1210
woooee
43
You want to first count the number of asterisks; that will be the cut-off. Next, compare the given word to all possibles. To allow for two letters that are the same, say a word has two "o"s for example, sort both words and compare with a loop, comparing each letter in turn. Increment through the word if a letter is found.

You can also use lists, which is probably easier. Use the first word as the control. Convert the second word to a list. Iterate through the first word letter by letter and if the letter is found in the list, delete it, otherwise add one to a misses variable. Note that either word can be the longer so you have to allow for both cases.

Of course, it the length of the words is off by more than the number of asterisks it is an automatic fail.
May 2 '10 #2
Glenton
391 Expert 256MB
Presumably (from the previous posts) you've solved the problem of doing this without any asterisk. So why not just have loops going from a-z replacing each asterisk with one letter, and checking them all.

Of course, this is terribly inefficient to *run*, but might be efficient to code!

Itertools is your friend for this kind of thing.

Otherwise, write a function that tells you how much overlap there is between a test word, and the letters in your list, and how many letters are left over. If the number of letters left over is less than the number of asterisks then you can make the word.

It's this the exercise where you're not allowed to use set functions? Because that would make your life easy!
May 4 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Johannes Nix | last post by:
Hello, yesterday I met a cute person (after my dance class) who told me about an interesting experiment regarding cognition. People were told to read a typed text; However, in every word in the...
3
by: Xah Lee | last post by:
20050226 exercise: generate all possible pairings given a list that is a set partitioned into subsets, generate a list of all possible pairings of elements in any two subset. Example: ...
9
by: superprad | last post by:
"X-No-Archive: yes" what I am looking for is 1. To create a list of different words of various lengths(1-15) using A-Z,a-z,0-9 and punctuations.Basically anything that could be found on a...
9
by: dan | last post by:
this is a program to count average letters per word. i am able to count the total number of letters, but not words. How do you count the total number of words in a text file, so i am able to divide...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
2
by: brock | last post by:
I'd like to be able to take a string and search within it for all words (of the longest length possible) that are possibly contained within it (in sequence, we're not re-ordering the letters in the...
18
lee123
by: lee123 | last post by:
is there a way to make random words or letters instead of numbers? lee123
6
by: toefraz | last post by:
Hi all. I've been given an assignment to construct an algorithm that can find all words that occur within a given word. For example: Given furthermore, solutions could be form, the, further,...
14
by: natarajmtech | last post by:
Hi all, I have to do a translation of a word into different possible word based on a dictionary, which define differ meaning for each letter in a word. For example, The word "VAEFGH" to be...
6
by: Jimmy45 | last post by:
Hello, I am writing a "scrabble code" on Python. I have some problem with getting possible words from the given letters. For example: Given letters are: 'c', 'r', 'n', '*' Here '*' can be any...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.