aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-07-24 16:19:40 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-07-24 16:19:40 +0100
commit03a388d4f9d5f19957f1ca8d90f585c0c9bbe8b1 (patch)
tree6082a02067287c9efd2ccfa82ca28cd1432e8f28
parentd4ef2c42773b5878950e4b55741fb2934705ddeb (diff)
downloadperlweeklychallenge-club-03a388d4f9d5f19957f1ca8d90f585c0c9bbe8b1.tar.gz
perlweeklychallenge-club-03a388d4f9d5f19957f1ca8d90f585c0c9bbe8b1.tar.bz2
perlweeklychallenge-club-03a388d4f9d5f19957f1ca8d90f585c0c9bbe8b1.zip
- Added Swift solution to the "Zero Matrix" task of Challenge #068.
-rw-r--r--challenge-068/mohammad-anwar/swift/ch-1.swift90
1 files changed, 90 insertions, 0 deletions
diff --git a/challenge-068/mohammad-anwar/swift/ch-1.swift b/challenge-068/mohammad-anwar/swift/ch-1.swift
new file mode 100644
index 0000000000..ea0c701008
--- /dev/null
+++ b/challenge-068/mohammad-anwar/swift/ch-1.swift
@@ -0,0 +1,90 @@
+import Foundation
+
+/*
+Perl Weekly Challenge - 068
+
+Task #1: Zero Matrix
+
+https://perlweeklychallenge.org/blog/perl-weekly-challenge-068
+*/
+
+enum ParamError: Error {
+ case missingRowsandCols
+ case missingCols
+}
+
+var rows:Int = 0
+var cols:Int = 0
+var matrix = [[Int]]()
+
+do {
+ let paramCount:Int = Int(CommandLine.argc)
+
+ if paramCount <= 1 {
+ throw ParamError.missingRowsandCols
+ }
+ else if paramCount <= 2 {
+ throw ParamError.missingCols
+ }
+
+ rows = Int(CommandLine.arguments[1])!
+ cols = Int(CommandLine.arguments[2])!
+ matrix = generateMatrix(rows, cols)
+}
+catch ParamError.missingRowsandCols {
+ rows = 3
+ cols = 3
+ matrix = [[1, 0, 1], [1, 1, 1], [1, 0, 1]]
+}
+catch ParamError.missingCols {
+ rows = Int(CommandLine.arguments[1])!
+ cols = 3
+ matrix = generateMatrix(rows, cols)
+}
+
+var zeroMatrix:[[Int]] = initZeroMatrix(rows, cols)
+for r in 0..<rows {
+ for c in 0..<cols {
+ if matrix[r][c] == 0 {
+ // make zero row
+ for i in 0..<cols {
+ zeroMatrix[r][i] = 0
+ }
+ // make zero col
+ for j in 0..<rows {
+ zeroMatrix[j][c] = 0
+ }
+ }
+ }
+}
+
+print(zeroMatrix)
+
+
+// Functions
+
+func generateMatrix(_ rows:Int, _ cols:Int) -> [[Int]] {
+ var matrix = [[Int]]()
+ for _ in 1...rows {
+ var row:[Int] = []
+ for _ in 1...cols {
+ row.append(Int.random(in: 0..<2))
+ }
+ matrix.append(row)
+ }
+
+ return matrix
+}
+
+func initZeroMatrix(_ rows:Int, _ cols:Int) -> [[Int]] {
+ var matrix = [[Int]]()
+ for _ in 1...rows {
+ var row:[Int] = []
+ for _ in 1...cols {
+ row.append(1)
+ }
+ matrix.append(row)
+ }
+
+ return matrix
+}