aboutsummaryrefslogtreecommitdiff
path: root/challenge-063/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2024-09-18 19:56:42 -0400
committerDave Jacoby <jacoby.david@gmail.com>2024-09-18 19:56:42 -0400
commitf86f5e2fec16020c1d86f9028fb0f61cfeac106e (patch)
tree0fd388a696b51ffde5a7bfe8519a74e1caf42461 /challenge-063/paulo-custodio/python/ch-2.py
parentff8719c86653d5ad3121955e9494a0010527c2b9 (diff)
parent0052ec63ca70eaa6d9ffb1926c294dbfd85f8c05 (diff)
downloadperlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.tar.gz
perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.tar.bz2
perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-063/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-063/paulo-custodio/python/ch-2.py47
1 files changed, 47 insertions, 0 deletions
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 ""))