aboutsummaryrefslogtreecommitdiff
path: root/challenge-247/pokgopun/python/ch-2.py
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2023-12-12 20:41:18 +1100
committerMichael Manring <michael@manring>2023-12-12 20:41:18 +1100
commitc6413356e405abf0255e5f901ea2a10ca71b656b (patch)
tree82a1f2aec1cb74a6bc74e3840c23dbd7e5747b06 /challenge-247/pokgopun/python/ch-2.py
parent98df168725aa587bd4db1e24018dae1ca77b29da (diff)
downloadperlweeklychallenge-club-c6413356e405abf0255e5f901ea2a10ca71b656b.tar.gz
perlweeklychallenge-club-c6413356e405abf0255e5f901ea2a10ca71b656b.tar.bz2
perlweeklychallenge-club-c6413356e405abf0255e5f901ea2a10ca71b656b.zip
pwc247 solution in python
Diffstat (limited to 'challenge-247/pokgopun/python/ch-2.py')
-rw-r--r--challenge-247/pokgopun/python/ch-2.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/challenge-247/pokgopun/python/ch-2.py b/challenge-247/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..54c77654be
--- /dev/null
+++ b/challenge-247/pokgopun/python/ch-2.py
@@ -0,0 +1,65 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-247/
+"""
+
+Task 2: Most Frequent Letter Pair
+
+Submitted by: [55]Jorg Sommrey
+ __________________________________________________________________
+
+ You are given a string S of lower case letters 'a'..'z'.
+
+ Write a script that finds the pair of consecutive letters in S that
+ appears most frequently. If there is more than one such pair, chose the
+ one that is the lexicographically first.
+
+Example 1
+
+Input: $s = 'abcdbca'
+Output: 'bc'
+
+'bc' appears twice in `$s`
+
+Example 2
+
+Input: $s = 'cdeabeabfcdfabgcd'
+Output: 'ab'
+
+'ab' and 'cd' both appear three times in $s and 'ab' is lexicographically smalle
+r than 'cd'.
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 17th December
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def mflp(string: str):
+ l = len(string)
+ if l <= 2:
+ return ""
+ dct = dict()
+ for i in range(l-1):
+ pair = string[i:i+2]
+ if dct.get(pair) is None:
+ dct[pair] = 1
+ else:
+ dct[pair] += 1
+ lst = list((-dct[x],x) for x in dct.keys())
+ lst.sort()
+ #print(lst)
+ return lst[0][1]
+
+import unittest
+
+class TestMflp(unittest.TestCase):
+ def test1(self):
+ for inpt, otpt in {
+ 'abcdbca': 'bc',
+ 'cdeabeabfcdfabgcd': 'ab',
+ }.items():
+ self.assertEqual(mflp(inpt),otpt,f"error: most frequent letter pair of '{inpt}' must be '{otpt}'")
+
+unittest.main()