Python中的伪代码

0 投票
1 回答
1668 浏览
提问于 2025-04-18 07:42

我对Python还比较陌生。

我现在正在做的一个练习是为以下问题写出Python的伪代码:

write an algorithm that given a dictionary (list) of words, finds up to 10 anagrams of a given word.

我对如何解决这个问题感到很困惑。

目前我写的(其实连伪代码都算不上)是:

# Go through the words in the list
# put each letter in some sort of array
# Find words with the letters from this array 

我觉得这太笼统了,我在网上查找了一些可以用的具体函数,但没有找到。

有没有人能帮我推荐一些具体的函数,这样我可以写出稍微具体一点的伪代码?

1 个回答

1

这里有一些帮助,但我不会为你写代码

#define a key method
        #determine the key using each set of letters, such as the letters of a word in 
        #alphabetical order
        #keyof("word") returns "dorw"
        #keyof("dad") returns "add"
        #keyof("add") returns "add"

#ingest the word set method
    #put the word set into a dictionary which maps
    #key->list of up to 10 angrams

#get angrams method
    #accept a word as a parameter
    #convert the word to its key
    #look up the key in the dictionary
    #return the up to 10 angrams

#test case: add "dad" and "add" to the word set. 
#    getting angrams for "dad" should return "dad" and "add"
#test case: add "palm" and "lamp" to the word set. 
#    getting angrams for "palm" should return "palm" and "lamp"

#consider storing 11 angrams in the list 
#a01, a02, a03, a04, a05, a06, a07, a08, a09, a10, a11. 
#Then if a01 is provided, you can return a02-a11, which is 10 angrams

撰写回答