diff options
Diffstat (limited to 'challenge-008')
| -rw-r--r-- | challenge-008/archargelod/README | 1 | ||||
| -rwxr-xr-x | challenge-008/archargelod/nim/ch_1.nim | 13 | ||||
| -rwxr-xr-x | challenge-008/archargelod/nim/ch_2.nim | 23 | ||||
| -rw-r--r-- | challenge-008/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-008/zapwai/README | 1 | ||||
| -rw-r--r-- | challenge-008/zapwai/c/ch-1.c | 22 | ||||
| -rw-r--r-- | challenge-008/zapwai/c/ch-2.c | 31 | ||||
| -rw-r--r-- | challenge-008/zapwai/javascript/ch-1.js | 30 | ||||
| -rw-r--r-- | challenge-008/zapwai/javascript/ch-2.js | 30 | ||||
| -rw-r--r-- | challenge-008/zapwai/perl/ch-1.pl | 24 | ||||
| -rw-r--r-- | challenge-008/zapwai/perl/ch-2.pl | 24 | ||||
| -rw-r--r-- | challenge-008/zapwai/python/ch-1.py | 15 | ||||
| -rw-r--r-- | challenge-008/zapwai/python/ch-2.py | 22 | ||||
| -rw-r--r-- | challenge-008/zapwai/rust/ch-1.rs | 16 | ||||
| -rw-r--r-- | challenge-008/zapwai/rust/ch-2.rs | 21 |
15 files changed, 274 insertions, 0 deletions
diff --git a/challenge-008/archargelod/README b/challenge-008/archargelod/README new file mode 100644 index 0000000000..6cd57e1074 --- /dev/null +++ b/challenge-008/archargelod/README @@ -0,0 +1 @@ +Solution by archargelod diff --git a/challenge-008/archargelod/nim/ch_1.nim b/challenge-008/archargelod/nim/ch_1.nim new file mode 100755 index 0000000000..2dd3eccd25 --- /dev/null +++ b/challenge-008/archargelod/nim/ch_1.nim @@ -0,0 +1,13 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import std/math + +proc fivePerfectNumbers(): array[5, int] = + const Primes = [2, 3, 5, 7, 13] + for i, p in Primes: + result[i] = (2 ^ (p - 1)) * (2 ^ p - 1) + +when isMainModule: + import std/unittest + suite "Perfect numbers": + test "first 5 perfect numbers": + check fivePerfectNumbers() == [6, 28, 496, 8128, 33550336] diff --git a/challenge-008/archargelod/nim/ch_2.nim b/challenge-008/archargelod/nim/ch_2.nim new file mode 100755 index 0000000000..d96b378446 --- /dev/null +++ b/challenge-008/archargelod/nim/ch_2.nim @@ -0,0 +1,23 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import std/strutils + +proc center(lines: varargs[string]): seq[string] = + let maxOffset = block: + var maxLen = 0 + for line in lines: + if line.len > maxLen: + maxLen = line.len + maxLen div 2 + + for line in lines: + result.add ' '.repeat(maxOffset - line.len div 2) & line + +when isMainModule: + import std/unittest + + const Example = ["This", "is", "a test of the", "center function"] + const Expected = [" This", " is", " a test of the", "center function"] + + suite "Centering strings": + test "Example 1": + check center(Example) == Expected diff --git a/challenge-008/jaldhar-h-vyas/blog.txt b/challenge-008/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..5981a81ab2 --- /dev/null +++ b/challenge-008/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2019/05/perl_weekly_challenge_week_8.html diff --git a/challenge-008/zapwai/README b/challenge-008/zapwai/README new file mode 100644 index 0000000000..037b3777ef --- /dev/null +++ b/challenge-008/zapwai/README @@ -0,0 +1 @@ +Solutions by David Ferrone. diff --git a/challenge-008/zapwai/c/ch-1.c b/challenge-008/zapwai/c/ch-1.c new file mode 100644 index 0000000000..95e59fca82 --- /dev/null +++ b/challenge-008/zapwai/c/ch-1.c @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <math.h> + +int is_perfect(int num) { + int sum = 0; + for (int i = 1; i < 1 + (int) num/2; i++) + if (num % i == 0) + sum += i; + return (sum == num); +} + +void perfects() { + for (int p = 2; p < 15; p++) { + int num = pow(2, p-1) * (pow(2,p) - 1); + if (is_perfect(num)) printf("%d ", num); + } + printf("\n"); +} + +int main() { + perfects(); +} diff --git a/challenge-008/zapwai/c/ch-2.c b/challenge-008/zapwai/c/ch-2.c new file mode 100644 index 0000000000..0791fccb40 --- /dev/null +++ b/challenge-008/zapwai/c/ch-2.c @@ -0,0 +1,31 @@ +#include <stdio.h> +#include <string.h> + +int max(int len, int nums[]) { + int max = 0; + for (int i = 0; i < len; i++) { + if (max < nums[i]) + max = nums[i]; + } + return max; +} + +void center(int wordlen, char* words[]) { + int lens[wordlen]; + for (int i = 0; i < wordlen; i++) { + lens[i] = strlen(words[i]); + } + int M = max(wordlen, lens); + for (int i = 0; i < wordlen; i++){ + for (int j = 0; j < M - lens[i]; j++) + if (j % 2 == 0) + printf(" "); + printf("%s\n", words[i]); + } +} + +int main() { + char* l[] = {"This", "is", "a test of the", "center function"}; + int len = sizeof(l) / sizeof(char*); + center(len, l); +} diff --git a/challenge-008/zapwai/javascript/ch-1.js b/challenge-008/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..44351de732 --- /dev/null +++ b/challenge-008/zapwai/javascript/ch-1.js @@ -0,0 +1,30 @@ +function is_perfect(num) { + let divs = list_divisors(num); + let sum = 0; + divs.forEach(item => { + sum += item; + }); + return (num == sum); +} + +function list_divisors(num) { + let divs = [1]; + for (let i = 2; i <= num/2; i++) { + if (num % i == 0) + divs.push(i); + } + return divs; +} + +let perfect_numbers = []; + +for (let p = 2; p <= 15; p++) { + let num = Math.pow(2, p-1) * (Math.pow(2,p) - 1); + if (is_perfect(num)) + perfect_numbers.push(num); + if (perfect_numbers.length == 5) + break; +} + +console.log(perfect_numbers); + diff --git a/challenge-008/zapwai/javascript/ch-2.js b/challenge-008/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..e2f7dfaa46 --- /dev/null +++ b/challenge-008/zapwai/javascript/ch-2.js @@ -0,0 +1,30 @@ +function center(words) { + let lens = []; + for (let word of words) + lens.push(word.length); + let m = max(lens); + let ind = 0; + for (let l of lens) { + let sp = (m - l) / 2; + let s = ""; + for (let i = 0; i < sp; i++) + s += " "; + s += words[ind] + "<br>"; + ind++; + document.write(s); + } +} + + +function max (nums) { + let max = 0; + for (let n of nums) { + if (max < n) + max = n; + } + return max; +} + +let words = ["This", "is", "a test of the", "center function"]; +center(words); +console.log(max([3, 5, 7, 1, 15])); diff --git a/challenge-008/zapwai/perl/ch-1.pl b/challenge-008/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..a3099a4411 --- /dev/null +++ b/challenge-008/zapwai/perl/ch-1.pl @@ -0,0 +1,24 @@ +use v5.36; +use List::Util qw( sum ); + +sub is_perfect ($num) { + my @div = grep { $num % $_ == 0 } (1 .. int $num/2); + return ($num == sum @div); +} + +sub get_primes($cap) { + my @primes = (2); + for my $num (3 .. $cap) { + my $cnt = 0; + for my $p (@primes) { + $cnt++ if ($num % $p != 0); + } + push @primes, $num if ($cnt == @primes); + } + return @primes; +} + +my @primes = get_primes(15); +my @input = map { 2**($_ - 1) * (2**$_ - 1) } @primes; +say join(" ", grep is_perfect($_), @input); + diff --git a/challenge-008/zapwai/perl/ch-2.pl b/challenge-008/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..052a4f3751 --- /dev/null +++ b/challenge-008/zapwai/perl/ch-2.pl @@ -0,0 +1,24 @@ +use v5.30; +sub center { + my @list = @_; + my $long = 0; + foreach (@list) { + $long = length $_ if (length $_ > $long); + } + my $cnt; + do { + $cnt = 0; + foreach (@list) { + if (length $_ < $long) { + $cnt++; + add_space($_); + } + } + } while ($cnt != 0); + return \@list; +} + +sub add_space { $_[0] = " $_[0] "; } + +my $ref = center("This", "is", "a test of the", "center function"); +say foreach (@$ref); diff --git a/challenge-008/zapwai/python/ch-1.py b/challenge-008/zapwai/python/ch-1.py new file mode 100644 index 0000000000..097dad9c94 --- /dev/null +++ b/challenge-008/zapwai/python/ch-1.py @@ -0,0 +1,15 @@ +def is_perfect(num): + divs = [] + for i in range(1, int(num/2) + 1): + if num % i == 0: + divs.append(i) + return (sum(divs) == num); + +def perfects() : + for i in range(2, 15): + num = pow(2, i - 1) * (pow(2,i) - 1); + if is_perfect(num): + print(num,"", end='') + +perfects(); +print() diff --git a/challenge-008/zapwai/python/ch-2.py b/challenge-008/zapwai/python/ch-2.py new file mode 100644 index 0000000000..2cb5b039a9 --- /dev/null +++ b/challenge-008/zapwai/python/ch-2.py @@ -0,0 +1,22 @@ +def max(nums): + m = 0 + for n in nums: + if m < n: + m = n + return m + +def center(words): + lens = [] + for w in words: + lens.append(len(w)) + M = max(lens) + for i in range(len(words)): + sp = "" + k = M - len(words[i]) + for j in range(k): + if j % 2 == 0: + sp += " " + print(sp, words[i]) + +words = ["This", "is", "a test of the", "center function"] +center(words) diff --git a/challenge-008/zapwai/rust/ch-1.rs b/challenge-008/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..ed92c01686 --- /dev/null +++ b/challenge-008/zapwai/rust/ch-1.rs @@ -0,0 +1,16 @@ +fn main() { perfects(); } + +fn perfects() { + for p in 2 .. 15 { + let num = 2_i32.pow(p-1) * (2_i32.pow(p) - 1); + if is_perfect(num) { print!("{} ", num); } + } + println!(); +} + +fn is_perfect(n :i32) -> bool { + let mut divs = vec![]; + for i in 1 .. 1 + n/2 as i32 {if n % i == 0 { divs.push(i); }} + return n == divs.into_iter().sum(); +} + diff --git a/challenge-008/zapwai/rust/ch-2.rs b/challenge-008/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..552d549fea --- /dev/null +++ b/challenge-008/zapwai/rust/ch-2.rs @@ -0,0 +1,21 @@ +fn main() { + let words = vec!["This", "is", "a test of the", "center function"]; + center(words); +} + +fn center(words : Vec<&str>) { + let mut v = Vec::new(); + for w in &words { + v.push(w.len()); + } + let m = v.into_iter().max().unwrap(); + for word in words { + for i in 0 .. (m - word.len()) { + if i % 2 == 0 { + print!(" "); + } + } + println!("{}", word); + } +} + |
