Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #38088 > unrolled thread
| Started by | manish pamnani <manishpamnani169@gmail.com> |
|---|---|
| First post | 2020-01-19 00:05 -0800 |
| Last post | 2020-01-19 15:39 -0800 |
| Articles | 11 — 4 participants |
Back to article view | Back to comp.lang.javascript
Compare the Triplets | Printing two values. manish pamnani <manishpamnani169@gmail.com> - 2020-01-19 00:05 -0800
Re: Compare the Triplets | Printing two values. "R.Wieser" <address@not.available> - 2020-01-19 10:08 +0100
Re: Compare the Triplets | Printing two values. manish pamnani <manishpamnani169@gmail.com> - 2020-01-19 09:08 -0800
Re: Compare the Triplets | Printing two values. manish pamnani <manishpamnani169@gmail.com> - 2020-01-19 09:09 -0800
Re: Compare the Triplets | Printing two values. "R.Wieser" <address@not.available> - 2020-01-19 19:10 +0100
Re: Compare the Triplets | Printing two values. Julio Di Egidio <julio@diegidio.name> - 2020-01-19 09:42 -0800
Re: Compare the Triplets | Printing two values. "R.Wieser" <address@not.available> - 2020-01-19 10:47 +0100
Re: Compare the Triplets | Printing two values. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-01-19 13:03 -0800
Re: Compare the Triplets | Printing two values. Julio Di Egidio <julio@diegidio.name> - 2020-01-19 14:05 -0800
Re: Compare the Triplets | Printing two values. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-01-19 15:21 -0800
Re: Compare the Triplets | Printing two values. Julio Di Egidio <julio@diegidio.name> - 2020-01-19 15:39 -0800
| From | manish pamnani <manishpamnani169@gmail.com> |
|---|---|
| Date | 2020-01-19 00:05 -0800 |
| Subject | Compare the Triplets | Printing two values. |
| Message-ID | <83a2fe4f-4564-4943-9515-b9bfc09b686a@googlegroups.com> |
Hey,
I am solving a problem on hackerrank(https://www.hackerrank.com/challenges/compare-the-triplets/problem?h_r=next-challenge&h_v=zen). The problem states to complete the function
compareTriplets which must return an array of two integers, the first being ALice's score and the secoond being Bob's.
compareTriplets has the following parameter(s):
a: an array of integers representing Alice's challenge rating
b: an array of integers representing Bob's challenge rating
For more detail on the problem, refer to the following link:
https://www.hackerrank.com/challenges/compare-the-triplets/problem?h_r=next-challenge&h_v=zen
For the same challenge i wrote the following solution:
```
function compareTriplets(a, b) {
for (let i = 0; i < 4; i ++) {
points=[];
pointsArray=[];
if (a[i] > b[i]) {
points[i]=1;
pointsArray.push(points[i])
}
if (a[i] === b[i]) {
points[i]=0;
}
if (b[i] > a[i]) {
points[i]=1;
pointsArray.push(points[i])
}
return pointsArray;
}
}
```
The problem states that the output should be like [1,1] i.e. [points earned by alice, points earned by bob]
But somehow it displays a array with four elements with the values [1], [], [1], []
Refer to https://ibb.co/GCkwM9j for further details.
Can anybody suggest anything?
[toc] | [next] | [standalone]
| From | "R.Wieser" <address@not.available> |
|---|---|
| Date | 2020-01-19 10:08 +0100 |
| Message-ID | <r016am$1p6l$1@gioia.aioe.org> |
| In reply to | #38088 |
manish,
> Can anybody suggest anything?
Most likely. :-)
> But somehow it displays a array with four elements with the values [1],
> [], [1], []
> for (let i = 0; i < 4; i ++) {
That counts from zero upto three, which is four elements. "Sample Input 0"
shows only three values for each player.
> points[i]=0;
You are not using it. What are you setting it ?
> points[i]=1;
> pointsArray.push(points[i])
Why the first line ? Why not simply push the value 1 ?
Also, you are *adding an element* with the result of the current
comparision, not (altering) the *total score* for each player.
> if (a[i] > b[i]) {
> points[i]=1;
> pointsArray.push(points[i])
....
> if (b[i] > a[i]) {
> points[i]=1;
> pointsArray.push(points[i])
Although the comparision is different for both, they both do the exact same
thing to the array. How do you/your program know for whom (Alice or Bob) it
is ?
> return pointsArray;
> }
> }
The above causes your function exits at the end of the first loop (with "i"
being zero), which also means that the result you posted has not been
generated by the posted code - the resulting array should either be empty or
contain a single element, the comparision result of the first scores by
Alice and Bob.
Bottom line, your program simply cannot return anything related to the
requested result. Both from not actually doing whats asked as well as
because errors in the code itself.
Biggest problem ? Using .push() . It /adds elements to the array/,
instead of incrementing a counter (stored in that array).
Just assume Alice and bob play ten rounds and alice has won them all. Your
resulting array would have ten elements with the value 1, instead of the
value 10 somewhere.
Hope that helps.
Regards,
Rudy Wieser
[toc] | [prev] | [next] | [standalone]
| From | manish pamnani <manishpamnani169@gmail.com> |
|---|---|
| Date | 2020-01-19 09:08 -0800 |
| Message-ID | <3adfc7da-2433-420b-99a2-150eb73771a4@googlegroups.com> |
| In reply to | #38089 |
On Sunday, January 19, 2020 at 2:38:45 PM UTC+5:30, R.Wieser wrote:
> manish,
>
> > Can anybody suggest anything?
>
> Most likely. :-)
>
> > But somehow it displays a array with four elements with the values [1],
> > [], [1], []
>
> > for (let i = 0; i < 4; i ++) {
>
> That counts from zero upto three, which is four elements. "Sample Input 0"
> shows only three values for each player.
>
> > points[i]=0;
>
> You are not using it. What are you setting it ?
>
> > points[i]=1;
> > pointsArray.push(points[i])
>
> Why the first line ? Why not simply push the value 1 ?
>
> Also, you are *adding an element* with the result of the current
> comparision, not (altering) the *total score* for each player.
>
> > if (a[i] > b[i]) {
> > points[i]=1;
> > pointsArray.push(points[i])
> ....
> > if (b[i] > a[i]) {
> > points[i]=1;
> > pointsArray.push(points[i])
>
> Although the comparision is different for both, they both do the exact same
> thing to the array. How do you/your program know for whom (Alice or Bob) it
> is ?
>
> > return pointsArray;
> > }
> > }
>
> The above causes your function exits at the end of the first loop (with "i"
> being zero), which also means that the result you posted has not been
> generated by the posted code - the resulting array should either be empty or
> contain a single element, the comparision result of the first scores by
> Alice and Bob.
>
> Bottom line, your program simply cannot return anything related to the
> requested result. Both from not actually doing whats asked as well as
> because errors in the code itself.
>
> Biggest problem ? Using .push() . It /adds elements to the array/,
> instead of incrementing a counter (stored in that array).
>
> Just assume Alice and bob play ten rounds and alice has won them all. Your
> resulting array would have ten elements with the value 1, instead of the
> value 10 somewhere.
>
> Hope that helps.
>
> Regards,
> Rudy Wieser
Hey Rudy,
Thanks for your suggestions
Thanks for helping out. I did some tweak to my function on your suggestion.
Firstly, I removed points[i] as i was not using it.
Secondly, I made two different integers for alice and Bob and pushed the value 1(if the value is greater) and 0(if both values are equal) .
Thirdly, i removed the push from the array and instead incremented the two integers.
The program looks something like this:
```
function compareTriplets(a, b) {
for (let i = 0; i < 3; i ++) {
points=[];
pointsArray=[];
alice=0;
bob=0;
if (a[i] > b[i]) {
// points[i]=1;
alice++;
// console.log(pointsArray)
}
if (a[i] === b[i]) {
// points[i]=0;
// alice.push(0)
// bob.push(0)
}
if (b[i] > a[i]) {
// points[i]=1;
bob++;
// console.log(pointsArray)
}
console.log('alice',alice)
console.log("bob",bob)
}
}
compareTriplets([1,2,3], [3,2,1])
```
The program still prints the value seperately, not in one single array. Sorry to bother you again but, can you have a suggestion on this.
image.png
[toc] | [prev] | [next] | [standalone]
| From | manish pamnani <manishpamnani169@gmail.com> |
|---|---|
| Date | 2020-01-19 09:09 -0800 |
| Message-ID | <a681267b-3ad5-481d-8394-d2cfbc25817b@googlegroups.com> |
| In reply to | #38091 |
On Sunday, January 19, 2020 at 10:38:34 PM UTC+5:30, manish pamnani wrote:
> On Sunday, January 19, 2020 at 2:38:45 PM UTC+5:30, R.Wieser wrote:
> > manish,
> >
> > > Can anybody suggest anything?
> >
> > Most likely. :-)
> >
> > > But somehow it displays a array with four elements with the values [1],
> > > [], [1], []
> >
> > > for (let i = 0; i < 4; i ++) {
> >
> > That counts from zero upto three, which is four elements. "Sample Input 0"
> > shows only three values for each player.
> >
> > > points[i]=0;
> >
> > You are not using it. What are you setting it ?
> >
> > > points[i]=1;
> > > pointsArray.push(points[i])
> >
> > Why the first line ? Why not simply push the value 1 ?
> >
> > Also, you are *adding an element* with the result of the current
> > comparision, not (altering) the *total score* for each player.
> >
> > > if (a[i] > b[i]) {
> > > points[i]=1;
> > > pointsArray.push(points[i])
> > ....
> > > if (b[i] > a[i]) {
> > > points[i]=1;
> > > pointsArray.push(points[i])
> >
> > Although the comparision is different for both, they both do the exact same
> > thing to the array. How do you/your program know for whom (Alice or Bob) it
> > is ?
> >
> > > return pointsArray;
> > > }
> > > }
> >
> > The above causes your function exits at the end of the first loop (with "i"
> > being zero), which also means that the result you posted has not been
> > generated by the posted code - the resulting array should either be empty or
> > contain a single element, the comparision result of the first scores by
> > Alice and Bob.
> >
> > Bottom line, your program simply cannot return anything related to the
> > requested result. Both from not actually doing whats asked as well as
> > because errors in the code itself.
> >
> > Biggest problem ? Using .push() . It /adds elements to the array/,
> > instead of incrementing a counter (stored in that array).
> >
> > Just assume Alice and bob play ten rounds and alice has won them all. Your
> > resulting array would have ten elements with the value 1, instead of the
> > value 10 somewhere.
> >
> > Hope that helps.
> >
> > Regards,
> > Rudy Wieser
>
> Hey Rudy,
>
> Thanks for your suggestions
> Thanks for helping out. I did some tweak to my function on your suggestion.
>
> Firstly, I removed points[i] as i was not using it.
> Secondly, I made two different integers for alice and Bob and pushed the value 1(if the value is greater) and 0(if both values are equal) .
> Thirdly, i removed the push from the array and instead incremented the two integers.
>
> The program looks something like this:
>
> ```
> function compareTriplets(a, b) {
> for (let i = 0; i < 3; i ++) {
> points=[];
> pointsArray=[];
> alice=0;
> bob=0;
> if (a[i] > b[i]) {
> // points[i]=1;
> alice++;
> // console.log(pointsArray)
> }
> if (a[i] === b[i]) {
> // points[i]=0;
> // alice.push(0)
> // bob.push(0)
> }
> if (b[i] > a[i]) {
> // points[i]=1;
> bob++;
> // console.log(pointsArray)
> }
> console.log('alice',alice)
> console.log("bob",bob)
>
> }
>
> }
>
>
> compareTriplets([1,2,3], [3,2,1])
> ```
>
> The program still prints the value seperately, not in one single array. Sorry to bother you again but, can you have a suggestion on this.
>
> image.png
https://ibb.co/txcZYLF
[toc] | [prev] | [next] | [standalone]
| From | "R.Wieser" <address@not.available> |
|---|---|
| Date | 2020-01-19 19:10 +0100 |
| Message-ID | <r0262k$csu$1@gioia.aioe.org> |
| In reply to | #38092 |
manish,
>> Firstly, I removed points[i] as i was not using it.
Nope, you didn't. Both arrays are still created in your code.
>> Secondly, I made two different integers for alice and Bob
Thats one good way to do it. :-)
>> pushed the value 1(if the value is greater)
The phrase you where looking for is "incremented by one", not "pushed the
value 1" - especially when you already used the word "push" to indicate
adding elements to an array (and thus might cause confusion)
> and 0(if both values are equal) .
Not in the code you've posted (you commented that out).
And as such you can remove the whole "if (a[i] === b[i]) { }" block, as it
doesn't do anything.
>> The program looks something like this:
...
>> The program still prints the value seperately, not in one single array.
Hmmm... I can interpret that in different ways. Yes, you have printed the
"alice" and "bob" scores in two seperate "console.log()" lines. But the
problem is that you also get three groups of it.
The cause ? Your "for" loop (still) starts and ends at the wrong places.
Hint: Make "alice" win all of the games. What result does your program show
? If anything, it /should/ show the results 1, 2 and 3 for alice, and 0, 0,
0 for bob. But it doesn't. Can you see why ?
Regards,
Rudy Wieser
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2020-01-19 09:42 -0800 |
| Message-ID | <22f31f36-d850-4b42-9789-b86c915c7397@googlegroups.com> |
| In reply to | #38091 |
On Sunday, 19 January 2020 18:08:34 UTC+1, manish pamnani wrote:
> On Sunday, January 19, 2020 at 2:38:45 PM UTC+5:30, R.Wieser wrote:
<snip>
> The program looks something like this:
You have commented out the old parts which is very confusing: take a copy
of the code before changing it if you like, then do change it and make it
clean in order to keep working with it. Commented code only makes reading
the code more difficult. Your code, cleaned up of the unwanted parts, and
with some vertical spacing added, already looks like this:
> ```
> function compareTriplets(a, b) {
> for (let i = 0; i < 3; i++) {
> let alice = 0; // let (or var)!, and spaces
> let bob = 0;
>
> if (a[i] > b[i]) { // this series of if's needs cleanup too
> alice++;
> }
> if (a[i] === b[i]) {
> }
> if (b[i] > a[i]) {
> bob++;
> }
>
> console.log("alice", alice); // semi-colons, quotes, spaces
> console.log("bob", bob);
> }
> }
> ```
>
> The program still prints the value seperately
You may want to print to the console for debugging, but the function should
*return* an array with two elements, i.e. Bob's and Alice's scores, not print
them out. Do you know how to instantiate a 2-element array?
Anyway, the fact that it works one element at a time is obvious if you
notice that you are initialising the scores as well as printing out
their values at each and every iteration (namely, inside the loop)...
You should initialise the scores, then do the loop, then read out
the values (then put them into a two element array that you finally
return).
HTH,
Julio
[toc] | [prev] | [next] | [standalone]
| From | "R.Wieser" <address@not.available> |
|---|---|
| Date | 2020-01-19 10:47 +0100 |
| Message-ID | <r018j9$3dv$1@gioia.aioe.org> |
| In reply to | #38088 |
manish,
I overlooked the below
> for (let i = 0; i < 4; i ++) {
> points=[];
> pointsArray=[];
Clearing the arrays /inside/ the loop means that if something is returned it
will only be the /last/ comparision result ...
To be honest, it looks like you slapped a "for" loop around something that
worked for a single comparision (with a "return" at the bottom of it), and
expected it to keep working.
Alas, computers do exactly what you tell them to do, instead of doing what
you ment. :-)
Suggestion: When you have trouble with a program have it print the
intermediate results. And in the case of loops print the loopcounter too.
It allows you to track what the code is doing and (hopefully) see where it
goes wrong (for starters, that your loop isn't looping at all)
Regards,
Rudy Wieser
[toc] | [prev] | [next] | [standalone]
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
|---|---|
| Date | 2020-01-19 13:03 -0800 |
| Message-ID | <8010fbd1-2ea8-4e26-8686-3316a1efa14f@googlegroups.com> |
| In reply to | #38088 |
On Sunday, January 19, 2020 at 2:05:15 AM UTC-6, manish pamnani wrote:
> Hey,
>
> I am solving a problem on hackerrank(https://www.hackerrank.com/challenges/compare-the-triplets/problem?h_r=next-challenge&h_v=zen). The problem states to complete the function
> compareTriplets which must return an array of two integers, the first being ALice's score and the secoond being Bob's.
>
> compareTriplets has the following parameter(s):
>
> a: an array of integers representing Alice's challenge rating
> b: an array of integers representing Bob's challenge rating
>
> For more detail on the problem, refer to the following link:
> https://www.hackerrank.com/challenges/compare-the-triplets/problem?h_r=next-challenge&h_v=zen
>
> For the same challenge i wrote the following solution:
>
> ```
> function compareTriplets(a, b) {
> for (let i = 0; i < 4; i ++) {
> points=[];
> pointsArray=[];
> if (a[i] > b[i]) {
> points[i]=1;
> pointsArray.push(points[i])
> }
> if (a[i] === b[i]) {
> points[i]=0;
> }
> if (b[i] > a[i]) {
> points[i]=1;
> pointsArray.push(points[i])
> }
> return pointsArray;
> }
> }
> ```
>
> The problem states that the output should be like [1,1] i.e. [points earned by alice, points earned by bob]
>
> But somehow it displays a array with four elements with the values [1], [], [1], []
> Refer to https://ibb.co/GCkwM9j for further details.
>
> Can anybody suggest anything?
```
function compareTriplets(a, b) {
return a.map((value, i) =>
value < b[i] ? [0,1] : value == b[i] ? [0,0] :[1,0]
).reduce((sum, next) =>
[sum[0] + next[0], sum[1] + next[1]]
)
}
```
Map and reduce methods could be combined if you only want a single pass
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2020-01-19 14:05 -0800 |
| Message-ID | <3463fd29-20b7-4f00-9c98-30cd7960f886@googlegroups.com> |
| In reply to | #38098 |
On Sunday, 19 January 2020 22:03:48 UTC+1, Michael Haufe (TNO) wrote:
> On Sunday, January 19, 2020 at 2:05:15 AM UTC-6, manish pamnani wrote:
<snip>
> > Can anybody suggest anything?
>
> ```
> function compareTriplets(a, b) {
> return a.map((value, i) =>
> value < b[i] ? [0,1] : value == b[i] ? [0,0] :[1,0]
> ).reduce((sum, next) =>
> [sum[0] + next[0], sum[1] + next[1]]
> )
> }
> ```
>
> Map and reduce methods could be combined if you only want a single pass
Congratulations, not only you just spoil the exercise, but with an
atrocious piece of code indeed. Way to go...
*Plonk*
Julio
[toc] | [prev] | [next] | [standalone]
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
|---|---|
| Date | 2020-01-19 15:21 -0800 |
| Message-ID | <ed1c83e7-cf94-4f89-95d4-6bc1cc4595c9@googlegroups.com> |
| In reply to | #38099 |
On Sunday, January 19, 2020 at 4:05:06 PM UTC-6, Julio Di Egidio wrote:
> On Sunday, 19 January 2020 22:03:48 UTC+1, Michael Haufe (TNO) wrote:
> > On Sunday, January 19, 2020 at 2:05:15 AM UTC-6, manish pamnani wrote:
> <snip>
> > > Can anybody suggest anything?
> >
> > ```
> > function compareTriplets(a, b) {
> > return a.map((value, i) =>
> > value < b[i] ? [0,1] : value == b[i] ? [0,0] :[1,0]
> > ).reduce((sum, next) =>
> > [sum[0] + next[0], sum[1] + next[1]]
> > )
> > }
> > ```
> >
> > Map and reduce methods could be combined if you only want a single pass
>
> Congratulations, not only you just spoil the exercise, but with an
> atrocious piece of code indeed. Way to go...
Says the man who has yet to present a single piece of non-procedural non-imperative code in this group AFAICT. Spare me your pettiness.
At least your punching upwards I guess.
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2020-01-19 15:39 -0800 |
| Message-ID | <f5bf9c23-8a18-4c69-933a-b90d99ebc7a5@googlegroups.com> |
| In reply to | #38102 |
On Monday, 20 January 2020 00:21:40 UTC+1, Michael Haufe (TNO) wrote: > On Sunday, January 19, 2020 at 4:05:06 PM UTC-6, Julio Di Egidio wrote: > > Congratulations, not only you just spoil the exercise, but with an > > atrocious piece of code indeed. Way to go... > > Says the man who has yet to present a single piece of non-procedural non- > imperative code in this group AFAICT. Spare me your pettiness. On the contrary, whatever I have presented, and it's more in a month than you do in a year, you have never even been able to engage. So it's libel, intellectual dishonesty, incompetence, and atrocious code. Get another hobby. *Plonk* Julio
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web