Given an integer n as input write a program to output the numbers of digit in it.
Input
12346
456
Output
5
3
Constraint
n<=10^9
Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It only takes a minute to sign up.
Sign up to join this community,
,
means length...
Edit: You seem to have changed your question... please don't do that, but I still have a solution:
n%{,n}/
Thanks @Howard for reminding me about n
to push newline and using separate strings instead of one, to save 5 chars ;)
Solutions that work with negative numbers:
'-'-,
n%{'-'-,n}/
s/\D//;$_=length
run as:
perl -ple 's/\D//;$_=length'
s///
, but I think you need a g
at the end for numbers like 1_000_000_000? Also, how about $_=s/\d//g
? :)
\$\endgroup\$
Jan 28, 2014 at 18:05
perl -ple"$_=y/0-9//"
seems like the proper way to do it.
\$\endgroup\$
I'm trying to stay away from the simple string length functions, but the BF solution basically does a count.
floor(log10(abs(n)))+1
Thanks to @plg for the correction when n is a power of 10. This invokes undefined behavior when n=0, but that case can be returned by a simple check.
($n==0)?1:floor(log(abs($n),10))+1;
>>+[<+>++++++++++,----------]<-<++++++++[>++++++<-]>.
log10()
: ceil(log10(abs(n))
EDIT: this won't work for powers of 10!
\$\endgroup\$
Jan 28, 2014 at 20:02
floor(log10(abs(n))) + 1
\$\endgroup\$
Jan 28, 2014 at 20:41
?Zp
Sample run:
bash-4.2$ dc -e '?Zp' <<< '12346'
5
bash-4.2$ dc -e '?Zp' <<< '345'
3
-123
as input.
\$\endgroup\$
print len(`abs(input())`)
works for both positive and negative integers
echo $n<0?(strlen($n)-1):strlen($n)
(without php opening tags <?php
& ?>
)
Pretty simple, ints can be interpreted as strings, therefore $n<0
has a minus-char that should be removed in the true-statement of the ternary operator. If its positive then we just count the length ;)
<?
) so just prepend that to your code and you'll be good.
\$\endgroup\$
i->(""+(i<0?-i:i)).length();
public static void main(String[] args) {
IntFunction<Integer> func = i -> ("" + (i < 0 ? -i : i)).length();
System.out.println(func.apply(12345)); //5
}
#.~f`+#@
Multiline support (same as above, but print count at byte <15 and reset counter)
#@~f`:2*j\.+
~$1+2j@.
makes more sense for single number.
\$\endgroup\$
2j
, I usually forget about that, but you're right that it's prettier that way.
\$\endgroup\$
main(k){scanf("%i",&k);printf("%i\n",(int)log10(abs(k))+1);}
main(k)
\$\endgroup\$
A DELPHI PROGRAM FOR CONSOLE
program P;
{$APPTYPE CONSOLE}
uses
System.SysUtils;
var a: Integer;
begin
readln(a);
writeln(length(IntToStr(a)));
end.
Ruby
$*.first.size
or
ARGV.first.size
$*[0].size
? (Aside from the negative integers issue.)
\$\endgroup\$
Jan 29, 2014 at 4:21
package main;import."fmt";func main(){s:="";Scan(&s);Print(len(s))}
lambda a:len(`abs(a)`)
I actually built this myself, before seeing Wasi's answer! This is how I thought of it:
lambda
.len
? False
! What about n<0
? Should the -
be counted? False
!-
in just a few bytes? abs
![Thinking ends]
f:{#$x}
Example:
f 12346
5
f 456
3
Though only 2 characters (#$) are required to find the length of the input number.
#$123444
6
convert the input to string.
$123456
"123456"
count the number of characters:
#$123456
6
Q equivalent of this code:
f:{count string x}
f 123456
6
alert((""+n).length)
alert()
?
\$\endgroup\$
Jan 26, 2014 at 16:37
return
but forgot that return
has one more character than alert
.
\$\endgroup\$
Jan 26, 2014 at 16:41
The solution at 18 characters (identical in concept to the python solution of Wasi):
nchar(abs(scan()))
And the funnier one at 33 characters:
which(!abs(scan())%/%10^(1:9))[1]
Assuming value is already stored:
l?_
To make user input:
`_l?_
Newline support with interactive input:
[`_~l?_]
Assuming input via standard input:
grep -o [0-9]|wc -l
Multi-line version - 48 characters:
while read L;do echo $L|grep -o [0-9]|wc -l;done
$_=()=$_=~/\d/g
$_=/^-?(\d+)$/?($1<=10**9?length($1)."\n":$_):$_
NB. If the input ($_
) is an integer (positive or negative) and comes under the constraint then it amends $_
to the number of digits. Otherwise it just leaves the input as is.
Usage example:
perl -ple '... insert above code...'
ρ'n'
The first symbol, rho, checks the number of cells in an array, where one character of a string is equal to one cell.
It's slightly longer than the other C solution, but it works on arbitrarily large numbers (numbers with 2^32 - 1
digits rather than numbers up to 2^32 - 1
).
EDIT: It's now shorter than the other C solution!
c;main(n){n-10?main(getchar(),c+=n>47):printf("%d\n",c);}
Note: if you call the program with exactly 9 arguments, it will fail. But why would you do that??
($n-replace"-",'').length
where $n is a number
$x="$n".length;if($n-ge0){$x}else{$x-1}
\$\endgroup\$
'-'
instead of "\D"
.
\$\endgroup\$
þg
Try it online or verify all test cases.
Explanation:
þ # Take the digits of the input
g # And output the length
idh\-=[tLe]L
Gets input from command line arguments. If the first character of input is "-", push the length of the all but first items of the string and immediately end the program. Else, push the length of the string. Then implicitly print.
Does not work with multiline.
i push input
d duplicate
h first character of
\- push the character "-"
= if top two values are equal, push 1, if not, 0
[ if top value is truthy (not [] or 0), go into the block. if not, go past the end of the block.
t push the all but first items of the stack's top value (literally s[1:] in python)
L length
e end the program
] end loop
L length
n
will be negative, since "n<=10^9
". Just sayin'. ;-) \$\endgroup\$00501
return3
or5
? \$\endgroup\$