aboutsummaryrefslogtreecommitdiff
path: root/challenge-111/colin-crain/python
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-05-09 11:13:18 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-05-09 11:13:18 +0100
commitcf696513d18df1e5ebc2b8ef91039d91b620a4eb (patch)
treefff50dea6d6b456c1a63c5c2ed9744b63e244308 /challenge-111/colin-crain/python
parent6b9623ccf51641c88c81bbc7dc0c563d7d77b9d9 (diff)
downloadperlweeklychallenge-club-cf696513d18df1e5ebc2b8ef91039d91b620a4eb.tar.gz
perlweeklychallenge-club-cf696513d18df1e5ebc2b8ef91039d91b620a4eb.tar.bz2
perlweeklychallenge-club-cf696513d18df1e5ebc2b8ef91039d91b620a4eb.zip
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-111/colin-crain/python')
-rw-r--r--challenge-111/colin-crain/python/ch-1.py66
-rw-r--r--challenge-111/colin-crain/python/ch-2.py48
2 files changed, 114 insertions, 0 deletions
diff --git a/challenge-111/colin-crain/python/ch-1.py b/challenge-111/colin-crain/python/ch-1.py
new file mode 100644
index 0000000000..e9dad11ff7
--- /dev/null
+++ b/challenge-111/colin-crain/python/ch-1.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python3
+#
+#
+# are-we-in.py
+#
+# Search Matrix
+# Submitted by: Mohammad S Anwar
+# You are given 5x5 matrix filled with integers such that each row is
+# sorted from left to right and the first integer of each row is greater
+# than the last integer of the previous row.
+#
+# Write a script to find a given integer in the matrix using an
+# efficient search algorithm.
+#
+# Example
+#
+# Matrix: [ 1, 2, 3, 5, 7 ]
+# [ 9, 11, 15, 19, 20 ]
+# [ 23, 24, 25, 29, 31 ]
+# [ 32, 33, 39, 40, 42 ]
+# [ 45, 47, 48, 49, 50 ]
+#
+# Input: 35
+# Output: 0 since it is missing in the matrix
+#
+# Input: 39
+# Output: 1 as it exists in the matrix
+#
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+import sys
+
+def inArray(mat, val):
+ if (val > mat[-1][-1]) | (val < mat[0][0]):
+ return False
+ row = 0
+ while val > mat[row][-1]:
+ row += 1
+ return val in mat[row]
+
+
+
+mat = [[ 1, 2, 3, 5, 7 ],
+ [ 9, 11, 15, 19, 20 ],
+ [ 23, 24, 25, 29, 31 ],
+ [ 32, 33, 39, 40, 42 ],
+ [ 45, 47, 48, 49, 50 ]] ;
+
+default = 22
+
+if len(sys.argv) == 1:
+ val = default
+else:
+ val = sys.argv[1]
+
+res = inArray(mat, val)
+
+print(res)
+
+
+
+
+
diff --git a/challenge-111/colin-crain/python/ch-2.py b/challenge-111/colin-crain/python/ch-2.py
new file mode 100644
index 0000000000..b7aadfd3b0
--- /dev/null
+++ b/challenge-111/colin-crain/python/ch-2.py
@@ -0,0 +1,48 @@
+#!/usr/bin/env python3
+#
+#
+# get-in-line.py
+#
+# Ordered Letters
+# Submitted by: E. Choroba
+#
+# Given a word, you can sort its letters alphabetically (case
+# insensitive). For example, “beekeeper” becomes “beeeeekpr” and
+# “dictionary” becomes “acdiinorty”.
+#
+# Write a script to find the longest English words that don’t change
+# when their letters are sorted.
+#
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+dictFile = '/usr/share/dict/words'
+
+def isSorted(word):
+ if len(word) < 3:
+ return False
+ for i in range(1, len(word)):
+ if word[i-1] > word[i]:
+ return False
+ return True
+
+bag = {}
+
+f = open(dictFile, "r")
+for line in f:
+ line = line.rstrip('\r\n').lower()
+ if isSorted(line):
+ bag[line] = len(line)
+f.close
+
+maxLen = max( v for v in bag.values() )
+longWords = [ w for w in bag if len(w) == maxLen ]
+
+print("longest word length", maxLen, "letters", "\n")
+for word in longWords:
+ print(word)
+
+