Created
August 10, 2014 10:46
-
-
Save yuheiomori/95960705119b4b25c5f2 to your computer and use it in GitHub Desktop.
The Major Element(CodeEval) in Python 3.x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
import sys | |
def major_element(elements): | |
m = dict() | |
for e in elements: | |
m.setdefault(e, 0) | |
m[e] += 1 | |
if m[e] > len(elements) / 2: | |
return e | |
return None | |
def main(): | |
with open(sys.argv[1], "r") as f: | |
for line in f: | |
elements = line.rstrip().split(',') | |
print(major_element(elements)) | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment