aboutsummaryrefslogtreecommitdiff
path: root/challenge-074/lubos-kolouch/python/ch-2.py
blob: da21ecc6fb82941045b8a513dae81e6039e82ad1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/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)

        result += fnr_queue[-1] if fnr_queue else '#'

    return result


assert get_fnr('ababc') == 'abb#c'
assert get_fnr('xyzzyx') == 'xyzyx#'