aboutsummaryrefslogtreecommitdiff
path: root/challenge-074
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-09-24 10:23:09 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-09-24 10:23:09 +0100
commitd3415eb4ede0c67098433eda84e84843267a7d62 (patch)
tree9a8e610a72c778a1041753069686175c19d0031b /challenge-074
parent915160b5e8bee9e5a712a9fcc6b047b788c78828 (diff)
downloadperlweeklychallenge-club-d3415eb4ede0c67098433eda84e84843267a7d62.tar.gz
perlweeklychallenge-club-d3415eb4ede0c67098433eda84e84843267a7d62.tar.bz2
perlweeklychallenge-club-d3415eb4ede0c67098433eda84e84843267a7d62.zip
Add Python solution to challenge 074
Diffstat (limited to 'challenge-074')
-rw-r--r--challenge-074/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-074/paulo-custodio/perl/ch-2.pl32
-rw-r--r--challenge-074/paulo-custodio/python/ch-1.py45
-rw-r--r--challenge-074/paulo-custodio/python/ch-2.py54
4 files changed, 116 insertions, 17 deletions
diff --git a/challenge-074/paulo-custodio/perl/ch-1.pl b/challenge-074/paulo-custodio/perl/ch-1.pl
index a97295a806..04cb491c3f 100644
--- a/challenge-074/paulo-custodio/perl/ch-1.pl
+++ b/challenge-074/paulo-custodio/perl/ch-1.pl
@@ -2,7 +2,7 @@
# Challenge 074
#
-# TASK #1 › Majority Element
+# TASK #1 > Majority Element
# Submitted by: Mohammad S Anwar
# You are given an array of integers of size $N.
#
diff --git a/challenge-074/paulo-custodio/perl/ch-2.pl b/challenge-074/paulo-custodio/perl/ch-2.pl
index 7c81bac6ab..da34170991 100644
--- a/challenge-074/paulo-custodio/perl/ch-2.pl
+++ b/challenge-074/paulo-custodio/perl/ch-2.pl
@@ -2,7 +2,7 @@
# Challenge 074
#
-# TASK #2 › FNR Character
+# TASK #2 > FNR Character
# Submitted by: Mohammad S Anwar
# You are given a string $S.
#
@@ -10,23 +10,23 @@
# -> 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’
+# 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 ‘#’
+# 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 '#'
use Modern::Perl;
diff --git a/challenge-074/paulo-custodio/python/ch-1.py b/challenge-074/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..cce27f69b9
--- /dev/null
+++ b/challenge-074/paulo-custodio/python/ch-1.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python3
+
+# Challenge 074
+#
+# TASK #1 > Majority Element
+# Submitted by: Mohammad S Anwar
+# You are given an array of integers of size $N.
+#
+# Write a script to find the majority element. If none found then print -1.
+#
+# Majority element in the list is the one that appears more than
+# floor(size_of_list/2).
+#
+# Example 1
+# Input: @A = (1, 2, 2, 3, 2, 4, 2)
+# Output: 2, as 2 appears 4 times in the list which is more than floor(7/2).
+#
+# Example 2
+# Input: @A = (1, 3, 1, 2, 4, 5)
+# Output: -1 as none of the elements appears more than floor(6/2).
+
+import sys
+
+def majority_elem(a):
+ # count instances of each element, get max
+ count = {}
+ max_count = 0
+ max_elem = None
+ for x in a:
+ if x in count:
+ count[x] += 1
+ else:
+ count[x] = 1
+
+ if count[x] > max_count:
+ max_count, max_elem = count[x], x
+
+ # check if majority
+ if max_count > int(len(a)/2):
+ return max_elem
+ else:
+ return -1
+
+a = [int(x) for x in sys.argv[1:]]
+print(majority_elem(a))
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))