aboutsummaryrefslogtreecommitdiff
path: root/challenge-083
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-083')
-rw-r--r--challenge-083/jeongoon/go/ch-1.go32
-rw-r--r--challenge-083/jeongoon/go/ch-2.go190
-rw-r--r--challenge-083/jeongoon/haskell/Combinations.hs25
-rw-r--r--challenge-083/jeongoon/haskell/ch-1.hs20
-rw-r--r--challenge-083/jeongoon/haskell/ch-2.hs53
-rw-r--r--challenge-083/jeongoon/perl/CombinationsIndex.pm123
-rw-r--r--challenge-083/jeongoon/perl/ch-1.pl31
-rw-r--r--challenge-083/jeongoon/perl/ch-2.pl79
-rw-r--r--challenge-083/jeongoon/raku/ch-2.raku22
-rw-r--r--challenge-083/jeongoon/raku/ch-2.without-race.raku23
-rw-r--r--challenge-083/jeongoon/raku/ch-2.yet-another-race.raku38
-rw-r--r--challenge-083/tyler-wardhaugh/clojure/README.md8
-rw-r--r--challenge-083/tyler-wardhaugh/clojure/deps.edn7
-rw-r--r--challenge-083/tyler-wardhaugh/clojure/pom.xml14
-rw-r--r--challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/core.clj12
-rw-r--r--challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t1.clj32
-rw-r--r--challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t2.clj55
-rw-r--r--challenge-083/tyler-wardhaugh/clojure/test/tw/weekly/c83_test.clj15
-rw-r--r--challenge-083/tyler-wardhaugh/lua/README.md6
-rwxr-xr-xchallenge-083/tyler-wardhaugh/lua/ch-1.lua20
-rwxr-xr-xchallenge-083/tyler-wardhaugh/lua/ch-2.lua34
-rwxr-xr-xchallenge-083/tyler-wardhaugh/lua/run.lua9
-rwxr-xr-xchallenge-083/tyler-wardhaugh/lua/test.lua20
23 files changed, 811 insertions, 57 deletions
diff --git a/challenge-083/jeongoon/go/ch-1.go b/challenge-083/jeongoon/go/ch-1.go
new file mode 100644
index 0000000000..d4f073f3e3
--- /dev/null
+++ b/challenge-083/jeongoon/go/ch-1.go
@@ -0,0 +1,32 @@
+package main
+
+import (
+ "os"
+ "fmt"
+ "strings"
+)
+
+func usage() {
+ fmt.Println( "Usage: go run ch-1.go \"a single string\"" )
+}
+
+func main() {
+ if len(os.Args[1:]) != 1 {
+ usage()
+ os.Exit(1);
+ }
+
+ words := strings.Split( os.Args[1], " " );
+ if len(words) < 3 {
+ fmt.Fprintln( os.Stderr,
+ "words must contain at least three words" )
+ os.Exit(2);
+ }
+
+ middleCharsExceptWhitespace := 0
+ for _, c := range words[ 1 : len(words)-1 ] {
+ middleCharsExceptWhitespace += len(c)
+ }
+
+ fmt.Println( middleCharsExceptWhitespace )
+}
diff --git a/challenge-083/jeongoon/go/ch-2.go b/challenge-083/jeongoon/go/ch-2.go
new file mode 100644
index 0000000000..60433a850f
--- /dev/null
+++ b/challenge-083/jeongoon/go/ch-2.go
@@ -0,0 +1,190 @@
+package main
+
+import (
+ "os"
+ "fmt"
+ "strconv"
+)
+
+func usage() {
+ fmt.Println( "Usage: go run ch-2.go <natural numbers> ..." );
+}
+
+type MaybeNat string
+
+func (str MaybeNat) Nat() Nat {
+ n, err := strconv.Atoi(string(str))
+ if err == nil {
+ return(Nat(n))
+ } else {
+ return(Nat(0))
+ }
+}
+
+func (str MaybeNat) isNatural() bool {
+ return(str.Nat() > 0)
+}
+
+type Nat int
+func (n Nat) String() string {
+ return fmt.Sprintf("%d", n)
+}
+
+/*
+ * Another my own version of making combinations without recursive call
+ * explanation in perl:
+ * https://github.com/jeongoon/pwc-by-me/blob/master/075/perl/CombinationsIndex.pm
+ * I found that this implementation very fast in Perl,
+ * so it will be the same in golang especially when compiled.
+ */
+
+func combinationsIndex( M int, N int ) [][]int {
+ // M: number of selection ( 0 ... (M-1) )
+ // N: number of choice
+ if M < N {
+ return [][]int{}
+ }
+
+ initRoomSize := M - N
+ room := make([]int, N)
+ pos := make([]int, N)
+ // https://stackoverflow.com/questions/39984957/is-it-possible-to-initialize-slice-with-specific-values
+ for i := range room {
+ room[i] = initRoomSize
+ }
+ for i := range pos {
+ pos[i] = i
+ }
+
+ var combis [][]int
+ new_case := make([]int, N)
+ copy(new_case, pos)
+ combis = append( [][]int{}, new_case )
+
+ cursor := N - 1 // initial: index of last elements in selection
+
+ for {
+ if room[cursor] > 0 {
+ room[cursor]--
+ pos[cursor]++
+ new_case := make([]int, N)
+ copy(new_case, pos)
+ combis = append(combis, new_case)
+ } else {
+ cursor_moved := false
+ for i := cursor; i > 0; i-- {
+ if room[i-1] > 0 {
+ cursor = i-1
+ cursor_moved = true
+ break
+ }
+ }
+ if cursor_moved {
+ new_room := room[cursor] - 1
+ base_pos := pos[cursor];
+ for p, i := 1, cursor; i < N; i++ {
+ room[i] = new_room
+ pos[i] = base_pos + p
+ p++ // p++, i++ not working on for()
+ }
+ new_case := make([]int, N)
+ copy(new_case, pos)
+ combis = append(combis, new_case)
+ cursor = N - 1
+ } else {
+ break
+ }
+ }
+
+ }
+
+ return combis
+}
+
+func main() {
+ if len(os.Args[1:]) < 1 {
+ usage();
+ os.Exit(1);
+ }
+
+ var N []Nat
+ all_good := true
+ debug := false
+
+ for _, str := range os.Args[1:] {
+ if MaybeNat(str).isNatural() {
+ N = append(N, MaybeNat(str).Nat())
+ } else {
+ all_good = false
+ break
+ }
+ }
+
+ if ! all_good {
+ usage();
+ os.Exit(2);
+ }
+
+ lenN := len(N)
+ if lenN == 1 {
+ // already minimum: no need to change
+ fmt.Println( "0" );
+ // but not error
+ os.Exit(0);
+ }
+
+ totalSum := 0
+ maxNum := 0
+ for _, n := range N {
+ totalSum += int(n)
+ if maxNum < int(n) {
+ maxNum = int(n)
+ }
+ }
+
+ minPositiveSum := totalSum
+ minPositiveElems := len(N)
+ for i := 1; i < lenN; i++ {
+ for _, cb := range combinationsIndex(lenN, i) {
+ negativeGroupSum := 0
+ for _, idx := range cb {
+ negativeGroupSum += int(N[idx])
+ }
+
+ diff := totalSum - 2 * negativeGroupSum
+ maybeNewElems := len(cb)
+
+ if debug {
+ fmt.Println( "before: ", diff,
+ " with ", maybeNewElems,
+ " <=> ", minPositiveSum,
+ " with ", minPositiveElems )
+ }
+
+ if diff >= 0 &&
+ minPositiveSum >= diff {
+ if minPositiveSum == diff {
+ if minPositiveElems > maybeNewElems {
+ minPositiveElems = maybeNewElems
+ }
+ } else { // minPositiveElems > diff
+ minPositiveElems = maybeNewElems
+ }
+
+ minPositiveSum = diff
+ }
+
+ if debug {
+ fmt.Println( "after: ", diff,
+ " with ", maybeNewElems,
+ " <=> ", minPositiveSum,
+ " with ", minPositiveElems )
+ }
+
+ if minPositiveSum == 0 && minPositiveElems == 1 {
+ break;
+ }
+ }
+ }
+ fmt.Println( minPositiveElems )
+}
diff --git a/challenge-083/jeongoon/haskell/Combinations.hs b/challenge-083/jeongoon/haskell/Combinations.hs
new file mode 100644
index 0000000000..f8c5324f43
--- /dev/null
+++ b/challenge-083/jeongoon/haskell/Combinations.hs
@@ -0,0 +1,25 @@
+{- Copyright (c) 2020 JEON Myoungjin <jeongoon@g... > -}
+
+module Combinations
+ ( combinations
+ ) where
+
+combinations :: [a] -> Int -> [[a]]
+combinations [] _ = []
+combinations (m:ms) 1 = [m] : (combinations ms 1)
+combinations [_] 2 = []
+combinations [e,f] 2 = sequence [ [e],[f] ]
+combinations (m:ms) 2 = sequence [ [m], ms ] ++ (combinations ms 2)
+combinations mls n =
+ case totalLen `compare` n of
+ LT -> []
+ EQ -> [mls]
+ _ -> [ let leaders = map (mls!!) ids
+ in leaders ++ followers |
+ ids <- combinations [ 0 .. room ] n',
+ let skipCount = (last ids) + 1,
+ followers <- (combinations (drop skipCount mls) 2) ]
+ where
+ totalLen = length mls
+ room = totalLen - 2
+ n' = n - 2
diff --git a/challenge-083/jeongoon/haskell/ch-1.hs b/challenge-083/jeongoon/haskell/ch-1.hs
new file mode 100644
index 0000000000..1d108ae629
--- /dev/null
+++ b/challenge-083/jeongoon/haskell/ch-1.hs
@@ -0,0 +1,20 @@
+import System.Environment
+import System.Exit
+
+{- test with:
+runhaskell ch-1.hs "Perl W e e k l y Challenge" # output: 6
+-}
+main = do
+ args <- getArgs;
+ if length args /= 1
+ then die "Usage: runhaskell ch-1.hs <a string with 3 or more words"
+ else let ws = words (args !! 0)
+ wl = length ws
+ in
+ if wl < 3 then die "we need 3 words at least"
+ else (putStrLn.
+ show.
+ length.
+ foldr1 (++).
+ take (wl-2).
+ drop 1) ws
diff --git a/challenge-083/jeongoon/haskell/ch-2.hs b/challenge-083/jeongoon/haskell/ch-2.hs
new file mode 100644
index 0000000000..c64170400a
--- /dev/null
+++ b/challenge-083/jeongoon/haskell/ch-2.hs
@@ -0,0 +1,53 @@
+import System.Environment
+import System.Exit
+import Data.Char (isNumber)
+import Data.Maybe (isJust, catMaybes)
+import Data.List (sum)
+import Combinations (combinations)
+
+
+{- test with:
+ runhaskell ch-2.hs 12 7 4 # answer: 12
+ runhaskell ch-2.hs 12 7 4 5 6 9 20 12 7 4 5 6 9 20 9 4 2 1 13 8 # answer: 6
+ # or in order to get faster result, you can compile and execute:
+ ghc ch-2.hs
+ ./ch-2 12 7 4 5 6 9 20 12 7 4 5 6 9 20 9 4 2 1 13 8 # answer: 6
+-}
+
+answerFlipArray :: (Integral a) => [a] -> Int
+answerFlipArray nums
+ | totalSum == 1 = 0
+ | otherwise = answerWith totalSum totalLen numCombis
+ where
+ totalSum = sum nums
+ totalLen = length nums
+ halfLen = totalLen `div` 2
+ numCombis = (foldr1 (++) .
+ map (combinations nums))
+ [ 1 .. halfLen ]
+
+ answerWith _ minElems [] = minElems
+ answerWith minSum minElems (aCombi:otherCombis) =
+ case (positiveSum `compare` minSum) of
+ LT -> answerWith positiveSum newElems otherCombis
+ EQ -> answerWith minSum (min newElems minElems) otherCombis
+ GT -> answerWith minSum minElems otherCombis
+ where
+ len = length aCombi
+ sm = sum aCombi
+ sm' = totalSum - sm
+ (positiveSum, newElems) =
+ case (sm `compare` sm') of
+ LT -> ( sm' - sm, len )
+ EQ -> ( 0, (min len (totalLen - len)) )
+ GT -> ( sm - sm', (totalLen - len) )
+
+main = do
+ (catMaybes.map (\nStr ->
+ if (all isNumber nStr) then Just(read nStr :: Int)
+ else Nothing )) `fmap` getArgs
+ >>= (\nums ->
+ if length nums < 1 then
+ die "Usage: runhaskell ch-2.hs <natural num> ..."
+ else
+ putStrLn $ show ( answerFlipArray nums ) )
diff --git a/challenge-083/jeongoon/perl/CombinationsIndex.pm b/challenge-083/jeongoon/perl/CombinationsIndex.pm
new file mode 100644
index 0000000000..385c8905cd
--- /dev/null
+++ b/challenge-083/jeongoon/perl/CombinationsIndex.pm
@@ -0,0 +1,123 @@
+# -*- Mode: cperl; cperl-indent-level:4; tab-width: 8; indent-tabs-mode: nil -*-
+# Copyright (c) 2013,2020 JEON Myoungjin <jeongoon@gmail.com>
+
+package CombinationsIndex;
+use strict; use warnings;
+
+use version 0.77; our $VERSION = version->declare( 'v0.3' );
+
+use parent 'Exporter';
+our @EXPORT_OK = qw(combinationsIndex);
+
+=pod
+
+=head1 Basic concept
+
+If we find the combintions when choosing 3 digits from index of 0 .. 4
+which shown below
+
+ 0 1 2 3 4
+ ^1 ^2 ^3 initial selection: index position: ( 0, 1, 2 )
+
+to get next combination we can move ^3 cursor from 2 to 3
+
+ 0 1 2 3 4
+ ^1 ^2 ^3 note: ^3 can move up to 4
+
+as you can see ^3 can only go up to 4, next movement we can imagine is that
+moving ^2 to next one and ^3 is just followed by ^2
+and next movement will be again ^3 to the index 4
+
+ 0 1 2 3 4 => 0 1 2 3 4 => 0 1 2 3 4
+ ^1 ^2 ^3 ^1 ^2 ^3 ^1 ^2 ^3
+
+and last case of combinations will be
+
+ 0 1 2 3 4
+ ^1 ^2 ^3
+
+so I make two arrays to record
+ 1. where each cursor is pointing now,
+ 2. how many rooms left for each cursor to move
+
+and also remember what is the current cursor move next.
+
+so we can also make combinations without recursive routine.
+
+=cut
+
+sub combinationsIndex ( $$ ) {
+ # I changed the order of arguments since v0.3
+ my $M = $_[0]; # choice" 0 .. ($M - 1)
+ my $N = $_[1]; # number of selection
+
+ my @result;
+
+ # minimum sanity check
+ if ( $M < $N ) {
+ warn "unable to choose $N from given selection of $M";
+ return ();
+ }
+
+ my ( @room, # number of spaces(rooms) each
+ @pos, # current position of cursor
+ $next_csr # next cursor to move
+ );
+
+ # set initial values ...
+ {
+ # each finger can move to right number of ( M-N ) space(s).
+ @room = ( $M-$N ) x $N;
+ @pos = 0 .. ($N - 1);
+ $next_csr = $N - 1; # last cursor at rightmost
+
+ # initial record; note: use not index number but real value
+ push @result, [ @pos ];
+ }
+
+ {
+ if ( $room[$next_csr] > 0 ) {
+ # current csr can move to right so just do it.
+ ++$pos[ $next_csr];
+ --$room[$next_csr]; # room decreased of course
+
+ # and make a record
+ push @result, [ @pos ];
+ redo;
+ }
+ else {
+ # no more room to move
+ # so find the next cursor to move
+ my $found = 0;
+ for ( my $i = $next_csr; $i > 0; --$i ) {
+ if ( $room[ $i-1 ] > 0 ) {
+ $next_csr = $i-1;
+ $found = 1;
+ last ;
+ }
+ }
+
+ if ( $found ) {
+ # move all the cursors which are starts from
+ # $next_csr to last one
+ @pos[ $next_csr .. ($N-1) ]
+ = map { $pos[$next_csr] + $_ } 1 .. ($N-$next_csr);
+ # note: all these finger has same room when moved
+ @room[ $next_csr .. ($N-1) ]
+ = ( $room[ $next_csr ] - 1 ) x ($N-$next_csr);
+
+ # and make a record
+ push @result, [ @pos ];
+
+ # next finger to move will be ($N-1)
+ # or even if it is not next loop will find anohter
+ $next_csr = ($N-1);
+
+ redo; # if we can move next cursor
+ }
+ }
+ }
+ @result;
+}
+
+!!"^^";
diff --git a/challenge-083/jeongoon/perl/ch-1.pl b/challenge-083/jeongoon/perl/ch-1.pl
new file mode 100644
index 0000000000..6db83a6e0b
--- /dev/null
+++ b/challenge-083/jeongoon/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+# -*- Mode: cperl; cperl-indent-level:4 tab-width: 8; indent-tabs-mode: nil -*-
+# -*- coding: utf-8 -*-
+
+use strict; use warnings;
+use v5.26;
+
+use List::Util qw(sum);
+
+sub usage {
+ say 'Usage: perl ch-1.pl <a string with 3 or more words>', "\n",
+ 'ex) perl ch-1.pl "Perl Weekly Challenge"';
+}
+
+package main;
+
+my @words;
+
+@ARGV == 1 or usage, exit 1;
+@words = split /\s/, $ARGV[0];
+@words > 2 or usage, exit 2;
+
+say(
+ sum
+ map {length} # count each length
+ @words[
+ 1 # from the second word
+ ..
+ $#words-1 # to the second last one
+ ]
+ );
diff --git a/challenge-083/jeongoon/perl/ch-2.pl b/challenge-083/jeongoon/perl/ch-2.pl
new file mode 100644
index 0000000000..75af463b95
--- /dev/null
+++ b/challenge-083/jeongoon/perl/ch-2.pl
@@ -0,0 +1,79 @@
+#!/usr/bin/env perl
+# -*- Mode: cperl; cperl-indent-level:4 tab-width: 8; indent-tabs-mode: nil -*-
+# -*- coding: utf-8 -*-
+
+use strict; use warnings;
+use v5.26;
+use List::Util qw(all sum);
+
+use FindBin;
+use lib ( $FindBin::Bin );
+use CombinationsIndex v0.3 qw(combinationsIndex);
+
+sub usage {
+ say 'Usage: perl ch-2.pl [-d|--debug] <natural num> ... ', "\n",
+ 'ex) perl ch-2.pl 12 2 10';
+}
+
+sub combinations ($$$) {
+ my ( $indexCount, $minSelection, $maxSelection ) = @_;
+ map { combinationsIndex( $indexCount, $_ ) }
+ $minSelection .. $maxSelection;
+}
+
+package main;
+
+my @N = grep { ! /-(h|-*help)/ } @ARGV;
+@N == @ARGV or usage, exit 0;
+
+@N = grep { ! /-(d|-*debug)/ } @ARGV;
+our $d = @N != @ARGV;
+
+( @N > 0
+ and
+ all { int($_) eq $_ and $_ > 0 } @N ) or usage, exit 1;
+
+@N == 1 and ( say( "0" ), exit 0 ); # already mimimum
+
+my $totalSum = sum @N;
+my $halfLen = int( .5 * @N ); # reduce the combinations in half
+
+# initial values ...
+my $minElems = +@N;
+my $minSum = $totalSum;
+
+for my $combi ( combinations( +@N, 1, $halfLen ) ) {
+ my $aSum = sum @N[ @$combi ];
+ my $bSum = $totalSum - $aSum;
+
+ my $curr =
+ ( # $aSum == $bSum
+ [ 0, ( scalar @$combi < $halfLen
+ ? scalar @$combi : scalar( @N - @$combi) ) ],
+ # $aSum > $bSum
+ [ $aSum - $bSum, scalar ( @N - @$combi) ],
+ # $aSum < $bSum
+ [ $bSum - $aSum, scalar @$combi ] )[ $aSum <=> $bSum ];
+
+ print "[sum: $$curr[0], elems: $$curr[1]] with @N[@$combi] ... " if $d;
+
+ if ( $$curr[0] > $minSum ) { # minimum sum not changed
+ say "skipped." if $d;
+ next;
+ }
+ elsif ( $$curr[0] < $minSum ) { # minimum sum cahnged
+ # so does minimum elements
+ say "**minium sum changed**" if $d;
+ ( $minSum, $minElems ) = @$curr;
+ }
+ elsif ( $$curr[1] < $minElems ) { # minimum sum is same
+ # also elements is less
+ say "**minimum count changed**" if $d;
+ $minElems = $$curr[1];
+ }
+ else {
+ say "" if $d;
+ }
+}
+
+say $minElems;
diff --git a/challenge-083/jeongoon/raku/ch-2.raku b/challenge-083/jeongoon/raku/ch-2.raku
index 45fbdcee95..0506ce2dc8 100644
--- a/challenge-083/jeongoon/raku/ch-2.raku
+++ b/challenge-083/jeongoon/raku/ch-2.raku
@@ -7,9 +7,29 @@
use v6.d;
multi MAIN ($n where * > 0) { say 0 }
+
+# without race
multi MAIN (*@n where { @n.all ~~ Int and @n.all > 0 }) {
my $s = @n.sum;
@n.combinations( 1..^ @n ).
+ map(
+ -> \n {
+ with $s - 2 * n.sum { # same as ( $s- n.sum ) - n.sum
+ next if $_ < 0;
+ $_ => n.elems
+ }
+ } ).
+ min.
+ value.
+ say
+}
+
+# tested with: raku jeongoon/raku/ch-2.raku --r 12 7 4 5 6 9 20 12 7 4 5 6 9 20 9 4 2 1 13
+# finished in under 4 seconds on my laptop
+multi MAIN ( Bool:D :$r, *@n where { @n.all ~~ Int and @n.all > 0 }) {
+ $*ERR.say( "using race ..." );
+ my $s = @n.sum;
+ @n.combinations( 1..^ @n ).
race( :8degree:500batch ).
map(
-> \n {
@@ -18,7 +38,7 @@ multi MAIN (*@n where { @n.all ~~ Int and @n.all > 0 }) {
$_ => n.elems
}
} ).
- race( :8degree:5000batch ).
+ race( :8degree:500batch ).
min.
value.
say
diff --git a/challenge-083/jeongoon/raku/ch-2.without-race.raku b/challenge-083/jeongoon/raku/ch-2.without-race.raku
deleted file mode 100644
index 26dc21fbec..0000000000
--- a/challenge-083/jeongoon/raku/ch-2.without-race.raku
+++ /dev/null
@@ -1,23 +0,0 @@
-#!/usr/bin/env raku
-# -*- Mode: Raku; indent-tabs-mode: nil; coding: utf-8 -*-
-# vim: set et ts=4 sw=4:
-
-# personal-blog: https://dev.to/jeongoon/weekly-challenge-083-task-2-raku-2cjm
-
-use v6.d;
-
-multi MAIN ($n where * > 0) { say 0 }
-multi MAIN (*@n where { @n.all ~~ Int and @n.all > 0 }) {
- my $s = @n.sum;
- @n.combinations( 1..^ @n ).
- map(
- -> \n {
- with $s - 2 * n.sum { # same as ( $s- n.sum ) - n.sum
- next if $_ < 0;
- $_ => n.elems
- }
- } ).
- min.
- value.
- say
-}
diff --git a/challenge-083/jeongoon/raku/ch-2.yet-another-race.raku b/challenge-083/jeongoon/raku/ch-2.yet-another-race.raku
index 911d0f2136..6e0a952d6d 100644
--- a/challenge-083/jeongoon/raku/ch-2.yet-another-race.raku
+++ b/challenge-083/jeongoon/raku/ch-2.yet-another-race.raku
@@ -3,34 +3,40 @@
# vim: set et ts=4 sw=4:
# personal-blog: https://dev.to/jeongoon/weekly-challenge-083-task-2-raku-2cjm
-# I hoped that this will be faster but it wasn't :-/
+# I wished this implementation faster but it wasn't
+# I guess Raku doesn't like conditional flow control
+# so... generating is simpler and faster than filtering ??
use v6.d;
multi MAIN ($n where * > 0) { say 0 }
multi MAIN (*@n where { @n.all ~~ Int and @n.all > 0 }) {
- my \B = @n.Bag;
- my \S = @n.sum;
+ my \TotalSum = @n.sum;
my $half-len-floor = @n.elems div 2;
- @n.combinations( 1.. $half-len-floor ).
- race( :8degree:1000batch ).
+ @n.combinations( 1.. $half-len-floor ). # reduced combinations length
+ race( :8degree:500batch ).
map(
- -> $G
+ -> $grp # current combination
{
- my $a = $G.sum; # sum of selected group
- my $b = (S - $G.sum);
- given $a - $b {
- if $_ < 0 {
- -$_ => $G.elems
- } elsif $_ == 0 {
- |( 0 => $G.elems, 0 => (B (-) $G.Bag).total )
- } else {
- $_ => (B (-) $G.Bag).total
+ my $a = $grp.sum; # sum of selected group
+ my $b = (TotalSum - $grp.sum);
+ given $a cmp $b {
+ when Less {
+ ($b-$a) => $grp.elems
+ }
+ when More {
+ ($a-$b) => @n.elems - $grp.elems
+ }
+ default {
+ 0 => ($grp.elems < $half-len-floor
+ # using shorter elems
+ ?? $grp.elems !! @n.elems - $grp.elems)
}
}
}
).
- race( :8degree:1000batch ).
+ race( :8degree:500batch ).
min.
+ value.
say
}
diff --git a/challenge-083/tyler-wardhaugh/clojure/README.md b/challenge-083/tyler-wardhaugh/clojure/README.md
index 546cb8db65..bf37bd62cc 100644
--- a/challenge-083/tyler-wardhaugh/clojure/README.md
+++ b/challenge-083/tyler-wardhaugh/clojure/README.md
@@ -1,4 +1,4 @@
-# tw.weekly.c82
+# tw.weekly.c83
The Weekly Challenge - #082 - Tyler Wardhaugh
@@ -7,7 +7,7 @@ The Weekly Challenge - #082 - Tyler Wardhaugh
Run the project directly (shows default output from both tasks):
- $ clojure -M -m tw.weekly.c82.core
+ $ clojure -M -m tw.weekly.c83.core
Run the project's tests (which are samples from the task descriptions):
@@ -15,11 +15,11 @@ Run the project's tests (which are samples from the task descriptions):
Run Task #1 with input
- $ clojure -M -m tw.weekly.c82.t1 M N
+ $ clojure -M -m tw.weekly.c83.t1 S
Run Task #2 with input:
- $ clojure -M -m tw.weekly.c82.t2 S1 S2
+ $ clojure -M -m tw.weekly.c83.t2 A1 A2 A3...
## Project Template
diff --git a/challenge-083/tyler-wardhaugh/clojure/deps.edn b/challenge-083/tyler-wardhaugh/clojure/deps.edn
index c692c74c45..e3e3cdbca6 100644
--- a/challenge-083/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-083/tyler-wardhaugh/clojure/deps.edn
@@ -1,6 +1,7 @@
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
- org.clojure/math.numeric-tower {:mvn/version "0.0.4"}}
+ org.clojure/math.combinatorics {:mvn/version "0.1.6"}
+ net.cgrand/xforms {:mvn/version "0.19.2"}}
:aliases
{:test {:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}}
@@ -11,5 +12,5 @@
:main-opts ["-m" "cognitect.test-runner"
"-d" "test"]}
:uberjar {:extra-deps {seancorfield/depstar {:mvn/version "1.0.94"}}
- :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c82.jar"
- "-C" "-m" "tw.weekly.c82"]}}}
+ :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c83.jar"
+ "-C" "-m" "tw.weekly.c83"]}}}
diff --git a/challenge-083/tyler-wardhaugh/clojure/pom.xml b/challenge-083/tyler-wardhaugh/clojure/pom.xml
index 92eb55d64a..7d5a4bb861 100644
--- a/challenge-083/tyler-wardhaugh/clojure/pom.xml
+++ b/challenge-083/tyler-wardhaugh/clojure/pom.xml
@@ -2,11 +2,11 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tw.weekly</groupId>
- <artifactId>tw.weekly.c82</artifactId>
+ <artifactId>tw.weekly.c83</artifactId>
<version>0.1.0-SNAPSHOT</version>
- <name>tw.weekly.c82</name>
- <description>Challenge #082</description>
- <url>https://github.com/tw.weekly/tw.weekly.c82</url>
+ <name>tw.weekly.c83</name>
+ <description>Challenge #083</description>
+ <url>https://github.com/tw.weekly/tw.weekly.c83</url>
<licenses>
<license>
<name>Eclipse Public License</name>
@@ -19,9 +19,9 @@
</developer>
</developers>
<scm>
- <url>https://github.com/tw.weekly/tw.weekly.c82</url>
- <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c82.git</connection>
- <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c82.git</developerConnection>
+ <url>https://github.com/tw.weekly/tw.weekly.c83</url>
+ <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c83.git</connection>
+ <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c83.git</developerConnection>
<tag>HEAD</tag>
</scm>
<dependencies>
diff --git a/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/core.clj b/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/core.clj
new file mode 100644
index 0000000000..d25ddca2de
--- /dev/null
+++ b/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/core.clj
@@ -0,0 +1,12 @@
+(ns tw.weekly.c83.core
+ (:require [tw.weekly.c83.t1 :as t1])
+ (:require [tw.weekly.c83.t2 :as t2])
+ (:gen-class))
+
+(defn -main
+ "Run all tasks"
+ [& _]
+ (println "Task #1")
+ (t1/-main)
+ (println "Task #2")
+ (t2/-main))
diff --git a/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t1.clj b/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t1.clj
new file mode 100644
index 0000000000..36ca66aec0
--- /dev/null
+++ b/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t1.clj
@@ -0,0 +1,32 @@
+(ns tw.weekly.c83.t1
+ (:require [clojure.string :as str]))
+
+;;; Task description for TASK #1 › Words Length
+; Submitted by: Mohammad S Anwar
+; You are given a string $S with 3 or more words.
+;
+; Write a script to find the length of the string except the first and last words ignoring whitespace.
+;
+; Example 1:
+; Input: $S = "The Weekly Challenge"
+; Output: 6
+;
+;
+; Example 2:
+; Input: $S = "The purpose of our lives is to be happy"
+; Output: 23
+;
+;;;
+
+(defn inner-words-length
+ "Return the combined length of the inner words, ignoring whitespace."
+ [s]
+ (let [source (->> (str/split s #" ") (drop 1) (drop-last 1))]
+ (transduce (map count) + source)))
+
+(defn -main
+ "Run Task 1 with a strings S, defaulting to the first example given in the task description."
+ [& args]
+ (let [S (or (some-> args first) "The Weekly Challenge")
+ len (inner-words-length S)]
+ (println len)))
diff --git a/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t2.clj b/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t2.clj
new file mode 100644
index 0000000000..1dc22c8372
--- /dev/null
+++ b/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t2.clj
@@ -0,0 +1,55 @@
+(ns tw.weekly.c83.t2
+ (:require [clojure.edn :as edn])
+ (:require [clojure.math.combinatorics :as combo])
+ (:require [net.cgrand.xforms :as x]))
+
+;;; Task description for
+; TASK #2 › Flip Array
+; Submitted by: Mohammad S Anwar
+; You are given an array @A of positive numbers.
+;
+; Write a script to flip the sign of some members of the given array so that
+; the sum of the all members is minimum non-negative.
+;
+; Given an array of positive elements, you have to flip the sign of some of its
+; elements such that the resultant sum of the elements of array should be
+; minimum non-negative(as close to zero as possible). Return the minimum no. of
+; elements whose sign needs to be flipped such that the resultant sum is
+; minimum non-negative.
+;
+; Example 1:
+; Input: @A = (3, 10, 8)
+; Output: 1
+
+; Explanation:
+; Flipping the sign of just one element 10 gives the result 1 i.e. (3) + (-10) + (8) = 1
+;
+;
+; Example 2:
+; Input: @A = (12, 2, 10)
+; Output: 1
+;
+; Explanation:
+; Flipping the sign of just one element 12 gives the result 0 i.e. (-12) + (2) + (10) = 0