Just implemented a solution in bash, it worked ok.
Then i tried
python -c "some one liner to solve this"
and it actually worked. Is this intended?
Just implemented a solution in bash, it worked ok.
Then i tried
python -c "some one liner to solve this"
and it actually worked. Is this intended?
Vraiment obliger Ă utiliser le bash -_-
Jâai tentĂ© tout ce qui me passait par la tĂȘte mais rien nây fait jâai soit un problĂšme de performance soit des erreurs que je ne comprends pas.
Quelquâun pourrait il mâĂ©clairer sur le sens de cette erreur :
/tmp/Answer.sh: line 19: minDistB = - : syntax error: operand expected (error token is "- ")at Answer.sh. on line 19
la ligne :
       ((minDistB = ${tab[${i}]}-${tab[${j}]} ))
Merci
Essaye de chercher un peu sur google, avec les termes: âsubstract shell scriptâ je suis tombĂ© sur ça et lĂ tu as plein dâinfos.
Bon courage pour finir le bash rider
Merci mais déjà lu
Si jâai postĂ©, câest que ca fait dĂ©jĂ bien plusieurs heures que je regarde.
Jâai fini par regarder les forum car je pensais avoir manquĂ© une idĂ©e dâalgorythme.
Je suis super déçu de me rendre compte que non il nây a pas dâidĂ©e nouvelle.
Juste Ă©crire soit mĂȘme un tri nâest pas performant ( au great je ne mây attendais pasâŠ)
Ca mâennerve assez de me prendre la tĂȘte sur un problĂšme liĂ© au langage
Je suis parti dans le man du sort mais sérieux ⊠pourquoi lié une récompense à un langage -_-
Surtout du bash quoiâŠ
Du coup je suis Ă la recherche de la fameuse page stackoverflow qui donne la solution pour le sort
Ou de tout exemple de comment utiliser la fonction sort sur un tableau
Merci dâavance
[EDIT] Bon ben finalement mĂȘme avec un tri fait par un sort, ca ne passe pas au niveau tempsâŠ
Je seche
mon tri :
readarray -t sorted < <(for a in â${tab[@]}â; do echo â$aâ; done | sort -n)
what is bash rider?
I was stuck on the Bash version for awhile. That is, the large data set problem was too slow. After much frustration, I was able to narrow down the problem. I hope this is a help to someone.
Array access can be very problematic in Bash, for a number of reasons. This seems to be especially true when using any kind arithmetic in your index. For example:
${val[$n-1]} seems perfectly reasonable. But hereâs the experience I had.
I rigged my test to always print out 47 at the bottom, then I started tweaking the loop variable to see how many pieces of data I could process and still get to the end before timing out. That is, instead of i < N, my loop was i < 10000. (N in this case is 99999) After some tinkering around, I found that my code could do about 36,000 iterations and still make it to the end in time. So I needed to cut that run time to about a third. (Mind you, this was the bash version of the same solution I had no trouble with in C++.)
I donât want to give anything away, so Iâm just going to give an abstract of a part of the solution in pseudo code.
for i < N, i++
{
read P
for j > 0, jâ
{
if(val[j-1] < whatever) etc
}
x = val[j-1] - y
}
As I said, I had to lower N to 36000 to get this code to work. Hereâs where things got weird. I tried removing the arithmetic from that last line, making it:
x = val[j] - y
This had no problem at all executing up to 99999. Simply changing that j-1 to j tripled my speed. (In case youâre wondering, this was all happening inside a condition wherein j was greater than 0.) That gave me the idea to try something a little odd:
jâ
x = val[j] - y
That gave me the decrement I wanted, and took the arithmetic out of the indexer. But still, the performance dropped back to the 36000 level. After many frustrated and convoluted attempts to make Bash do roughly the same thing, this is the solution that for whatever reason gave me the performance I needed:
for i < N, i++
{
read P
for j > 0, jâ
{
if(val[j-1] < whatever) etc
data = val[j-1]
}
x = data - y
}
So the moral of the story is:
I canât tell you why certain things worked and certain things didnât. Just be very careful how you access arrays in Bash, because theyâre in reality linked lists, and Bash can be weird about how it parses variables, since theyâre all in reality strings.
Câest assez malin ça
Par contre jâai dĂ» utilisĂ© tail pour que ça passe
whats the problem with my code
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
/**
Auto-generated code below aims at helping you parse
the standard input according to the problem statement.
**/
int main()
{int h=999;
int N=2;
int c=0;
int s;
for (int i = 0; i <N; i++) {
int Pi;
scanf("%d", &Pi);
if (i>0)
{
c=(Pi-s);
if(h >c){h=c;}
} s=Pi;
}
printf("%d \n",h );
//return 0;
}
In some tests, horses arenât sorted. You have to sort them before seeking the minimal difference.
Dire que câĂ©tait marquĂ© sous mon nez
Bon en revanche ca ne suffit pas de mon cĂŽtĂ© :â(
Est ce que let est plus lent que (( )) ?
Je ne sais pas trop comment ca fonctionne mais un jour il veut bien (()) un jour il ne veut plus. Aujourdâhui jâai du repasser avec let car plus moyen dâutiliser les parenthĂšse âŠ
Hi all !
Just for fun :
I struggled to get the last test case with the numerous horses in bash⊠I didnât understand why my algorithm was slow.
So, I tried something I never thought about before : if compilers like gcc and g++ were available from the bash interpreter, then all I had to do was to compile a C/C++ program and then run it.
So, I did something like this :
#!/bin/bash
read N
g++ -o horses -x c++ - <<CODE
/* Here document with your C++ code */
// âŠ
CODE
./horses
And the âAh momentâ ! It worked !!!
PS : I know itâs dirty. And admins may soon want to restrict the use of external programsâŠ
Hah, I had the exact same solution, except I used python instead of C++.
I donât see how they could restrict the use of external programs, as bash is basically 99% âexternal programsâ and is useless without them.
Did someone manage to do 100% in c++? Iâm stuck at 63% just because my code is âslowâ.
Djiiz method is not working for me.
Did someone manage to do 100% in c++?
Yes i did, and seriously? C++ has strong sorting function and is pretty fast most of the time. So if you canât manage it, then you donât have found the key to that puzzle
Keep trying
Are you trying to solve it in Bash or in âlegitâ C++? Because Djiiz method is a kind of hack for the Bash achievement.
@CvxFous: This kind of puzzle has a different timeout depending on the language. As PHP interpreter is written in C, standard functions like sorting are actually really fast too and gives you way more time for the rest of your algorithm than in C++.
Hello everybody.
I would try in âpureâ Bash at first, but on hudge datas number it was a shame of slow.
Thanks to Sparshong and Libe4 's ideas. I switched my mind, and also use the Bash best friends commands : sed, sort and awk.
I have to say that in this exercice I learned more about awk than i expected. Usually, i uesed awk to print some field, and thatâs all (usually, in my Bash script, i donât care about time speed ^^).
Becarefull, according to code in Awk, there is a fields size limit in tis Awk version (mine hasnât this).
I had to transform the string into an array.
Hello,
Jâai le mĂȘme problĂšme, as tu trouvĂ© pourquoi ce test ne passe pas ?
Merci.
Si on te donne un indice, câest presque la solutionâŠ
Mais tu fais une action deux fois, alors quâune seule suffirait.
hi every one
my code complete every cases except many horses⊠i use java and use hashset to store inputs and with every inputs my code computes its difference with previous numbers and find min and so on till last number, i believe it has not any overflow but it would not accept it and say Process has timed out
i would be appreciate if any one can help
for (int i = 0; i < N; i++) {
get current number from input
for(every member of the hashset){
if ( diff(current number and member) < min)
min=diff(number and member)
if(min==0)break;
}
hash set add current number
if(min==0)break;
}