4
\$\begingroup\$

In this exercise, you have to analyze records of temperature to find the closest to zero.

Write a program that prints the temperature closest to 0 among input data.

Input

  • N, the number of temperatures to analyse (optional). This will be nonzero.
  • The N temperatures expressed as integers ranging from -273 to 5526.

Output

Output the temperature closest to 0. If two temperatures are equally close, take the positive one. For instance, if the temperatures are -5 and 5, output 5.

Example

Input

    5
    1 -2 -8 4 5

Output

    1

This challenge is similar to this one on CodinGame, you can view the problem statement source here. Some modifications have been made to the text.

\$\endgroup\$
7
  • 22
    \$\begingroup\$ Mirror of CodinGame problem statement. The site might not appreciate having solutions be made publicly available. I'm not sure what our current consensus is on problems taken from elsewhere. The problem isn't copy-pasted but some of your text is taken verbatim. \$\endgroup\$
    – xnor
    Commented Mar 4, 2017 at 17:27
  • 4
  • \$\begingroup\$ I've edited in most of the original problem statement, it's probably best to include as much of the original text as possible. Also, if you could link directly to the problem (the interactive part), that would be great. \$\endgroup\$
    – Riker
    Commented Mar 4, 2017 at 19:11
  • \$\begingroup\$ @Riker that edit has changed the problem being asked. The input formats are stricter and there is a special case for no input \$\endgroup\$
    – Blue
    Commented Mar 4, 2017 at 19:13
  • 1
    \$\begingroup\$ I made the count input optional. \$\endgroup\$
    – xnor
    Commented Mar 4, 2017 at 22:20

37 Answers 37

1
2
0
\$\begingroup\$

dc, 53 bytes

Fd^sr[d*v]sA[ddlr+0=Asr]sR[dlAxlrlAx!<Rs_z0<M]dsMxlrp

Try it online!

We're going to use the register r to hold our final result, so the first thing we need to do is put a too-big number in there so that our first comparison always succeeds. I think the largest number we can push in two bytes is 165 (which is less than the required 5526), but Fd^ gives us 15^15 which is plenty large. Macro A, [d*v]sA just does the square root of the square to give us absolute value.

Macro R handles putting a value into register r; we'll come back to it in a second. Macro M, [dlAxlrlAx!<Rs_z0<M]dsMx is our main macro. Duplicates the value on the stack, runs A to get ABS, then pushes the value in r and does the same. !<R checks to see if ABS(r) is greater than or equal to not less than ABS(top of stack), and if so it runs R. Since we had to leave behind a copy of the original top of stack for R to potentially use, we store to a useless register (s_) to pop it. z0<M just loops M until the stack is empty.

Macro R, [ddlr+0=Asr]sR duplicates the top of stack twice (we need to leave something behind for M to gobble up when we return). At this point we know that of the two absolute values, our new value is less than or equal to our old value. If they're equal, and one is positive and the other is negative, we always need the positive. Fortunately, knowing this we can easily check for the case of one being the inverse of the other by simply adding them and comparing to 0. If so, we run A, and no matter what else happened we know we're storing whatever is left in r.

After M has gone through the whole stack, we load r and print it.

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

SmileBASIC, 56 bytes

DEF Z N,T
RSORT T
DIM V[N]ARYOP 2,V,T,T
SORT V,T?T[0]END

First, the input list is sorted, so the positive temperature will be chosen if there's a tie. Then each temperature is squared and stored in another array. The original array is sorted by this array, and the first item is printed.

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

Octave, 19 bytes

@(x)min(complex(x))

Try it online!

This exploits the fact that min with complex input compares by absolute value, then by angle:

For complex arguments, the magnitude of the elements are used for comparison. If the magnitudes are identical, then the results are ordered by phase angle in the range (-pi, pi].

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

C#, 171 bytes

using static System.Console;using System.Linq;class s{static void Main()=>Write(ReadLine()=="0"?0:ReadLine().Split(' ').Select(int.Parse).OrderBy(v=>v*(.1-v)).Last());}

Try it online!

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

Headass, 56 bytes

O{ON-)+E:U}.U+OUO[{UO(])P:N(+)+E:}.UO[U-O{UO(])P:N(+)E:}

Try It Online!

Increments / decrements a pair of counters until the value of the counter matches one of the inputs, with the positive counter having the first go (after 0 of course) to prioritize it over negative answers.

Positive counter hereby referred to as +counter and negative counter as -counter

Code breakdown:

O{ON-)+E:U}.  code block 0
O O           initialize both counters to 0
 {ON-)  :U}   put all inputs on the queue
      +E      then move to code block 1
           .  end code block

U+OUO[{UO(])P:N(+)+E:}.  code block 1 (-counter, starting at 0)
U+O                      increment +counter and put back on queue
   UO[                   store 0/-counter to r2 and put back on queue
      {       N(+)  :}   for each input
       UO                  enqueue input
         (])               if input is equal to 0/-counter
            P:               print value and halt
                  +E     then go to code block 2
                      .  end code block

UO[U-O{UO(])P:N(+)E:}    code block 2 (+counter, starting at 1)
UO[                      store +counter to r2 and put back on queue
   U-O                   decrement -counter and put back on queue
      {       N(+) :}    for each input
       UO                  enqueue input
         (])               if input is equal to +counter
            P:               print value and halt
                  E      then go to code block 1

I tried doing this with only 2 code blocks, 1 to init and one to handle both counters, but that meant that whichever value came first between positive and negative would win, which is wrong. There might be additional logic that could be added to such a loop which, combined with some more logic-golfing, could be shorter than this version, but i still think its pretty good.

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

J-uby, 12 bytes

:min_by+:abs

Attempt This Online!

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

APL (Dyalog Unicode), 16 bytes

{⌈/⍵/⍨a=⌊/a←|¨⍵}

Try it on TryAPL.org!

\$\endgroup\$
1
2

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.