diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-07-07 22:30:46 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-07 22:30:46 +0100 |
| commit | dbfda338166ad93b7111056115cba860d9314d7c (patch) | |
| tree | a88cd326a10a469ed3d49c729624b23cd8032e34 | |
| parent | c511be5ac2dca2c3443e1259e70c1551cc37517f (diff) | |
| parent | 3dc19c692e316e365ef61ae7074776a1b96bbd8e (diff) | |
| download | perlweeklychallenge-club-dbfda338166ad93b7111056115cba860d9314d7c.tar.gz perlweeklychallenge-club-dbfda338166ad93b7111056115cba860d9314d7c.tar.bz2 perlweeklychallenge-club-dbfda338166ad93b7111056115cba860d9314d7c.zip | |
Merge pull request #1919 from waltman/branch-for-challenge-068
python code for challenge 68
| -rw-r--r-- | challenge-068/walt-mankowski/python/ch-1.py | 38 | ||||
| -rw-r--r-- | challenge-068/walt-mankowski/python/ch-2.py | 40 |
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-068/walt-mankowski/python/ch-1.py b/challenge-068/walt-mankowski/python/ch-1.py new file mode 100644 index 0000000000..b2c22a3923 --- /dev/null +++ b/challenge-068/walt-mankowski/python/ch-1.py @@ -0,0 +1,38 @@ +def print_mat(m): + for row in m: + print(row) + print() + +def zero_mat(m): + num_rows = len(m) + num_cols = len(m[0]) + + row_flags = [False for _ in range(num_rows)] + col_flags = [False for _ in range(num_cols)] + for r in range(num_rows): + for c in range(num_cols): + if m[r][c] == 0: + row_flags[r] = True + col_flags[c] = True + + for r in range(num_rows): + for c in range(num_cols): + if row_flags[r] or col_flags[c]: + m[r][c] = 0 + +m1 = [[1,0,1], + [1,1,1], + [1,1,1]] + +m2 = [[1,0,1], + [1,1,1], + [1,0,1]] + +print_mat(m1) +print_mat(m2) + +zero_mat(m1) +zero_mat(m2) + +print_mat(m1) +print_mat(m2) diff --git a/challenge-068/walt-mankowski/python/ch-2.py b/challenge-068/walt-mankowski/python/ch-2.py new file mode 100644 index 0000000000..5f0902ab23 --- /dev/null +++ b/challenge-068/walt-mankowski/python/ch-2.py @@ -0,0 +1,40 @@ +class Node: + def __init__(self, val): + self.val = val + self.next = None + +def make_list(a): + L = Node(a[0]) + cur = L + for val in range(1, len(a)): + node = Node(a[val]) + cur.next = node + cur = node + return L + +def print_list(L): + while L: + print(L.val, end='') + if L.next: + print(' => ', end=''); + L = L.next + print() + +def reorder_list(L): + # save the list in an array + a = [] + while L: + a.append(L) + L = L.next + + # now reorder things + n = len(a)-1 + for i in range(0, int(n/2)): + a[n-i].next = a[i].next + a[i].next = a[n-i] + a[n-i-1].next = None + +L = make_list(range(1,11)) +print_list(L) +reorder_list(L) +print_list(L) |
