Detect Voter Fraud in Python

PythonServer Side ProgrammingProgramming

<p>Suppose we have a list of votes, where each element in the list has two elements [c_id, v_id], the c_id is the candidate id and v_id is the voter id. We have to check whether any voter has voted more than once or not.</p><p>So, if the input is like [[5, 1],[5, 0],[5, 4],[5, 3],[5, 0]], then the output will be True as [5,0] is present twice</p><p>To solve this, we will follow these steps &minus;</p><p>make a new set named all</p><ul class="list"><li>for each vote in votes, do<ul class="list"><li>insert (vote[1]) into all</li></ul></li><li>return true when size of all is not same as size of votes</li></ul><p>Let us see the following implementation to get better understanding &minus;</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/dlNnCCrc" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">class Solution: &nbsp; &nbsp;def solve(self, votes): &nbsp; &nbsp; &nbsp; all = set() &nbsp; &nbsp; &nbsp; for vote in votes: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;all.add(vote[1]) &nbsp; &nbsp; &nbsp; return len(all) != len(votes) ob = Solution() votes = [[5, 1],[5, 0],[5, 4],[5, 3],[5, 0]] print(ob.solve(votes))</pre><h2>Input</h2><pre class="result notranslate">[[5, 1],[5, 0],[5, 4],[5, 3],[5, 0]]</pre><h2>Output</h2><pre class="result notranslate">True</pre>
raja
Updated on 22-Sep-2020 11:21:02

Advertisements