3

What would be a way to write a function that given any positive number the function would return one, but for a negative number or zero it would return zero.

Also what mental process should I go through to approach such a problem?

For example:

f(45) = 1;

f(10) = 1;

f(1) = 1;

f(0) = 0;

f(-3) = 0;

2 Answers2

3

It would be piecewise function like this $$f(n) = \begin{cases} 1, & \text{if $n > 0$} \\ 0, & \text{if $n \le 0$ } \end{cases}$$

2

$$f(x)=\lfloor\frac{\text{sgn}(x)+1}{2}\rfloor$$

msm
  • 7,147