aboutsummaryrefslogtreecommitdiff
path: root/challenge-282/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-09-05 18:43:38 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-09-05 18:43:38 +0100
commit73c1b95cf0dd273786e892c654a76da97658eea1 (patch)
treeb8d09032813e9525bed2db20025d4fb9bb5b50f2 /challenge-282/paulo-custodio/python/ch-2.py
parentf10dac9c38d04b762a0ce20fb1be160fd0d9b02c (diff)
downloadperlweeklychallenge-club-73c1b95cf0dd273786e892c654a76da97658eea1.tar.gz
perlweeklychallenge-club-73c1b95cf0dd273786e892c654a76da97658eea1.tar.bz2
perlweeklychallenge-club-73c1b95cf0dd273786e892c654a76da97658eea1.zip
Add Python solution to challenge 282
Diffstat (limited to 'challenge-282/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-282/paulo-custodio/python/ch-2.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-282/paulo-custodio/python/ch-2.py b/challenge-282/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..aa1f15080a
--- /dev/null
+++ b/challenge-282/paulo-custodio/python/ch-2.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env perl
+
+# Challenge 282
+#
+# Task 2: Changing Keys
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given an alphabetic string, $str, as typed by user.
+#
+# Write a script to find the number of times user had to change the key to type
+# the given string. Changing key is defined as using a key different from the
+# last used key. The shift and caps lock keys won't be counted.
+#
+# Example 1
+#
+# Input: $str = 'pPeERrLl'
+# Ouput: 3
+#
+# p -> P : 0 key change
+# P -> e : 1 key change
+# e -> E : 0 key change
+# E -> R : 1 key change
+# R -> r : 0 key change
+# r -> L : 1 key change
+# L -> l : 0 key change
+#
+# Example 2
+#
+# Input: $str = 'rRr'
+# Ouput: 0
+#
+# Example 3
+#
+# Input: $str = 'GoO'
+# Ouput: 1
+
+import re
+import sys
+
+str = sys.argv[1].upper()
+str, count = re.subn(r'(.)\1*', r'\1', str)
+
+print(len(str)-1)