मैं एक प्रोग्राम बनाने की कोशिश कर रहा हूं जो टेक्स्ट फ़ाइल में प्रत्येक शब्द के लिए सभी एनाग्राम (सूची में) ढूंढ सकता है (जिसमें लगभग ~ 370k शब्द '\ n' से अलग होते हैं)।
मैंने पहले ही पायथन में कोड लिखा है। और मुझे दौड़ने में लगभग एक घंटा लगा। और बस सोच रहा था कि ऐसा करने का एक और अधिक कुशल तरीका है या नहीं।
मेरा कोड
from tqdm.auto import tqdm
ls = open("words.txt","r").readlines()
ls = [i[:-1] for i in ls]
ls = [[i,''.join(sorted(i))] for i in ls]
ln = set([len(i[1]) for i in tqdm(ls)])
df = {}
for l in tqdm(ln):
df[l] = [i for i in ls if len(i[0]) == l]
full = {}
for m in tqdm(ls):
if full.get(m[0]) == None:
temp = []
for i in df[len(m[0])]:
if i[1] == m[1] and i[0] != m[0]:
temp.append(i[0])
for i in temp:
full[i] = temp
यदि अन्य भाषाओं में इसे लिखने के अधिक कुशल तरीके हैं (रस्ट, सी, सी ++, जावा ...) यह वास्तव में सहायक होगा यदि आप इसे :)
भी पोस्ट कर सकते हैं
1 उत्तर
खोज कुंजी के रूप में वर्ण द्वारा वर्णानुक्रम में क्रमबद्ध शब्द का उपयोग करने की दिशा है। और हो सकता है कि आप अपने कोड में इस लाइन के साथ पहले से ही ऐसा कर रहे हों (मैं शायद ही कभी अजगर का उपयोग करता हूं):
[[i,''.join(sorted(i))] for i in ls]
वैसे भी यह मेरी सी ++ आपकी समस्या पर है। यहां लाइव डेमो : https://onlinegdb.com/_gauHBd_3
#include <algorithm> // for sorting
#include <string>
#include <unordered_map> // for storing words/anagrams
#include <iostream>
#include <fstream>
#include <set>
// create a class that will hold all words
class dictionary_t
{
public:
// load a text file with one word per line
void load(const std::string& filename)
{
std::ifstream file{ filename };
std::string word;
while (file >> word)
{
add_anagram(word);
}
}
auto& find_anagrams(const std::string& word)
{
const auto key = get_key(word);
// intentionally allow an empty entry to be made if word has no anagrams yet
// for readability easier error handling (not for space/time efficiency)
auto& anagrams = m_anagrams[key];
return anagrams;
}
// show all anagrams for a word
void show_anagrams(const std::string& word)
{
std::cout << "anagrams for word '" << word << "' are : ";
auto anagrams = find_anagrams(word);
for (const auto& anagram : anagrams)
{
if (anagram != word)
{
std::cout << anagram << " ";
}
}
std::cout << "\n";
}
private:
// this function is key to the whole idea
// two words are anagrams if they sort their letters
// to the same order. e.g. beast and betas both sort (alphabetically) to abest
std::string get_key(const std::string& word)
{
std::string key{ word };
// all anagrams sort to the same order of characters.
std::sort(key.begin(), key.end());
return key;
}
void add_anagram(const std::string& word)
{
// find the vector of anagrams for this word
auto& anagrams = find_anagrams(word);
// then add word to it (I use a set so all words will be unique even
// if input file contains duplicates)
anagrams.insert(word);
}
std::unordered_map<std::string, std::set<std::string>> m_anagrams;
};
int main()
{
dictionary_t dictionary;
dictionary.load("words.txt");
dictionary.show_anagrams("beast");
dictionary.show_anagrams("tacos");
return 0;
}
संबंधित सवाल
नए सवाल
python
पायथन एक बहु-प्रतिमान है, गतिशील रूप से टाइप किया हुआ, बहुउद्देशीय प्रोग्रामिंग भाषा है। यह एक साफ और एक समान वाक्यविन्यास सीखने, समझने और उपयोग करने के लिए त्वरित होने के लिए डिज़ाइन किया गया है। कृपया ध्यान दें कि अजगर 2 आधिकारिक तौर पर 01-01-2020 के समर्थन से बाहर है। फिर भी, संस्करण-विशिष्ट पायथन सवालों के लिए, [अजगर -२.०] या [अजगर -३.x] टैग जोड़ें। पायथन वेरिएंट (जैसे, ज्योथन, PyPy) या लाइब्रेरी (उदा।, पांडस और न्यूमपी) का उपयोग करते समय, कृपया इसे टैग में शामिल करें।
tops
औरpots
औरstop
सभीopst
बन गए और उन्हें 4-अक्षर वाले शब्दों के शब्दकोश से खोजा जा सकता है।