aboutsummaryrefslogtreecommitdiff
path: root/challenge-282/sgreen/python
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-08-18 15:09:16 +0100
committerGitHub <noreply@github.com>2024-08-18 15:09:16 +0100
commitd833fec8f6b78d515f045bcbe24f29e2ada46670 (patch)
treecbff229e0221aa7330b135761d8953973c11bddc /challenge-282/sgreen/python
parent786a7af32b40f7790ca44a06db0b0295d2a15927 (diff)
parentadd8d7fd4eb2ad39bd4e70bc0f2566b3d5528f21 (diff)
downloadperlweeklychallenge-club-d833fec8f6b78d515f045bcbe24f29e2ada46670.tar.gz
perlweeklychallenge-club-d833fec8f6b78d515f045bcbe24f29e2ada46670.tar.bz2
perlweeklychallenge-club-d833fec8f6b78d515f045bcbe24f29e2ada46670.zip
Merge pull request #10637 from simongreen-net/master
sgreen solutions to challenge 282
Diffstat (limited to 'challenge-282/sgreen/python')
-rwxr-xr-xchallenge-282/sgreen/python/ch-1.py30
-rwxr-xr-xchallenge-282/sgreen/python/ch-2.py32
-rwxr-xr-xchallenge-282/sgreen/python/test.py21
3 files changed, 83 insertions, 0 deletions
diff --git a/challenge-282/sgreen/python/ch-1.py b/challenge-282/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..91178462c1
--- /dev/null
+++ b/challenge-282/sgreen/python/ch-1.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def good_integer(n: int) -> str:
+ # Convert the integer to a string
+ value = str(n)
+ length = len(value)
+
+ for pos in range(length-2):
+ if (
+ value[pos] == value[pos+1]
+ and value[pos] == value[pos+2]
+ and (pos == 0 or value[pos] != value[pos-1])
+ and (pos + 3 == length or value[pos] != value[pos+3])
+ ):
+ return value[pos:pos+3]
+
+ return '-1'
+
+
+def main():
+ # Convert input into an integer
+ result = good_integer(int(sys.argv[1]))
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-282/sgreen/python/ch-2.py b/challenge-282/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..4673e3aef7
--- /dev/null
+++ b/challenge-282/sgreen/python/ch-2.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def key_changes(s: str) -> int:
+ # Convert the string to lower case
+ s = s.lower()
+
+ # Start with the first letter
+ current_key = s[0]
+
+ # Count the number of times we change keys
+ changes = 0
+
+ for letter in s:
+ if letter != current_key:
+ # We need to change key
+ current_key = letter
+ changes += 1
+
+ return changes
+
+
+def main():
+ # Convert input into integers
+ result = key_changes(sys.argv[1])
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-282/sgreen/python/test.py b/challenge-282/sgreen/python/test.py
new file mode 100755
index 0000000000..d9a717e276
--- /dev/null
+++ b/challenge-282/sgreen/python/test.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertEqual(ch_1.good_integer(12344456), '444')
+ self.assertEqual(ch_1.good_integer(1233334), '-1')
+ self.assertEqual(ch_1.good_integer(10020003), '000')
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.key_changes('pPeERrLl'), 3)
+ self.assertEqual(ch_2.key_changes('rRr'), 0)
+ self.assertEqual(ch_2.key_changes('GoO'), 1)
+
+
+if __name__ == '__main__':
+ unittest.main()