I advise you to add āerror printsā to your algorithm to know the values of CT and p for example. Youāll quickly see the mistake in your code. Good luck!
Thanks! That did it. My code has a section that tests if the input is numeric, and sets the output to zero if thatās the case. The initial input values are string values, not integers. I ran intval() on the input value first, and it works now.
I am stuckā¦ I donāt know where is the bug.Can anyone help me out?
/**
* 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
var inputs = readline().split(' ');
var arr=[];
for (let i = 0; i < n; i++) {
var t = parseInt(inputs[i]); // a temperature expressed as an integer ranging from -273 to 5526
if(t<0){return 1}
arr.push(t);
}
var closeToZero=Math.min(...arr);
// if(closeToZero<0){return 1}
// Write an action using print()
// To debug: printErr('Debug messages...');
print('closeToZero');
I will revisit this later in the day that i am writing this but some tips or subtle hints would be greatly appreciated. This is logic that i have in the standard loop for the problem.
if(i >= 1){
if(Math.abs(pastNum) < Math.abs(t)){
result = String.valueOf(pastNum);
}
else if (Math.abs(t) < Math.abs(pastNum)) {
result = String.valueOf(t);
}
else if (Math.abs(pastNum) == Math.abs(t)){
if(pastNum > 0){
result = String.valueOf(pastNum);
}
else if (t > 0){
result = String.valueOf(t);
}
else{
result = String.valueOf(t);
}
}
}
else{
pastNum = t;
}
So guys, Iām a total noob and trying to solve thisā¦ Iāve managed to get through most of the tests with the code I wrote (though itās probably not the most convenient way) But I donāt see how I can pick the positive temperature instead of the negative one, any help on this?
my code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
int main()
{
int zero = 0;
int result = 0 ;
int n; // the number of temperatures to analyse
cin >> n; cin.ignore();
//loop temperatures on row
for (int i = 0; i < n; i++) {
int t; // a temperature expressed as an integer ranging from -273 to 5526
cin >> t; cin.ignore();
if(i == 0 || (t < zero && ((zero - result) > (zero - t))) || ((t > zero && (zero + result) > (zero + t)))) {
result = t;}
}
// Write an action using cout. DON'T FORGET THE "<< endl"
// To debug: cerr << "Debug messages..." << endl;
cout << result << endl;
Many thanks, did not know about the sqrt() function. Had to include the <math.h> library though. Iāve changed my if statement to the following and now itās working!
if(sqrt(t*t) == sqrt(result*result)){
result = sqrt(result*result);
}
else if(i == 0 || (t < zero && ((zero - result) > (zero - t))) || ((t > zero && (zero + result) > (zero + t)))) {
result = t;}
Thanks, did not know about that yet. I now managed to get the abs() function in, instead of calculating the absolute integer myself. However, many of the functions I see in the solutions I havenāt familiarized myself yet, so I guess Iāll have to learn a little more before I can fully understand them, thanks again!
I havenāt been able to do any of the test case with any of the codes I have written. It always returns the furthest number. This is what I have.
import sys
import math
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
n = int(input()) # the number of temperatures to analyse
# print(type(input())) # String
if n == 0:
closest=0
else:
for i in (input().split()):
# t: a temperature expressed as an integer ranging from -273 to 5526
t = int(i)
closest = 5527
if abs(t)<int(closest):
closest = t
# Write an action using print
# print("Debug messages...", file=sys.stderr)
print(closest)
My code works well on any other website, but itās not working on CodinGame. It works for 1,2,4, but it tells me it returns ā12ā for #3 and ā-4ā for #5. Any thoughts?
My code:
n = int(input()) # the number of temperatures to analyse
templist = {}
realtemp = []
abstemp = []
newlist =[]
for i in input().split():
# t: a temperature expressed as an integer ranging from -273 to 5526
a = int(i) # log the
t = (abs(int(i)))
realtemp.append(a)
abstemp.append(t)
def dedupe(list):
for m in list:
if (list.count(m)+list.count(-m))>1:
list[m]=abs(m)
return list
newtemp = dedupe(realtemp)
templist= dict(zip(newtemp,abstemp))
answer = min(templist, key=templist.get)
it seems you just need to implement the following rule to validate all test cases:
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).
does any one know why (b) keep sending 0 as output ?
where and how to declare b to prevent this from happening?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
int main()
{
int n; // the number of temperatures to analyse
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int t;
// a temperature expressed as an integer ranging from -273 to 5526
scanf("%d",&t);
printf("t=%d\n",t);// just for debugging
if (abs(t)<b){b=t;}
printf("b=%d\n",b);
if (n==i) {printf("%d\n",b);}
}
// Write an action using printf(). DON'T FORGET THE TRAILING \n
// To debug: fprintf(stderr, "Debug messages...\n");
//printf("%d\n",b);
return 0;
}
There is an error in the description or in the testcases in one of the games. You should accept both values that are same distance from the 0. There is no clarification that in case of two values being same distant from 0 we choose positive one. Not a valid testcase or not valid description.
You complicate too much just append them to one array and do one pass of the values with checking for value. You have constrains for the input just init to value like 10000.
result = cur = 10000
for c in tmp:
if abs(c) < cur:
cur = abs(c)
result = c