我的号码多久?


26

挑战

给定一个整数,Q范围为-(2^100) ≤ Q ≤ 2^100,输出该数字的位数(以10为底)。

规则

是的,您可以将数字作为字符串并找到其长度。

允许所有数学函数。

您可以使用任何底数作为输入,但是输出必须是以10为底的数字的长度。

不要将负号计为负数。该数字永远不会有小数点。

零可以是一位或零位。

假设输入将始终是有效整数。

例子

Input > Output

-45 > 2
12548026 > 8
33107638153846291829 > 20
-20000 > 5
0 > 1 or 0

获奖

以字节为单位的最短代码获胜。

Answers:


10

Brachylog,1个字节

l

在线尝试!

另一种内置解决方案,但该解决方案的名称最短(除非有人找到一种以零字节为单位执行此任务的语言)。这在Brachylog 1和Brachylog 2中都应该起作用。

这是一个函数提交(TIO链接包含一个命令行参数,使解释器运行单个函数而不是整个程序),部分是因为否则我们将不得不在输出上花费字节,部分是因为Brachylog的负数语法数字有点不寻常,并且使该程序成为函数可以解决有关输入语法的任何潜在参数。

经常让我感到困扰的是,Bracylog的大多数内建函数都将负数视为正数,但是事实最终在这里派上了用场。我想每种高尔夫语言都需要权衡取舍。


这是我停止滚动的地方...这太离谱了!
Bogdan Alexandru

39

出租车,1118字节

1 is waiting at Starchild Numerology.Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to Chop Suey.Go to Chop Suey:n 1 r 1 l 4 r 1 l.Pickup a passenger going to Crime Lab.'-' is waiting at Writer's Depot.Go to Writer's Depot:n 1 l 3 l.Pickup a passenger going to Crime Lab.Go to Crime Lab:n 1 r 2 r 2 l.Switch to plan "n" if no one is waiting.-1 is waiting at Starchild Numerology.[n]0 is waiting at Starchild Numerology.Go to Starchild Numerology:s 1 r 1 l 1 l 2 l.Pickup a passenger going to Cyclone.Pickup a passenger going to Addition Alley.Go to Cyclone:e 1 l 2 r.[r]Pickup a passenger going to Cyclone.Pickup a passenger going to Addition Alley.Go to Zoom Zoom:n.Go to Addition Alley:w 1 l 1 r.Pickup a passenger going to Addition Alley.Go to Chop Suey:n 1 r 2 r.Switch to plan "f" if no one is waiting.Pickup a passenger going to Sunny Skies Park.Go to Sunny Skies Park:n 1 l 3 l 1 l.Go to Cyclone:n 1 l.Switch to plan "r".[f]Go to Addition Alley:n 1 l 2 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:n 1 r 1 r.Pickup a passenger going to Post Office.Go to Post Office:n 1 l 1 r.

在线尝试!

取消高尔夫:

1 is waiting at Starchild Numerology.
Go to Post Office: west 1st left 1st right 1st left.
Pickup a passenger going to Chop Suey.
Go to Chop Suey: north 1st right 1st left 4th right 1st left.
Pickup a passenger going to Crime Lab.
'-' is waiting at Writer's Depot.
Go to Writer's Depot: north 1st left 3rd left.
Pickup a passenger going to Crime Lab.
Go to Crime Lab: north 1st right 2nd right 2nd left.
Switch to plan "n" if no one is waiting.
-1 is waiting at Starchild Numerology.
[n]
0 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 1st right 1st left 1st left 2nd left.
Pickup a passenger going to Cyclone.
Pickup a passenger going to Addition Alley.
Go to Cyclone: east 1st left 2nd right.
[r]
Pickup a passenger going to Cyclone.
Pickup a passenger going to Addition Alley.
Go to Zoom Zoom: north.
Go to Addition Alley: west 1st left 1st right.
Pickup a passenger going to Addition Alley.
Go to Chop Suey: north 1st right 2nd right.
Switch to plan "f" if no one is waiting.
Pickup a passenger going to Sunny Skies Park.
Go to Sunny Skies Park: north 1st left 3rd left 1st left.
Go to Cyclone: north 1st left.
Switch to plan "r".
[f]
Go to Addition Alley: north 1st left 2nd left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: north 1st right 1st right.
Pickup a passenger going to Post Office.
Go to Post Office: north 1st left 1st right.

说明:

Pickup the input and split it into individual characters
Pickup the value 1.
If the first character a hyphen, add -1. Otherwise, add 0.
Keep picking up characters and adding 1 until you're out.
Convert the running total to a string and print to stdout.

8
我长期以来一直是这种交流的潜伏者,但从未见过这样的事情
爪哇杯(Cup of Java)

7
如果数目足够长,这会用光吗?
罗伯特·弗雷泽

5
这比脑力劳动更大。
欧米茄

1
@RobertFraser这就是为什么我们在Zoom Zoom的每个循环都停在plan "r"。我只是对其进行了高达100,000位数的测试,而且它从来没有用完。我没有计算出来,但我想它所收取的票价足以支付所用的汽油,因此它在每个回路中都充满了油箱。
敬酒工程师

1
@CupofJava噢,我的天哪,我怎么忘了莎士比亚
Engineer Toast


14

直流,3

?Zp

请注意,通常dc要求提供负数,_而不是更常见的数-。但是,在这种情况下,都可以使用。如果-给出,则将其dc视为空堆栈上的减法,throws dc: stack empty,然后继续其余的数字;因此结果没有什么不同。

在线尝试

?    # input
 Z   # measure length
  p  # print

难道这不只是Z功能提交吗?dc是带有quote + dup + eval运算符的连接语言,因此可以重用任意代码串。



5

爱丽丝,16字节

//; 'q<)e
o!@i -

在线尝试!

说明

为此找到一个像样的布局非常棘手。由于空格,<和和;,我仍然不太满意,但这是我目前能做的最好的事情。

字符串长度是Alice中不存在的那些非常常见的内置函数之一,因为它的输入是字符串,而输出是整数(并且所有Alice命令严格来说都是整数到整数或字符串到字符串)。我们可以通过以下方式测量字符串的长度:在“序数”模式下将其写入磁带,然后在“基数”模式下找到其末端。

/      Reflect to SE. Switch to Ordinal. While in Ordinal mode, the IP will bounce
       diagonally up and down through the code.
!      Store an implicit empty string on the tape, does nothing.
;      Discard an implicit empty string, does nothing.
i      Read all input as a string.
'-     Push "-".
<      Set the horizontal component of the IP's direction to west, so we're bouncing
       back now.
-      Remove substring. This deletes the minus sign if it exists.
'i     Push "i".
;      Discard it again.
!      Store the input, minus a potential minus sign, on the tape.
/      Reflect to W. Switch to Cardinal. The IP immediately wraps to the
       last column.
e)     Search the tape to the right for a -1, which will be found at the end
       of the string we stored there.
<      Does nothing.
q      Push the tape head's position, which is equal to the string length.
'<sp>  Push " ".
;      Discard it again.
/      Reflect to NW. Switch to Ordinal. The IP immediately bounces off
       the top boundary to move SW instead.
o      Implicitly convert the string length to a string and print it.
       IP bounces off the bottom left corner, moves back NE.
/      Reflect to S. Switch to Cardinal.
!      Store an implicit 0 on the tape, irrelevant.
       The IP wraps back to the first line.
/      Reflect to NE. Switch to Ordinal. The IP immediately bounces off
       the top boundary to move SE instead.
@      Terminate the program.

我也尝试使用H(绝对值)来照顾基数模式下的减号,但是额外的模式切换总是使我的尝试更加昂贵。


4

PHP,23字节

<?=-~log10(abs($argn));

在线尝试!

绝对值以10为底的对数加上一个强制转换为int的对数

输入log10为零时返回INF为false

更好的方法是$argn$argn?:1+3字节替换

PHP,27字节

<?=strlen($argn)-($argn<0);

字符串长度减去布尔值小于零

+2字节用于字符串比较 $argn<"0"

在线尝试!

PHP,32字节

<?=preg_match_all("#\d#",$argn);

在线尝试!

正则表达式计算所有数字

35字节

<?=strlen($argn)-strspn($argn,"-");

在线尝试!

字符串长度减去计数 -

strspn


1
例如,第一个不起作用10,因为的^优先级较低。您可以使用修复它-~
user63956 '17

为什么不简单<?=strlen(abs($argn));
roberto17年

@ user63956在输入零的情况下,带有log10的版本不起作用,因此我将其删除。
约尔格Hülsermann

1
@JörgHülsermann为什么不只是$argn?:1?使用log10()和将为26个字节abs()
user63956 '17

1
@JörgHülsermann -~$x等同于((int)$x)+1<?=-~log10(abs($argn?:1));似乎有效。
user63956 '17

4

Fortran 95(gfortran),121 96 95字节

program c
character b
call get_command_argument(1,b,length=i)
print*,i-index(b,'-')
end program

说明:
从参数的长度中减去“-”号的索引。
在Fortran中,数组从1开始,如果找不到符号,index()返回0。

编辑:切换到隐式整数“ i”,也合并参数getter。

编辑:-1字节感谢@Tsathoggua


1
欢迎来到PPCG!
Martin Ender

3

PowerShell,24字节

"$args"-replace'-'|% Le*

将输入args的“绝对”值转换为字符串,并获取其“长度”属性。

比1个字节短 "".Length

直到有人找到在PS中获取数字绝对值的更好方法,这可能会变得很短。


怎么"$args".trim('-')|% Le*样 :)
不论


3

brainfuck,37个字节

-[+>+[+<]>+]>->,[-<->]<[>+>],[<+>,]<.

输出是按字节值。

在线尝试!

说明

-[+>+[+<]>+]>->  Constant for 45 (from esolangs wiki)
,                Read a byte of input
[-<->]           Subtract that byte from 45
<[>+>]           If the result is nonzero then increment a cell and move to the right
                 (0 means it was a minus; so not counted)
,[<+>,]          Read a byte and increment the cell to its left until EOF is reached
<.               Print the cell that was being incremented

是否可以在TIO链接中添加页脚,以将结果输出为数字?
Beta衰减

@BetaDecay添加
商务猫

太好了,谢谢:D
Beta Decay

3

Ruby,15 11 + 1 = 16 12字节

使用-n标志。

p~/$/-~/\d/

在线尝试!

说明

                  # -n flag gets one line of input implicitly
p                 # Print
 ~/$/             # Position of end of line (aka string length) in input
     -            # minus
      ~/\d/       # Position of first digit (1 if negative number, 0 otherwise)

1
这是什么魔术?
Chowlett

2
@Chowlett添加了一个解释。
价值墨水

2

果冻,2 个字节

DL

在线尝试!

这确实符合要求:

DL - Main link number n         e.g. -45
D  - convert to a decimal list       [-4,-5]
 L - get the length                  2

那是一个有趣的内置函数,D对小数有效吗?会-1.2输出[-1,-0.2]吗?我自己尝试过,但事实并非如此。
魔术章鱼缸

1
不完全是,基本转换只能归结为单位,因此,例如,654.321D会产生[6,5,4.321](实际上是[6.0,5.0,4.321000000000026]
乔纳森·艾伦

[-6.0, -5.0, -4.321000000000026],实际上,显然。
魔术章鱼缸

啊-是的,刚刚编辑过-浮点运算。
乔纳森·艾伦


2

Japt,5个字节

a s l

在线尝试!

说明

 a s l
Ua s l
Ua     # take the absolute value of the input
   s   # and turn it into a string
     l # and return its length


2

JavaScript(ES6),27 26 25 24字节

将输入作为字符串。

s=>s.match(/\d/g).length
  • 感谢Arnauld,节省了两个字节。

您的标题为23个字节,但您的代码为24个...但是,这是23个字节:s=>`${s>0?s:-s}`.length
唐·黑斯廷斯

谢谢@DomHastings。您应该将您的答案作为单独的答案发布,因为这是我的另一种方法。
毛茸茸的


2

Java,30个 24字节

i->(""+i.abs()).length()

假设i是一个BigInteger。而且,该类型是上下文相关的,因此不需要导入,如测试代码所示。

测试

// No imports
class Pcg120897 {
  public static void main(String[] args) {
    java.util.function.ToIntFunction<java.math.BigInteger> f =
        // No full class declaration past here
        i->(""+i.abs()).length()
        // No full class declaration before here
      ;
    System.out.println(f.applyAsInt(new java.math.BigInteger("-1267650600228229401496703205376"))); // -(2^100)
    System.out.println(f.applyAsInt(new java.math.BigInteger("1267650600228229401496703205376"))); // (2^100)
  }
}

节省

  • 30-> 24个字节:感谢@cliffroot

+""代替.toString()
悬崖根

2
+1用于提供示例代码,以显示如何调用该代码,并阐明i答案的类型。我认为更多的lambda答案应该做到这一点。


1

脑筋急转弯,63字节

([({})]((((()()()()())){}{})){}{})((){[()](<{}>)}{})([{}][]<>)

在线尝试!

这是62个字节的代码,而+1个字节用于-a标志。

我尝试了另外两种方法,但是不幸的是,它们都更长:

([]<({}[((((()()()()())){}{})){}{}]<>)((){[()](<{}>)}{})>)({}[{}])

([]<>)<>({}<>)((((([][]())){}{})){}{}[{}])((){[()](<{}>)}{})([{}]{})

应该是一个简短的答案。实际上,如果我们不必支持负数,我们可以这样做:

([]<>)

但是我们必须将第一个输入与第45个(ASCII -)首先进行比较,这是此答案的大部分字节数。

算术解决方案可能会更短。


我数了62个字节。
完全人类

1
@totallyhuman看到我的编辑。
DJMcMayhem

49个字节:([{}]((((()()()()())){}{})){}{})({(<()>)}{}[]<>)
Nitrodon's




1

爱丽丝Alice),10个字节(无竞争)

 /d/
O@IHc

在线尝试!

这是一个非竞争性的解决方案,因为在发布此挑战时,命令c已在官方(且仅是:D)解释器中进行了错误处理。同时,Martin Ender对其进行了修复,因此现在可以使用。

说明

指令指针/多次通过两个镜像(),因此可能很难遵循。我将尝试使用基本方向尽可能清楚地解释它(例如N向上,SW向下向左...)。我称/1最左边的镜子/2为最右边的镜子。

Command    Direction    Comment
               E        Execution starts from the upper-left corner going right
   /1        E → SE     Passing through the mirror changes direction and switches
                        to ordinal mode (string operations)
   I        SE → NE     Push the input string to the stack, then bounce against
                        the bottom of the code
   /2       NE → S      Back to cardinal mode (numeric operations)
   H           S        Pop n, push abs(n). Execution wraps from bottom to top
   /2        S → SE     Ordinal mode again
   c        SE → NW     Pop s, push each char of s separatedly. Bounce against
                        the bottom right corner
   /2       NW → W      Cardinal mode
   d           W        Push the depth of the stack (which is now equal to 
                        the number of characters in abs(input))
   /1     W → NW → SW   Pass through the mirror, then bounce agains the top
   O        SW → NE     Output the result, then bounce on the bottom left corner
   /1       NE → S      Last mirror, I promise
   @           S        Terminate execution

1

GNU Make,78个字节

命令式:

$(eval T=$1)$(foreach D,$(shell seq 9),$(eval T=$(subst $D,? ,$T)))$(words $T)

功能样式,113字节:

$(eval 2?=$(shell seq 9))$(if $2,$(call $0,$(subst $(word 1,$2),? ,$1),$(wordlist 2,$(words $2),$2)),$(words $1))

Pure Make,83个字节:

$(eval T=$1)$(foreach D,0 1 2 3 4 5 6 7 8 9,$(eval T=$(subst $D,? ,$T)))$(words $T)

1

C ++,80 76字节

#include<string>
int main(int,char**c){printf("%d",strlen(c[1])-(*c[1]<46));}

打印参数的长度,如果第一个字符为负号bool1则减1,因为可以保证转换为if true0iffalse

  • 4个字节,感谢@Squidy指出我可以使用<46代替=='-',并尊重数组而不是[]

您可以通过替换c[1][0]=='-'为来减少4个字节,*c[1]<46因为我们可以假定输入将始终是有效整数。(除非允许使用'-'以外的前缀...)
Squidy '17

@Squidy哦,哇,真好找!我竭尽全力试图缩短时间,甚至从未想过!感谢您的建议,尤其是注册PCCG以便通知我!
塔斯州

1

TI-Basic(TI-84 Plus CE,OS 5.2+),6字节

length(toString(abs(Ans

TI-Basic是一种标记化语言;length(toString(每个是两个字节。

Ans用作隐式输入;最后(唯一)行的值隐式返回。

很简单,将绝对值除掉负号,转换为字符串,返回字符串的长度。

6字节的数学方法不适用于0:

1+log(abs(Ans

哪些计算器有toString(
kamoroso94

@ kamoroso94 TI-84 Plus CE
pizzapant184 '17

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.