41
\$\begingroup\$

Task

Given a non-negative integer n, output 1 if n is 0, and output the value of n otherwise.

Input

A non-negative integer.

  • If you would like to accept the string as input, the string would match the following regex: /^(0|[1-9][0-9]*)$/, i.e. it must not have any leading zeroes, except when it is 0.
  • If you accept a real integer as input, you may assume that the integer is within the handling capability of the language.

Output

A positive integer, specified above. Leading zeroes are not allowed. Your output should match the regex /^[1-9][0-9]*$/.

Testcases

input output
    0      1
    1      1
    2      2
    3      3
    4      4
    5      5
    6      6
    7      7

Scoring

This is , so shortest answer in bytes wins.

Standard loopholes apply.

\$\endgroup\$
  • 1
    \$\begingroup\$ You should probably put a link to the TNB CMC, since that's where this challenge came from. \$\endgroup\$ – mbomb007 May 3 '17 at 20:53
  • \$\begingroup\$ Does the answer need to be a full function, or can it be the body? \$\endgroup\$ – Caleb Kleveter May 3 '17 at 20:58
  • 1
    \$\begingroup\$ @CalebKleveter The default rule in PPCG is that the answer is either a function or a full program, but not snippets. \$\endgroup\$ – Leaky Nun May 4 '17 at 1:56
  • \$\begingroup\$ Can we print the output with a leading zero? \$\endgroup\$ – MD XF Dec 26 '17 at 21:54
  • \$\begingroup\$ @MDXF yes, you can. \$\endgroup\$ – Leaky Nun Dec 26 '17 at 22:30

119 Answers 119

1
\$\begingroup\$

C#, 11 bytes

n=>n<1?1:n;

This compiles to a Func<int, int>.

\$\endgroup\$
1
\$\begingroup\$

Starry, 14 bytes

, +'      +*`.

Try it online!

Explanation

Space shown as _ .

,           Read integer and push to stack
_+          Duplicate
'           If non-zero jump to branch label
______+     Push 1
*           Add
`           Mark branch label
.           Print as a number
\$\endgroup\$
  • \$\begingroup\$ My first Starry answer \$\endgroup\$ – Luis Mendo May 2 '17 at 23:10
1
\$\begingroup\$

Clojure, 12 bytes

#(get[1]% %)

The get function takes an associative data structure, a key, and a default value. Vectors are associative using sequential position as the key. So, [1] is a vector with the value 1 at position 0. If get is called with parameter 0 it will return 1, otherwise no other keys exist in the vector so it returns the default value of the parameter.

\$\endgroup\$
1
\$\begingroup\$

Desmos, 13 bytes

f(x)=max(x,1)

Try it here

\$\endgroup\$
  • \$\begingroup\$ Desmos tips: you can have lists of integers like this. \$\endgroup\$ – Leaky Nun May 4 '17 at 1:58
1
\$\begingroup\$

Clojure, 10 bytes

#(max 1%)

Not much to explain.

\$\endgroup\$
  • \$\begingroup\$ I think you can shorten this to #(max 1%) (9 bytes). tio.run/nexus/… \$\endgroup\$ – Dennis May 4 '17 at 14:37
1
\$\begingroup\$

Julia, 13 12 bytes

f(n)=n<1?1:n

Unfortunately, Julia doesn't do implicit casting from int to bool, so I have to burn an entire 3 characters just to do a comparison to zero. Saved one byte by safely assuming the number isn't negative. Still too verbose for my taste, though.

\$\endgroup\$
1
\$\begingroup\$

Groovy, 7 bytes

{it?:1}

Elvis operator; if true, return self, else return 1. Only false integer value auto-unboxxed to false in Groovy is 0. Thusly, exactly the spec.

\$\endgroup\$
1
\$\begingroup\$

Symbolic Python, 11 bytes

_+=_==(_>_)

Try it online!

_+=           # Output = Input +
   _==(_>_)                       Input == 0
\$\endgroup\$
1
\$\begingroup\$

Google Sheets, 9 Bytes

Anonymous worksheet function that takes input from range A1 and outputs to the calling cell

=Max(1,A1
\$\endgroup\$
  • \$\begingroup\$ This look very cool mate :) \$\endgroup\$ – NTCG Jan 4 '18 at 8:07
1
\$\begingroup\$

Bash, 43 bytes

if [ $1 -eq 0 ];then echo 1;else echo $1;fi
\$\endgroup\$
1
\$\begingroup\$

Python 21 Bytes

int(max('1',input()))

Takes input from REPL environment

\$\endgroup\$
1
\$\begingroup\$

Aceto, 9 8 bytes

rid0=`1p

read an integer and duplicate it, then push 0. Are they =? Then (`) push a 1. print the top element.

\$\endgroup\$
1
\$\begingroup\$

PowerShell, 24 22 17 Bytes

blatantly stolen from here

!($a=$args[0])+$a

Explanation

Invert the value, returning 0 for non-0 numbers, and 1 for 0, then add the intitial to it.

this makes it basically 1/0 + value, so for 0 the first value is 1, any other numbers it's 0.

examples:
# !0+0 = 1
# !1+1 = 1
# !9+9 = 9
\$\endgroup\$
  • 1
    \$\begingroup\$ Could you instead use !$a as your index check? You wouldn't be able to set $a in your current manner, but I think something like this would work? Try it online! \$\endgroup\$ – Sinusoid May 2 '17 at 21:13
  • 1
    \$\begingroup\$ @Sinusoid that worked perfectly for -2, but turns out the C answer was a bit ahead of us both, didn't think of the method at all. \$\endgroup\$ – colsw May 3 '17 at 9:46
  • \$\begingroup\$ That is certainly interesting! I will need to keep this method in mind :P \$\endgroup\$ – Sinusoid May 3 '17 at 13:21
1
\$\begingroup\$

Chip, 20 18 bytes

eaABb
*`\\-!
fcCDd

Try it online!

How?

 aABb
            Copy the low 4 bits from input to output
 cCDd

e
*           Set the higher bits of output, so that the values are ASCII digits
f

eaABb
*           Replicate any ASCII digits on input to output
fcCDd

            -
     !      Produce a high signal, but only during the first byte
            -

 aAB
 `\\-*      Set the lowest bit of output, if the four low bits of input are unset
  CD

 aAB        Set the lowest bit of output, if the four low bits
 `\\-!      of input are unset, and only on the first byte
  CD
\$\endgroup\$
1
\$\begingroup\$

Julia 0.6, 10 bytes

x->x<1?1:x

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ also 10 bytes: x->x+(x<1) \$\endgroup\$ – Giuseppe Jan 17 '18 at 19:03
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 49 47 36 bytes

	N =INPUT
	N =EQ(N) 1
	OUTPUT =N
END

Try it online!

\$\endgroup\$
1
\$\begingroup\$

><>, 6 bytes

:?!1n;

Could have been 1 byte shorter if ? had the opposite conditional behaviour.

:?       check if nonzero, then either
  !      a) skip the next instruction or
   1     b) push 1 to the stack
    n    print
     ;   terminate
\$\endgroup\$
  • \$\begingroup\$ Take the input via the -v flag instead. It saves a byte, and it's less suspect than using the char code of a character \$\endgroup\$ – Jo King Apr 13 '18 at 8:17
  • \$\begingroup\$ 5 bytes \$\endgroup\$ – Jo King Apr 13 '18 at 10:34
  • \$\begingroup\$ Terminating with an error wasn't legal when I used to be around. \$\endgroup\$ – Hohmannfan Apr 13 '18 at 12:14
1
\$\begingroup\$

JavaScript (Node.js), 10 bytes

n=>n&&n||1

Try it online!

here is one more

JavaScript (Node.js), 8 bytes

n=>n?n:1

Try it online!

and lastly

JavaScript (Node.js), 7 bytes

n=>n||1

Try it online!

\$\endgroup\$
  • \$\begingroup\$ From the BASIC answer n=>n+!n (also 7 bytes) should also work ;) \$\endgroup\$ – Shieru Asakoto Sep 6 at 5:39
1
\$\begingroup\$

Gol><>, 5 bytes

I:z+h

Try it online!

Given n, calculate n + !n, print as int and halt. Unfortunately Gol><> doesn't have implicit input option, so the bytes are the same as regular ><>.

\$\endgroup\$
1
\$\begingroup\$

Sed, 8 bytes

Input in any number base, without any leading zeros. (It doesn't even need to be a consistent base!)

s/^0/1/

0 is the only number that begins with 0; just change that to 1.

\$\endgroup\$
1
\$\begingroup\$

Acc!!, 75 72 bytes

N
Count i while 0^(_%12) {
Write 49
1
}
Count i while _/48 {
Write _
N
}

Try it online!

Explanation

The problem can be described as:

  • If the first character read is 0, output 1 and halt.
  • Otherwise, read a string of digits and echo them to output.

N reads a character and stores its code into the accumulator. The first loop triggers if the character read was 0 (i.e. ASCII 48):

  • If _ == 48, then (_%12) is 0, and 0^0 is 1, which is truthy.
  • If 49 <= _ <= 57, then (_%12) is between 1 and 9, and 0^(_%12) is 0, which is falsey.

Inside the first loop, we write a 1 (ASCII 49) and set the accumulator to 1. This breaks out of the loop because:

  • If _ == 1, then (_%12) is 1, and 0^1 is 0, which is falsey.

So when we reach the second loop, the accumulator is either 1 or some number between 49 and 57 (inclusive). The condition this time is _/48, which is truthy for values >= 48. So if the accumulator is 1 (meaning that we went into the first loop), we skip the second loop. Otherwise, we enter it. We write whatever's in the accumulator back to output and read another character, until the ASCII code we read is less than 48 (probably 10 for newline). Then we halt.

\$\endgroup\$
1
\$\begingroup\$

BitCycle, 8 7 bytes

-1 byte thanks to Jo King

?v<
!+~

Try it online!

Uses the -U flag to convert decimal inputs to "signed unary," which in this case means unary for positive integers and 0 for zero.

How it works

Let's run two example inputs: 3 and 0.

An input of 3 gets converted to 111 and emerges from the source (?) moving east. It follows the arrow down to the +, where the 1 bits turn right (west). They fall into the sink (!) and are converted back to decimal 3 and output.

An input of 0 gets converted to 0. When the 0 bit reaches the +, it turns left into the dupneg (~). Here the 0 turns right (south, off the playfield) and a negated copy of it turns left (north). This negated copy, being a 1 bit, goes back around to the + and turns right. It falls into the sink, is converted to decimal 1, and is output.

\$\endgroup\$
0
\$\begingroup\$

CJam, 5 bytes

ri1e>

Try it online!

Explanation

ri   e# Read input as an integer
1    e# Push 1
e>   e# Maximum. Implictly display
\$\endgroup\$
0
\$\begingroup\$

Batch, 17 bytes

@cmd/cset/a%1+!%1
\$\endgroup\$
0
\$\begingroup\$

Math++, 3 bytes

?|1

("Body must be at least 30 characters")

\$\endgroup\$
0
\$\begingroup\$

Microscript II, 4 bytes

1sN|

Rough translation:

x=1
push x
x=readnum()
x|=pop()

x is then printed implicitly at the end.

\$\endgroup\$
0
\$\begingroup\$

braingasm, 4 bytes

;z+:

; reads a number from stdin, z+ increments that number if it is 0, : prints.

\$\endgroup\$
0
\$\begingroup\$

Ruby, 12 bytes

->n{n<1?1:n}

Try it online!


Alternate version that reads directly from STDIN instead of being a function. Same number of bytes after counting the -p flag (11+1 = 12 bytes). Effectively a port of the Retina solution.

sub /^0/,?1

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Nice, I wanted to post ->x{[x,1].max}, but your solution is 2 bytes shorter. \$\endgroup\$ – Eric Duminil May 3 '17 at 10:10
0
\$\begingroup\$

Beam, 40 bytes

This reads the input as a string as it doesn't have any other option. Basically it checks if the first character is 0 by removing 48 from it. If 0 increment and output as a number, otherwise output it as a number, then read the rest of the input outputting it as characters.

 r'''''>`----\
 :+n++(------/
/<:<
r@
\u

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Befunge-98, 5 bytes

&:!+.

Try it Online! (Warning - will take 60 seconds per test because of how TIO treats &)

Explanation

&:       Push 2 copies of the input
  !      Logical not the 1st copy - i.e. push 1 if 0, push 0 otherwise
   +     Add them together - this results in +0 normally, but +1 in the case of 0
    .    Print that value
         Loops back around to the first command
&        Because there's no more input, the TIO interpreter stalls for a minute and ends.

Befunge-93 Variant, 6 bytes

It's trivial to implement Befunge-93 in a similar way, except we can't use the & to end the program this time. Thus, the code is 1 byte longer, for 6 bytes:

&:!+.@

Try it Online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.