aboutsummaryrefslogtreecommitdiff
path: root/challenge-214/roger-bell-west/python/ch-1.py
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-05-02 14:47:44 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-05-02 14:47:44 +0800
commitfb913ef4e973a29450b92ca89588e839bf6635f6 (patch)
tree94abff55c6006a2b0a4b1da93a5e661753e00fea /challenge-214/roger-bell-west/python/ch-1.py
parentefea8410c80647939ae587536a4f1f66dd2c3a4a (diff)
parent6cc2e38f43011f65d7deaf1e03cf55e4306a53e5 (diff)
downloadperlweeklychallenge-club-fb913ef4e973a29450b92ca89588e839bf6635f6.tar.gz
perlweeklychallenge-club-fb913ef4e973a29450b92ca89588e839bf6635f6.tar.bz2
perlweeklychallenge-club-fb913ef4e973a29450b92ca89588e839bf6635f6.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-214/roger-bell-west/python/ch-1.py')
-rwxr-xr-xchallenge-214/roger-bell-west/python/ch-1.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/challenge-214/roger-bell-west/python/ch-1.py b/challenge-214/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..7eaa04b4f5
--- /dev/null
+++ b/challenge-214/roger-bell-west/python/ch-1.py
@@ -0,0 +1,44 @@
+#! /usr/bin/python3
+
+import unittest
+
+def rankscore(a):
+ av = dict()
+ for s in a:
+ if s in av:
+ av[s] += 1
+ else:
+ av[s] = 1
+ kk = list(av.keys())
+ kk.sort()
+ kk.reverse()
+ rank = 1
+ tab = dict()
+ for k in kk:
+ siz = av[k]
+ r = ""
+ if rank <= 3:
+ r = ["G", "S", "B"][rank - 1]
+ else:
+ r = str(rank)
+ if siz > 1:
+ r += "="
+ tab[k] = r
+ rank += siz
+ return [tab[i] for i in a]
+
+class TestRankscore(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(rankscore([1, 2, 4, 3, 5]), ["5", "4", "S", "B", "G"], 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(rankscore([8, 5, 6, 7, 4]), ["G", "4", "B", "S", "5"], 'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(rankscore([3, 5, 4, 2]), ["B", "G", "S", "4"], 'example 3')
+
+ def test_ex4(self):
+ self.assertEqual(rankscore([2, 5, 2, 1, 7, 5, 1]), ["4=", "S=", "4=", "6=", "G", "S=", "6="], 'example 4')
+
+unittest.main()