55
\$\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\$
7
  • 1
    \$\begingroup\$ You should probably put a link to the TNB CMC, since that's where this challenge came from. \$\endgroup\$
    – mbomb007
    Commented May 3, 2017 at 20:53
  • \$\begingroup\$ Does the answer need to be a full function, or can it be the body? \$\endgroup\$ Commented May 3, 2017 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
    Commented May 4, 2017 at 1:56
  • \$\begingroup\$ Can we print the output with a leading zero? \$\endgroup\$
    – MD XF
    Commented Dec 26, 2017 at 21:54
  • \$\begingroup\$ @MDXF yes, you can. \$\endgroup\$
    – Leaky Nun
    Commented Dec 26, 2017 at 22:30

160 Answers 160

1 2 3 4 5
6
0
\$\begingroup\$

Pyt, 3 bytes

1←↑

Try it online!

1       push 1
 ←      get input
  ↑     get maximum value on the stack; implicit print
\$\endgroup\$
0
\$\begingroup\$

Python, 15 Bytes

lambda x:x or 1

Thanks for choosing a falsey value to avoid!

Try it online!

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

Desmoslang, 14 13 Bytes

IS+1-\sgn(DOT

Old:

ISE\{D=0,D\}OT
\$\endgroup\$
0
\$\begingroup\$

Lua, 24 bytes

print(0<0+...and...or 1)

Try it online!

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

Python 3, 21 bytes

lambda n:{n:n,0:1}[n]

Try it online!

Also note that {0:1,n:n} doesn't work because if n is 0 the n:n overrides 0:1

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

Gema, 5 characters

\A0=1

Sample run:

bash-5.2$ gema '\A0=1' <<< '0'
1

bash-5.2$ gema '\A0=1' <<< '10'
10

Try it online! / Try all test cases online!

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

Swift, 19 18 17 bytes

var f={max(1,$0)}

Taking advantage of the "non-negative" rule here.

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

Easyfuck, 5 bytes

Solution:

+$":'

Explanation:

+ increments the cell at the pointer
$ copies cell at the pointer into storage
" takes in an 8 bit integer
: puts max(current cell, storage) into the current cell
' outputs an 8 bit integer
\$\endgroup\$
-1
\$\begingroup\$

Excel VBA 17 bytes

[a1]=IIF(n=0,1,n)

Simple if else statement you can run it from vba immediate window

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Hey, just a note on convention with VBA and Excel VBA for PPCG - you must take input from either a cell or from a passed variable. You can output to the immediate window, msgbox, function value, to a cell or by modifying a passed variable. This means that your above solution is not valid as it does not take input from cell or a passed variable, but rather relies on the variable being passed to it. \$\endgroup\$ Commented Jun 14, 2017 at 20:29
  • 5
    \$\begingroup\$ Save two bytes and make it a valid answer with ?-([A1]=0)+[A1] \$\endgroup\$ Commented Aug 7, 2017 at 14:00
-1
\$\begingroup\$

JS ES5, 26 bytes

function (n){return n?n:1}
\$\endgroup\$
3
  • \$\begingroup\$ Save one byte by removing the space after function. \$\endgroup\$
    – Adalynn
    Commented Aug 6, 2017 at 0:30
  • \$\begingroup\$ n||1 \$\endgroup\$
    – l4m2
    Commented Dec 18, 2017 at 20:42
  • \$\begingroup\$ I think you can also write this as function(n)n||1 (taking the n||1 idea from l4m2) \$\endgroup\$
    – user41805
    Commented Jan 5, 2018 at 15:15
1 2 3 4 5
6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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