首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >卢卡斯和斐波纳契成对

卢卡斯和斐波纳契成对
EN

Code Golf用户
提问于 2018-08-24 07:04:46
回答 2查看 938关注 0票数 6

无差别编程的影响下,我提出了一个类似挑战的想法。

这里,如果每个字符的外观数是Lucas数(2,1,3,4,7,.),那么字符串就是Lucas字符串,如果每个字符的外观数是Fibonacci数(1,2,3,5,8,.),则字符串就是Fibonacci字符串。

以下是一些具体的例子:

代码语言:javascript
复制
"Programming Puzzles"
Appearance Count:   P a e g i l m n o r s u z
                  1 2 1 1 2 1 1 2 1 1 2 1 1 2

所有计数都是Lucas数和Fibonacci数,因此这既是Lucas字符串,也是Fibonacci字符串。

代码语言:javascript
复制
"Sandbox for Proposed Challenges"
Appearance Count:   C P S a b d e f g h l n o p r s x
                  3 1 1 1 2 1 2 3 1 1 1 2 2 4 1 2 2 1

所有的计数都是卢卡斯的数字,所以这是卢卡斯的字符串。但是,o的计数不是Fibonacci数,所以这不是Fibonacci字符串。

代码语言:javascript
复制
"Here are some concrete examples: "
Appearance Count:   : H a c e l m n o p r s t x
                  5 1 1 2 2 8 1 2 1 2 1 3 2 1 1

所有计数都是Fibonacci,所以这是Fibonacci字符串。然而,和e的计数不是Lucas数,所以这不是Lucas字符串。

代码语言:javascript
复制
"Non-discriminating Programming"
Appearance Count:   - N P a c d g i m n o r s t
                  1 1 1 1 2 1 1 3 5 3 4 2 3 1 1

因为i的计数不是Lucas数,n的计数也不是Fibonacci数,所以这既不是Lucas字符串,也不是Fibonacci字符串。

挑战

编写两个程序,一个Lucas程序和一个Fibonacci程序,它们都接受字符串作为输入,即:

  • Lucas程序检查输入是否为Fibonacci;以及
  • Fibonacci程序检查输入是否为Lucas,

其中,"Lucas“和"Fibonacci”的定义如上。

规则

  • 您的程序可以选择从STDIN或作为函数/程序参数接收输入。
  • 您的程序必须输出或返回您选择的2(2)个不同值中的一个,一个用于真实值,另一个用于虚值,并且该选择必须在各个程序之间保持一致,即不能让一个程序返回0/1,而另一个返回true/false
  • 如果输入彼此的源,则程序必须输出或返回一个真实值,也就是说,如果输入Fibonacci程序的源,卢卡斯程序必须返回一个真实值,反之亦然。
  • 这是允许程序都是卢卡斯和斐波纳契。
  • 没有使用的字符显然不被计算,以防您怀疑,因为0不在Lucas序列中。
  • 每个程序中的不同字符数不限于Lucas或Fibonacci数。
  • 只允许完整的程序或函数。片段不被接受。
  • 标准漏洞显然是被禁止的。

评分

分数计算如下:

  • 对于每个程序,将the number of distinct characters used乘以the maximum number of appearances of the characters in the source
  • 将上一步中计算的两个值相加。这是你的分数。

示例:

代码语言:javascript
复制
Lucas code :    20 distinct characters, MAX(appearance of each character)=7
Fibonacci code: 30 distinct characters, MAX(appearance of each character)=5
Score: 20*7 + 30*5 = 140 + 150 = 290

测试用例

代码语言:javascript
复制
Input                                   | Output of ...    
                                        | Lucas Program      | Fibonacci Program
----------------------------------------+---------------------------------------
<Your Lucas program>                    | True if all appe-  | True
(Lucas string by rules)                 | arance counts are  |
                                        | exactly 1, 2 or 3; |
                                        | False otherwise    |
----------------------------------------+--------------------+------------------
<Your Fibonacci program>                | True               | True if all appe-
(Fibonacci string by rules)             |                    | arance counts are
                                        |                    | exactly 1, 2 or 3;
                                        |                    | False otherwise
----------------------------------------+--------------------+------------------
"Programming Puzzles"                   | True               | True
(Both Lucas and Fibonacci)              |                    |     
----------------------------------------+--------------------+------------------
"Sandbox for Proposed Challenges"       | False              | True
(Lucas but not Fibonacci)               |                    |     
----------------------------------------+--------------------+------------------
"Here are some concrete examples: "     | True               | False
(Fibonacci but not Lucas)               |                    |
----------------------------------------+--------------------+------------------
"Non-discriminating Programming"        | False              | False
(Neither Lucas nor Fibonacci)           |                    |
----------------------------------------+--------------------+------------------
"Hello world!"                          | True               | True
(Both Lucas and Fibonacci)              |                    |     
----------------------------------------+--------------------+------------------
"18446744073709551616"                  | False              | True
(Lucas but not Fibonacci)               |                    |     
----------------------------------------+--------------------+------------------
"Lucas and Fibonacci are IN pair"       | True               | False
(Fibonacci but not Lucas)               |                    |
----------------------------------------+--------------------+------------------
"Lucas and Fibonacci are in pair"       | False              | False
(Neither Lucas nor Fibonacci)           |                    |

获奖标准

在每种语言中得分最低的投稿获胜。

EN

回答 2

Code Golf用户

发布于 2018-08-24 08:28:37

05AB1E,得分: 10 +8= 18

这可能是对Kevin帖子的评论,但我已经独立开发了它,技术也略有不同。因此,我是自己张贴它。

Fibonacci程序,检查Lucas (10字节)

代码语言:javascript
复制
€¢Dā0šÅgÃQ

在网上试试!

代码语言:javascript
复制
€¢Dā0šÅgÃQ -> Full program
€¢         -> Count the occurrences of each character in the string. Call this list C.
  Dā       -> Duplicate and yield the range [1 ... len string].
    0š     -> Prepend 0. [0 ... len string].
      Åg   -> Nth Lucas number (for each).
        Ã  -> Intersection with C.
         Q -> Check equality with C.

Lucas程序,检查Fibonacci (8字节)

代码语言:javascript
复制
€¢DāÅfÃQ

在网上试试!

代码语言:javascript
复制
€¢DāÅfÃQ -> Full program
€¢       -> Count the occurrences of each character in the string. Call this list C.
  Dā     -> Duplicate and yield the range [1 ... len string].
    Åf   -> Nth Fibonacci number (for each). We don't need the 0th this time!
      Ã  -> Intersection with C.
       Q -> Check equality with C.
票数 4
EN

Code Golf用户

发布于 2018-08-24 13:57:58

Pyt,11*1+12*1=23字节

两者都返回0为真实,1为虚假。

Fibonacci试验:

代码语言:javascript
复制
ĐỤ⇹ɔ2ᴇřḞ\ٱ

在网上试试!

卢卡斯试验:

代码语言:javascript
复制
ĐỤ⇹ɔ02ᴇŘĿ\ٱ

在网上试试!

解释:

代码语言:javascript
复制
                    Implicit input
Đ                   Duplicate the input
 Ụ                  Get list of unique characters
  ⇹                 Swap the top two elements on the stack
   ɔ                Count the number of occurrences of each character in the input
    2ᴇř             Push [1,...,100] (should be more than enough)
       Ḟ            Get the first 100 Fibonacci numbers
        \           Get all character counts that aren't Fibonacci numbers
         Ł          Get the number of such counts
          ±         Output the sign of the number of such counts
                    Implicit output
代码语言:javascript
复制
                    Implicit input
ĐỤ⇹ɔ                get count of occurrences of each character
    02ᴇŘ            Push [0,1,...,100] (should be more than enough)
        Ŀ           Get the first 101 Lucas numbers
         \          Get all character counts that aren't Lucas numbers
          Ł         Get the number of such counts
           ±        Output the sign of the number of such counts
                    Implicit output
票数 0
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/171122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档