aboutsummaryrefslogtreecommitdiff
path: root/challenge-160
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-04-11 18:57:00 +0100
committerGitHub <noreply@github.com>2022-04-11 18:57:00 +0100
commitff05f19479d6c3f5336191888e5d03087efbfd3f (patch)
tree40998c0e16b9b65b0bf2fe871f47398e28ce1fa0 /challenge-160
parent6fe8c627e52bb559a8460c6ba69532fe7f82a516 (diff)
parente7cf820fcec156b8991cb22465b755aaefd7c71b (diff)
downloadperlweeklychallenge-club-ff05f19479d6c3f5336191888e5d03087efbfd3f.tar.gz
perlweeklychallenge-club-ff05f19479d6c3f5336191888e5d03087efbfd3f.tar.bz2
perlweeklychallenge-club-ff05f19479d6c3f5336191888e5d03087efbfd3f.zip
Merge pull request #5912 from pokgopun/pwc160
pwc160 solution in go
Diffstat (limited to 'challenge-160')
-rw-r--r--challenge-160/pokgopun/go/ch-1.go51
-rw-r--r--challenge-160/pokgopun/go/ch-2.go51
-rw-r--r--challenge-160/pokgopun/perl/ch-1.pl24
-rw-r--r--challenge-160/pokgopun/perl/ch-2.pl19
4 files changed, 145 insertions, 0 deletions
diff --git a/challenge-160/pokgopun/go/ch-1.go b/challenge-160/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..4e6506934b
--- /dev/null
+++ b/challenge-160/pokgopun/go/ch-1.go
@@ -0,0 +1,51 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "regexp"
+ "strconv"
+ "strings"
+)
+
+func main() {
+ m := []string{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
+ //fmt.Println(m)
+ re := regexp.MustCompile(`\S+$`)
+ var num []int
+ if len(os.Args) > 1 {
+ num = argInts()
+ } else {
+ num = []int{5, 7, 6}
+ }
+ for _, n := range num {
+ fmt.Println("Input: $n =", n)
+ str := m[n-1]
+ for {
+ r := re.FindString(str)
+ if r == "four" {
+ str += " is magic"
+ break
+ } else {
+ nxt := m[len(r)-1]
+ str += " is " + nxt + ", " + nxt
+ r = str
+
+ }
+ }
+ fmt.Printf("Output: %v.\n\n", strings.Title(str[:1])+str[1:])
+ }
+}
+func argInts() (s []int) {
+ if len(os.Args) > 1 {
+ for _, v := range os.Args[1:] {
+ i, err := strconv.Atoi(v)
+ if err != nil {
+ log.Fatal(err)
+ }
+ s = append(s, i)
+ }
+ }
+ return s
+}
diff --git a/challenge-160/pokgopun/go/ch-2.go b/challenge-160/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..e8a953980c
--- /dev/null
+++ b/challenge-160/pokgopun/go/ch-2.go
@@ -0,0 +1,51 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "strconv"
+)
+
+func main() {
+ var num [][]int
+ if len(os.Args) > 3 {
+ num = [][]int{argInts()}
+ } else {
+ num = [][]int{
+ []int{1, 3, 5, 7, 9},
+ []int{1, 2, 3, 4, 5},
+ []int{2, 4, 2},
+ }
+ }
+ for _, n := range num {
+ fmt.Println("Input: @n =", n)
+ o := -1
+ for i := 1; i < len(n)-1; i++ {
+ if sum(n[:i]) == sum(n[i+1:]) {
+ o = i
+ break
+ }
+ }
+ fmt.Printf("Output: %v\n\n", o)
+ }
+}
+func sum(s []int) (r int) {
+ for _, v := range s {
+ r += v
+ }
+ return r
+}
+
+func argInts() (s []int) {
+ if len(os.Args) > 1 {
+ for _, v := range os.Args[1:] {
+ i, err := strconv.Atoi(v)
+ if err != nil {
+ log.Fatal(err)
+ }
+ s = append(s, i)
+ }
+ }
+ return s
+}
diff --git a/challenge-160/pokgopun/perl/ch-1.pl b/challenge-160/pokgopun/perl/ch-1.pl
new file mode 100644
index 0000000000..cf260e0431
--- /dev/null
+++ b/challenge-160/pokgopun/perl/ch-1.pl
@@ -0,0 +1,24 @@
+use strict;
+use warnings;
+
+my @m= qw/one two three four five six seven eight nine/;
+
+my @n = @ARGV && join("",@ARGV) =~ /^\d+$/ ? @ARGV : (5,7,6);
+
+foreach my $n (@n) {
+ printf "Input: \$n = %d\n", $n;
+ my $str = $m[$n-1];
+ {
+ my $prev = $1 if $str =~ /(\S+)$/;
+ if ($prev eq "four") {
+ $str .= " is magic";
+ last;
+ } else {
+ my $next = $m[length($prev)-1];
+ $str .= " is $next, $next";
+ }
+ redo;
+ }
+ $str =~ s/^(.)/\U$1/;
+ printf "Output: %s.\n\n", $str;
+}
diff --git a/challenge-160/pokgopun/perl/ch-2.pl b/challenge-160/pokgopun/perl/ch-2.pl
new file mode 100644
index 0000000000..f4795fc255
--- /dev/null
+++ b/challenge-160/pokgopun/perl/ch-2.pl
@@ -0,0 +1,19 @@
+use strict;
+use warnings;
+use List::Util qw/sum/;
+
+my @samples = @ARGV > 3 && join("",@ARGV) =~ /^\d+$/ ? ([@ARGV]) : ([1,3,5,7,9],[1,2,3,4,5],[2,4,2]);
+
+foreach (@samples) {
+ my @sample = @$_;
+ printf "Input: \@n = (%s)\n", join(", ", @sample);
+ my $o = -1;
+ for (my $i=1; $i < @sample-1; $i++){
+ if ( sum(@sample[0..$i-1]) == sum(@sample[$i+1..@sample-1]) ){
+ $o = $i;
+ last;
+ }
+ }
+ printf "Output: $o\n\n";
+}
+