aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-19 07:07:58 +0100
committerGitHub <noreply@github.com>2020-10-19 07:07:58 +0100
commitd46f83e37e3c252e2f566da9a78f7ad8c8254671 (patch)
treed460185b78d3ad9c3180b2ca79c5aff292155317
parentd7cc7d8fd62bb12e0129ac28c7c0bc211f560397 (diff)
parentc83ed6980345c9941b75df3c2884c3007befb2e3 (diff)
downloadperlweeklychallenge-club-d46f83e37e3c252e2f566da9a78f7ad8c8254671.tar.gz
perlweeklychallenge-club-d46f83e37e3c252e2f566da9a78f7ad8c8254671.tar.bz2
perlweeklychallenge-club-d46f83e37e3c252e2f566da9a78f7ad8c8254671.zip
Merge pull request #2563 from jeongoon/master
[ch-082/jeongoon] I think this is correct one {ch-2.go, Ch2PlanA.pm}
-rw-r--r--challenge-082/jeongoon/go/ch-2.go31
-rw-r--r--challenge-082/jeongoon/perl/Ch2PlanA.pm17
2 files changed, 31 insertions, 17 deletions
diff --git a/challenge-082/jeongoon/go/ch-2.go b/challenge-082/jeongoon/go/ch-2.go
index a0246c7b2e..4cca7b4a72 100644
--- a/challenge-082/jeongoon/go/ch-2.go
+++ b/challenge-082/jeongoon/go/ch-2.go
@@ -19,24 +19,28 @@ import (
type MaybeIntereaved string
+type SavedPlace struct {
+ Apos int
+ Bpos int
+}
+
func (C MaybeIntereaved) IsInterleavedFrom (A string, B string) bool {
Alen, Blen, Clen := len(A), len(B), len(C)
if Alen + Blen != Clen {
return false
}
- Apin, Bpin := -1, -1 // if * >= 0, we have plan B
+ savedPos := []SavedPlace{}
+
checkingPlanB := false
for Ai, Bi, Ci := 0, 0, 0 ;; Ci = Ai + Bi {
if checkingPlanB {
- if Bpin > -1 {
- // note: it was A[Ai] == B[Bi] == C[Ci]
- // and tried A already.
- Bi = Bpin + 1
- Ai = Apin
- Apin, Bpin = -1, -1
- checkingPlanB = false
+ if len( savedPos ) > 0 {
+ Ai = savedPos[0].Apos
+ Bi = savedPos[0].Bpos
+ savedPos = savedPos[1:]
+
Ci = Ai + Bi
} else {
// there is no plan B ...
@@ -56,14 +60,21 @@ func (C MaybeIntereaved) IsInterleavedFrom (A string, B string) bool {
continue
}
} else if Bi == Blen {
- return A[Ai:] == string(C[Ci:])
+ if A[Ai:] == string(C[Ci:]) {
+ return true
+ } else {
+ checkingPlanB = true
+ continue
+ }
}
if A[Ai] == B[Bi] {
if A[Ai] != C[Ci] {
checkingPlanB = true
} else {
// remember this node
- Apin, Bpin = Ai, Bi
+ savedPos = append(
+ []SavedPlace{ SavedPlace { Apos: Ai,
+ Bpos: (Bi+1) } }, savedPos...)
// try plan A first
Ai++
}
diff --git a/challenge-082/jeongoon/perl/Ch2PlanA.pm b/challenge-082/jeongoon/perl/Ch2PlanA.pm
index 398d48acf5..b85eff1b9c 100644
--- a/challenge-082/jeongoon/perl/Ch2PlanA.pm
+++ b/challenge-082/jeongoon/perl/Ch2PlanA.pm
@@ -26,8 +26,7 @@ sub isInterleaving ($$$) {
if ( $checkingPlanB ) {
last if @saved == 0; # there is no plan B ...
- ( $Ai, $Bi ) = @saved;
- @saved = ();
+ ( $Ai, $Bi ) = @{pop @saved};
$checkingPlanB = 0; # reset status
}
$Ci = $Ai + $Bi;
@@ -45,16 +44,20 @@ sub isInterleaving ($$$) {
}
}
elsif ( $Bi == $Blen ) { # used $B all
- # but no need to check plan B
- # just because we always take 'A' when we have to choose
- $interleaved = ( substr $A, $Ai ) eq ( substr $C, $Ci )
+ if ( (substr $A, $Ai) eq (substr $C, $Ci) ) {
+ # and rest of A is same as rest of C
+ $interleaved = 1
+ }
+ else {
+ ( $checkingPlanB = 1, redo );
+ }
}
else {
my ( $headA, $headB ) = ((substr $A, $Ai, 1), (substr $B, $Bi , 1));
if ( $headA eq $headB ) {
if ( $headA eq ( substr $C, $Ci, 1 ) ) {
# save this place
- @saved = ( $Ai, ($Bi+1) );
+ push @saved, [ $Ai, ($Bi+1) ];
# then try A (always) first for next case
++$Ai
}
@@ -81,4 +84,4 @@ sub isInterleaving ($$$) {
$interleaved
}
-!"yes! no!";
+!!"yes! no!";