aboutsummaryrefslogtreecommitdiff
path: root/ch-2.pl
diff options
context:
space:
mode:
authorFung Cheok Yin <61836418+E7-87-83@users.noreply.github.com>2020-08-19 10:24:43 +0800
committerGitHub <noreply@github.com>2020-08-19 10:24:43 +0800
commit3a85a00c68fe0718ec916493826ea63e0f8a7066 (patch)
tree2ff800bee5f112891e7effbdded474202c951567 /ch-2.pl
parent00576254dae94b522130208c0ac3e19167827ddd (diff)
downloadperlweeklychallenge-club-3a85a00c68fe0718ec916493826ea63e0f8a7066.tar.gz
perlweeklychallenge-club-3a85a00c68fe0718ec916493826ea63e0f8a7066.tar.bz2
perlweeklychallenge-club-3a85a00c68fe0718ec916493826ea63e0f8a7066.zip
Add files via upload
Diffstat (limited to 'ch-2.pl')
-rw-r--r--ch-2.pl48
1 files changed, 48 insertions, 0 deletions
diff --git a/ch-2.pl b/ch-2.pl
new file mode 100644
index 0000000000..bae04b73b3
--- /dev/null
+++ b/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+# Perl Weekly Challenge #074 Task 2 FNR character
+# task statement:
+# 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.
+# Usage: ch-2.pl [string]
+
+use strict;
+use warnings;
+#use Test::More tests => 5;
+
+sub fnr {
+ my @uniquestack;
+ my %charcount;
+ my $ans = "";
+ my @characters = split //, $_[0];
+ for my $char (@characters) {
+ if (!exists $charcount{$char} ) {
+ push @uniquestack , $char;
+ $charcount{$char} = 1;
+ $ans .= $char;
+ }
+ else {
+ $charcount{$char}++;
+ @uniquestack = grep { $charcount{$_} == 1 } @uniquestack;
+ $ans .= (scalar @uniquestack != 0) ? $uniquestack[-1] : "#";
+ }
+ }
+ return $ans;
+
+}
+
+print fnr("$ARGV[0]");
+
+=pod
+is_deeply( fnr("ababc") , "abb#c", "example1 provided");
+is_deeply( fnr("xyzzyx") , "xyzyx#", "example2 provided");
+is_deeply( fnr("abcdef") , "abcdef", "trival");
+is_deeply( fnr("aaabbb") , "a##b##", "repeats");
+is_deeply( fnr(
+ "thequickbrownfoxjumpsoverthelazydog") ,
+ "thequickbrownffxjjmpssvvvvvvlazyddg",
+ "long sentence"
+);
+=cut