方案中的字谜

发布于 2024-10-02 20:04:41 字数 121 浏览 3 评论 0原文

有没有人尝试过在方案中生成字谜???

我有一个列表 (ABCDEF),需要创建长度为 4 的字谜词。如“AAAA”、“ABCD”、“BCBC”等。 我完全困惑了。 :(

有人可以提醒我如何去做吗?

Has anyone tried generating anagrams in scheme???

I have a list ( A B C D E F) and need to create anagrams of length 4. Like 'AAAA','ABCD','BCBC' etc.
I am totally confused. :(

Could someone please give me an heads-up on how I could go about it??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

俯瞰星空 2024-10-09 20:04:41

嗯...最近我解决了非常相似的任务 - 从 (0 1) 创建长度为 N 的字谜。这是我的解决方案。可能对您有帮助:

;; 
;; (define (generate n)) -> create list of string where
;;    * length of each string is n
;;    * each symbol of string is '0' or '1'
;;    * the list has all possible combinations of symbols '0' and '1'

(define (generate n)
(generate-engine (list "0" "1") n))

(define (generate-engine lst n)
 (cond
  [(= n 1) lst]
  [else (generate-engine (append (add-to-list lst "0") (add-to-list lst "1")) (- n 1))])    
)

(define (add-to-list lst symbol)
 (cond
  [(empty? lst) empty]
  [else (cons (add-to-element (first lst) symbol) (add-to-list (rest lst) symbol))]))

(define (add-to-element element symbol)
 (string-append element symbol)
)

;; example 

(generate 3)

结果:

(list "000" "100" "010" "110" "001" "101" "011" "111")

Hmm... Recently I solved very similar task - create anagrams of length N from (0 1). Here is my solution. May be it helps you:

;; 
;; (define (generate n)) -> create list of string where
;;    * length of each string is n
;;    * each symbol of string is '0' or '1'
;;    * the list has all possible combinations of symbols '0' and '1'

(define (generate n)
(generate-engine (list "0" "1") n))

(define (generate-engine lst n)
 (cond
  [(= n 1) lst]
  [else (generate-engine (append (add-to-list lst "0") (add-to-list lst "1")) (- n 1))])    
)

(define (add-to-list lst symbol)
 (cond
  [(empty? lst) empty]
  [else (cons (add-to-element (first lst) symbol) (add-to-list (rest lst) symbol))]))

(define (add-to-element element symbol)
 (string-append element symbol)
)

;; example 

(generate 3)

Result:

(list "000" "100" "010" "110" "001" "101" "011" "111")
欢烬 2024-10-09 20:04:41

对于每个 i = 0..x(其中 x 是字谜词的长度),生成一个随机数 r,使得 0 0 r ;= r <= n 其中 n 是字母列表中的元素数量。现在使用 r 作为字母列表的索引并将其连接到结果。

For each i = 0..x where x is the length of the anagram, generate a random number r such that 0 <= r <= n where n is the number of elements in the list of letters. Now use r as an index into the list of letters and concatenate it to the result.

日暮斜阳 2024-10-09 20:04:41

长度为 1 的基本情况

字谜词只是列表中的项目。返回列表。

递归情况

长度为 n 的字谜词是列表中附加到所有长度为 (- n 1) 的字谜词的每个项目。

由此,您应该能够编写一些基本案例和递归案例的示例。您可能需要一个辅助函数。

Base case

Anagrams of length 1 are just the items in the list. Return the list.

Recursive case

Anagrams of length n are each of the items in the list appended to all of the anagrams of length (- n 1).

From that you ought to be able to cook up some example of the base case and recursive case. You'll probably need a helper function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文