0
\$\begingroup\$

Description :

You are given a string of numbers (positive and negative but not decimals) along with the number of entries like so.

Like so

8 '1 2 5 0 7 -9 12 -8'

Task :

Your job is to return the number closest to zero be it +ve or -ve. Your input will be separated by spaces only. You are free to return the answer in any way you like namely :

  1. A string
  2. A float (Why would you though)
  3. An array (One digit array I guess)
  4. A number (preferred)

Since this is code golf the answer with the shortest will be the winner. I guess?

Have fun.

code snippets :

console.log(`6
'1 -2 -8 4 3 5'`) // should return 1
console.log(`3
'-12 -5 -137'`) // should return -3
console.log(`8 
'1 2 5 0 7 -9 12 -8'`) // should return 0

Note :

This is my first attempt at this. Apologies in advance for any mistakes, do tell so I can correct them. Thanks

\$\endgroup\$
  • \$\begingroup\$ 1). updated my bad. \$\endgroup\$ – Muhammad Salman Apr 12 '18 at 12:15
  • \$\begingroup\$ 2). Ok updated. I was not aware that is is a duplicate. \$\endgroup\$ – Muhammad Salman Apr 12 '18 at 12:15
  • \$\begingroup\$ Can we take the input as a list of numbers? \$\endgroup\$ – Adám Apr 12 '18 at 12:16
  • \$\begingroup\$ Welcome to the site! As user202729 has mentioned, this is almost an exact duplicate of this existing challenge. Be sure to use the Sandbox to check if your challenges are duplicates and to catch any errors before posting. Also, make sure to read Things to avoid when writing challenges to see what to avoid and what to include. \$\endgroup\$ – caird coinheringaahing Apr 12 '18 at 12:16
  • 3
    \$\begingroup\$ Sorry , just joined I was not aware of this. I will be sure to check before posting. Thanks for helping. \$\endgroup\$ – Muhammad Salman Apr 12 '18 at 12:20
0
\$\begingroup\$

Python 2, 41 bytes

lambda n:min(map(abs,map(int,n.split())))

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ oh i didn't see that this question had been closed while I was writing my answer.... \$\endgroup\$ – X1M4L Apr 12 '18 at 12:59
  • \$\begingroup\$ Thanks for the answer , but since this already had an answer it was closed. \$\endgroup\$ – Muhammad Salman Apr 12 '18 at 13:51

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