aboutsummaryrefslogtreecommitdiff
path: root/challenge-180/mohammad-anwar/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-180/mohammad-anwar/python')
-rw-r--r--challenge-180/mohammad-anwar/python/ch-1.py52
-rw-r--r--challenge-180/mohammad-anwar/python/ch-2.py37
2 files changed, 89 insertions, 0 deletions
diff --git a/challenge-180/mohammad-anwar/python/ch-1.py b/challenge-180/mohammad-anwar/python/ch-1.py
new file mode 100644
index 0000000000..5ce058ecf5
--- /dev/null
+++ b/challenge-180/mohammad-anwar/python/ch-1.py
@@ -0,0 +1,52 @@
+#!/usr/bin/python3
+
+'''
+
+Week 180:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-180
+
+Task #1: First Unique Character
+
+ You are given a string, $s.
+
+ Write a script to find out the first unique character in the
+ given string and print its index (0-based).
+
+'''
+
+import unittest
+
+def firstUniqueCharacter(str) -> int:
+ char_list = []
+ char_hash = {}
+ for c in list(str):
+ if c == ' ':
+ next
+ c = c.lower()
+
+ if c in char_hash:
+ char_hash[c] += 1
+ else:
+ char_list.append(c)
+ char_hash[c] = 1
+
+ i = 0
+ for c in char_list:
+ if char_hash[c] == 1:
+ return i
+
+ i = i + 1
+
+ return i
+
+#
+#
+# Unit test class
+
+class TestFirstUniqueCharacter(unittest.TestCase):
+ def test_FirstUniqueCharacter(self):
+ self.assertEqual(firstUniqueCharacter('Perl Weekly Challenge'), 0)
+ self.assertEqual(firstUniqueCharacter('Long Live Perl'), 1)
+
+unittest.main()
diff --git a/challenge-180/mohammad-anwar/python/ch-2.py b/challenge-180/mohammad-anwar/python/ch-2.py
new file mode 100644
index 0000000000..127805093f
--- /dev/null
+++ b/challenge-180/mohammad-anwar/python/ch-2.py
@@ -0,0 +1,37 @@
+#!/usr/bin/python3
+
+'''
+
+Week 180:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-180
+
+Task #2: Trim List
+
+ You are given list of numbers, @n and an integer $i.
+
+ Write a script to trim the given list where element is less than
+ or equal to the given integer.
+
+'''
+
+import unittest
+
+def trimList(i, n):
+ trim_list = []
+ for j in n:
+ if j > i:
+ trim_list.append(j)
+
+ return trim_list
+
+#
+#
+# Unit test class
+
+class TestTrimList(unittest.TestCase):
+ def test_TrimList(self):
+ self.assertEqual(trimList(3, [1, 4, 2, 3, 5]), [4, 5])
+ self.assertEqual(trimList(4, [9, 0, 6, 2, 3, 8, 5]), [9, 6, 8, 5])
+
+unittest.main()