diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-10-27 16:56:58 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-10-27 16:56:58 +0100 |
| commit | ab2bc8f0e752be377a2bf6fd18432782e2542461 (patch) | |
| tree | e7e265c09ef07afb8df727be68888b392732d0eb | |
| parent | bd2cd1cff92a45da675e78a83093168fbe1f4c06 (diff) | |
| parent | 32a96f326670dd20f0319ab687af593409d586cd (diff) | |
| download | perlweeklychallenge-club-ab2bc8f0e752be377a2bf6fd18432782e2542461.tar.gz perlweeklychallenge-club-ab2bc8f0e752be377a2bf6fd18432782e2542461.tar.bz2 perlweeklychallenge-club-ab2bc8f0e752be377a2bf6fd18432782e2542461.zip | |
Merge branch 'master' into devel
| -rw-r--r-- | challenge-136/abigail/README.md | 2 | ||||
| -rw-r--r-- | challenge-136/abigail/c/ch-1.c | 4 | ||||
| -rw-r--r-- | challenge-136/abigail/pascal/ch-1.p | 64 | ||||
| -rw-r--r-- | challenge-136/abigail/pascal/ch-2.p | 27 | ||||
| -rw-r--r-- | challenge-136/mohammad-anwar/python/ch-2.py | 63 | ||||
| -rw-r--r-- | challenge-136/mohammad-anwar/swift/ch-2.swift | 106 |
6 files changed, 264 insertions, 2 deletions
diff --git a/challenge-136/abigail/README.md b/challenge-136/abigail/README.md index df95d28447..b1b1f2b0ef 100644 --- a/challenge-136/abigail/README.md +++ b/challenge-136/abigail/README.md @@ -10,6 +10,7 @@ * [Java](java/ch-1.java) * [Lua](lua/ch-1.lua) * [Node.js](node/ch-1.js) +* [Pascal](pascal/ch-1.p) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) * [R](r/ch-1.r) @@ -27,6 +28,7 @@ * [Java](java/ch-2.java) * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) +* [Pascal](pascal/ch-2.p) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) * [R](r/ch-2.r) diff --git a/challenge-136/abigail/c/ch-1.c b/challenge-136/abigail/c/ch-1.c index d1c218aee5..d6fcc4d898 100644 --- a/challenge-136/abigail/c/ch-1.c +++ b/challenge-136/abigail/c/ch-1.c @@ -20,8 +20,8 @@ long long gcd (long long u, long long v) { long long u_odd = u % 2; long long v_odd = v % 2; - return u == v || !v ? u - : !u ? v + return u == v || !v ? u + : !u ? v : !u_odd && !v_odd ? gcd (u >> 1, v >> 1) << 1 : !u_odd && v_odd ? gcd (u >> 1, v) : u_odd && !v_odd ? gcd (u, v >> 1) diff --git a/challenge-136/abigail/pascal/ch-1.p b/challenge-136/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..391a4fe69f --- /dev/null +++ b/challenge-136/abigail/pascal/ch-1.p @@ -0,0 +1,64 @@ +Program XXX; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out < input-file *) +(* *) + +(* *) +(* Find the GCD, using Stein's algorithm *) +(* (https://en.wikipedia.org/wiki/Binary_GCD_algorithm) *) +(* *) +function gcd (u, v: integer): integer; + var + u_odd, v_odd: boolean; + + begin + u_odd := u mod 2 <> 0; + v_odd := v mod 2 <> 0; + + if (u = v) or (v = 0) then gcd := u + else if (u = 0) then gcd := v + else if not u_odd and not v_odd then gcd := gcd (u >> 1, v >> 1) << 1 + else if not u_odd and v_odd then gcd := gcd (u >> 1, v) + else if u_odd and not v_odd then gcd := gcd (u, v >> 1) + else if u > v then gcd := gcd (u - v, v) + else gcd := gcd (v - u, u); + end; + + +(* *) +(* Return true if number is a power of n, that is, number == n ^ p *) +(* for some non-negative integer p. Return false otherwise. *) +(* *) +function is_power_of_n (number, n: integer): boolean; + begin + if number < 1 then is_power_of_n := false + else if number = 1 then is_power_of_n := true + else if number mod n <> 0 then is_power_of_n := false + else is_power_of_n := is_power_of_n (number div n, n); + end; + +function is_power_of_2 (number: integer): boolean; + begin + is_power_of_2 := is_power_of_n (number, 2); + end; + + + +var + m, n, r: integer; + +begin + while (not eof) do begin + readln (m, n); + r := gcd (m, n); + if (r > 1) and is_power_of_2 (r) then + writeln (1) + else + writeln (0) + end +end. diff --git a/challenge-136/abigail/pascal/ch-2.p b/challenge-136/abigail/pascal/ch-2.p new file mode 100644 index 0000000000..6515b4e5ca --- /dev/null +++ b/challenge-136/abigail/pascal/ch-2.p @@ -0,0 +1,27 @@ +Program XXX; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-2.out ch-2.p; ./ch-2.out < input-file *) +(* *) + +function count (target, this_fib, prev_fib: integer): integer; + begin + if target < this_fib then count := 0 + else if target = this_fib then count := 1 + else count := count (target - this_fib, this_fib + prev_fib, this_fib) + + count (target, this_fib + prev_fib, this_fib); + end; + +var + n: integer; + +begin + while (not eof) do begin + readln (n); + writeln (count (n, 1, 1)); + end +end. diff --git a/challenge-136/mohammad-anwar/python/ch-2.py b/challenge-136/mohammad-anwar/python/ch-2.py new file mode 100644 index 0000000000..046d88ed53 --- /dev/null +++ b/challenge-136/mohammad-anwar/python/ch-2.py @@ -0,0 +1,63 @@ +#!/usr/bin/python3 + +''' + +Week 136: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-136 + +Task #2: Fibonacci Sequence + + You are given a positive number $n. + + Write a script to find how many different sequences you can create using Fibonacci numbers where the sum of unique numbers in each sequence are the same as the given number. + +''' + +import sys +import unittest +from itertools import combinations + +# +# +# This is translated from my Perl solution for the same task: +# +# https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-136/mohammad-anwar/perl/ch-2.pl + +def fibonacci_series_upto(_sum): + fibonacci_series = [1, 2] + while fibonacci_series[-1] + fibonacci_series[-2] <= _sum: + fibonacci_series.append(fibonacci_series[-1] + fibonacci_series[-2]) + + return fibonacci_series + +def fibonacci_sequence(_sum): + fibonacci_series = fibonacci_series_upto(_sum) + fibonacci_sum = [] + + for i in range(1, _sum): + if i > len(fibonacci_series): + break + + for comb in combinations(fibonacci_series, i): + if sum(comb) == _sum: + fibonacci_sum.append(comb) + + return len(fibonacci_sum) + +# +# +# Unit test class + +class TestFibonacciSequence(unittest.TestCase): + + def test_example_1(self): + self.assertEqual(fibonacci_sequence(16), 4, 'Example 1') + + def test_example_2(self): + self.assertEqual(fibonacci_sequence(9), 2, 'Example 2') + + def test_example_3(self): + self.assertEqual(fibonacci_sequence(15), 2, 'Example 3') + +unittest.main() diff --git a/challenge-136/mohammad-anwar/swift/ch-2.swift b/challenge-136/mohammad-anwar/swift/ch-2.swift new file mode 100644 index 0000000000..653b663dbd --- /dev/null +++ b/challenge-136/mohammad-anwar/swift/ch-2.swift @@ -0,0 +1,106 @@ +import Foundation + +/* + +Week 136: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-136 + +Task #2: Fibonacci Sequence + + You are given a positive number $n. + + Write a script to find how many different sequences you can create using Fibonacci numbers where the sum of unique numbers in each sequence are the same as the given number. + +*/ + +enum ParamError: Error { + case missingNum + case invalidNum +} + +do { + let paramCount:Int = Int(CommandLine.argc) + + if paramCount <= 1 { + throw ParamError.missingNum + } + + let num:Int = Int(CommandLine.arguments[1])! + + if num >= 1 { + print(fibonacci_sequence(num)) + } + else { + throw ParamError.invalidNum + } +} +catch ParamError.missingNum { + print("Missing number.") +} +catch ParamError.invalidNum { + print("Invalid number.") +} +catch let error { + print(error) +} + +// +// +// Function + +func fibonacci_series(_ sum:Int) -> Array<Int> { + + var fibonacci:[Int] = [1, 2] + var size:Int = fibonacci.count + + while fibonacci[size - 1] + fibonacci[size - 2] <= sum { + fibonacci.append(fibonacci[size - 1] + fibonacci[size - 2]) + size = fibonacci.count + } + + return fibonacci +} + +func fibonacci_sequence(_ sum:Int) -> Int { + + let fibonacci:[Int] = fibonacci_series(sum) + var count:Int = 0 + + let comb = fibonacci.combinations + for c in comb { + if c.count == 0 { + continue + } + + let _sum:Int = c.reduce(0, +) + + if _sum == sum { + count = count + 1 + } + } + + return count +} + +// +// Stackoverflow +// +// https://stackoverflow.com/questions/50264717/get-all-possible-combination-of-items-in-array-without-duplicate-groups-in-swift/50265976 + +extension Array { + var combinations: [[Element]] { + if count == 0 { + return [self] + } + else { + let tail = Array(self[1..<endIndex]) + let head = self[0] + + let first = tail.combinations + let rest = first.map { $0 + [head] } + + return first + rest + } + } +} |
