From 72c2dd4fea1c86c7a37a839392d1088230829a0c Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 22 Aug 2020 13:52:24 +0200 Subject: Solutions 074 Python LK --- challenge-074/lubos-kolouch/python/ch_1.py | 37 ++++++++++++++++++++++++ challenge-074/lubos-kolouch/python/ch_2.py | 46 ++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 challenge-074/lubos-kolouch/python/ch_1.py create mode 100644 challenge-074/lubos-kolouch/python/ch_2.py 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 diff --git a/challenge-074/lubos-kolouch/python/ch_2.py b/challenge-074/lubos-kolouch/python/ch_2.py new file mode 100644 index 0000000000..f4888236e1 --- /dev/null +++ b/challenge-074/lubos-kolouch/python/ch_2.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +""" FNR character from Perl weekly challenge 074 task 2 """ + + +# =============================================================================== +# +# FILE: ch-_.py +# +# USAGE: ./ch_2.py +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-074/ +# +# TASK #2 › FNR Character +# +# AUTHOR: Lubos Kolouch +# VERSION: 1.0 +# CREATED: 08/22/2020 12:40:09 PM +# =============================================================================== + + +def get_fnr(in_str): + """ Find and return the FNR in the given string """ + + fnr_count = dict() + fnr_queue = list() + + result = '' + + for char in in_str: + fnr_count[char] = fnr_count.get(char, 0) + 1 + + if fnr_count[char] == 1: + fnr_queue.append(char) + else: + fnr_queue.remove(char) + + if fnr_queue: + result += fnr_queue[-1] + else: + result += '#' + + return result + + +assert get_fnr('ababc') == 'abb#c' +assert get_fnr('xyzzyx') == 'xyzyx#' -- cgit From fc350103aee950ecd6ae058c985e9f3e94710e2b Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 22 Aug 2020 13:56:19 +0200 Subject: Cosmetic change CH 2 --- challenge-074/lubos-kolouch/python/ch_2.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/challenge-074/lubos-kolouch/python/ch_2.py b/challenge-074/lubos-kolouch/python/ch_2.py index f4888236e1..da21ecc6fb 100644 --- a/challenge-074/lubos-kolouch/python/ch_2.py +++ b/challenge-074/lubos-kolouch/python/ch_2.py @@ -34,10 +34,7 @@ def get_fnr(in_str): else: fnr_queue.remove(char) - if fnr_queue: - result += fnr_queue[-1] - else: - result += '#' + result += fnr_queue[-1] if fnr_queue else '#' return result -- cgit