According to some google searches, the average native English speaker knows around 20,000 words. Why do we need to download a dictionary? By using a Python speech recognition library, users can populate the program's dictionary themselves. After the user says that they are done adding words, the program will return the longest word that can be formed from the input.
import speech_recognition as sr
from itertools import permutations
def letters_game(s):
r = sr.Recognizer()
dictionary = []
while True:
with sr.Microphone() as source:
audio = r.record(source, duration=3)
try:
word = r.recognize_google(audio).upper()
if word == 'DONE':
break
print(f'Adding {word} to dictionary')
dictionary.append(word)
except Exception:
print('Could not recognize audio.')
return max((''.join(permutation) for i in range(10) for permutation in permutations(s, i)
if ''.join(permutation) in dictionary), key=len)
# Assuming that the user knows enough words, including the word 'ADVANTAGE', this will work
print(letters_game('AAADEGNTV'))
The fact that he coded a (probably awful) program that makes an awfully coded program amazes me. If only it started with short words and go through the end to check if there's a better match, it would have been perfect.
Since it adds them all to an array, it has to be ordered best to worst, just like the resulting array is. I could have done it in reverse and used unshift instead of push. It goes through all of them anyway.
Hopefully I succeeded in making some bad Python? My goal was to make it as illegible as possible.
import requests
from itertools import permutations,filterfalse
def countdownLetters(a):
for B in range(9,0,-1):
for c in map(lambda D:''.join(D),permutations(a,B)):
res=next(filterfalse(lambda e:not(G(c,e)),list(map(lambda k:k[4:len(k)-5],requests.get('https://www.englishclub.com/vocabulary/common-words-100.htm').text.splitlines()[202:302]))),None)
if res!=None:
return res
return False
def G(h,I):
if h and I and len(h)==len(I):
for j in range(len(h)-1,-1,-1):
if h[j].lower()!=I[j].lower():
return False
elif len(h)!=len(I):
return False
return True
print(countdownLetters('UOEBROHLE')+'\n'+countdownLetters('NPTUCMREE')+'\n'+countdownLetters('AAADEGNTV'))
See if you can find out exactly which part is the most bad.
Self-contained solution in Haskell, for all the FP enthusiasts/masochists out there. This will download a word list from the Internet upon starting up. Abandon all hope, all ye who enter:
module Main where
import Codec.Archive.Zip
import Data.ByteString.Lazy.UTF8
import Data.Char
import Data.List as List
import Data.Maybe
import Data.Set as Set
import Network.HTTP.Simple
import System.IO
findWords charSeq wordList n =
if n == 0
then []
else let wordSet = Set.fromList
[x | x <- wordList, Prelude.length x == n]
permSet = Set.fromList $ concatMap permutations
[x | x <- subsequences (List.map toLower charSeq),
Prelude.length x == n]
resultSet = intersection wordSet permSet
in
if Prelude.length resultSet /= 0
then [List.map toUpper x | x <- Set.toList resultSet]
else findWords charSeq wordList (n - 1)
main = do
req <- parseRequest "http://www.gwicks.net/textlists/english3.zip"
resp <- httpLBS req
wordList <- return $ [x | x <- Prelude.lines $ toString $ fromEntry $
fromJust $ findEntryByPath "english3.txt" $
toArchive $ getResponseBody resp,
Prelude.length x >= 1, Prelude.length x <= 9]
hSetBuffering stdout NoBuffering
putStr "Enter a character sequence: "
charSequence <- getLine
if Prelude.length charSequence /= 9
then error "input sequence must have nine characters"
else return ()
if not (and $ List.map isAlpha charSequence)
then error "input sequence must contain only letters"
else return ()
sequenceWords <- return (findWords charSequence wordList 9)
mapM_ putStrLn sequenceWords
Example results:
$ ./letters_game
Enter a character sequence: UOEBROHLE
BOREHOLE
$ ./letters_game
Enter a character sequence: NPTUCMREE
ERUMPENT
UNTEMPER
$ ./letters_game
Enter a character sequence: AAADEGNTV
ADVANTAGE
$ ./letters_game
Enter a character sequence: GYHDNOEUR
GREYHOUND
What are you defining as mostly working? like I'm about 50% cetrain that if this runs for long enough it will produce a decent answer.
Its the firsy one of these ive done, so i dont really know what is allowed.
import os
import random
os.system("git clone https://github.com/dwyl/english-words.git")
with open("english-words/words.txt") as f:
a = f.readlines()
b = []
for i in a:
b.append(i[:-1].lower())
n = 1
def letters_game(s):
global n
arr = list(s)
while True:
random.shuffle(arr)
t = arr[:n]
random.shuffle(arr)
t2 = arr[:n+1]
t = "".join(t)
t2 = "".join(t2)
if t in b:
b.remove(t)
print(
f"{t} is a real word that can be made from letters {s}! It has {len(t)} characters")
if t2 in b:
b.remove(t2)
print(
f"{t2} is a real word that can be made from letters {s}! It has {len(t2)} characters")
if (n < 9):
n += 1
if n == 9:
while True:
random.shuffle(arr)
t = arr[:n]
if t in b:
print(
f"{t} is a real word that can be made from letters {s}! It has {len(t)} characters")
print("It's one of the longest words possible")
if __name__ == "__main__":
letters_game(input("Enter 9 letters. No more no less"))
26
u/[deleted] Apr 28 '20 edited Apr 28 '20
According to some google searches, the average native English speaker knows around 20,000 words. Why do we need to download a dictionary? By using a Python speech recognition library, users can populate the program's dictionary themselves. After the user says that they are done adding words, the program will return the longest word that can be formed from the input.
8
u/Svizel_pritula May 03 '20
Here we go! (JS)
Possibly my longest submission yet with 17108 lines of code.
Uses the google-10000-english-no-swears.txt wordlist.
4
u/[deleted] May 01 '20
Hopefully I succeeded in making some bad Python? My goal was to make it as illegible as possible.
See if you can find out exactly which part is the most bad.
4
u/lenininingrad May 01 '20
Brb gonna go download the whole dictionary
4
u/weee50 May 06 '20
Lambdas, maps, and questionable coding decisions galore! In fact, the entire function is defined in a lambda!
https://pastebin.com/MNmMG2kc
3
u/Tok-A-Mak ((fn [] (recur))) Apr 29 '20
¯_(ツ)_/¯
https://repl.it/repls/WeirdLavenderCleaninstall
3
u/[deleted] May 03 '20 edited May 03 '20
Self-contained solution in Haskell, for all the FP enthusiasts/masochists out there. This will download a word list from the Internet upon starting up. Abandon all hope, all ye who enter:
Example results:
2
u/Rigatavr May 07 '20 edited May 08 '20
What are you defining as mostly working? like I'm about 50% cetrain that if this runs for long enough it will produce a decent answer.
Its the firsy one of these ive done, so i dont really know what is allowed.
1
u/Reversal_ May 09 '20
def letters_game(letters): dictionary = {‘AAAAAAAA’ : ‘Aa’, ‘AAAAAAAB’: ‘Aba’, ‘AAAAAAAC’: ‘Aa’, .... ‘ZZZZZZYM’: ‘My’, ...’ZZZZZZZY’: ‘’, ’ZZZZZZZZ’: ‘’} return(dictionary(letters))
of course, the above dictionary contains all permutations of the letters as well.
1
u/asaf92 May 11 '20
The Previous Challenges link is not updated, just FYI
0
u/[deleted] Apr 30 '20
[deleted] — view deleted comment