Last active
November 16, 2022 19:12
-
-
Save AlessandroLorenzi/79a09afa88fe749b572baa26d5ec7ca3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package equal_stack | |
func equalStacks(h1 []int32, h2 []int32, h3 []int32) int32 { | |
allHeights1 := stackAllHeights(h1) | |
allHeights2 := stackAllHeights(h2) | |
allHeights3 := stackAllHeights(h3) | |
var commonBetween12 []int32 | |
for i := 0; i < len(allHeights1); i++ { | |
for j := 0; j < len(allHeights2); j++ { | |
if allHeights1[i] == allHeights2[j] { | |
commonBetween12 = append(commonBetween12, allHeights1[i]) | |
} | |
} | |
} | |
for l := 0; l < len(commonBetween12); l++ { | |
for k := 0; k < len(allHeights3); k++ { | |
if allHeights3[k] == commonBetween12[l] { | |
return commonBetween12[l] | |
} | |
} | |
} | |
return 0 | |
} | |
func stackHeight(h []int32) int32 { | |
var height int32 | |
for _, v := range h { | |
height += v | |
} | |
return height | |
} | |
func stackAllHeights(h1 []int32) []int32 { | |
var allHeights []int32 | |
for i := 0; i < len(h1); i++ { | |
allHeights = append(allHeights, stackHeight(h1[i:])) | |
} | |
return allHeights | |
} | |
import ( | |
"testing" | |
) | |
func TestStackHeight(t *testing.T) { | |
h := []int32{3, 2, 1, 1, 1} | |
if stackHeight(h) != 8 { | |
t.Errorf("stackHeight(%v) != 8", h) | |
} | |
} | |
func TestStackAllHeights(t *testing.T) { | |
h1 := []int32{3, 2, 1, 1, 1} | |
expectedAllHeights := []int32{8, 5, 3, 2, 1} | |
allHeights := stackAllHeights(h1) | |
if len(allHeights) != len(h1) { | |
t.Errorf("stackAllHeights(%v) != %v", len(allHeights), len(h1)) | |
} | |
for i := 0; i < len(allHeights); i++ { | |
if allHeights[i] != expectedAllHeights[i] { | |
t.Errorf("StackAllHeights(%v) position %v != %v", allHeights[i], i, expectedAllHeights[i:]) | |
} | |
} | |
} | |
func TestCompareTwoStacks(t *testing.T) { | |
h1 := []int32{3, 2, 1, 1, 1} | |
h2 := []int32{4, 3, 2} | |
h3 := []int32{1, 1, 4, 1} | |
if equalStacks(h1, h2, h3) != 5 { | |
t.Errorf("compareTwoStacks(%v, %v, %v) != 5", h1, h2, h3) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment