aboutsummaryrefslogtreecommitdiff
path: root/challenge-273/bruce-gray/python/ch-1.py
diff options
context:
space:
mode:
authorUtil <bruce.gray@acm.org>2024-06-15 21:38:47 -0500
committerUtil <bruce.gray@acm.org>2024-06-15 21:38:47 -0500
commit72ee76ae0df98e4c506a597210a9fb7fecc27e1c (patch)
tree18c71731b0ac6d28df3356e3bffb08dfe3f95c1f /challenge-273/bruce-gray/python/ch-1.py
parent9ef842e7e7d8cfe407f22fa985dad2455ab3c227 (diff)
downloadperlweeklychallenge-club-72ee76ae0df98e4c506a597210a9fb7fecc27e1c.tar.gz
perlweeklychallenge-club-72ee76ae0df98e4c506a597210a9fb7fecc27e1c.tar.bz2
perlweeklychallenge-club-72ee76ae0df98e4c506a597210a9fb7fecc27e1c.zip
Add TWC 273 solutions by Bruce Gray, in Raku, Perl, and Python.
Diffstat (limited to 'challenge-273/bruce-gray/python/ch-1.py')
-rwxr-xr-xchallenge-273/bruce-gray/python/ch-1.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/challenge-273/bruce-gray/python/ch-1.py b/challenge-273/bruce-gray/python/ch-1.py
new file mode 100755
index 0000000000..804a3359a6
--- /dev/null
+++ b/challenge-273/bruce-gray/python/ch-1.py
@@ -0,0 +1,22 @@
+#!python
+import math
+def lround(n):
+ return math.floor(n + 0.5)
+
+def task1(str, char):
+ return lround(100 * str.count(char) / len(str))
+
+
+import unittest
+class TestTask1(unittest.TestCase):
+ def t(self, str, char, expected):
+ self.assertEqual(task1(str, char), expected, f"task1('{str}', '{char}')")
+
+ def test_1(self): self.t('perl' , 'e', 25)
+ def test_2(self): self.t('java' , 'a', 50)
+ def test_3(self): self.t('python' , 'm', 0)
+ def test_4(self): self.t('ada' , 'a', 67)
+ def test_5(self): self.t('ballerina' , 'l', 22)
+ def test_6(self): self.t('analitik' , 'k', 13)
+
+unittest.main()