aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-07-04 21:12:28 +0100
committerGitHub <noreply@github.com>2022-07-04 21:12:28 +0100
commitdd596a0ad89f6f3bd110730db92e32f67be47214 (patch)
tree11eb9671fe2a3a8dbb49beabf937d300e42b1f62
parente8a37e247f745be1a60c21daa913d6dd68566638 (diff)
parent1228dc0588dbe4c3bdd5d25798edfc1debf3b638 (diff)
downloadperlweeklychallenge-club-dd596a0ad89f6f3bd110730db92e32f67be47214.tar.gz
perlweeklychallenge-club-dd596a0ad89f6f3bd110730db92e32f67be47214.tar.bz2
perlweeklychallenge-club-dd596a0ad89f6f3bd110730db92e32f67be47214.zip
Merge pull request #6389 from pokgopun/pwc172
pwc172 solution in go
-rw-r--r--challenge-172/pokgopun/go/ch-1.go82
-rw-r--r--challenge-172/pokgopun/go/ch-2.go86
-rw-r--r--challenge-172/pokgopun/perl/ch-1.pl37
-rw-r--r--challenge-172/pokgopun/perl/ch-2.pl47
4 files changed, 252 insertions, 0 deletions
diff --git a/challenge-172/pokgopun/go/ch-1.go b/challenge-172/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..dace6e8075
--- /dev/null
+++ b/challenge-172/pokgopun/go/ch-1.go
@@ -0,0 +1,82 @@
+/* https://theweeklychallenge.org/blog/perl-weekly-challenge-172/
+
+Task 1: Prime Partition
+
+Submitted by: [50]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given two positive integers, $m and $n.
+
+ Write a script to find out the Prime Partition of the given number. No
+ duplicates allowed.
+
+ For example,
+Input: $m = 18, $n = 2
+Output: 5, 13 or 7, 11
+
+Input: $m = 19, $n = 3
+Output: 3, 5, 11
+
+*/
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "strings"
+
+ "github.com/jbarham/primegen"
+)
+
+func main() {
+ var m, n uint64
+ if len(os.Args) > 2 {
+ _, err := fmt.Sscanf(strings.Join(os.Args[1:], " "), "%d %d", &m, &n)
+ if err != nil {
+ log.Fatal(err)
+ }
+ } else {
+ log.Fatal("need two positive integers m and n")
+ }
+ pg := primegen.New()
+ var (
+ s []uint64
+ p uint64
+ )
+ for {
+ p = pg.Next()
+ if p > m {
+ break
+ }
+ s = append(s, p)
+ }
+ for _, v := range getCombo(int(n), s) {
+ p = 0
+ for _, i := range v {
+ p += i
+ }
+ if p == m {
+ fmt.Println(v)
+ }
+ }
+}
+func getCombo(n int, e []uint64) (r [][]uint64) {
+ var c []uint64
+ var sc []uint64
+ cTree(n, e, c, func(s []uint64) {
+ sc = make([]uint64, len(s))
+ copy(sc, s)
+ r = append(r, sc)
+ })
+ return r
+}
+func cTree(n int, e []uint64, c []uint64, f func(s []uint64)) {
+ if len(c) == n || len(c)+len(e) == n {
+ f(append(c, e...)[:n])
+ } else {
+ for i := 0; len(c)+len(e)-i >= n; i++ {
+ cTree(n, e[i+1:], append(c, e[i]), f)
+ }
+ }
+}
diff --git a/challenge-172/pokgopun/go/ch-2.go b/challenge-172/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..79635247d5
--- /dev/null
+++ b/challenge-172/pokgopun/go/ch-2.go
@@ -0,0 +1,86 @@
+/* https://theweeklychallenge.org/blog/perl-weekly-challenge-172/
+
+Task 2: Five-number Summary
+
+Submitted by: [51]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers.
+
+ Write a script to compute the five-number summary of the given set of
+ integers.
+
+ You can find the definition and example in the [52]wikipedia page.
+*/
+/*
+lower and upper quartile using Method 1 mentioned in https://en.wikipedia.org/wiki/Quartile#Discrete_distributions
+
+Computing methods
+Discrete distributions
+For discrete distributions, there is no universal agreement on selecting the quartile values.[3]
+
+Method 1
+
+1) Use the median to divide the ordered data set into two-halves.
+ If there is an odd number of data points in the original ordered data set, do not include the median (the central value in the ordered list) in either half.
+ If there is an even number of data points in the original ordered data set, split this data set exactly in half.
+
+2) The lower quartile value is the median of the lower half of the data. The upper quartile value is the median of the upper half of the data.
+
+This rule is employed by the TI-83 calculator boxplot and "1-Var Stats" functions.
+*/
+
+package main
+
+import (
+ "bufio"
+ "fmt"
+ "log"
+ "os"
+ "sort"
+ "strconv"
+)
+
+func main() {
+ var s []int
+ if len(os.Args) > 1 {
+ for _, v := range os.Args[1:] {
+ n, err := strconv.Atoi(v)
+ if err != nil {
+ log.Fatal(err)
+ }
+ s = append(s, n)
+ }
+ } else {
+ s = []int{2, 3, 5, 7, 11, 13}
+ }
+ sort.SliceStable(s, func(i, j int) bool {
+ return s[i] < s[j]
+ })
+ l := len(s)
+ w := bufio.NewWriter(os.Stdout)
+ fmt.Fprintln(w, "Input:", s)
+ name := []string{"sample minimum", "lower quartile", "median", "upper quartile", "sample maximum"}
+ var value []float64
+ if len(s) == 1 {
+ for i := 0; i < 5; i++ {
+ value = append(value, float64(s[0]))
+ }
+ } else {
+ value = []float64{float64(s[0]), median(s[:l/2]), median(s), median(s[l/2+l%2:]), float64(s[l-1])}
+ }
+ for i, v := range name {
+ fmt.Fprintln(w, v, ":", value[i])
+ }
+ w.Flush()
+}
+func median(s []int) float64 {
+ l := len(s)
+ if l == 1 {
+ return float64(s[0])
+ }
+ if l%2 == 0 {
+ return (float64(s[l/2]) + float64(s[l/2-1])) / 2
+ }
+ return float64(s[l/2])
+}
diff --git a/challenge-172/pokgopun/perl/ch-1.pl b/challenge-172/pokgopun/perl/ch-1.pl
new file mode 100644
index 0000000000..b4ad616a31
--- /dev/null
+++ b/challenge-172/pokgopun/perl/ch-1.pl
@@ -0,0 +1,37 @@
+### /* https://theweeklychallenge.org/blog/perl-weekly-challenge-172/
+###
+### Task 1: Prime Partition
+###
+### Submitted by: [50]Mohammad S Anwar
+### __________________________________________________________________
+###
+### You are given two positive integers, $m and $n.
+###
+### Write a script to find out the Prime Partition of the given number. No
+### duplicates allowed.
+###
+### For example,
+### Input: $m = 18, $n = 2
+### Output: 5, 13 or 7, 11
+###
+### Input: $m = 19, $n = 3
+### Output: 3, 5, 11
+###
+### */
+use strict;
+use warnings;
+use Math::Prime::Util qw/forprimes/;
+use Math::Combinatorics;
+
+die "need two positive integers m and n\n" unless @ARGV==2 && join(" ",@ARGV[0,1]) =~ /^\d+ \d+$/;
+my ($m, $n) = @ARGV;
+
+my @p;
+forprimes { push @p, $_ } $m;
+
+my $c = Math::Combinatorics->new( count => $n, data => \@p );
+{
+ last unless my @combo = $c->next_combination();
+ printf("%s\n", join(", ",@combo)) if eval(join(" + ",@combo))==$m;
+ redo;
+}
diff --git a/challenge-172/pokgopun/perl/ch-2.pl b/challenge-172/pokgopun/perl/ch-2.pl
new file mode 100644
index 0000000000..fe31c8bea7
--- /dev/null
+++ b/challenge-172/pokgopun/perl/ch-2.pl
@@ -0,0 +1,47 @@
+### /* https://theweeklychallenge.org/blog/perl-weekly-challenge-172/
+###
+### Task 2: Five-number Summary
+###
+### Submitted by: [51]Mohammad S Anwar
+### __________________________________________________________________
+###
+### You are given an array of integers.
+###
+### Write a script to compute the five-number summary of the given set of
+### integers.
+###
+### You can find the definition and example in the [52]wikipedia page.
+### */
+### /*
+### lower and upper quartile using Method 1 mentioned in https://en.wikipedia.org/wiki/Quartile#Discrete_distributions
+###
+### Computing methods
+### Discrete distributions
+### For discrete distributions, there is no universal agreement on selecting the quartile values.[3]
+###
+### Method 1
+###
+### 1) Use the median to divide the ordered data set into two-halves.
+### If there is an odd number of data points in the original ordered data set, do not include the median (the central value in the ordered list) in either half.
+### If there is an even number of data points in the original ordered data set, split this data set exactly in half.
+###
+### 2) The lower quartile value is the median of the lower half of the data. The upper quartile value is the median of the upper half of the data.
+###
+### This rule is employed by the TI-83 calculator boxplot and "1-Var Stats" functions.
+### */
+use strict;
+use warnings;
+
+die "please provide integers to calculate five-number summary" unless @ARGV && join(" ",@ARGV) =~ /^\d+(\s\d+)*$/;
+my @a = sort{$a <=> $b} @ARGV;
+
+printf "Input: (%s)\n", join(", ",@a);
+my @fn = ($a[-1], @a==1 ? $a[0] : median(@a[int(@a/2)+@a%2..$#a]), median(@a), @a==1 ? $a[0] : median(@a[0..int(@a/2)-1]), $a[0]);
+my $i = 5;
+foreach (qw/sample_minimum lower_quartile median upper_quartile sample_maximum/){
+ printf "%s: %s\n", $_, $fn[--$i];
+}
+
+sub median{
+ return @_ % 2 ? $_[int(@_/2)] : ($_[@_/2-1] + $_[@_/2])/2;
+}