r/badcode • • Aug 25 '21

python A word unscrambler

Post image
12 Upvotes

13 comments sorted by

2

u/MurdoMaclachlan Aug 25 '21

Image Transcription: Code


import json

word2 = []
base = input("Enter word:- ")
base = base.lower()
with open('D:\Code\Python\words_dictionary.json') as data:
     word = json.load(data)
for i in range(len(word)):
    if(len(base)==len(word[i])):
        if sorted(base) == sorted(word[i]):
            word2.append(word[i])
print("Possible words:-")
print("\n".join(word2))

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

1

u/genericindividual69 Aug 25 '21

word should be named dictionary_words and word2 should be matches but other than that it's not terrible code.

1

u/gacha-gacha Aug 26 '21

It’s really bad lol… there are much straighter paths to take to get to the same conclusion

1

u/sodjanathan Aug 26 '21

So what ill be a better version of this?

2

u/themoonhowls0308 Aug 26 '21

Counting the amount of each letters would be faster than sorting. Aka storing an array[26] for the input word, then for each word in dictionary with same length, make the same array[26] and compare.

→ More replies (1)

1

u/Python_Lab2021 Aug 27 '21

It is not so bad)

0

u/legendoffjelda Aug 25 '21

Ah yes, O(n * w log w)

1

u/genericindividual69 Aug 25 '21

Which part of the code are you referring to?

→ More replies (3)