0% found this document useful (0 votes)
16 views4 pages

Python vs Java Election Vote Tallying

Uploaded by

nagpalsanjna1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Python vs Java Election Vote Tallying

Uploaded by

nagpalsanjna1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON:

import random

# Input generation: Randomly generate 200 votes from the list of candidates

candidates = ["Alice", "Ben", "Clara", "David", "Eva"]

votes = [[Link](candidates) for _ in range(200)]

# Tally dictionary

tally = {"Alice": 0, "Ben": 0, "Clara": 0, "David": 0, "Eva": 0}

# Process votes

for vote in votes:

tally[vote] += 1

if tally[vote] > 100:

print(vote)

exit()

# Final check after all votes

max_votes = max([Link]())

top_candidates = []

for name in tally:

if tally[name] == max_votes:

top_candidates.append(name)

# Tiebreak if needed

winner = [Link](top_candidates)

print(winner)
JAVA
import [Link];

public class Election {

public static void main(String[] args) {

String[] candidates = {"Alice", "Ben", "Clara", "David", "Eva"};

int[] tally = new int[5];

Random rand = new Random();

for (int i = 0; i < 200; i++) {

int voteIndex = [Link](5);

tally[voteIndex]++;

if (tally[voteIndex] > 100) {

[Link](candidates[voteIndex]);

return;

int maxVotes = 0;

for (int count : tally) {

if (count > maxVotes) {

maxVotes = count;

// Collect candidates with max votes

int[] topIndices = new int[5];

int topCount = 0;

for (int i = 0; i < 5; i++) {

if (tally[i] == maxVotes) {

topIndices[topCount++] = i;
}

// Random tiebreak

int winnerIndex = topIndices[[Link](topCount)];

[Link](candidates[winnerIndex]);

}
1. Difficulty: Python was faster to write due to its concise syntax. Java required more time,
especially for arrays and random logic.
2. Control structures: Both languages handled loops and conditionals well. Python’s dynamic
typing made vote processing simpler.
3. Avoiding built-ins: Writing manual tally logic deepened understanding of array/dictionary
operations and forced clarity in control flow.
4. Time taken: Python took less time due to its structure and syntax while Java took more time due
to setup and array handling.
5. Readability: Python is easier to read and debug. Java is more verbose but offers strong type
safety and structure.

References:

[Link]

Copilot for code refinements

You might also like