简体   繁体   English

在使用递归的球拍中,如果列表“ L”的总和为n但L中的值没有重复,我如何返回#true

[英]In Racket using recursion how do I return #true if a list “L” sums to n but no values in L are duplicated

This question may be a little complex, I am unsure though, this question appears at least pretty complicated to me. 这个问题可能有点复杂,但是我不确定,这个问题对我来说似乎至少很复杂。 I am essentially trying to write a piece of code that consists of a (listof Num) and a Num. 我本质上是在尝试编写由(listof Num)和Num组成的一段代码。 My function is meant to output #true if L is an Egyptian Fraction that represents n. 如果L是表示n的埃及分数,则我的函数将输出#true。 An Egyptian Fraction is the sum of distinct fractions where the numerators are 1 just in case some don't know. 埃及分数是分子为1的不同分数的总和,以防万一某些人不知道。 I currently have a start to my code and have an idea to solve the question but don't know where to go next. 目前,我已经开始编写代码,并且有解决该问题的想法,但不知道下一步该怎么做。 I would highly prefer if the answer was kept in the same format as it is now. 我非常希望答案保持现在的格式。 I am practicing recursion and trying to improve with it. 我正在练习递归并尝试对其进行改进。

In my code I have a base case where if the list is empty it outputs #false. 在我的代码中,我有一个基本情况,如果列表为空,则输出#false。

I then wrote a case where if any values in L were repeated then the function outputs #false 然后,我写了一个情况,如果重复L中的任何值,则函数输出#false

I then wrote a case where if the sum of all the values of L equals the value of n the function would return #true. 然后,我写了一个情况,如果L的所有值的总和等于n的值,该函数将返回#true。

My last case simply outputs #false meaning that the sum of the values in L did not equal n. 我的最后一种情况只是输出#false,表示L中的值之和不等于n。

The thought process for my code appears to be correct but my code does not actually work. 我的代码的思考过程似乎是正确的,但是我的代码实际上不起作用。 This is my code 这是我的代码

(define (egyptian? L n)
  (cond
    [(empty? L) #false]
    [(equal? (first L) (first (rest L)) (egyptian? (rest L))) #false]
    [( = n (+ (first L) (egyptian? (rest L))))#true]
    [else #false]))

This is what the function should output 这是函数应该输出的

(check-expect (egyptian? (list 1/2 1/3 1/6) 1) #true)
(check-expect (egyptian? (list 1/2 1/4 1/5 1/20) 1) #true)
(check-expect (egyptian? (list 1/2 1/3 1/4 1/5 1/6) 1.5) #false)
(check-expect (egyptian? (list 1/2 1/2 1/2 1/2) 1) #false)

As you can see the first two cases are #true because the sum of the values in the lists are equal to "n". 如您所见,前两种情况为#true,因为列表中值的总和等于“ n”。 The third case is incorrect because the sum of values in the list do not equal "n". 第三种情况是不正确的,因为列表中的值之和不等于“ n”。 The fourth case is incorrect because values in the list are duplicated. 第四种情况是不正确的,因为列表中的值是重复的。 Hopefully I have provided enough information about the question I am struggling on. 希望我已经提供了有关我正在努力解决的问题的足够信息。

The best strategy for this (and many other problems) is to split the problem in smaller parts. 最好的策略(以及许多其他问题)是将问题分成较小的部分。 Let's start with just finding the sum of all the numbers in a list, this should be easy: 让我们从查找列表中所有数字的总和开始,这应该很容易:

(define (sum L)
  (if (empty? L)
      0
      (+ (first L)
         (sum (rest L)))))

Now, let's write a procedure that checks if the elements in a list are unique. 现在,让我们编写一个过程,以检查列表中的元素是否唯一。 This is not as simple as checking one item in the list with the next one, the repeated item could be anywhere in the list! 这并不像检查列表中的一项与下一项一样简单,重复项可以在列表中的任何位置!

(define (unique? L)
  (cond ((empty? L) #true)
        ((member (first L) (rest L)) #false)
        (else (unique? (rest L)))))

With these procedures in place, our main problem becomes trivial to solve: 有了这些程序,我们的主要问题就变得难以解决:

(define (egyptian? L n)
  (and (unique? L)
       (= (sum L) n)))

You see now, how powerful is the idea of function composition! 现在您将看到,功能组合的概念多么强大! In your attempted solution you were mixing the code for three different concerns in a single procedure, and that made things real hard to understand. 在您尝试的解决方案中,您在一个过程中混合了三个不同问题的代码,这使事情变得很难理解。

Another nice effect of splitting the problem in smaller parts is that you can switch implementations later, for more efficient solutions. 将问题分解为较小的部分的另一个不错的效果是,您可以稍后切换实现,以获得更有效的解决方案。 For example, this is how we'd write the sum and unique? 例如,这就是我们写sumunique? procedures in idiomatic Racket: 惯用球拍中的程序:

(define (sum L)
  (apply + L))

(define (unique? L)
  (= (length L) (set-count (list->set L))))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在球拍中,如何使用递归求和列表中的交替值 - In Racket how do i sum the alternating values in a list using recursion 如何使用递归删除 Racket 中列表中的第一个和最后一个元素 - How do I remove the first and last element in a list in Racket using recursion l10n / i18n:如何使用动态项目列表处理短语? - l10n/i18n: how to handle phrases with dynamic list of items? 在Racket中,如何使用struct而不是仅使用高阶函数或递归来查找列表的长度 - In Racket how do i find the length of a list using struct instead of just using only higher-order functions or recursion 给定一个列表和一个位掩码,如何返回True为真的索引值? - Given a list and a bitmask, how do I return the values at the indices that are True? 如何在我的代码中解决此错误:ValueError: 'l' is not in list, error - How do I solve this error within my code: ValueError: 'l' is not in list, error 给定一个标记为1到N的列表L,以及一个将“随机”元素从考虑中删除的过程,如何有效地跟踪min(L)? - Given a list L labeled 1 to N, and a process that “removes” a random element from consideration, how can one efficiently keep track of min(L)? 如何拆分字符串列表并使用递归返回元组? - How do you split a string list and return tuples using recursion? 在Racket中,如何返回包含两个不同字典中出现的所有键的列表 - In Racket how do I return a list that contains all the keys which occur in two different dictionaries 在球拍中,如何仅使用string-> list或list-> string函数替换字符串中的单词? - In racket how do I replace word in string using string->list or list->string function only?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM