塔尔,她吹!


21

Arrr ...嗨,我妈!松开主帆!充满右舷!啊,感受你头发中的风!

是的,我衷心...我需要一点你的技巧!我的船员比我自己更现代化……我仍然更喜欢指南针(请参阅此处了解更多信息,Arrr ...),而我的船员却总是头疼。我想要一种简单的方法来将他们称为“脑袋”的那一幕转换成我能理解的,Arrr!

我需要的是代码tha'be takin'输入的数字(小数也可以),例如tha',0 <= the headin' < 360并且它会作为最接近的罗盘点出现在答案中!啊!

以下是一些示例:

> heading 0.1

North

> heading 11.25

North by East

> heading 22.7

North Northeast

> heading 44.99

Northeast

> heading 91

East

现在,如果您不愿意让它恰好落在两个罗盘点的中间,那么请不要烦恼,我要衷心...我希望代码能吐出Between <point 1> and <point 2>,例如heading 5.625会说Between North and North by East 这只会发生,因为' H = 5.625 + 11.25(N)h'是'headin',而N是0到31之间(包括0和31)的整数...

两个限制

1)我不希望使用数组来存储点的数据或'headin's ... Tha'be cheatin',先生,'ye be betin'尝到了我的under弹...像过去一样被计算!啊!

2)最短的代码获胜,否则我将继续走下去... Arrr!


10
50 '(撇号)在一个问题中。脱下帽子!:D
Wasi 2014年

是在JavaScript中使用带有数字键作弊的对象?我是说yaarrr!是骗子吗,你是陆虎吗?
Tim Seguine 2014年

1
这不应该是“ N是0到31之间的整数”吗?
nwellnhof 2014年

@nwellnhof你是对的!已更新... :)
WallyWest

1
我认为您应该启动代码高尔夫,其目标是将您的问题翻译为拼写正确的英语!
sergiol

Answers:


6

Perl 5.10的使用取代,231 228 226 224

@c=(north,east,south,west);
@q=qw(P PbR P-Q QbP Q QbR R-Q RbP);
sub p{$j=$_[0]>>3&3;$_=$q[7&pop];s/P/$c[$j]/;s/Q/$c[$j+1&2]$c[$j|1]/;s/R/$c[$j+1&3]/;s/b/ by /;ucfirst}
$a=<>/11.25+.5;
say$a==int$a&&'Between '.p($a-1).' and ',p$a

添加了四个换行符以提高可读性。

编辑:使用打了2个字节pop。谢谢@Dom Hastings

编辑:少使用2个字节qw()


22

我花了太多时间在这里寻宝,但这是Java的解决方案:

public class Aaaaarrrr {

    public static void main(String[] aaarrrgs) {

        float heading = Float.parseFloat(aaarrrgs[0]);

        final List<String> points = Arrays.asList("North",
                "North by east", "North-northeast", "Northeast by north",
                "Northeast", "Northeast by east", "East-northeast",
                "East by north", "East", "East by south", "East-southeast",
                "Southeast by east", "Southeast", "Southeast by south",
                "South-southeast", "South by east", "South", "South by west",
                "South-southwest", "Southwest by south", "Southwest",
                "Southwest by west", "West-southwest", "West by south", "West",
                "West by north", "West-northwest", "Northwest by west",
                "Northwest", "Northwest by north", "North-northwest",
                "North by west");


        float cp = heading / 360.0f * 32.0f;
        if (cp % 1 == 0.5f)
             System.out.print("Between " + points.get((int)Math.floor(cp)) + " and ");

         System.out.println(points.get(Math.round(cp)));

    }
}

编辑如果我将上面的代码最小化并使其变得丑陋,它将变为:

Java,770个字符

import java.util.*;class A{public static void main(String[] r){List<String> l=Arrays.asList("North","North by east","North-northeast","Northeast by north","Northeast","Northeast by east","East-northeast","East by north","East","East by south","East-southeast","Southeast by east","Southeast","Southeast by south","South-southeast","South by east","South","South by west","South-southwest","Southwest by south","Southwest","Southwest by west","West-southwest","West by south", "West","West by north","West-northwest","Northwest by west","Northwest","Northwest by north","North-northwest","North by west");float c=Float.parseFloat(r[0])/360.0f*32.0f;if (c%1==0.5f) System.out.print("Between "+l.get((int)Math.floor(c))+" and ");System.out.println(l.get(Math.round(c)));}}

24
+1代表aaarrrgs
DankoDurbić,2014年

3
我确定这对“最短代码获胜”规则没有帮助,但是由于我还是使用Java ...
Jeen Broekstra 2014年

我很欣赏这种方法,尽管您使用的是Arrays.asList命令,但请原谅我对Java的了解,但这不是使用存储阵列吗?还是这是一种以列表形式引入数组的偷偷摸摸的方法?;)
WallyWest 2014年

@Jeen,您能告诉我,如果您真心向往并减少那里的代码,可以得到什么吗?
WallyWest 2014年

2
@ Eliseod'Annunzio给数组打了个电话,我以为您只是说我不能用它来存储名称和标题间隔之间的映射。该数组(列表)仅存储实际名称。但是,如果这超出了规则,我想可以将其改为使用一条switch语句(不幸的是有一些额外的字符)。哦,并添加了相同代码的压缩版本。我会考虑通过实际更改其工作方式来进一步降低它的使用率。
Jeen Broekstra 2014年

6

Python,264

n='north'
e='east'
s='south'
w='west'
b=' by '
def f(H):x,y,z=(n,e,s,w,e,s,w,n,n+e,s+e,s+w,n+w)[int(H%360/90)::4];return(x,x+b+y,x+'-'+z,z+b+x,z,z+b+y,y+'-'+z,y+b+x)[int(H%90*4/45)].capitalize()
h=input()+5.625
print h%11.25and f(h)or'Between '+f(h-1)+' and '+f(h)

根据Wikipedia页面,这将使用大写字母,并且应该适用于任何数字。


1
Arrr,那是对函数的巧妙使用...
WallyWest'1

4

Arrr Python,336岁

A,R,r=int,input()/360.*32,' by #South#north#West#East#south#North#west#east#-#/#Between#and'.split('#')
a=''.join(r[A(c,16)]for c in'6A608A6928A6802A68A6808A4928A402A4A405A4958A1808A18A1805A1958A108A1A107A1957A1705A17A1707A3957A305A3A302A3927A6707A67A6702A6927A607').split('/')
if R%1==.5:print r[11],a[A(R)],r[12],
print a[A(round(R))]

谢谢@Jeen


我在这里有点困惑@AsksAnyway,那里的绳子有什么作用?...
Arrr

它“计算”指南针点的名称。这是减少代码大小的一种方法。
AsksAnyway,2014年

2

Perl 5.10的,262 257 254

类似于Python解决方案之一:

$n=north;$e=east;$s=south;$w=west;
@d=($n,$n.$e,$e,$s.$e,$s,$s.$w,$w,$n.$w,$n);
sub p{$j=pop;$i=$j>>2;ucfirst(($d[$i],"$d[$i] by $d[$i+2&~1]","$d[$i+1&~1]-$d[$i|1]","$d[$i+1] by $d[$i&~1]")[$j&3])}
$a=<>/11.25+.5;
say$a==int$a&&'Between '.p($a-1).' and ',p$a

添加了四个换行符以提高可读性。

编辑:多亏了@Dom Hastings,减少了三个字节


嘿@nwellnhof,看起来很全面!您可以再保存几个字符,将;末尾的删除,sub p然后切换shiftpop
Dom Hastings 2014年

@nwellhoff我不知道Perl不用引号就可以接受字符串吗?(re:line 1)
WallyWest

1
如果@ Eliseod'Annunzio Perl不与函数名称或保留字冲突,则可以在非严格模式下接受不带引号的字符串。
nwellnhof 2014年
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.