aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-12-29 21:32:22 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-12-29 21:32:22 +0000
commitbe4bcff58a2fd4292277db4fea95c449fd515c63 (patch)
tree268df6fb088db2329c3f0b97dddb220c9fead7e1
parent324e4a6e685944946e370b34cde79342bf1f88b0 (diff)
downloadperlweeklychallenge-club-be4bcff58a2fd4292277db4fea95c449fd515c63.tar.gz
perlweeklychallenge-club-be4bcff58a2fd4292277db4fea95c449fd515c63.tar.bz2
perlweeklychallenge-club-be4bcff58a2fd4292277db4fea95c449fd515c63.zip
- Added Swift solution to the task "Dot Product" of week 145.
-rw-r--r--challenge-145/mohammad-anwar/swift/ch-1.swift100
1 files changed, 100 insertions, 0 deletions
diff --git a/challenge-145/mohammad-anwar/swift/ch-1.swift b/challenge-145/mohammad-anwar/swift/ch-1.swift
new file mode 100644
index 0000000000..47fc6f87bf
--- /dev/null
+++ b/challenge-145/mohammad-anwar/swift/ch-1.swift
@@ -0,0 +1,100 @@
+import Foundation
+
+/*
+
+Week 145:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-145
+
+Task #1: Dot Product
+
+ You are given 2 arrays of same size, @a and @b.
+
+ Write a script to implement Dot Product.
+
+*/
+
+enum ParamError: Error {
+ case missingListA
+ case missingListB
+ case invalidListA
+ case invalidListB
+ case mismatchList
+}
+
+do {
+ let paramCount:Int = Int(CommandLine.argc)
+
+ if paramCount <= 1 {
+ throw ParamError.missingListA
+ }
+ if paramCount <= 2 {
+ throw ParamError.missingListB
+ }
+
+ let a:String = CommandLine.arguments[1]
+ let b:String = CommandLine.arguments[2]
+ if isValidList(a) {
+ if isValidList(b) {
+ let list_a = a.components(separatedBy: ",")
+ let list_b = b.components(separatedBy: ",")
+
+ if list_a.count == list_b.count {
+ var dp:Int = 0
+ for i in 0...list_a.count-1 {
+ let _a:Int = Int(list_a[i])!
+ let _b:Int = Int(list_b[i])!
+
+ dp += _a * _b
+ }
+
+ print(dp)
+ }
+ else {
+ throw ParamError.mismatchList
+ }
+ }
+ else {
+ throw ParamError.invalidListB
+ }
+ }
+ else {
+ throw ParamError.invalidListA
+ }
+}
+catch ParamError.missingListA {
+ print("Missing list A.")
+}
+catch ParamError.missingListB {
+ print("Missing list B.")
+}
+catch ParamError.invalidListA {
+ print("Invalid list A.")
+}
+catch ParamError.invalidListB {
+ print("Invalid list B.")
+}
+catch ParamError.mismatchList {
+ print("Mismatch list.")
+}
+catch let error {
+ print(error)
+}
+
+//
+//
+// Functions
+
+func isValidList(_ list:String) -> Bool {
+
+ let pattern = "^[\\-?\\d\\,?\\s?]+$"
+ let regex = try! NSRegularExpression(pattern: pattern)
+ let range = NSRange(location: 0, length: list.utf16.count)
+
+ if regex.firstMatch(in: list, options: [], range: range) != nil {
+ return true
+ }
+ else {
+ return false
+ }
+}