斐波那契日快乐!


20

背景

今天(或昨天)是(或曾经是)11/23或斐波那契日!有什么比做斐波那契蛋糕更好的庆祝方式了?


例子

3

    ii
i_i_ii_i_i 

8

              ii              
              ii              
              ii              
              ii              
              ii              
              ii              
              ii              
              ii              
            i ii i            
            i ii i            
            i ii i            
            i ii i            
            i ii i            
          i i ii i i          
          i i ii i i          
          i i ii i i          
        i i i ii i i i        
        i i i ii i i i        
      i i i i ii i i i i      
    i i i i i ii i i i i i    
i_i_i_i_i_i_i_ii_i_i_i_i_i_i_i

挑战

你不是真的在做蛋糕,只是在做蜡烛,因为我不能画蛋糕

要制作蛋糕,您必须首先获得升序排列的第一个n 斐波那契数字。蜡烛i的高度由当前斐波那契数的值确定。蜡烛之间用下划线(_)分隔。

蛋糕应该对称。因此,应该将蜡烛翻转并连接起来。

示例构造

Input: 6
First 6 Fibonacci Numbers: 1, 1, 2, 3, 5, 8
Candle heights:

          i
          i
          i
        i i
        i i
      i i i
    i i i i
i i i i i i
-----------
1 1 2 3 5 8

Output would be:
          ii
          ii
          ii
        i ii i
        i ii i
      i i ii i i
    i i i ii i i i
i_i_i_i_i_ii_i_i_i_i_i 

参考斐波那契数

作为参考,以下是前15个斐波那契数字。在这项挑战中,您将从开始1

1,1,2,3,5,8,13,21,34,55,89,144,233,377,610

9
我很遗憾地通知美国的西海岸,对于世界上大多数国家,11/23结束或接近尾声:(这只是从原来代码高尔夫最快的代码

1
@sysreq没关系,他们只需要立即前往西海岸,然后他们就可以在过去5个小时内享受斐波那契一日游(夏威夷7天):p
Downgoat

6
这个挑战将在43年内成为传奇(11/23/58)。
Arcturus

9
在43年中,mm / dd / yy日期格式将成为历史记录。所有聪明的生物都会切换到yyyy / mm / dd ... :-P

1
@sysreq鼓励以冗长的语言发布高尔夫球答案。当然,您并没有与Pyth竞争,但可以与相同语言或相当详细程度的语言与其他答案竞争。
马丁·恩德

Answers:


2

Pyth,31个字节

jaPJ_.ts_BuaaGks>4GtQ]\idXeJ" _

在线尝试:演示

说明:

jaPJ_.ts_BuaaGks>4GtQ]\idXeJ" _   implicit: Q = input number
          u        tQ]\i          reduce the list [1, ..., Q-2], start with G=["i"]
            aGk                      append the empty string to G
           a   s>4G                  append the sum of the last 4 strings in G to G
                                  this gives ["i", "", "i", "", "ii", "", "iii",..]
       s_B                        extend the list with the reversed list
     .t                  d        pad to a rectangle with spaces and transposes
   J_                             reverse the order and assign to J
  PJ                              remove the last string of J
 a                                and append
                         XeJ" _   the last string of J with spaces replaced by "_"
j                                 print each string on a separate line

6

J,58个字节

3 :''' i_''{~|.|:(,|.)(#&1,2#~0&=)"*}.,0,.(%-.-*:)t.1+i.y'

用途(%-.-*:)t.为斐波那契数代。稍后可能会进行解释。

用法:

   f=.3 :''' i_''{~|.|:(,|.)(#&1,2#~0&=)"*}.,0,.(%-.-*:)t.1+i.y'
   f 5
        ii        
        ii        
      i ii i      
    i i ii i i    
i_i_i_i_ii_i_i_i_i

在这里在线尝试。


5

CJam,41 39字节

"i""_"1$ri({1$1$4$+}*]),f{Se[}W<_W%+zN*

这会打印大量的前导空白。在CJam解释器中在线尝试。

怎么运行的

"i""_"1$ e# Push "i", "_", and a copy of "i".
ri(      e# Read an integer from STDIN and subtract 1.
{        e# Do that many times:
  1$     e#   Copy the last underscore.
  1$4$+  e#   Copy the last strings of i's and concatenate them.
}*       e#
]),      e# Wrap the results in an array, pop the last string, and get its length.
f{       e# For each remaining string, push the string and the length; then:
  Se[    e#   Pad the string to that length by prepending spaces.
}
W<       e# Remove the last string (underscore).
         e# We have now generated the columns of the left half of the output.
_W%+     e# Append a reversed copy (columns of right half).
z        e# Transpose rows with columns.
N*       e# Separate the rows by linefeeds.

3

TeaScript, 93 84 76 +1 = 77字节

+1字节表示“输入是数字吗?” 复选框

r×ß(p.R((w=F(x©-F(i¬©+"i"R(F(i¬±)t¡ß(j=i<w-1?" ":"_",(A=l¿i>0?j+l:l)µ)+Av©j§

非高尔夫版本:

r(x)m(#(p.R((w=F(x))-F(i+1))+"i"R(F(i+1))))t()m(#(j=i<w-1?" ":"_",(A=ls``.m(#i>0?j+l:l)j``)+Av))j`
`

感谢@Vɪʜᴀɴ的提示。


您应该可以使用该r函数代替A(x)f(0),例如r(x)m(#
Downgoat 2015年

@Vɪʜᴀɴ谢谢。该怎么Array.dupe()办,似乎没有用?
intrepidcoder

Array.dupe可能是一个不好的名字,它从数组中删除重复项。
Downgoat

@Vɪʜᴀɴ您应该将每个方法的摘要与输入/输出参数一起放入属性名称部分。另外,Array.repeat和String.reverse也将很好。
intrepidcoder

1
@sysreq我的意思是不打高尔夫球,对不起您造成的混乱。
intrepidcoder

3

Python 2,117字节

a=b='i'
l=a,
exec"l+='_',b,;a,b=b,b+a;"*~-input()
for r in map(None,*l+l[::-1])[::-1]:print''.join(x or' 'for x in r)

这个想法很简单:在从下到上,从左到右的列中生成图片,镜像的右半边是左半边。这些列是通过在的字符串上i散布有_的用于底部行。

要从底部开始打印带有列的图片,我们需要旋转图片,这意味着转置和反转。不幸的是,Python没有简单的方法来转置不等长行的数组。内置zip截断到最短的行。这使用了map(None,_)技巧,但是之后必须将所有的都转换None为空格。


2

Haskell中,182个 176字节

import Data.List
f=0:scanl(+)1f
b n c s|length s<n=b n c(c:s)|0<1=s
m s=s++reverse s
c n=mapM_ putStrLn$transpose$m$map(b(f!!n)' ')$intersperse"_"$map(\x->[1..f!!x]>>"i")[1..n]

致电c

f/programming/232861/fibonacci-code-golf无耻地被盗)


您可以替换flip replicate 'i'.(f!!)\x->[1..f!!x]>>"i"
nimi 2015年

万分谢意!这是我第一次真正了解单子高尔夫球技巧,即使我自己还没有提出这些建议。没有任何线索f仍然如何工作……
Leif Willerts 2015年

1

Matlab,172岁字节

不幸的是,Matlab尚未在Fibonacci函数中构建,字符串操作有点麻烦。

function t(n);f=@(n)getfield([0 1;1 1]^n,{3});m=char(flipud(bsxfun(@(a,b)(a<=f(b/2)&mod(b,2)==0)*'i',(1:f(n))',2:2*n)));m(end,2:2:end)='_';[m fliplr(m)]

带换行符:

function t(n);
f=@(n)getfield([0 1;1 1]^n,{3});
m=char(flipud(bsxfun(@(a,b)(a<=f(b/2)&mod(b,2)==0)*'i',(1:f(n))',2:2*n)));
m(end,2:2:end)='_';
[m fliplr(m)]

1

红宝石,151个 146 142 137 132字节

->n{s=1,1;3.upto(n){s<<s[-1]+s[-2]};s.map!{|i|[' ']*(s[-1]-i)+[?i]*i}.transpose.map!{|a|v=a*'_ '[a.count(?i)<=>n];puts v+v.reverse}}

137字节

->n{s=1,1;3.upto(n){s<<s[-1]+s[-2]};o=s.map{|i|[' ']*(s[-1]-i)+[?i]*i}.transpose.map{|a|v=a*' ';v+v.reverse};o[-1]=o[-1].tr' ',?_;puts o}

142字节

->n{s=1,1;(3..n).map{s<<s[-1]+s[-2]};puts s.map{|i|[' ']*(s[-1]-i)+[?i]*i}.transpose.map{|a|v=a*' ';v+v.reverse}.tap{|c|c[-1]=c[-1].tr' ',?_}}

146字节

->n{s=1,1;(3..n).map{s<<s[-1]+s[-2]};puts s.map{|i|[' ']*(s[-1]-i)+[?i]*i}.transpose.map{|a|v=a.join' ';v+v.reverse}.tap{|c|c[-1]=c[-1].tr' ',?_}}

151字节

->n{s=1,1;(3..n).map{s<<s[-1]+s[-2]};puts s.map{|i|[' ']*(s[-1]-i)+['i']*i}.transpose.map{|a|v=a.join ' ';v+v.reverse}.tap{|c|c[-1]=c[-1].tr ' ', '_'}}

取消高尔夫:

-> n {
  s = 1,1
  3.upto(n) {
    s << s[-1] + s[-2]
  }
  s.map! { |i|
    [' '] * (s[-1]-i) + [?i] * i
  }.
  transpose.
  map! { |a|
    v = a * '_ '[a.count(?i)<=>n]
    puts v + v.reverse
  }
}

用法:

->n{s=1,1;3.upto(n){s<<s[-1]+s[-2]};s.map!{|i|[' ']*(s[-1]-i)+[?i]*i}.transpose.map!{|a|v=a*'_ '[a.count(?i)<=>n];puts v+v.reverse}}[8]

输出:

              ii              
              ii              
              ii              
              ii              
              ii              
              ii              
              ii              
              ii              
            i ii i            
            i ii i            
            i ii i            
            i ii i            
            i ii i            
          i i ii i i          
          i i ii i i          
          i i ii i i          
        i i i ii i i i        
        i i i ii i i i        
      i i i i ii i i i i      
    i i i i i ii i i i i i    
i_i_i_i_i_i_i_ii_i_i_i_i_i_i_i

1

Python 2,213

DSM节省了12个字节。

def f(r):
 a=b=1
 while r:yield a;a,b=b,a+b;r-=1
n=list(f(input()))
n.remove(1)
h=max(n)-1
r=1
while h:
 l='  '*(len(n)+1)+('i '*r)[:-1];print(l+l[::-1]);h-=1
 if h in n:r+=1;n.pop()
l='i_'*r+'i_i'
print l+l[::-1]

非高尔夫版本。

max_height = input()
def fib(r):
    a=b=1
    while r:
        yield a
        a,b = b, a + b
        r-=1

numbers = [x for x in fib(max_height) if x>1]
highest = max(numbers) -1
rows = 1
while highest:
    line =' '*((len(numbers)+1)*2) + ' '.join('i'*rows)
    print(line + line[::-1])
    highest -= 1
    if highest in numbers:
        rows += 1
        numbers.pop()

line = '_'.join('i'*(rows+2))
print(line + line[::-1])
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.