diff options
| author | Myoungjin JEON <jeongoon@gmail.com> | 2020-07-27 16:10:03 +1000 |
|---|---|---|
| committer | Myoungjin JEON <jeongoon@gmail.com> | 2020-07-27 16:10:03 +1000 |
| commit | 9f5aee78a5569f7e0c8f742dd907663bd1e04642 (patch) | |
| tree | 8f864d8438c06983ab9438e1e9839628cfe18520 | |
| parent | fde6b9fbc1e5fa1378acd6b1b5c6aed854f4f156 (diff) | |
| parent | 450eab302dee5452d66be1f388cea4fafb0b5a31 (diff) | |
| download | perlweeklychallenge-club-9f5aee78a5569f7e0c8f742dd907663bd1e04642.tar.gz perlweeklychallenge-club-9f5aee78a5569f7e0c8f742dd907663bd1e04642.tar.bz2 perlweeklychallenge-club-9f5aee78a5569f7e0c8f742dd907663bd1e04642.zip | |
Merge remote-tracking branch 'upstream/master'
92 files changed, 4412 insertions, 1207 deletions
diff --git a/challenge-065/mohammad-anwar/swift/ch-1.swift b/challenge-065/mohammad-anwar/swift/ch-1.swift new file mode 100644 index 0000000000..c353776ff9 --- /dev/null +++ b/challenge-065/mohammad-anwar/swift/ch-1.swift @@ -0,0 +1,112 @@ +import Foundation + +/* +Perl Weekly Challenge - 065 + +Task #1: Digits Sum + +https://perlweeklychallenge.org/blog/perl-weekly-challenge-065/ +*/ + +do { + let paramCount:Int = Int(CommandLine.argc) + + if paramCount <= 1 { + throw ParamError.missingDigitsAndSum + } + else if paramCount <= 2 { + throw ParamError.missingSum + } + + let digits:String = String(CommandLine.arguments[1]) + let sum:String = String(CommandLine.arguments[2]) + + let start:Int = try startIndex(d: digits) + let stop:Int = try stopIndex(d: digits) + + print(digitsSum(start, stop, sum)) +} +catch ParamError.missingDigitsAndSum { + print("Missing Digits and Sum") +} +catch ParamError.missingDigits { + print("Missing Digits") +} +catch ParamError.missingSum { + print("Missing Sum") +} +catch let error { + print(error) +} + +// +// +// Functions + +enum ParamError: Error { + case missingDigitsAndSum + case missingDigits + case missingSum +} + +func digitsSum(_ start:Int, _ stop:Int, _ sum:String) -> String { + + var s:String = "" + for i in start...stop { + let _sum:String = sumDigits(n: String(i)) + if sum == _sum { + if s == "" { + s = String(i) + } + else { + s = s + ", " + String(i) + } + } + } + + return s +} + +func startIndex(d digits:String) throws -> Int { + + guard let count:Int = Int(digits) else { + throw ParamError.missingDigits + } + + var index:String = "1" + for _ in 1..<count { + index = index + "0" + } + + guard let s:Int = Int(index) else { return Int() } + return s +} + +func stopIndex(d digits:String) throws -> Int { + + guard let count:Int = Int(digits) else { + throw ParamError.missingDigits + } + + var index:String = "" + for _ in 1...count { + index = index + "9" + } + + guard let s:Int = Int(index) else { return Int() } + return s +} + +func sumDigits(n number:String) -> String { + + let digits = Array(number) + var sum:Int = 0 + + for i in digits { + if let i:Int = Int(String(i)) { + sum = sum + i + } + } + + return String(sum) +} 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) +} 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 +} diff --git a/challenge-069/colin-crain/perl/ch-1.pl b/challenge-069/colin-crain/perl/ch-1.pl index c1a3c8d434..ef6497300d 100644 --- a/challenge-069/colin-crain/perl/ch-1.pl +++ b/challenge-069/colin-crain/perl/ch-1.pl @@ -28,65 +28,58 @@ use feature ":5.26"; ## ## ## ## ## MAIN: -my ($A, $B) = @ARGV; ## low, high bounds -# ($A, $B) = (1000, 2000000000); +my ($A, $B) = @ARGV; ## low, high bounds + # the order is the length of the half number that is modified and -# joined to itself, with or without the pivot digit in the center. -# Thus a given order will generate numbers up to 2n+1 places long, or -# 10^(2n+1) The order is calculated to theoretically create numbers as -# large as B. As this value scales by magnitude 100, this number can -# be quite a bit larger, but will larger and we can guarantee none of -# the next larger order will be required. It serves as an upper bound +# joined to itself, with or without the pivot digit in the +# center. Thus a given order will generate numbers up to 2n+1 +# places long, or numbers up to but less than 10^(2n+1) The order +# is calculated to be large enough to create all numbers as large +# as B. As the maximum of the range scales by magnitude 100, the +# largest number created can still be quite a bit larger, but we +# can guarantee it will be large enough and also that none of the +# next larger order will be required. It serves as an upper bound # to the calculation space. my $order = int(length($B)/2); my @list = (0, 1, 6, 8, 9); my @center = (0, 1, 8); -my @num = @list[1..@list-1]; ## remove leadin |
