aboutsummaryrefslogtreecommitdiff
path: root/challenge-066
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-07-24 19:34:51 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-07-24 19:34:51 +0100
commitb1fc2dc2d779f100fb444bc509881dd34c6ac88e (patch)
tree5e5c4e1f0fb5d3986f92f3e95b4f7aaa827aedca /challenge-066
parent085928ccf6223236be5d0f7a425cde3bc78cc22a (diff)
downloadperlweeklychallenge-club-b1fc2dc2d779f100fb444bc509881dd34c6ac88e.tar.gz
perlweeklychallenge-club-b1fc2dc2d779f100fb444bc509881dd34c6ac88e.tar.bz2
perlweeklychallenge-club-b1fc2dc2d779f100fb444bc509881dd34c6ac88e.zip
- Added Swift solution to the "Divide Integer" task.
Diffstat (limited to 'challenge-066')
-rw-r--r--challenge-066/mohammad-anwar/swift/ch-1.swift82
1 files changed, 82 insertions, 0 deletions
diff --git a/challenge-066/mohammad-anwar/swift/ch-1.swift b/challenge-066/mohammad-anwar/swift/ch-1.swift
new file mode 100644
index 0000000000..6436d29fcd
--- /dev/null
+++ b/challenge-066/mohammad-anwar/swift/ch-1.swift
@@ -0,0 +1,82 @@
+import Foundation
+
+/*
+Perl Weekly Challenge - 066
+
+Task #1: Divide Integers
+
+https://perlweeklychallenge.org/blog/perl-weekly-challenge-066/
+*/
+
+enum ParamError: Error {
+ case missingDividendAndDivisor
+ case missingDivisor
+ case invalidDividend
+ case invalidDivisor
+}
+
+do {
+ let paramCount:Int = Int(CommandLine.argc)
+
+ if paramCount <= 1 {
+ throw ParamError.missingDividendAndDivisor
+ }
+ else if paramCount <= 2 {
+ throw ParamError.missingDivisor
+ }
+
+ let dividend:Int = Int(CommandLine.arguments[1])!
+ let divisor:Int = Int(CommandLine.arguments[2])!
+
+ var abs_dividend = abs(dividend)
+ let abs_divisor = abs(divisor)
+
+ if abs_dividend < abs_divisor {
+ throw ParamError.invalidDividend
+ }
+
+ if divisor == 0 {
+ throw ParamError.invalidDivisor
+ }
+
+ var sign:String = "";
+ if dividend < 0 {
+ if divisor > 0 {
+ sign = "-"
+ }
+ }
+ else {
+ if divisor < 0 {
+ sign = "-"
+ }
+ }
+
+ var i:Int = 0
+ while abs_dividend >= abs_divisor {
+ i += 1
+ abs_dividend -= abs_divisor
+ }
+
+ if sign != "" {
+ i += 1
+ print(i)
+ }
+ else {
+ print(sign + String(i))
+ }
+}
+catch ParamError.missingDividendAndDivisor {
+ print("Missing Dividend and Divisor")
+}
+catch ParamError.missingDivisor {
+ print("Missing Divisor")
+}
+catch ParamError.invalidDividend {
+ print("Invalid Dividend. (Dividend > Divisor)")
+}
+catch ParamError.invalidDivisor {
+ print("Invalid Divisor. Divisor > 0")
+}
+catch let error {
+ print(error)
+}