[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Javascript classes, Please take a look at my question below

Rahul Reddy

4/8/2015 5:46:00 PM

The seats allocated to a party are calculated by this method in two steps:

In the first step the corresponding number of votes that are attributable to a party is multiplied by the total number of available seats and divided by the total number of valid votes.

In the second step the result is split into the integer portion and the rest. The integer parts are attributed to the respective party as seats. The remaining seats are allocated to parties in the order of the size of the fractional portions.
Exemplary data (assuming all votes are valid):

Total seats: 15
Votes: A => 15000, B => 5400, C => 5500, D => 5550

Result should be
Seats: A => 7, B => 2, C = 3, D = > 3

Write a class that calculates for a variable number of parties and seats and include the needed two lines how to call your class.
8 Answers

Erwin Moller

4/9/2015 9:14:00 AM

0

On 4/8/2015 7:46 PM, Rahul Reddy wrote:
> The seats allocated to a party are calculated by this method in two steps:
>
> In the first step the corresponding number of votes that are attributable to a party is multiplied by the total number of available seats and divided by the total number of valid votes.
>
> In the second step the result is split into the integer portion and the rest. The integer parts are attributed to the respective party as seats. The remaining seats are allocated to parties in the order of the size of the fractional portions.
> Exemplary data (assuming all votes are valid):
>
> Total seats: 15
> Votes: A => 15000, B => 5400, C => 5500, D => 5550
>
> Result should be
> Seats: A => 7, B => 2, C = 3, D = > 3
>
> Write a class that calculates for a variable number of parties and seats and include the needed two lines how to call your class.
>

Dear Rahul,

You asked this question before.
All that responded to you told you to do your own work.
Except Dennis, he actually wrote some 'code' for you. ;-)

If you have a piece of code *you* wrote all by yourself, please post it.
I am sure some kind soul in here will help you from there.
But nobody is willing to write it from scratch for you.
(Unless you pay our regular hourly wages).

Regards,
Erwin Moller


--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens

JJ

4/9/2015 12:51:00 PM

0

On Wed, 8 Apr 2015 10:46:28 -0700 (PDT), Rahul Reddy wrote:
> The seats allocated to a party are calculated by this method in two steps:
>
> In the first step the corresponding number of votes that are attributable to a party is multiplied by the total number of available seats and divided by the total number of valid votes.
>
> In the second step the result is split into the integer portion and the rest. The integer parts are attributed to the respective party as seats. The remaining seats are allocated to parties in the order of the size of the fractional portions.
> Exemplary data (assuming all votes are valid):
>
> Total seats: 15
> Votes: A => 15000, B => 5400, C => 5500, D => 5550
>
> Result should be
> Seats: A => 7, B => 2, C = 3, D = > 3
>
> Write a class that calculates for a variable number of parties and seats and include the needed two lines how to call your class.

This is more like math problem rather than JavaScript.

Here's an example for A.

Total_Votes = A + B + C + D
A_Share% = A / Total_Votes x 100
A = A_Share x Total_Seats / 100

Thomas 'PointedEars' Lahn

4/9/2015 2:17:00 PM

0

JJ wrote:

> On Wed, 8 Apr 2015 10:46:28 -0700 (PDT), Rahul Reddy wrote:
>> [Programming homework question]
>
> This is more like math problem rather than JavaScript.
>
> Here's an example for A.
>
> Total_Votes = A + B + C + D
> A_Share% = A / Total_Votes x 100
> A = A_Share x Total_Seats / 100

<facepalm/>

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Evertjan.

4/9/2015 2:38:00 PM

0

JJ <jj4public@vfemail.net> wrote on 09 apr 2015 in comp.lang.javascript:

> On Wed, 8 Apr 2015 10:46:28 -0700 (PDT), Rahul Reddy wrote:
[..]
>> Write a class that calculates for a variable number of parties and
>> seats and include the needed two lines how to call your class.
>
> This is more like math problem rather than JavaScript.

I would suggest the possibility that the OP indeed talks about
"Javascript classes", where his homework assignment originates.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Stefan Weiss

4/9/2015 7:45:00 PM

0

On 2015-04-08 19:46, Rahul Reddy wrote:
> In the second step the result is split into the integer portion and
> the rest. The integer parts are attributed to the respective party as
> seats. The remaining seats are allocated to parties in the order of the
> size of the fractional portions.

I would reject this algorithm as incomplete.

Example input: 10 seats, votes: 50 A, 35 B, 15 C

Does C get one or two seats?
What should the program do if it can't find an unambiguous distribution?

(Amusingly, Denis's solution would solve this specific example by simply
adding a seat.)

- stefan

JJ

4/10/2015 3:58:00 AM

0

On Thu, 09 Apr 2015 16:17:03 +0200, Thomas 'PointedEars' Lahn wrote:
>
> <facepalm/>

<wiggle/>

Denis McMahon

4/10/2015 2:22:00 PM

0

On Thu, 09 Apr 2015 21:44:45 +0200, Stefan Weiss wrote:

> (Amusingly, Denis's solution would solve this specific example by simply
> adding a seat.)

Denis didn't claim his solution was complete either. ;)

Although it might provide a starting point for someone trying to develop
their own code. Or not.

--
Denis McMahon, denismfmcmahon@gmail.com

Richard Damon

4/12/2015 2:14:00 AM

0

On 4/9/15 3:44 PM, Stefan Weiss wrote:
> On 2015-04-08 19:46, Rahul Reddy wrote:
>> In the second step the result is split into the integer portion and
>> the rest. The integer parts are attributed to the respective party as
>> seats. The remaining seats are allocated to parties in the order of the
>> size of the fractional portions.
>
> I would reject this algorithm as incomplete.
>
> Example input: 10 seats, votes: 50 A, 35 B, 15 C
>
> Does C get one or two seats?
> What should the program do if it can't find an unambiguous distribution?
>
> (Amusingly, Denis's solution would solve this specific example by simply
> adding a seat.)
>
> - stefan
>

There is a fundamental problem with ANY vote system of how to handle
ties. The simplest case would be 1 seat, two parties, equal votes. The
next level would be 2 seats, 3 parties, or 3 seat, 2 parties with equal
votes. Fundamentally, such a case (due to the symmetry) can't (from what
I see) have a solution other than going to chance or a new vote, or
maybe have some other body "vote" to break the tie. With typical number
of votes in a real election, the odds of having a true tie gets small,
and if the number of seat is large having a lottery choose might not be
unreasonable (perhaps add a extra tie breaking rule biasing cases like
the above one way or the other, but these couldn't handle the 50/25/25
case due to symmetry, that would need to go to another stage (lottery or
re-vote, etc).

Sometimes adding a seat is a viable solution, but properly if you add
(or remove) a seat, you should redo the whole calculation and I suspect
it may be possible that there are cases when you do this the extra seat
goes elsewhere. You also run into the issue that normally the number of
seats is set by a more fundamental law (and may be constrained by the
actually number of physical seats).