diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-08-23 09:04:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-23 09:04:17 +0100 |
| commit | bb7fd02e8f49a66946a48b8bd5c60d01ab5a86c9 (patch) | |
| tree | 5cc258a2dd908f795f83cabc41283f83bf5b90b3 /challenge-074/lubos-kolouch/python/ch_1.py | |
| parent | 72c8355c9d6aaba5af6e323ca8e6d5c5d60a3f1c (diff) | |
| parent | fc350103aee950ecd6ae058c985e9f3e94710e2b (diff) | |
| download | perlweeklychallenge-club-bb7fd02e8f49a66946a48b8bd5c60d01ab5a86c9.tar.gz perlweeklychallenge-club-bb7fd02e8f49a66946a48b8bd5c60d01ab5a86c9.tar.bz2 perlweeklychallenge-club-bb7fd02e8f49a66946a48b8bd5c60d01ab5a86c9.zip | |
Merge pull request #2118 from LubosKolouch/chal_074_LK
Solutions 074 LK Python
Diffstat (limited to 'challenge-074/lubos-kolouch/python/ch_1.py')
| -rw-r--r-- | challenge-074/lubos-kolouch/python/ch_1.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/challenge-074/lubos-kolouch/python/ch_1.py b/challenge-074/lubos-kolouch/python/ch_1.py new file mode 100644 index 0000000000..6c8beadeaa --- /dev/null +++ b/challenge-074/lubos-kolouch/python/ch_1.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +""" Solution 1 Perl weekly challenge 074 """ + + +# =============================================================================== +# +# FILE: ch_1.py +# +# USAGE: ./ch_1.py +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-074/ +# +# TASK #1 › Majority Element +# +# AUTHOR: Lubos Kolouch +# VERSION: 1.0 +# CREATED: 08/22/2020 12:40:09 PM +# =============================================================================== + +def get_majority_element(arr): + """ Return majority element, if any """ + + counts = dict() + + arr_size_half = len(arr) / 2 + + for item in arr: + counts[item] = counts.get(item, 0) + 1 + + if counts[item] > arr_size_half: + return item + + return -1 + + +assert get_majority_element([1, 2, 2, 3, 2, 4, 2]) == 2 +assert get_majority_element([1, 3, 1, 2, 4, 5]) == -1 |
