HW2 LOGIC!!!!

e
enricomaria.follega (710 points)
4 8 13
asked Nov 6, 2021 in HW2 by enricomaria.follega (710 points)
recategorized Nov 6, 2021 by andrea.sterbini
I started to code it about 2 days ago and i'm stuck. The score of the code i wrote does not match with the score i should expect. I didn't get why since i've followed the description line by line.

I continue to read the description again and again but doubts remains.

According to the description:

      "Ogni elemento della sequenza di A viene confrontato con l'elemento corrispondente della sequenza di B" ->  that is   A - B, which is different from B - A. Then

      "se la differenza fra il valore di A e il valore di B è inferiore o uguale ad un parametro k" ... "viene assegnato un punto al concorrente che ha generato il valore più alto"; ->      A - B il punto va a max(A,B)

      "se la differenza fra il valore di A e il valore di B è superiore a k" ... "viene assegnato un punto al concorrente che ha generato il valore più basso" ->      A - B il punto va a min(A,B)

      "se la differenza fra il valore di A e il valore di B è" ... " pareggio" ->      that is diff=0  "a nessuno"

Than at the end of the game if there is a tie between players

       "vince il giocatore che ha generato la sequenza con somma totale dei valori inferiore." ->      that is  min(SumSequenceOfA, SumSequenceOfB)

We know how it works so i will not stop on all the details comparing letters by letters but just on the points where i have doubts.

- First example, if we compare the first and the second player we should make (A - B) -> (a - c), the difference between the first and the subsequent player. The professor wrote (c - a) and then wrote the difference between (a - c)= -2. that's fine i guess indeed the expected result is the same with the one i have,   

- Second example, mine is completely wrong i get [2, 1, 0], but again he wrote (c - a) this time with (c - a) result =2 though and it's different to (a - c)= -2 so the score should go to player 1 not to player 0; between plr0 and plr2 the score should go to plr2, the result is (-2, 0, 2); and between plr1 and plr2 to plr2 again since the result is (0, 2, 2). Expected Leaderboard (0, 2, 1).

Also for the other 2 examples i get (2, 0, 1) and (2, 1, 0) respectively whereas the Expected Leaderboard should be (0, 1, 2) and (1, 2, 0).

  Don't know maybe i didn't get something. Do you have suggestions?

checking the other example like ["aaa", "bbb", "ccc", "ddd", "eee"] k=5 and k=3, i get in both (4, 3, 2, 1, 0) as result. of course are subsequent letters (a-b) so it will always be negative, modifing k from 5 to 3 nothing should change, still i don't get why his result is different.
262 views

2 Answers

andrea.sterbini (172780 points)
514 935 1789
answered Nov 6, 2021 by andrea.sterbini (172,780 points)
have you computed the absolute value of the difference or just the difference?
e
enricomaria.follega (710 points)
4 8 13
commented Nov 6, 2021 by enricomaria.follega (710 points)
Should i take the absolute difference between the two? I guess that in my code i get the absolute value, i'm not sure though.

In my code:

   it subtracts ( A - B ), it just take a and b and then subtracts them, than according to the result (<=, > k ), it goes back to the list of the combinations and picks the greatest/smalles value, then according to that value the score is assigned. After the game it checks the two scores and state who won.

of course though doing ( B - A ) is not the same. i get the opposite result.

suggestions?
KilianS (3590 points)
5 9 27
commented Nov 6, 2021 by KilianS (3,590 points)
there is the abs() function, which basically returns the difference(absolute value).

in other words: abs(a-b) would be the same as abs(b-a)
e
enricomaria.follega (710 points)
4 8 13
commented Nov 7, 2021 by enricomaria.follega (710 points)

HONOR TO MY SAVIOR!!!!!!!!!!!!!!!

i didn't know of that formula. yeah that was the mistake. i also thought the difference was absolute before, i must have been drunk.

How do i give you points?!?!??! 

i will create fake accounts to give you other points too winklaughdevil

Thanks, really mate.yes

e
enricomaria.follega (710 points)
4 8 13
commented Nov 7, 2021 by enricomaria.follega (710 points)

write the same comment as new answer or i will give the point to the professor yes

Luigi Pizza (6120 points)
11 20 65
answered Nov 6, 2021 by Luigi Pizza (6,120 points)
reshown Nov 8, 2021 by Luigi Pizza

So, the homework can be divided into two different parts: comparing the sequences and comparing the result of each sequence. For simplicity's sake let's say that we have 

A = ['aacda'],   B = ['caaad'],   k = 2

we take the map with function ord, obtaining:

A = [97,97,99,100,97],   B = [99,97,97,97,100]

Now we compare one to one each element of these lists:

A[0]-B[0],  A[1]-B[1],  A[2]-B[2], ...

getting:

-2, 0, 2, 3, -3

Based on how we have structured the subtraction: 

A will get a point if the difference is: 0 < A[x]-B[x] <= 2 or A[x]-B[x] <= -2

B will get a point if the difference is: -2 < A[x]-B[x] <= 0 or A[x]-B[x] >= 2

In this case we have that A will get 2 points and B will get 2 points. We have a tie.

At this point we sum up all the ords of the two lists above, but their result is the same:

 image

So we compare the 2 sequences:

A>B

So A wins.

I can't really tell you what you did wrong in your code, you can go down in any of these steps, just ckeck, either with the debugger or a lot of prints for the error in simple cases, hopefully you'll find it before midnight

andrea.sterbini (172780 points)
514 935 1789
commented Nov 6, 2021 by andrea.sterbini (172,780 points)

why don't you give him just your code? angry

e
enricomaria.follega (710 points)
4 8 13
commented Nov 6, 2021 by enricomaria.follega (710 points)

testing out your test (['aacda', 'caaad'], k=2) i get as result B winner. which should be the correct one, by hand calculation too.

the passages in my code looks correct to me, dunno. :|

Prof what if i send you the code and you fix it for me? i would go to have lunch :D.     It's just logic now 1+2, codeing is done, should be at least.