diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-15 21:01:53 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-15 21:01:53 +0000 |
| commit | c9792b8a02f80c49e9bb4f0a1056c98d41e693a4 (patch) | |
| tree | 290c0fe2def621c2f251befea6bd4086b6a9af84 | |
| parent | b4722f365b56ebb8a01ce4d4b136f16d26698cd6 (diff) | |
| parent | 15ff62419201a3142b71d16cb5e919334a92d097 (diff) | |
| download | perlweeklychallenge-club-c9792b8a02f80c49e9bb4f0a1056c98d41e693a4.tar.gz perlweeklychallenge-club-c9792b8a02f80c49e9bb4f0a1056c98d41e693a4.tar.bz2 perlweeklychallenge-club-c9792b8a02f80c49e9bb4f0a1056c98d41e693a4.zip | |
Merge pull request #9590 from zapwai/branch-for-recent-python
Recent Python scripts
| -rw-r--r-- | challenge-250/zapwai/python/ch-1.py | 17 | ||||
| -rw-r--r-- | challenge-250/zapwai/python/ch-2.py | 13 | ||||
| -rw-r--r-- | challenge-251/zapwai/python/ch-1.py | 18 | ||||
| -rw-r--r-- | challenge-251/zapwai/python/ch-2.py | 24 | ||||
| -rw-r--r-- | challenge-252/zapwai/python/ch-1.py | 14 | ||||
| -rw-r--r-- | challenge-252/zapwai/python/ch-2.py | 13 | ||||
| -rw-r--r-- | challenge-253/zapwai/python/ch-1.py | 17 | ||||
| -rw-r--r-- | challenge-253/zapwai/python/ch-2.py | 53 | ||||
| -rw-r--r-- | challenge-254/zapwai/python/ch-1.py | 17 | ||||
| -rw-r--r-- | challenge-254/zapwai/python/ch-2.py | 23 |
10 files changed, 209 insertions, 0 deletions
diff --git a/challenge-250/zapwai/python/ch-1.py b/challenge-250/zapwai/python/ch-1.py new file mode 100644 index 0000000000..5a2619e08c --- /dev/null +++ b/challenge-250/zapwai/python/ch-1.py @@ -0,0 +1,17 @@ +def proc(ints): + index = -1 + for i in range(len(ints)): + if i % 10 == ints[i]: + index = i + break + print("Input: ", ints) + print("Output: ", index) + +ints = [0, 1, 2] +proc(ints) + +ints = [4,3,2,1] +proc(ints) + +ints = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] +proc(ints) diff --git a/challenge-250/zapwai/python/ch-2.py b/challenge-250/zapwai/python/ch-2.py new file mode 100644 index 0000000000..b7d48ba00c --- /dev/null +++ b/challenge-250/zapwai/python/ch-2.py @@ -0,0 +1,13 @@ +import re +def proc(a): + nums = list( map( lambda x: len(x), a ) ) + pattern = r'\b(\d+)\b' + for i in range(len(a)): + if re.search(pattern,a[i]): + nums[i] = int(re.search(pattern, a[i]).group(1)) + print("Input: ", a) + print("Output: ", max(nums)) +a = ("perl", "2", "000", "python", "r4ku"); +proc(a) +a = ("001", "1", "000", "0001"); +proc(a) diff --git a/challenge-251/zapwai/python/ch-1.py b/challenge-251/zapwai/python/ch-1.py new file mode 100644 index 0000000000..380969324a --- /dev/null +++ b/challenge-251/zapwai/python/ch-1.py @@ -0,0 +1,18 @@ +def proc(ints): + print("Input: ints =", ints) + sum = 0 + while (ints): + a = ints[0] + if len(ints) == 1: + sum += a + break + ints = ints[1:] + b = ints.pop() + sum += int(str(a)+str(b)) + print("Output:", sum) +ints = [6, 12, 25, 1] +proc(ints) +ints = [10, 7, 31, 5, 2, 2] +proc(ints) +ints = [1, 2, 10] +proc(ints) diff --git a/challenge-251/zapwai/python/ch-2.py b/challenge-251/zapwai/python/ch-2.py new file mode 100644 index 0000000000..a431f815a3 --- /dev/null +++ b/challenge-251/zapwai/python/ch-2.py @@ -0,0 +1,24 @@ +m = [ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17], + ] + +print("Input: M = ") +for i in range(len(m)): + print("\t",m[i]) + +luck = -1 +mins = [] +maxs = [] +for i in range(len(m)): + mins.append( min(m[i]) ) +for j in range(len(m[0])): + col = [] + for i in range(len(m)): + col.append( m[i][j] ) + maxs.append( max(col) ) +for val in mins: + for val2 in maxs: + if val == val2: + print(f"Output: {val}") + break diff --git a/challenge-252/zapwai/python/ch-1.py b/challenge-252/zapwai/python/ch-1.py new file mode 100644 index 0000000000..541d927568 --- /dev/null +++ b/challenge-252/zapwai/python/ch-1.py @@ -0,0 +1,14 @@ +def proc(ints): + spec = [] + for i in range(len(ints)): + if len(ints) % (i+1) == 0: + spec.append(ints[i]) + sum = 0 + for v in spec: + sum += v*v + print("Input: ints =", ints) + print(f"Output: {sum}") +ints = [1,2,3,4] +proc(ints) +ints = [2, 7, 1, 19, 18, 3] +proc(ints) diff --git a/challenge-252/zapwai/python/ch-2.py b/challenge-252/zapwai/python/ch-2.py new file mode 100644 index 0000000000..998cba13e9 --- /dev/null +++ b/challenge-252/zapwai/python/ch-2.py @@ -0,0 +1,13 @@ +def proc(n): + print(f"Input : n = {n}") + l = [] + if n % 2 == 1: + l.append(0) + k = int( (n - 1) / 2 ) + val = 1 + for i in range(k): + l.append(val); l.append(-1*val) + val += 1 + print("Output:", l) +for n in [5, 3, 1]: + proc(n) diff --git a/challenge-253/zapwai/python/ch-1.py b/challenge-253/zapwai/python/ch-1.py new file mode 100644 index 0000000000..2c3aa4e255 --- /dev/null +++ b/challenge-253/zapwai/python/ch-1.py @@ -0,0 +1,17 @@ +def proc(words, separator): + list = [] + for word in words: + for subword in word.split(separator): + list.append(subword) + ans = [] + for l in list: + if len(l) > 0: + ans.append(l) + print("Input :", words, "separator =",separator) + print("Output:", ans) +words = ["one.two.three", "four.five", "six"] +separator = "." +proc(words, separator) +words = ["$perl$$", "$$raku$"] +separator = "$" +proc(words, separator) diff --git a/challenge-253/zapwai/python/ch-2.py b/challenge-253/zapwai/python/ch-2.py new file mode 100644 index 0000000000..5959d5bede --- /dev/null +++ b/challenge-253/zapwai/python/ch-2.py @@ -0,0 +1,53 @@ +def is_weaker(m, i, j): + rowi = m[i][:] + rowj = m[:][j] + (numi, numj) = (0, 0) + for u in rowi: + if u == 1: + numi += 1 + for v in rowj: + if v == 1: + numj += 1 + if numi < numj: + return 1 + elif (numi == numj and i < j): + return 1 + return 0 + +def mylist(length): + alist = list(range(length)) + while(1): + cnt = 0 + for i in range(length-1): + if not is_weaker(m, alist[i], alist[i+1]): + cnt += 1 + tmp = alist[i] + alist[i] = alist[i+1] + alist[i+1] = tmp + if cnt == 0: + break + return alist + +def proc(m): + print("Input: m = ") + for i in range(len(m)): + print("\t",m[i]) + list = mylist(len(m)) + print("\nOutput:", list,"\n") + +m = [ + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 1] +] +proc(m) + +m = [ + [1, 0, 0, 0], + [1, 1, 1, 1], + [1, 0, 0, 0], + [1, 0, 0, 0] +] +proc(m) diff --git a/challenge-254/zapwai/python/ch-1.py b/challenge-254/zapwai/python/ch-1.py new file mode 100644 index 0000000000..4e7bab74a8 --- /dev/null +++ b/challenge-254/zapwai/python/ch-1.py @@ -0,0 +1,17 @@ +def is_pow(n): + if n == 0: + return 0 + while(1): + if n % 3 == 0: + n = n / 3 + if n == 1: + return 1 + else: + return 0 + +def proc(n): + print("Input: n =", n) + print("Output:", is_pow(n)) + +for n in [27, 0, 6]: + proc(n) diff --git a/challenge-254/zapwai/python/ch-2.py b/challenge-254/zapwai/python/ch-2.py new file mode 100644 index 0000000000..084bb33383 --- /dev/null +++ b/challenge-254/zapwai/python/ch-2.py @@ -0,0 +1,23 @@ +def proc(s): + vow = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] + stash = [] + print("Input:", s) + for c in s: + if c in vow: + stash.append(c) + print("Output: ", end='') + for c in s: + if c in vow: + print(stash.pop(), end='') + else: + print(c, end='') + print() + +s = "Raku" +proc(s) +s = "Perl" +proc(s) +s = "Julia" +proc(s) +s = "Uiua" +proc(s) |
