高尔夫守则:四是魔术 [英] Code Golf: Four is magic

查看:48
本文介绍了高尔夫守则:四是魔术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上高中时听到的一个小谜题是这样的...

A little puzzle I heard while I was in high school went something like this...

  • 发问者会问我给他一个电话号码
  • 在听到数字后,发问者会反复对它进行某种转换(例如,他可能会说十是三),直到最终到达数字4(此时他会以四个是魔法)结束.
  • 无论如何,任何数字似乎最终都可以转换为四个.
  • The questioner would ask me to give him a number;
  • On hearing the number, the questioner would do some sort of transformation on it repeatedly (for example, he might say ten is three) until eventually arriving at the number 4 (at which point he would finish with four is magic).
  • Any number seems to be transformable into four eventually, no matter what.

目标是尝试找出转换函数,然后自己可靠地推动这个难题.

The goal was to try to figure out the transformation function and then be able to reliably proctor this puzzle yourself.

任何一步的转换功能都是

The transformation function at any step was to

  • 带上有问题的号码,
  • 计算英文单词表示形式中的字母数,忽略连字符或空格或和"(例如,十"中有3个字母,三十四"中有10个字母,一百四十") -三"中有20个字母).
  • 返回该字母数.

对于我曾经想测试的所有数字,其收敛为4.由于四个"中也有四个字母,因此这里将存在无限循环;取而代之的是,按照惯例,它仅被称为 magic 来结束序列.

For all of the numbers I have ever cared to test, this converges to 4. Since "four" also has four letters in it, there would be an infinite loop here; instead it is merely referred to as magic by convention to end the sequence.

您的挑战是创建一段代码,该代码将从用户那里读取一个数字,然后打印出重复显示变换功能的行,直到达到四个神奇"为止.

Your challenge is to create a piece of code that will read a number from the user and then print lines showing the transformation function being repeatedly applied until "four is magic" is reached.

特别是:

    解决方案本身必须是完整的程序.它们不能仅仅是在输入中包含数字因素的函数.
  1. 必须从标准输入中读取输入. (通过"echo"进行管道传输或使用输入重定向是可以的,因为这也来自于stdin)
  2. 输入应为数字形式.
  3. 对于变换函数的每个应用,应打印一行:a is b.,其中a和b是变换中数字的数字形式.
  4. 需要句号(句号)!
  5. 最后一行自然应该是4 is magic..
  6. 代码应为 0到99 的所有数字产生正确的输出.
  1. Solutions must be complete programs in and of themselves. They cannot merely be functions which take in a number-- factor in the input.
  2. Input must be read from standard input. (Piping from "echo" or using input redirection is fine since that also goes from stdin)
  3. The input should be in numeric form.
  4. For every application of the transformation function, a line should be printed: a is b., where a and b are numeric forms of the numbers in the transformation.
  5. Full stops (periods) ARE required!
  6. The last line should naturally say, 4 is magic..
  7. The code should produce correct output for all numbers from 0 to 99.

示例:

> 4
4 is magic.

> 12
12 is 6.
6 is 3.
3 is 5.
5 is 4.
4 is magic.

> 42
42 is 8.
8 is 5.
5 is 4.
4 is magic.

> 0
0 is 4.
4 is magic.

> 99
99 is 10.
10 is 3.
3 is 5.
5 is 4.
4 is magic.

获胜者是提交源代码字符数最少的提交者,也是正确.

您还可以尝试编写该代码的版本,该代码的每个应用转换函数都会打印出英文名称.原始输入仍为数字,但输出行应为数字的单词形式.

You may also try to write a version of the code which prints out the ENGLISH NAMES for the numbers with each application of the transformation function. The original input is still numeric, but the output lines should have the word form of the number.

(使用代码绘制形状可获得双倍奖励)

(编辑)一些澄清:

  1. 我确实希望在所有适用情况下(例如Nine is four. Four is magic.
  2. 不过,我不在乎大写.而且我不在乎如何分隔单词标记,尽管应该将它们分开:ninety-nine可以,ninety nine可以,ninetynine不能.
  1. I do want the word to appear on both sides in all applicable cases, e.g. Nine is four. Four is magic.
  2. I don't care about capitalization, though. And I don't care how you separate the word tokens, though they should be separated: ninety-nine is okay, ninety nine is okay, ninetynine is not okay.

关于挑战,我正在考虑将它们作为奖金竞赛的一个单独类别,因此,如果您这样做,请不要担心您的代码会比数字版本更长.

I'm considering these a separate category for bonus competition with regard to the challenge, so if you go for this, don't worry about your code being longer than the numeric version.

可以为每个版本随意提交一个解决方案.

Feel free to submit one solution for each version.

推荐答案

GolfScript- 101 96 93 92 91 90 94 86字节

90 → 94:固定输出,为10的倍数.
94 → 86:重组代码.使用基数100删除不可打印的字符.
86 → 85:更短地转换为字符串.

GolfScript - 101 96 93 92 91 90 94 86 bytes

90 → 94: Fixed output for multiples of 10.
94 → 86: Restructured code. Using base 100 to remove non-printable characters.
86 → 85: Shorter cast to string.

{n+~."+#,#6$DWOXB79Bd")base`1/10/~{~2${~1$+}%(;+~}%++=" is "\".
"1$4$4-}do;;;"magic."

这篇关于高尔夫守则:四是魔术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆