Reverse words cant seem to find them all - python

One thing I am trying to do is find words that are reversed and are greater or equal to 5 characters, for the following code I have it finds palindrome and any word that is the same backwards
However, I cannot figure out how to get it find 'Damon' or 'nomad'
I have tried using sorted() to get anagrams but unable to implement it.
import sys
word = []
backword = []
def main():
for lines in sys.stdin:
word.append(lines.strip())
for element in word:
if len(element.lower()) >= 5 and element.lower() == element[::-1].lower():
backword.append(element)
print(backword)
if __name__ == '__main__':
main()
Edit - Thank you for your solutions I have to find another way that does it fast...

If I understand correctly then you're looking for any word in a list that is the reverse of another word in the list? In that case you have a few options.
Case 1: You can precompute the list of reversed characters like so:
reversed = [element[::-1].lower() for element in word if len(element) >= 5]
backword = [element for element in word if element.lower() in reversed]
Making a map may also be faster:
reversed = map(lambda s: s.lower()[::-1], word)
backword = [element for element in word if element.lower() in reversed]
Case 2: You can reverse and check each time like so:
backword = [element for element in word if element[::-1] in word]
In the case that you have capitalized words that you haven't filtered out:
backword = [element for element in word if element[::-1] in word or element[::-1].lower().capitalize() in word]
In the case where you have mixed casing in word you should go with case 1 as you can precompute both values with reduced complexity due to the conditions in place.

Using the structure you have provided, this would work:
words = ['Damon', 'test', 'word', 'nomad']
reverse = []
words_set = set(map(str.lower, words))
for element in words:
if len(element.lower()) >= 5 and element.lower()[::-1] in words_set:
reverse.append(element)
print(reverse)
# ['Damon', 'nomad']

Related

How to print palindroms of given list of words usinf FOR LOOP?

I'am searching on the internet quite long but I'am not succesfull. I have a list of words:
words = ["kayak", "pas", "pes", "reviver", "caj", "osel", "racecar","bizon", "zubr", "madam"]
And I need to print only words that are palindromes. I found this solution:
words = ["kayak", "pas", "pes", "reviver", "caj", "osel", "racecar","bizon", "zubr", "madam"]
palindrome = list(filter(lambda x: (x == "".join(reversed(x))), words))
print(palindrome)
But I dont like it. I have to use FOR loop somehow and I dont know how. I tried many things but still dont get it.
Thank you.
Here is the example with explanations:
words = ['kayak', 'pas', 'pes', 'reviver', 'caj', 'osel', 'racecar','bizon', 'zubr', 'madam']
# Start with empty list
palindromes = []
# Iterate over each word
for word in words:
# Check if word is equal to same word in reverse order
is_palindrome = (word == word[::-1])
# Append to results if needed
if is_palindrome:
palindromes.append(word)
print(palindromes)
# => ['kayak', 'reviver', 'racecar', 'madam']
With a for loop:
palindrome = []
for i in words:
if i == "".join(reversed(i)):
palindrome.append(i)
print(palindrome)
Or list comprehension:
palindrome = [i for i in words if i == "".join(reversed(i))]
print(palindrome)
Output:
['kayak', 'reviver', 'racecar', 'madam']

Looking through a text file

I've been trying to create a function which gives a hint to the user who plays hangman.
The idea behind the function is that I'm having a list of 5k words plus and I need to sort it out through numerous indicators, such as the word should be the same as the pattern, if the pattern is a___le so the words that I should look for suppose to be in the same group and that if the user has numerous wrong letter it'll not consider the words whom includes this letters
I'm aware that I didn't do it in the most pythonic or elegant way but if someone can tell me what is going wrong here I'm always getting a empty list or a list containing the words with the same length but the other conditions are being constantly ignored.
def filter_words_list(words, pattern, wrong_guess_lst):
"""
:param words: The words I received from the main function
:param pattern: the pattern of the word in seach such as p__pl_
:param wrong_guess_lst: the set of wrong used letters of the users
:return: the function returns the words which are much to the conditions.
"""
list(wrong_guess_lst) # Since I am receiving it as a set I'm converting it to a list.
words_suggestions = [] # The list I'd like to put my suggested words.
for i in range(0, len(words)): # First loop matching between the length of the patterns and the words
if len(words[i]) == len(pattern):
for j in range(0, len(pattern)):
if pattern[j] != '_':
if pattern[j] == words[i][j]: # Checking if the letters of the words are a much.
for t in range(0, len(wrong_guess_lst)):
if wrong_guess_lst[t] != words[i][j]: # Does the same as before but only with the wrong guess lst.
words_suggestions.append(words[i])
return words_suggestions
I think this is what you are looking for (explanation in code comments):
def get_suggestions(words: list, pattern: str, exclude: list) -> list:
"""Finds pattern and returns all words matching it."""
# get the length of the pattern for filtering
length = len(pattern)
# create a filtered generator so that memory is not take up;
# it only give the items from the word list that match the
# conditions i.e. the same length as pattern and not in excluded words
filter_words = (word for word in words
if len(word) == length and word not in exclude)
# create a mapping of all the letters and their corresponding index in the string
mapping = {i: letter for i, letter in enumerate(pattern) if letter != '_'}
# return list comprehension that is made of words in the filtered words that
# match the condition that all letters are in the same place and have the same
# value as the mapping
return [word for word in filter_words
if all(word[i] == v for i, v in mapping.items())]
word_list = [
'peach', 'peace', 'great', 'good', 'food',
'reach', 'race', 'face', 'competent', 'completed'
]
exclude_list = ['good']
word_pattern = 'pe___'
suggestions = get_suggestions(word_list, word_pattern, exclude_list)
print(suggestions)
# output:
# ['peach', 'peace']
# a bit of testing
# order of items in the list is important
# it should be the same as in the word_list
patterns_and_answers = {
'_oo_': ['food'], # 'good' is in the excluded words
'_omp_____': ['competent', 'completed'],
'__ce': ['race', 'face'],
'gr_a_': ['great'],
'_a_e': ['race', 'face'],
'_ea__': ['peach', 'peace', 'reach']
}
for p, correct in patterns_and_answers.items():
assert get_suggestions(word_list, p, exclude_list) == correct
print('all test cases successfull')

List index out of range in indented for loops

I am trying to make a program that searches a list of strings, using a second list. The first list is a list of random jumbled up letters and the second is a list of words. The program is supposed to detect whether a word in the each word in the second list can be found in any of the strings in the first. If it is, then the word that was found is added to an empty, third list.
counter = 0
for a in FirstList:
for b in SecondList:
if SecondList[counter] in FirstList[counter]:
ThirdList += [SecondList[counter]]
counter += 1
if counter >= len(SecondList):
break
return ThirdList
However, it throws up an error on the first if statement claiming that the list index is out of range. I don't quite understand what I am missing, because I am not editing the contents of any of the lists that I am iterating through, which is what the reason for this error was in other posts about the same error.
The issue here is you are you using a counter which should break at the size of SecondList, but if the FirstList is smaller "counter" will be out of bounds for the FirstList.
I think this may be closer to what you are trying to achieve:
ThirdList = [word for word in SecondList for string in FirstList if word in string]
This is a list comprehension which is running through both lists and outputting any values from the second list which appear in any of the values in the first list.
So for example with the code:
FirstList = ["aadada", "asdtest", "hasdk"]
SecondList = ["test", "data"]
ThirdList = [word for word in SecondList for string in FirstList if word in string]
print(ThirdList)
You would receive the output: "test" as test is found in the FirstList.
You should not iterate over the two lists. Your counter is exceding the amount of elements in both lists. If I understand well what you are trying to do, you could use the following code:
ThirdList = []
for word in SecondList:
for random_word in FirstList:
if word in random_word and word not in ThirdList:
ThirdList.append(word)
return ThirdList
You can remove the second condition of the if statement if you have no problem with duplicate elements in ThirdList.

Optimization of Scrabble Algorithm

I'm trying to write an algorithm that by given to it a bunch of letters is giving you all the words that can be constructed of the letters, for instance, given 'car' should return a list contains [arc,car,a, etc...] and out of it returns the best scrabble word. The problem is in finding that list which contains all the words.
I've got a giant txt file dictionary, line delimited and I've tried this so far:
def find_optimal(bunch_of_letters: str):
words_to_check = []
c1 = Counter(bunch_of_letters.lower())
for word in load_words():
c2 = Counter(word.lower())
if c2 & c1 == c2:
words_to_check.append(word)
max_word = max_word_value(words_to_check)
return max_word,calc_word_value(max_word)
max_word_value - returns the word with the maximum value of the list given
calc_word_value - returns the word's score in scrabble.
load_words - return a list of the dictionary.
I'm currently using counters to do the trick but, the problem is that I'm currently on about 2.5 seconds per search and I don't know how to optimize this, any thoughts?
Try this:
def find_optimal(bunch_of_letters):
bunch_of_letters = ''.join(sorted(bunch_of_letters))
words_to_check = [word for word in load_words() if ''.join(sorted(word)) in bunch_of_letters]
max_word = max_word_value(words_to_check)
return max_word, calc_word_value(max_word)
I've just used (or at least tried to use) a list comprehension. Essentially, words_to_check will (hopefully!) be a list of all of the words which are in your text file.
On a side note, if you don't want to use a gigantic text file for the words, check out enchant!
from itertools import permutations
theword = 'car' # or we can use input('Type in a word: ')
mylist = [permutations(theword, i)for i in range(1, len(theword)+1)]
for generator in mylist:
for word in generator:
print(''.join(word))
# instead of .join just print (word) for tuple
Output:
c
a
r
ca
cr
...
ar rc ra car cra acr arc rca rac
This will give us all the possible combinations (i.e. permutations) of a word.
If you're looking to see if the generated word is an actual word in the English dictionary we can use This Answer
import enchant
d = enchant.Dict("en_US")
for word in mylist:
print(d.check(word), word)
Conclusion:
If want to generate all the combinations of the word. We use this code:
from itertools import combinations, permutations, product
word = 'word' # or we can use input('Type in a word: ')
solution = permutations(word, 4)
for i in solution:
print(''.join(i)) # just print(i) if you want a tuple

How many common English words of 4 letters or more can you make from the letters of a given word (each letter can only be used once)

On the back of a block calendar I found the following riddle:
How many common English words of 4 letters or more can you make from the letters
of the word 'textbook' (each letter can only be used once).
My first solution that I came up with was:
from itertools import permutations
with open('/usr/share/dict/words') as f:
words = f.readlines()
words = map(lambda x: x.strip(), words)
given_word = 'textbook'
found_words = []
ps = (permutations(given_word, i) for i in range(4, len(given_word)+1))
for p in ps:
for word in map(''.join, p):
if word in words and word != given_word:
found_words.append(word)
print set(found_words)
This gives the result set(['tote', 'oboe', 'text', 'boot', 'took', 'toot', 'book', 'toke', 'betook']) but took more than 7 minutes on my machine.
My next iteration was:
with open('/usr/share/dict/words') as f:
words = f.readlines()
words = map(lambda x: x.strip(), words)
given_word = 'textbook'
print [word for word in words if len(word) >= 4 and sorted(filter(lambda letter: letter in word, given_word)) == sorted(word) and word != given_word]
Which return an answer almost immediately but gave as answer: ['book', 'oboe', 'text', 'toot']
What is the fastest, correct and most pythonic solution to this problem?
(edit: added my earlier permutations solution and its different output).
I thought I'd share this slightly interesting trick although it takes a good bit more code than the rest and isn't really "pythonic". This will take a good bit more code than the other solutions but should be rather quick if I look at the timing the others need.
We're doing a bit preprocessing to speed up the computations. The basic approach is the following: We assign every letter in the alphabet a prime number. E.g. A = 2, B = 3, and so on. We then compute a hash for every word in the alphabet which is simply the product of the prime representations of every character in the word. We then store every word in a dictionary indexed by the hash.
Now if we want to find out which words are equivalent to say textbook we only have to compute the same hash for the word and look it up in our dictionary. Usually (say in C++) we'd have to worry about overflows, but in python it's even simpler than that: Every word in the list with the same index will contain exactly the same characters.
Here's the code with the slightly optimization that in our case we only have to worry about characters also appearing in the given word, which means we can get by with a much smaller prime table than otherwise (the obvious optimization would be to only assign characters that appear in the word a value at all - it was fast enough anyhow so I didn't bother and this way we could pre process only once and do it for several words). The prime algorithm is useful often enough so you should have one yourself anyhow ;)
from collections import defaultdict
from itertools import permutations
PRIMES = list(gen_primes(256)) # some arbitrary prime generator
def get_dict(path):
res = defaultdict(list)
with open(path, "r") as file:
for line in file.readlines():
word = line.strip().upper()
hash = compute_hash(word)
res[hash].append(word)
return res
def compute_hash(word):
hash = 1
for char in word:
try:
hash *= PRIMES[ord(char) - ord(' ')]
except IndexError:
# contains some character out of range - always 0 for our purposes
return 0
return hash
def get_result(path, given_word):
words = get_dict(path)
given_word = given_word.upper()
result = set()
powerset = lambda x: powerset(x[1:]) + [x[:1] + y for y in powerset(x[1:])] if x else [x]
for word in (word for word in powerset(given_word) if len(word) >= 4):
hash = compute_hash(word)
for equiv in words[hash]:
result.add(equiv)
return result
if __name__ == '__main__':
path = "dict.txt"
given_word = "textbook"
result = get_result(path, given_word)
print(result)
Runs on my ubuntu word list (98k words) rather quickly, but not what I'd call pythonic since it's basically a port of a c++ algorithm. Useful if you want to compare more than one word that way..
How about this?
from itertools import permutations, chain
with open('/usr/share/dict/words') as fp:
words = set(fp.read().split())
given_word = 'textbook'
perms = (permutations(given_word, i) for i in range(4, len(given_word)+1))
pwords = (''.join(p) for p in chain(*perms))
matches = words.intersection(pwords)
print matches
which gives
>>> print matches
set(['textbook', 'keto', 'obex', 'tote', 'oboe', 'text', 'boot', 'toto', 'took', 'koto', 'bott', 'tobe', 'boke', 'toot', 'book', 'bote', 'otto', 'toke', 'toko', 'oket'])
There is a generator itertools.permutations with which you can gather all permutations of a sequence with a specified length. That makes it easier:
from itertools import permutations
GIVEN_WORD = 'textbook'
with open('/usr/share/dict/words', 'r') as f:
words = [s.strip() for s in f.readlines()]
print len(filter(lambda x: ''.join(x) in words, permutations(GIVEN_WORD, 4)))
Edit #1: Oh! It says "4 or more" ;) Forget what I said!
Edit #2: This is the second version I came up with:
LETTERS = set('textbook')
with open('/usr/share/dict/words') as f:
WORDS = filter(lambda x: len(x) >= 4, [l.strip() for l in f])
matching = filter(lambda x: set(x).issubset(LETTERS) and all([x.count(c) == 1 for c in x]), WORDS)
print len(matching)
Create the whole power set, then check whether the dictionary word is in the set (order of the letters doesn't matter):
powerset = lambda x: powerset(x[1:]) + [x[:1] + y for y in powerset(x[1:])] if x else [x]
pw = map(lambda x: sorted(x), powerset(given_word))
filter(lambda x: sorted(x) in pw, words)
The following just checks each word in the dictionary to see if it is of the appropriate length, and then if it is a permutation of 'textbook'. I borrowed the permutation check from
Checking if two strings are permutations of each other in Python
but changed it slightly.
given_word = 'textbook'
with open('/usr/share/dict/words', 'r') as f:
words = [s.strip() for s in f.readlines()]
matches = []
for word in words:
if word != given_word and 4 <= len(word) <= len(given_word):
if all(word.count(char) <= given_word.count(char) for char in word):
matches.append(word)
print sorted(matches)
This finishes almost immediately and gives the correct result.
Permutations get very big for longer words. Try counterrevolutionary for example.
I would filter the dict for words from 4 to len(word) (8 for textbook).
Then I would filter with regular expression "oboe".matches ("[textbook]+").
The remaining words, I would sort, and compare them with a sorted version of your word, ("beoo", "bekoottx") with jumping to the next index of a matching character, to find mismatching numbers of characters:
("beoo", "bekoottx")
("eoo", "ekoottx")
("oo", "koottx")
("oo", "oottx")
("o", "ottx")
("", "ttx") => matched
("bbo", "bekoottx")
("bo", "ekoottx") => mismatch
Since I don't talk python, I leave the implementation as an exercise to the audience.

Categories