Temperatures puzzle discussion

#756

The issue is that input() read the line …
`num = int(input())` => num = `int('1 -2 -8 4 5')`
It is not correct …

What you can do is getting the values as string
`nums = input().split()`
then you can loop to use these but the base code generated by CG already do this for you :
`for i in input().split():`

No need to reinvent the wheel

#757

I don’t understand why on Javascript I failed this…

Two negative temperatures that are equal: {-10 -10}

Yet passed everything else with a 90%. Not sure how to ask for help since I can’t post code. Yes I understand that the answer itself is -10. What I don’t understand is

``````} else if (Math.abs(t) === Math.abs(x)) {
x = Math.abs(t);
}
``````

Fails that?

#758

If you use x as an answer, it will always be positive.
Your test works for {10 10} or {-10 10}, not for {-10 -10}.

#761

Hi! Im doing this puzzle in C and was wondering if anyone could offer some advice. My code works fine except that it cant detect when no tempertures were given. I’ve tried checking for EOF and NULL but i’m not really sure what to do in this situation.

#763

The puzzle gives you the number of temperatures you’re supposed to read as input in the variable `n` I think.

#764

hello, i’m a beginner in python (literaly just started with python 3) and i can’t figure out how to code this puzzle. i tried implimenting a list to my temperatures but that turned out to be a terrible idea.

``````n = int(input())

my_temps=[]
for i in input().split():
t=int(i)
my_temps.append(t)

result=my_temps[0]
if n==0:
result=0
else:
for i in range(len(my_temps)-1):
if abs(my_temps[i])<abs(result):
result=my_temps[i]

print(result)
``````

#765

At first, you don’t need “-1” in range() argument as long as upper bound not included in resulting sequence. Also, you can simply use “n” instead of len(my_temps)

And about logic - you need to read puzzle description carefully -> " If two numbers are equally close to zero, positive integer has to be considered closest to zero (for instance, if the temperatures are -5 and 5, then display 5)."

#766

I tried using n as you said but it didn’t quite work for me, probably due to a mistake i made. If not, the logic as you say is quite simple to understand but coding it is not as easy.

#767

Hello! I’ve used your code and modified it so it could pass all the validators after submitting

admin EDIT: please don’t share the solution of puzzles in the forum.

#768

I’m 8 days late to the party but don’t forget that if n=0, there will be no inputs to read on the next line.
input().split becomes problematic at that point.

#769

My JavaScript passes all tests except the complex test case. But when I test my code in the console in Chrome Dev tools and pass in the complex test case values, it returns the correct value.

Any ideas?

#770

And… I figured it out. In my code, I needed to test for equality in absolute values before testing for larger absolute value.

#771

hi in testcase 6 .
How to find if no temparatures are given or not in java

#772

aren’t you given the number of temperatures to read in input in variable N?

#774

Why can’t I use Math.abs in c#?

#775

You can but but it is not javascript …
In C# the method is Math.Abs()

#776

I think you should include test cases with few equal negative numbers.
I was able to pass all tests with wrong code

``````/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/

const n = parseInt(readline()); // the number of temperatures to analyse
if (!n) {
console.log(0);
return;
}

// My test case that is not covered
// const inputs = [-5, -5, 9, 8];
// console.error(inputs);

let result = 9999;

for (let i = 0; i < n; i++) {
const t = parseInt(inputs[i]); // a temperature expressed as an integer ranging from -273 to 5526

// Wrong code that works with all tests
// if (Math.abs(result) === Math.abs(t)) {

if (Math.abs(result) === Math.abs(t) && result !== t) {  // Right code that also works =)
result = Math.abs(t);
}

if (Math.abs(result) > Math.abs(t)) {
result = t;
}
}

// Write an action using console.log()
// To debug: console.error('Debug messages...');

console.log(result);``````

#777

Ok, so this is the solution I did in C++. It passed with 100% validation but I think there are some flaws in this, but if you’re too lazy just copy and paste it, lol.

If you didnt understand the “closest0”, it just holds the smallest value it’s given.

```````for (int i = 0; i < n; i++) {
int t; // a temperature expressed as an integer ranging from -273 to 5526
cin >> t; cin.ignore();
temp[i] = t;
closest0 = temp[0];
}
for (int j = 0; j < n; j++)
{
if(temp[j] >= 0 && temp[j] < closest0)
{
closest0 = temp[j];
}
else if(temp[j] < 0 && temp[j] > closest0)
{
closest0 = temp[j];
}
else if(closest0 < 0 && temp[j] >= 0)
{
closest0 *= -1;
}
}```````

#778

i tried the below logic which is the same one as the one in this discussion forum but it is failing on the first test case
Code:
t = int(i)
if math.fabs(t) < math.fabs(result) :
result = t
elif ( math.fabs(t) == math.fabs(result) ) :
if t > result : # found same value, but positive