aboutsummaryrefslogtreecommitdiff
path: root/challenge-193/sgreen/python
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-04 10:31:51 +0000
committerGitHub <noreply@github.com>2022-12-04 10:31:51 +0000
commite151ae2f7983d40d1108fb96917eb4763fb1199f (patch)
treeede74f5aec18b0469147fdd2b31923ce3e258938 /challenge-193/sgreen/python
parentb531cbef5f8fa8e9266e010cb5d47d7fd1457c65 (diff)
parent1d1515048145eb2257eca3c6a02de75491e15848 (diff)
downloadperlweeklychallenge-club-e151ae2f7983d40d1108fb96917eb4763fb1199f.tar.gz
perlweeklychallenge-club-e151ae2f7983d40d1108fb96917eb4763fb1199f.tar.bz2
perlweeklychallenge-club-e151ae2f7983d40d1108fb96917eb4763fb1199f.zip
Merge pull request #7200 from simongreen-net/master
Simon's solution to challenge 193
Diffstat (limited to 'challenge-193/sgreen/python')
-rwxr-xr-xchallenge-193/sgreen/python/ch-1.py18
-rwxr-xr-xchallenge-193/sgreen/python/ch-2.py39
2 files changed, 57 insertions, 0 deletions
diff --git a/challenge-193/sgreen/python/ch-1.py b/challenge-193/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..7497e1c61d
--- /dev/null
+++ b/challenge-193/sgreen/python/ch-1.py
@@ -0,0 +1,18 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(n):
+ # The format we want (a binary string of a given length)
+ fmt = f'0{n}b'
+
+ # Make a list with all possible binary values
+ l = [format(x, fmt) for x in range(2**n)]
+
+ # Show the list
+ print(*l, sep=', ')
+
+
+if __name__ == '__main__':
+ main(int(sys.argv[1]))
diff --git a/challenge-193/sgreen/python/ch-2.py b/challenge-193/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..cdc2d772f6
--- /dev/null
+++ b/challenge-193/sgreen/python/ch-2.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+import string
+import sys
+
+
+def main(s):
+ # Create letter to number mapping
+ letter_map = {}
+ for k, v in enumerate(string.ascii_lowercase):
+ letter_map[v] = k
+
+ occurrences = {}
+ for word in s:
+ # Calculate the differences between characters, and store this
+ # as a space separated string
+ diff = ' '.join([
+ str(letter_map[word[i]] - letter_map[word[i-1]])
+ for i in range(1, len(word))
+ ])
+
+ # Add the word to the occurrences dict
+ if diff not in occurrences:
+ occurrences[diff] = []
+ occurrences[diff].append(word)
+
+ # Find the unique words
+ unique_words = [v[0] for v in occurrences.values() if len(v) == 1]
+
+ if len(unique_words) > 1:
+ print('More than one unique string!')
+ elif len(unique_words) == 0:
+ print('No unique strings!')
+ else:
+ print(unique_words[0])
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])