普通句子


16

编写一个程序或函数,给定输入字符串和标准偏差σ,该程序或函数沿正态分布曲线以平均值0和标准偏差输出该字符串σ

正态分布曲线

y每个字符的坐标c为:

在此处输入图片说明

其中σ给定为输入,并且其中x在所述x轴的坐标c

  • 字符串中心的字符为x = 0。如果字符串的长度是偶数,则可以选择两个中间字符中的任意一个作为中心。
  • 字符按以下步骤分隔0.1(例如,位于中心左侧的一个字符具有x = -0.1,位于中间一个右侧的字符具有x = 0.1,等等)。

打印字符串

  • 线与字符一样,以分隔0.1
  • 每个字符打印在与该行y最接近自身的价值y值(如果该值恰恰是两条线的值之间,选择具有最大价值(随便怎么样round,通常回报1.00.5))。
  • 例如,如果y中心值(即最大值)0.78y坐标为,而第一个字符的坐标为0.2,则将有9行:中心字符在行上打印0,第一个字符在行上打印8

输入和输出

  • 您可以σ通过STDIN,函数参数或您所用语言中的任何类似形式将输入(字符串和)作为程序参数。
  • 该字符串将仅包含可打印ASCII字符。该字符串可以为空。
  • σ > 0
  • 您可以将输出打印到STDOUT文件中,或从函数中返回(只要它是一个字符串且不说每行的字符串列表)。
  • 尾随的新行是可以接受的。
  • 尾随空格是可以接受的,只要它们不使行的长度超过最后一行(因此,最后一行不包含尾随空格)。

测试用例

σ    String

0.5  Hello, World!

     , W     
   lo   or   
  l       l  
 e         d 
H           !



0.5  This is a perfectly normal sentence

                tly                
              ec    n              
             f       o             
            r         r            
           e           m           
          p             a          
        a                l         
      s                    se      
This i                       ntence



1.5  Programming Puzzles & Code Golf is a question and answer site for programming puzzle enthusiasts and code golfers.

                                                d answer site for p                                               
                                      uestion an                   rogramming                                     
                      Code Golf is a q                                        puzzle enthusia                     
Programming Puzzles &                                                                        sts and code golfers.



0.3  .....................

          .          
         . .         

        .   .        

       .     .       


      .       .      

     .         .     
    .           .    
   .             .   
...               ...

计分

这是

                 nsw                 
                a   er               
              t                      
             s         i             
            e           n            
           t                         
         or               by         
       sh                   te       
so the                        s wins.



1
我认为最后一个测试用例的第一行应该有3个点,而不是1个
。– addison

@addison我在这台计算机上没有参考实现,但是我不知道为什么Mego会得到不同的结果。他通过代码获得的结果似乎非常“块状”。我猜暂时忽略该测试用例。
致命

1
@TheBikingViking我会让它过去,很好。
致命

Answers:


2

Python 3中与SciPy的239个 233字节

from scipy import stats,around,arange
def f(s,t):
 l=len(t);p=[];y=around(stats.norm.pdf((arange(l)-l//2)*.1,scale=s),1)*10
 for i in range(l):p+=[[' ']*(max(y)-y[i])];p[i]+=[t[i]]+[' ']*(y[i]-y[0])
 for j in zip(*p):print(*j,sep='')

通过标准差s和字符串的参数接受输入t并将结果打印到STDOUT的函数。

怎么运行的

from scipy import stats,around,arange  Import the statistics, rounding and range functions
                                       from SciPy
def f(s,t):                            Function with input standard deviation s and string
                                       t
l=len(t);p=[]                          Define the much-used length of t as l and initialise
                                       the print values list p
arange(l)                              Generate a list of integer x values in [0,l)...
...-l//2*.1                            ...and scale such that 0 is at the middle character
                                       and the x-step is 0.1
stats.norm.pdf(...,scale=s)            Generate a list containing the y values for each x
                                       value by calling the normal probability
                                       density function scaled with s...
y=around(...,1)                        ...round all values to 1 decimal place...
...*10                                 ...and multiply by 10 to give the vertical index of
                                       each character
for i in range(l):...                  For all characters in t...
p+=[[' ']*(max(y)-y[i])]               ..add the number of lines below the character as
                                       spaces...
p[i]+=[t[i]]+[' ']*(y[i]-y[0])         ...add the character and the number of lines above
                                       the character as spaces

This leaves p containing a list for each desired output line, but transposed.

for j in zip(*p):...                   For every output line in the transpose of p...
print(*j,sep='')                       ...print the output line

在Ideone上尝试


2

红宝石:273254字节

->n,s{j,o,g,r,l=-(n.size/2),[],0,{}
n.gsub(/./){(r[((2*Math::PI)**-0.5*10*Math.exp(-(j/1e1)**2/2/s/s)/s).round]||="")<<$&
j+=1}
r.sort.map{|y, c|o<<(l ?$/*(y-l-1):"")+(" "*g)+(c[0,(h=c.size)/2])+(" "*(n.size-g*2-h))+(c[h/2,h])
g+=h/2
l=y}
puts o.reverse}

非常感谢Kevin Kevin节省了18个字节!


1
Lambda不需要括号:->n,s{...很好。分配多个变量时不需要括号:o,g,r,l=[],0,{}可以正常工作。$/可以代替?\n。运算顺序意味着您不必将所有乘法都放在第5行中。puts打印时自动展开数组并用换行符分隔它们。由于您可以取出并将其放在任何提及的位置,因此会稍微n.gsub(/./){...跳动。使您的哈希值字符串(以not 开头),您可以更改为n.each_char{...|c|$&c||=""||=[]c[...]*""c[...]
Value Ink
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.