diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2020-08-22 13:52:24 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2020-08-22 13:52:24 +0200 |
| commit | 72c2dd4fea1c86c7a37a839392d1088230829a0c (patch) | |
| tree | 1711ca3e041aeb256df9c95294e8dcf034d7cba8 /challenge-074/lubos-kolouch/python/ch_2.py | |
| parent | f1edf6f0ae2c124e49db2d97cd9d7294cffab3a7 (diff) | |
| download | perlweeklychallenge-club-72c2dd4fea1c86c7a37a839392d1088230829a0c.tar.gz perlweeklychallenge-club-72c2dd4fea1c86c7a37a839392d1088230829a0c.tar.bz2 perlweeklychallenge-club-72c2dd4fea1c86c7a37a839392d1088230829a0c.zip | |
Solutions 074 Python LK
Diffstat (limited to 'challenge-074/lubos-kolouch/python/ch_2.py')
| -rw-r--r-- | challenge-074/lubos-kolouch/python/ch_2.py | 46 |
1 files changed, 46 insertions, 0 deletions
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#' |
