Categories We Write About

Build an idea ranker based on votes

Here’s a simple and functional idea ranker based on votes, implemented in Python. This ranker allows users to add ideas, upvote or downvote them, and displays a ranked list sorted by vote count.

python
class Idea: def __init__(self, description): self.description = description self.votes = 0 def upvote(self): self.votes += 1 def downvote(self): self.votes -= 1 def __str__(self): return f"{self.description} (Votes: {self.votes})" class IdeaRanker: def __init__(self): self.ideas = {} def add_idea(self, key, description): if key in self.ideas: print("Idea key already exists.") else: self.ideas[key] = Idea(description) def vote(self, key, up=True): if key not in self.ideas: print("Idea not found.") return if up: self.ideas[key].upvote() else: self.ideas[key].downvote() def ranked_ideas(self): return sorted(self.ideas.items(), key=lambda item: item[1].votes, reverse=True) def show_ranked_ideas(self): ranked = self.ranked_ideas() for rank, (key, idea) in enumerate(ranked, start=1): print(f"{rank}. {idea}") # Example usage: if __name__ == "__main__": ranker = IdeaRanker() ranker.add_idea("idea1", "Create a mobile app for food delivery") ranker.add_idea("idea2", "Build a decentralized social network") ranker.add_idea("idea3", "Launch an online coding bootcamp") # Voting ranker.vote("idea1", up=True) ranker.vote("idea1", up=True) ranker.vote("idea2", up=True) ranker.vote("idea3", up=False) # Display ranked ideas ranker.show_ranked_ideas()

Features:

  • add_idea(key, description): Adds a new idea with a unique key.

  • vote(key, up=True): Allows upvoting or downvoting an idea.

  • show_ranked_ideas(): Displays the list of ideas ranked by votes.

You can adapt this further into a web app using Flask or Django, or turn it into an interactive command-line tool. Let me know if you’d like the ranker implemented in another language or framework (like JavaScript, React, or a Google Sheets formula).

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About