aboutsummaryrefslogtreecommitdiff
path: root/challenge-074/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
authorSolathian <horvath6@gmail.com>2025-08-02 16:28:12 +0200
committerSolathian <horvath6@gmail.com>2025-08-02 16:28:12 +0200
commit4014f0eb1fa46f39fee72c2add76ca47f2dd1637 (patch)
tree07ed523115445f773f90f3d0f08c83456457e54e /challenge-074/paulo-custodio/python/ch-2.py
parent83179303806e75ac6aa4c786cefbb89ab6ddeaf7 (diff)
parent698c027e7ef73ac2753c97d4e64d7fba2b2ddc95 (diff)
downloadperlweeklychallenge-club-4014f0eb1fa46f39fee72c2add76ca47f2dd1637.tar.gz
perlweeklychallenge-club-4014f0eb1fa46f39fee72c2add76ca47f2dd1637.tar.bz2
perlweeklychallenge-club-4014f0eb1fa46f39fee72c2add76ca47f2dd1637.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-074/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-074/paulo-custodio/python/ch-2.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-074/paulo-custodio/python/ch-2.py b/challenge-074/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..131c393c0e
--- /dev/null
+++ b/challenge-074/paulo-custodio/python/ch-2.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python3
+
+# Challenge 074
+#
+# TASK #2 > FNR Character
+# Submitted by: Mohammad S Anwar
+# You are given a string $S.
+#
+# Write a script to print the series of first non-repeating character (left
+# -> right) for the given string. Print # if none found.
+#
+# Example 1
+# Input: $S = 'ababc'
+# Output: 'abb#c'
+# Pass 1: "a", the FNR character is 'a'
+# Pass 2: "ab", the FNR character is 'b'
+# Pass 3: "aba", the FNR character is 'b'
+# Pass 4: "abab", no FNR found, hence '#'
+# Pass 5: "ababc" the FNR character is 'c'
+#
+# Example 2
+# Input: $S = 'xyzzyx'
+# Output: 'xyzyx#'
+# Pass 1: "x", the FNR character is "x"
+# Pass 2: "xy", the FNR character is "y"
+# Pass 3: "xyz", the FNR character is "z"
+# Pass 4: "xyzz", the FNR character is "y"
+# Pass 5: "xyzzy", the FNR character is "x"
+# Pass 6: "xyzzyx", no FNR found, hence '#'
+
+import sys
+
+def lnr_char(s):
+ out = ""
+ seen = {}
+ for i in range(len(s)):
+ ch = s[i]
+ if ch in seen:
+ seen[ch] += 1
+ else:
+ seen[ch] = 1
+ found = False
+ for j in range(i+1)[::-1]:
+ ch = s[j]
+ if ch in seen and seen[ch] == 1:
+ out += ch
+ found = True
+ break
+ if not found:
+ out += "#"
+ return out
+
+s = sys.argv[1]
+print(lnr_char(s))