任务
创建一个不使用内置阶乘函数来计算数字阶乘的程序。简单?要注意的是,您必须以haiku形式编写整个程序(包括对其进行测试)。
您可以根据需要使用任意多个句,但是在发音时,它们必须遵循5-7-5音节格式。
计分
这是一场人气竞赛,因此您必须获得最多的投票才能获胜。您的程序必须至少包含一个完整的句,并且所有句必须完整。
读取代码时,每个句的第一行将有5个音节,第二行将有7个,第三行将有5个。
then
标点符号可能有助于切割。对于kigo,不是那么确定...
创建一个不使用内置阶乘函数来计算数字阶乘的程序。简单?要注意的是,您必须以haiku形式编写整个程序(包括对其进行测试)。
您可以根据需要使用任意多个句,但是在发音时,它们必须遵循5-7-5音节格式。
这是一场人气竞赛,因此您必须获得最多的投票才能获胜。您的程序必须至少包含一个完整的句,并且所有句必须完整。
读取代码时,每个句的第一行将有5个音节,第二行将有7个,第三行将有5个。
then
标点符号可能有助于切割。对于kigo,不是那么确定...
Answers:
(在工作空间中求值;打开一个对话框,询问一个数字并在标准输出上打印结果):
"in" "this" 'poem,' "you" "must"
"pronounce" "characters" "like" "these:"
"return(^)," "and" "times(*);" "please".
"but" 'never' "in" "here"
"tell" "anyone" "about" "those"
"few" "parentheses".
"otherwise" "these" "words"
"are" "stupid" "and" "this" "coded"
"rhyme" "is" "wasted" Time.
"let" "us" "now" "begin"
"by" "defining," "in" Object
"and" compile: "the" "rhyme:"
'fac: "with" arg"ument"
"to" "compare" arg <"against" 2 ">"
"and" ifTrue: [ ^"return"
"["1] "or" ifFalse: "then"
["return"^ arg *"times" "the" "result"
"of" ("my"self ")getting"
"the" fac:"torial"
"of" "the" "number" arg "minus"-
1 "[(yes," "its" "easy")]'.
("Let" "me" "my"self "ask"
"for" "a" "number," "to" "compute"
"the" fac:"torial"
("by" "opening" "a"
"nice" Dialog "which" "sends" "a"
request: "asking" "for"
'the Number to use'
"(which" "is" "(" "treated" ) asNumber)
"then" print "the" "result".
我也尝试引入一些反思(“这首诗”)和kigo。此外,还包括一些西方风格的押韵元素(请->这些,时间->押韵);但是,既不说日语,也不说英语的原谅;-)
fact :: Int -> Int -- fact is Int to Int
fact x = product (range x) -- fact x is product range x
range x = [1..x] -- range x is 1 [pause] x
Haskell的教育时间:
range x
函数创建一个从1到的整数列表x
。fact x
函数将列表的所有值相乘range x
在一起以计算结果。fact
函数接受一个整数并返回一个整数。range x is 1 to x
是6个音节
protected static
int factorial(int n) {
if (n == 0) {
return n + 1;
} return factorial(n
- 1) * n;}
即使问题不是代码高尔夫,我也经常发现自己正在寻找答案。在这种情况下,我打了一些hai句。
我这样说:
如果n为零,则保护静态
int阶乘int n返回n加一个
返回乘数n
减去n
测试程序:
class Factorial { // class Factorial
public static void main(String[] // public static void main string
command_line_run_args) { // command line run args
int i = 0; // int i is zero
while (7 != 0) // while seven is not zero
System.out. // System dot out dot
println(i + "!" // print line i plus not
+ " = " + factorial( // plus is plus factorial
i += 1));} // i plus equals 1
protected static
int factorial(int n) {
if (n == 0) {
return n + 1;
} return factorial(n
- 1) * n;}}
注意,该程序开始0
快速输出s。那是溢出的结果。将每个值更改int
为,可以轻松获得更大的正确数字long
。
对于标准的发音System.out.println
,并public static void main(String[] args)
反映在程序中。
factorial←{×/⍳⍵}
阶乘是- 高达欧米茄
的自然产物
factorial←×/⍳
“最多输入”。
The Products of Love:
A haiku tragedy with
mathy undertones.
Romeo, a man.
Juliet, a maiden fair.
Friar John, a monk.
Act I: A Cycle.
Scene I: Pertinent Values.
[Enter Romeo]
[Enter Friar John]
Romeo: Listen to thy
heart. Thou art thyself.
Friar John: Thou art
as forthright as a songbird.
[Exit Friar John]
[Enter Juliet]
Romeo: Thou art as fair
as a violet.
Scene II: Questioning
Themselves. [Exit Juliet]
[Enter Friar John]
Friar John: Art thou
as just as the sum of me
and a nobleman?
Romeo: If so,
let us proceed to scene III.
[Exit Friar John]
[Enter Juliet]
Romeo: Thou art as good
as the product of
thyself and myself.
Juliet: Thou art as fierce
as the sum of an
eagle and thyself.
We must return to scene II.
Scene III: A Lengthy
Title for a Brief
Dénouement; Or, The Last Word.
[Exit Friar John]
[Enter Juliet]
Romeo: Open your heart.
[Exit Romeo]
(想象的)测试用例:
尽管其长度很大,但该程序仅将单个整数作为输入,并提供单个整数作为输出。所以:
6 ↵ 720
7 ↵ 5040
0 ↵ 1 1 ↵ 1
(“六,七二十。/七,五四十。/零,一。一,一。”)
完整的Python 2程序haifac.py
。运行为python haifac.py <n>
#run this full program
import operator as\
op; import sys#tem
#please provide an arg
n = sys.argv[1]
def haifac (n):
if n < 1:
return 1#to me at once
else:#do something else
return op.mul(
n, haifac(n - 1))
print haifac(int(n))
的发音:
运行此完整程序
导入运算符作为
op导入系统请提供一个arg
n等于sys arg v 1
定义hai fac n如果n小于1
,则一次将1 退还给我,再
执行其他操作return op dot mul
n hai fac n减1
打印hai fac int n
#to me at once
使仪表工作……
),{0>},{,,*}*
阅读为haiku,列举每个按键:
#close parenthesis
#comma open-brace zero
#greater-than close-brace
#comma open-brace
#comma comma asterisk
#close-brace asterisk
带有测试用例(5个hai句):
[1 2 3]4+ #generate the array [1 2 3 4]
{ #start creating block
),{0>},{,,*}* #actual factorial code
}% #close block and map across array (so that we should have [1! 2! 3! 4!])
[1 2 6]2.3**12++= #generate the array [1 2 6 24] and check for equality
读作句:
#open-bracket one
#space two space three close-bracket
#four plus open-brace
#close parenthesis
#comma open-brace zero
#greater-than close-brace
#comma open-brace
#comma comma asterisk
#close-brace asterisk
#close-brace percent-sign
#open-bracket one space two
#space six close-bracket
#two period three
#asterisk asterisk one
#two plus plus equals
全押句!
function haiku($can) { // function haiku can (5)
if ($can == 1) // if can is equal to one (7)
return ++$stun; // return increase stun (5)
if ($can == 0) { // if can equals ou (5)
echo "It is one, you know! "; //echo "It is one, you know! " (7)
return 1+$blow; } //return one plus blow (5)
if ($can > $fun) { //if can exceeds fun (5)
return haiku($can-1) //return haiku can less one (7)
*$can; }} //multiplied by can (5)
if (null == $knee) { // if null equals knee (5)
$free=0+3; // free equals zero plus three (7)
echo haiku($free); } // echo haiku free (5)
return plus plus stun
。
这利用了最著名的 haiku之一,并且已经有很多关于它的文章。
不知道为什么以前没有人做过 它甚至不需要任何努力!
首先,在阅读这首诗之前,我希望您向后倾斜,放松身心,并享受由诗歌周围巨大空白所带来的宁静。它强调了池塘,周围是广阔的风景。
古池や 蛙飞びこむ 水の音
如果您不会说日语,其发音如下:
富瑞克亚
嘉华祖到碧古墓
mi zu no o to
自然地,它是由morae计算的。所述kireji是や(亚)中,季语(季节性参考)是蛙(河津,青蛙, - >簧)。
使用官方页面上的linux解释器,您可以像这样使用它:
$ echo 5 | ./wspace .ws
f[x_]:= (* f of x defined *)
x f[x-1] (* x times f of x less 1 *)
f[1]=1 (* Mogami River *)
步行者可能会读到最后一行,因为“ f of 1 is 1”,但我无法抗拒向Basho喊叫。
Table[f[n], (* Table f of n *)
{n, 1, 10, 1}] (* n from 1 to 10 by 1 *)
ListLogPlot[%] (* ListLogPlot output *)
(1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800)
Rewrite any term
High-level functions abound —
Mathematica
括号不发音:
(define (fac n (so)) ; define fac n so
(if (= n 0) 1 ; if equals n zero 1
(* n (fac (dec n))))) ; times n fac dec n
(for (n 0 10) ; for n zero ten
; let's test from zero to ten
(println (fac n thus))) ; printline fac n thus
Lisp代码包括
许多括号
和一些功能
$r = 1; for(1 # r gets one for one
.. pop @ARGV) { $r *= # to pop arg v r splat gets
$_; } print $r; # the default print r
扔到一个名为 f.pl
并输出:
$ perl f.pl 3 6 $ perl f.pl 1-1 1美元perl f.pl 10 3628800 $
读为:
perl fpl三 perl fpl一少一 perl fpl十
这个人的中世纪:
prelude = ^^ do # prelude is clone do
require \prelude-ls # require prelude dash ls
{ product } = prelude # product is prelude
story = (ah) -> # story is ah such:
ones-misery = (one) -> # one's misery is one such
let death = product # let death be product
fight = [1 to one] # fight is one to one
why = (one) -> death <| fight # why is one such death take fight
ones-misery ah # one's misery ah
room = console.log # room is console log
room <| (story 10)! # room take story three bang
[null of { use : this }] # no of use is this
打印3628800
,是10!
。有点回旋处:函数story
返回一个函数ones-misery
,该始终返回答案。那样才是艺人。
没有填充注释或不必要的字符串!
额外的调试故事:
当得知某个错误为
“death
是undefined
” 时, 我大笑起来
Ruby-一个Hai句
ARGV.first.to_i.
tap do |please| puts 1.upto(
please ).inject( :*) end
阅读(忽略标点符号,但包括一个图释)是这样的:
arg vee first to i
tap do please puts one up to
please inject smile end
我知道使用现成的函数是违反规则的,但这就是我得到的。
想象一下,您的任务是教大型啮齿动物:
use Math::BigRat; use
feature 'say'; use warnings; say
new Math::BigRat($_)->bfac
我只能猜测最后一个单词的含义以及它的发音,但我向您保证它是一个音节。显然他不了解您想要从他那里得到什么,因此您必须详细说明(在您失去耐心的同时提高质量标准):
use Math::BaseConvert
':all'; no strict subs; no warnings;
reset and say fact($_)
仍然无济于事。然后,您必须用简单的英语来解释它:
no strict; no warnings;
use Math::Combinatorics;
say factorial($_)
接下来我不知道发生了什么,但是代码是有效的:
perl -nE 'use Math::BigRat; use feature "say"; use warnings; say new Math::BigRat($_)->bfac'
42
1405006117752879898543142606244511569936384000000000
和
perl -nE 'use Math::BaseConvert ":all"; no strict subs; no warnings; reset and say fact($_)'
33
8683317618811886495518194401280000000
和
perl -nE 'no strict; no warnings; use Math::Combinatorics; say factorial($_)'
16
20922789888000
lambda n: reduce(
lambda a, b: a * b,
range(1, n), n)
我的阅读方式:
lambda n: reduce lambda a b: a times b range 1 to n, n
`
C
#include <std\
io.h>
#include \
<stdlib.h>
int main(int argc
, char** argv)
{ // iteratively
// compute factorial here
long int n = \
0, i \
= 0, r = \
1 /*
product starts at one*/;
if (argc
> 1) { n =
strtol(argv[\
1], NULL, 10)
; if (n
< 0) {
printf("Arg must\
be >= 0\n");
exit(-
1);}
} i =
n;
while (i) { r
= r * i;
i
--;
} /* print
the result*/ printf(
"%d factorial\
equals %d\
\n", n
, r);
/*done*/}
的发音:
磅包含标准
I / O点h磅包含
标准lib点h
int main int arg c
逗号char star star arg v
open brace comment
在这里迭代计算阶乘
int等于int
零逗号i
等于零逗号r
等于一个注释
如果arg c
大于1,则乘积以一个分号开始
开括号n是
arg v的str-to-l子
逗号1空逗号10
如果
n小于零,
则以分号开头printf arg必须
大于或
等于零的反斜杠
n分号
出口负
一个分号部支柱
部支柱i等于
n分号
当我打开大括号时 r
等于r乘以i
分号我
减号分号
右大括号评论打印
结果printf
百分比d阶乘
等于d百分比
whack n逗号n
逗号分号
注释已完成
#
字符通常发音为尖锐或八齿形。
我删除了通常的C#使用,名称空间和类定义混乱,这将是第4个句。
public static void
Main(){int num = Convert.
ToInt32
(Console.ReadLine());
Console.WriteLine(num ==
0 ? 1 :
Enumerable.
Range(1, num).Aggregate
((i, j) => i * j));}
我读为
public static void
Main int num equals Convert
To int thirty-two
Console dot Read line
Console Write line num equals
zero? then one, else
Enumerable
Range 1 to num aggregate
i j i times j
module Haiku where -- read literally.
fac x = let in do -- = is read as 'equals'
product [1..x] -- product one to x
请注意,模块.. where在编译时会自动添加到任何Haskell代码中,而无需对其进行编写,因此实际上编写它是作弊行为。
do
,也不必如此Monad a => a
。
function factor (a) { // function factor a
if(!a){ return 1 || // if not a return 1 or
a & 17} // a and seventeen
else if (a + 1){ // else if a plus one
return a * factor // return a into factor
(a + ( - 1) ) }} // a plus minus one
我不是母语人士。所以,我用字典来计数音节。希望它足够好。欢迎任何反馈:)
我们可以使用填料吗?
Python 2 hai句:
number = num + 1
a = 13 + 4
b = 14
nuum = len([1])
for i in range(1, number):
nuum *= i
nuum equals length one
?
num equals length of the list
使得7个syllabes而不是5
nuum
为foo
(因为我正在读取是否为nu-um,这超出了限制。)
Befunge-93
三个hai句:
0&>:v
-:|:>1>
v#>$\
>:>>v
>$.@v<<
^#v*_
#>>>v
<<:\<>^
<#<#<
最后一行无疑是骗子。
在这里尝试:http : //www.bedroomlan.org/tools/befunge-93-playground