My solution passes all the tests but when I submit, the score computer seems to freeze.
We had an issue yesterday with the submits. Itās solved.
What are the testcases 7 and 8? they are not shown in the list of cases to execute. it ends at 6.
results panel yes, and there I was surprised to learn I failed test 7. The test cases panel only goes to 6.
yes, there are two additional validators compared to the tests. Their names should be self-explanatory in any case you fail them.
I struggle with getting a BASH version work (I am completely incompetent in bash).
In the main loop I try to get abs value by this:
t=${myArray[$((i))]}
ta=${t#-}
tc=${closest#-}
Then trying to check if a new best solution found by this:
if [ $ta -lt $tc ] || [ [ $ta -eq $tc ] && [ $t -ge 0 ] ]
then
closest=$t
fi;
However the if statement is invalid. How to put it right? Thanks.
okay, just a little web search solved it for me: replacing and -gt with the (( )) syntax.
if (( ( $ta < $tc ) || ( ( $ta == $tc ) && ( $t >= 0 ) ) ))
If you go for double-parenthesis syntax, you donāt need the dollar signs anymore.
Single-bracket syntax is IMHO to be avoided completely, but thereās little reason you shoudnāt have found any luck with double brackets. What was the actual problem with your statement?
It was syntax errorā¦
if [ $ta -lt $tc ] || [ [ $ta -eq $tc ] && [ $t -ge 0 ] ]
Single-bracket conditionals are a builtin in bash, but they strive to maintain compatibility with old-style conditionals using external command test. As such:
- they donāt nest (at least, not directly)
- they need a lot of escaping
Making your example to pass would need one of these:
- closest to your example:
[ $ta -lt $tc ] || [ \( $ta -eq $tc \) -a \( $t -ge 0 \) ]
Notice how the inner tests use parentheses, and they have to be escaped to be interpreted by[notbash. (theyāre actually not needed, itās just to stick to your template) &&and||have equal precedence inbash, and have a behavior equivalent to left-associative short-circuit, so in this case you actually donāt need any parentheses:
[ $ta -lt $tc ] || [ $ta -eq $tc ] && [ $t -ge 0 ]- or in a single expression:
test \( $ta -lt $tc \) -o \( \( $ta -eq $tc \) -a \( $t -ge 0 \) \)
that simplifies to:
test $ta -lt $tc -o $ta -eq $tc -a $t -ge 0
It happens to be ok here because the parameters are defined and non-zero-length, but so many things can go wrong when passing arguments from bash to test that itās best practice to just always quote variables: "$t".
All of this has been obsolete for years: if you have [[ (you do), use it instead of [.
Thanks! By a single post you at least tripled my bash knowledge⦠![]()
It seems bash is a competent tool in the skilled hands, but that is definitely not mine⦠My only remaining ambition with bash is to⦠reach Legend in the next contest with it⦠ehmā¦correction⦠to cash in an extra achievement plus a hefty 25 XPs in the āHorse racingā duals easy training puzzle.
I am not sure if the provided input parser skeleton for OCaml is correct:
(* Auto-generated code below aims at helping you parse )
( the standard input according to the problem statement. )
let n = int_of_string (input_line stdin) in ( the number of temperatures to analyse )
let line = input_line stdin in
for i = 0 to n - 1 do
( t: a temperature expressed as an integer ranging from -273 to 5526 )
let t = Scanf.sscanf line ā%dā (fun t ā (t)) in
();
done;
( Write an action using print_endline )
( To debug: prerr_endline āDebug messageā; *)
print_endline āresultā;
If I add only this line after the ālet tā line, it always prints always prints the same first number from the input line:
prerr_endline(string_of_int t);
OCaml gurus: what do i miss?
The parser is indeed incorrect (sscanf always reads the first integer of the line).
Use something like that instead:
let n = int_of_string (input_line stdin) in
let temperatures = List.map int_of_string (String.split_on_char ' ' (input_line stdin)) in
List.iter (fun t -> prerr_endline (string_of_int t)) temperatures
Or alternatively (closer to the provided parser), replace sscanf by scanf (note the space in the format string to eat all whitespaces).
let n = int_of_string (input_line stdin) in
for i = 0 to n - 1 do
let t = Scanf.scanf " %d" (fun t -> t) in
prerr_endline (string_of_int t);
done;
Thanks, that solved it.
Bonjour,
cāest pas trĆØs propre ce que jāai Ć©crit , mais en testant mon code dans jsfiddle.net , jāobtient les bons rĆ©sultats mais Ƨa ne fonctionne pas sur codingame
Is that the same as writing an except block for a different error? Im still learnig code so not sure.
Itās difficult to help you if you donāt tell us whatās not working for you. ![]()
What have you tested? What is blocking you?
I am troubleshooting the following code for the Puzzle āTemperaturesā related to finding temperatures closest to zero. Programming in C, I have tested it dozens of times on three different IDEs (Code::Blocks, Dev-C++, and CLion), all of which work perfectly. However, when I run in the CodinGame IDE, it produces an error. Specifically, it sends the value 1 to stdout instead of the expected correct value closest to zero. The strategy I used was to place the scanf() temperatures into an array of size n, order them by the absolute value (abs()) of the number, and printf() the value in the first index of the ordered array. Some simple program control at the end deals with the case of selecting positive over negative values when their absolute values are equal (e.g., if selecting between -5 and 5, choose 5). Does CodinGame not accept arrays or pointers?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
// User defined function prototype
void swap(int *xp, int *yp);
int main()
{
int n; // the number of temperatures to analyse
int temp[n]; // Declare an array of size n
scanf("%d", &n); // System reads size
for (int i = 0; i < n; i++)
{
int t;
temp[i] = scanf("%d", &t); //<-- inputs scanf() into an array
}
int j, i, min_idx;
for ( i = 0; i < n-1; i++ )
{
min_idx = i;
for ( j = i+1; j < n; j++ )
{
if ( abs(temp[j]) < abs(temp[min_idx]) )
{
min_idx = j;
}
}
// User defined function to swap array elements
swap(&temp[min_idx], &temp[i]);
}
// Program control to deal with 5 or -5
if ( abs(temp[0]) == abs(temp[1]) && temp[1] > temp[0] )
{
printf("%d\n", temp[1]);
}
else
printf("%d\n", temp[0]);
return 0;
}
/*swap() -- user defined function that swaps values from an array. */
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
temp[i] = scanf("%d", &t);
scanf returns the number of items scanned, so you just fill your array with 1ās.
