aboutsummaryrefslogtreecommitdiff
path: root/challenge-329/sgreen/python/ch-2.py
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2025-07-13 22:16:28 +1000
committerSimon Green <mail@simon.green>2025-07-13 22:16:28 +1000
commit864c708aecefc7d2acd14f07a4f86b4a9c2e8082 (patch)
treeb0b180f8a0c5b2a16fd93688f4ef31fbf30e0c4f /challenge-329/sgreen/python/ch-2.py
parentbd7fce4bd5d085c209a213f2daca1e79799c9e87 (diff)
downloadperlweeklychallenge-club-864c708aecefc7d2acd14f07a4f86b4a9c2e8082.tar.gz
perlweeklychallenge-club-864c708aecefc7d2acd14f07a4f86b4a9c2e8082.tar.bz2
perlweeklychallenge-club-864c708aecefc7d2acd14f07a4f86b4a9c2e8082.zip
sgreen solutions to challenge 329
Diffstat (limited to 'challenge-329/sgreen/python/ch-2.py')
-rwxr-xr-xchallenge-329/sgreen/python/ch-2.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-329/sgreen/python/ch-2.py b/challenge-329/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..a06379efe2
--- /dev/null
+++ b/challenge-329/sgreen/python/ch-2.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python3
+
+import sys
+
+def is_nice_string(s: str) -> bool:
+ # Check that every letter has a matching letter in the opposite case
+ for letter in s:
+ if letter.islower() and letter.upper() not in s:
+ return False
+ if letter.isupper() and letter.lower() not in s:
+ return False
+ return True
+
+
+def nice_string(input_string: str) -> str|None:
+ solution = None
+ # Calculate every combination of starting and ending positions
+ for start in range(len(input_string)-1):
+ for end in range(start+2, len(input_string)+1):
+ # See if this is a nice string
+ extract = input_string[start:end]
+ if is_nice_string(extract):
+ # If it is longer than the current nice string, update the solution variable
+ if solution is None or len(solution) < len(extract):
+ solution = extract
+
+ return solution
+
+
+def main():
+ result = nice_string(sys.argv[1])
+ if result is None:
+ result = ''
+ print('"'+result+'"')
+
+
+if __name__ == '__main__':
+ main()