aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-05-04 07:01:45 +0100
committerGitHub <noreply@github.com>2021-05-04 07:01:45 +0100
commit7c7291d9a792064aba44a3128ff79e74f7e36d7d (patch)
tree1cfab5f5dbb4764645e4c591087a254985bb7505
parentce61dcb0866e4414a04c68ffaa1aa234fe1ec7cc (diff)
parent265f43b0fd1793275cb97a3f735c7a545508ac0e (diff)
downloadperlweeklychallenge-club-7c7291d9a792064aba44a3128ff79e74f7e36d7d.tar.gz
perlweeklychallenge-club-7c7291d9a792064aba44a3128ff79e74f7e36d7d.tar.bz2
perlweeklychallenge-club-7c7291d9a792064aba44a3128ff79e74f7e36d7d.zip
Merge pull request #4012 from stuart-little/stuart-little_111_python
1st commit on 111_python
-rwxr-xr-xchallenge-111/stuart-little/python/ch-1.py32
-rwxr-xr-xchallenge-111/stuart-little/python/ch-2.py29
2 files changed, 61 insertions, 0 deletions
diff --git a/challenge-111/stuart-little/python/ch-1.py b/challenge-111/stuart-little/python/ch-1.py
new file mode 100755
index 0000000000..1457965e30
--- /dev/null
+++ b/challenge-111/stuart-little/python/ch-1.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python
+
+# run <script>
+
+import bisect
+import itertools
+import sys
+
+def searchMatrix(needle,mat):
+ flt = list(itertools.chain(*mat))
+ i = bisect.bisect_left(flt, needle)
+ if i != len(flt) and flt[i] == needle:
+ return 1
+ return 0
+
+ar = [
+ [ 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 ],
+]
+
+print("Array:")
+for row in ar:
+ print(row)
+print("")
+
+toSearch=(1,35,39,100)
+for x in toSearch:
+ print(f"Found {x}?")
+ print(searchMatrix(x,ar))
diff --git a/challenge-111/stuart-little/python/ch-2.py b/challenge-111/stuart-little/python/ch-2.py
new file mode 100755
index 0000000000..4b465407d6
--- /dev/null
+++ b/challenge-111/stuart-little/python/ch-2.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+
+# run <script> <path-to-dict-file, one word per line>
+
+import sys
+
+def isSorted(wrd):
+ return wrd.isalpha() and all([wrd.lower()[i] <= wrd.lower()[i+1] for i in range(0,len(wrd)-1)])
+
+def longestWith(lst,pred):
+ lngth=0
+ res=[]
+ for wrd in lst:
+ if (not pred(wrd)):
+ continue
+ l=len(wrd)
+ if l > lngth:
+ lngth = l
+ res=[wrd]
+ continue
+ if l == lngth:
+ res.append(wrd)
+ return res
+
+with open(sys.argv[1]) as fh:
+ wrds=filter(lambda ln: len(ln) > 0, map(lambda ln: ln.strip(), fh.readlines()))
+
+for wrd in longestWith(wrds,isSorted):
+ print(wrd)