Having a difficult time trying to get this solution to work, can anyone point me in the correct direction?
import java.util.*;
import java.io.*;
import java.math.*;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int result = 0;
int n = in.nextInt(); // the number of temperatures to analyse
int[] array = new int[n]; //new int[n];
if (n <= 0) {
result = 0;
}
for (int i = 0; i < n; i++) {
int t = in.nextInt(); // a temperature expressed as an integer ranging from -273 to 5526
array[i] = t;
}
for (int j = 0; j < array.length; j++) {
result = array[0];
if (Math.abs(array[j]) < Math.abs(result)) {
result = array[j];
}
if (Math.abs(array[j]) == Math.abs(result) && array[j] > 0) {
result = array[j];
}
//System.err.println("Debug messages..." + result);
}
// Write an answer using System.out.println()
System.out.println(result);
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int result = 0;
ArrayList<Integer> temperatures = new ArrayList<>();
if (n > 0 && n < 10000) {
result = in.nextInt();
for (int i = 0; i < n-1; i++) {
int t = in.nextInt();
temperatures.add(t);
if (t >= 0)
result = Math.min(t, result);
else
result = Math.max(t, result);
}
if (result < 0) {
for(int item : temperatures)
if (item == Math.abs(result))
result = item;
}
}
System.out.println(result);
}
n = int(input()) # the number of temperatures to analyse
temps = [int(i) for i in input().split()]
if len(temps)!=0:
abs_temps = [abs(i) for i in temps]
abs_minimum = min(abs_temps)
indices = [i for i,x in enumerate(abs_temps) if x == abs_minimum]
if len(indices)>1:
element_filter = [i for i in indices if temps[i]>0]
if not element_filter:
result = temps[indices[0]]
else:
result = temps[element_filter[0]]
else:
result = temps[indices[0]]
else:
result = 0
# Write an answer using print
# To debug: print("Debug messages...", file=sys.stderr, flush=True)
print(result)
My solution feels messy with multiple branches. Is there a better solution without using numpy?
Instead of reading all the temperatures at one go and storing them in a list, you may read the temperatures one by one and update your answer as required. You may try using no lists at all.
If you use a list you can also use the min function with a custom sorting key.
I give you an example.
Say you have a list of (r, g, b) color tuples.
You want the one that maximizes the sum r+g+b and, as a tie breaker, you want to favor those where r > g.
Youâd write something like that: max(colors, key=lambda c: (sum(c), c[0] > c[1]))
Auto-generated code below aims at helping you parse
the standard input according to the problem statement.
**/
int main()
{// a temperature expressed as an integer ranging from -273 to 5526
// the number of temperatures to analyse
int n,i,t[i],ans=5527,ab,def=0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
// a temperature expressed as an integer ranging from -273 to 5526
scanf("%d", &t[i]);
if(t[i]<ans)
{
ans=t[i];
}
else if(abs(t[i])==abs(ans) && t>ans)
{
ans=t[i];
}
}
if(n==0)
{
t[i]=0;
}
I only passed the first testcase. Also, i noticed that the if condition (t[i]<ans) can be also t[i]> ans or t[i]>5000 and it still passes the first test case. I do not know why is that. sorry still a beginner
Before pointing out the issues in your code, please note that you may format your code properly in this forum by using the </> button in the formatting toolbar.
Now, the issues in your code:
You have initialised t[i]. But you should have waited until you know the number of temperatures n (after the first scanf). Then you can initialise t[n] (not t[i]) properly.
In your first if, you are comparing t[i] with ans, but you should be comparing their absolute values instead.
In your else if, you are comparing t and ans, but you should be comparing t[i] and ans instead.
In your last if, you assign 0 to t[i]. You want to assign it to ans instead, right?
Instead of just fixing the code, please make sure you understand why the code should be fixed as described Keep it up.
If youâre a beginner and havenât learned programming before, this website can prove too difficult for you, as a certain level of programming knowledge is a pre-requisite to solve the puzzles here. You may learn it elsewhere before coming back here to practise, though. Alternatively, you may google the relevant aspects while you try to solve the puzzles here.
If youâre just confused about how the puzzles on this website work, you may take a look at the explanation here: https://www.codingame.com/faq
I donât know what numbers that validator contains, but does your code work correctly if -273 is the only number given, or is one of the given numbers?