aboutsummaryrefslogtreecommitdiff
path: root/challenge-063
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-063')
-rw-r--r--challenge-063/paulo-custodio/Makefile1
-rw-r--r--challenge-063/paulo-custodio/perl/ch-1.pl4
-rw-r--r--challenge-063/paulo-custodio/perl/ch-2.pl5
-rw-r--r--challenge-063/paulo-custodio/python/ch-1.py45
-rw-r--r--challenge-063/paulo-custodio/python/ch-2.py47
5 files changed, 98 insertions, 4 deletions
diff --git a/challenge-063/paulo-custodio/Makefile b/challenge-063/paulo-custodio/Makefile
index 0d5753b0fb..3eba7cf291 100644
--- a/challenge-063/paulo-custodio/Makefile
+++ b/challenge-063/paulo-custodio/Makefile
@@ -1,3 +1,4 @@
all:
perl perl/ch-1.pl
+ python3 python/ch-1.py
perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-063/paulo-custodio/perl/ch-1.pl b/challenge-063/paulo-custodio/perl/ch-1.pl
index d3900d26b7..bf8874c318 100644
--- a/challenge-063/paulo-custodio/perl/ch-1.pl
+++ b/challenge-063/paulo-custodio/perl/ch-1.pl
@@ -2,7 +2,7 @@
# Challenge 063
#
-# TASK #1 › Last Word
+# TASK #1 > Last Word
# Submitted by: Mohammad S Anwar
# Reviewed by: Ryan Thompson
#
@@ -10,7 +10,7 @@
# $regexp found in the given string, or undef if the string does not contain a
# word matching $regexp.
#
-# For this challenge, a “word” is defined as any character sequence consisting
+# For this challenge, a "word" is defined as any character sequence consisting
# of non-whitespace characters (\S) only. That means punctuation and other
# symbols are part of the word.
#
diff --git a/challenge-063/paulo-custodio/perl/ch-2.pl b/challenge-063/paulo-custodio/perl/ch-2.pl
index 70ad2a5098..c477a46a40 100644
--- a/challenge-063/paulo-custodio/perl/ch-2.pl
+++ b/challenge-063/paulo-custodio/perl/ch-2.pl
@@ -2,11 +2,12 @@
# Challenge 063
#
-# TASK #2 › Rotate String
+# TASK #2 > Rotate String
# Submitted by: Mohammad S Anwar
# Reviewed by: Ryan Thompson
#
-# Given a word made up of an arbitrary number of x and y characters, that word can be rotated as follows: For the ith rotation (starting at i = 1),
+# Given a word made up of an arbitrary number of x and y characters, that word
+# can be rotated as follows: For the ith rotation (starting at i = 1),
# i % length(word) characters are moved from the front of the string to the end.
# Thus, for the string xyxx, the initial (i = 1) % 4 = 1 character (x) is moved
# to the end, forming yxxx. On the second rotation, (i = 2) % 4 = 2 characters
diff --git a/challenge-063/paulo-custodio/python/ch-1.py b/challenge-063/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..5fb968ca0d
--- /dev/null
+++ b/challenge-063/paulo-custodio/python/ch-1.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python3
+
+# Challenge 063
+#
+# TASK #1 > Last Word
+# Submitted by: Mohammad S Anwar
+# Reviewed by: Ryan Thompson
+#
+# Define sub last_word($string, $regexp) that returns the last word matching
+# $regexp found in the given string, or undef if the string does not contain a
+# word matching $regexp.
+#
+# For this challenge, a "word" is defined as any character sequence consisting
+# of non-whitespace characters (\S) only. That means punctuation and other
+# symbols are part of the word.
+#
+# The $regexp is a regular expression. Take care that the regexp can only match
+# individual words! See the Examples for one way this can break if you are not
+# careful.
+#
+# Examples
+# last_word(' hello world', qr/[ea]l/); # 'hello'
+# last_word("Don't match too much, Chet!", qr/ch.t/i); # 'Chet!'
+# last_word("spaces in regexp won't match", qr/in re/); # undef
+# last_word( join(' ', 1..1e6), qr/^(3.*?){3}/); # '399933'
+
+import re
+import unittest
+
+def last_word(text, pattern):
+ words = text.split(' ')
+ for word in reversed(words):
+ if re.search(pattern, word, re.IGNORECASE):
+ return word
+ return None
+
+class TestLastWord(unittest.TestCase):
+ def test_last_word(self):
+ self.assertEqual(last_word(' hello world', r'[ea]l'), 'hello')
+ self.assertEqual(last_word("Don't match too much, Chet!", r'ch.t'), 'Chet!')
+ self.assertIsNone(last_word("spaces in regexp won't match", r'in re'))
+ self.assertEqual(last_word(' '.join(str(i) for i in range(1, int(1e6) + 1)), r'^(3.*?){3}'), '399933')
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/challenge-063/paulo-custodio/python/ch-2.py b/challenge-063/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..37f0bab8d2
--- /dev/null
+++ b/challenge-063/paulo-custodio/python/ch-2.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+
+# Challenge 063
+#
+# TASK #2 > Rotate String
+# Submitted by: Mohammad S Anwar
+# Reviewed by: Ryan Thompson
+#
+# Given a word made up of an arbitrary number of x and y characters, that word
+# can be rotated as follows: For the ith rotation (starting at i = 1),
+# i % length(word) characters are moved from the front of the string to the end.
+# Thus, for the string xyxx, the initial (i = 1) % 4 = 1 character (x) is moved
+# to the end, forming yxxx. On the second rotation, (i = 2) % 4 = 2 characters
+# (yx) are moved to the end, forming xxyx, and so on. See below for a complete
+# example.
+#
+# Your task is to write a function that takes a string of xs and ys and returns
+# the minimum non-zero number of rotations required to obtain the original
+# string. You may show the individual rotations if you wish, but that is not
+# required.
+#
+# Example
+# Input: $word = 'xyxx';
+#
+# Rotation 1: you get yxxx by moving x to the end.
+# Rotation 2: you get xxyx by moving yx to the end.
+# Rotation 3: you get xxxy by moving xxy to the end.
+# Rotation 4: you get xxxy by moving nothing as 4 % length(xyxx) == 0.
+# Rotation 5: you get xxyx by moving x to the end.
+# Rotation 6: you get yxxx by moving xx to the end.
+# Rotation 7: you get xyxx by moving yxx to the end which is same as the given word.
+# Output: 7
+
+import sys
+
+def count_rotations(in_str=""):
+ count = 0
+ str_val = in_str
+ while True:
+ count += 1
+ move = count % len(str_val)
+ str_val = str_val[move:] + str_val[:move]
+ if in_str == str_val:
+ break
+ return count
+
+print(count_rotations(sys.argv[1] if len(sys.argv) > 1 else ""))