mchirico / zDaily

Playground to hack and test ideas with Zoe
1 stars 2 forks source link

Day 91: Go Program #99

Open mchirico opened 3 years ago

mchirico commented 3 years ago

Write a Go program that takes two sequences of integers, and returns true if the 2nd sequence is a subset of the first.

The program should work for any sequence. Here is an example.

seq1 := []int{1,2,3,4,5}
seq2 := []int{4,2}

Above, seq2 is a subset of seq1.

func subset(seq1 []int, seq2 []int ) bool {

        /* write code here */
    return answer
}

Hints

You can do this with maps, which is one way. Below is a sample test function


func Test_subset(t *testing.T) {
    type args struct {
        seq1 []int
        seq2 []int
    }
    tests := []struct {
        name string
        args args
        want bool
    }{
        // TODO: Add test cases.
        {name: "Test true", args: args{seq1: []int{1, 2, 3, 4, 5}, seq2: []int{4, 2}}, want: true},
        {name: "Test false", args: args{seq1: []int{1, 2, 3, 4, 5}, seq2: []int{4, 2, 7}}, want: false},
        {name: "Test false", args: args{seq1: []int{1, 2, 3, 4, 5}, seq2: []int{14, 12, 7}}, want: false},
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := subset(tt.args.seq1, tt.args.seq2); got != tt.want {
                t.Errorf("subset() = %v, want %v", got, tt.want)
            }
        })
    }
}

Validate sequence

Possible solution

Here's what I came up with program; but, you should try it yourself first.

tacomonkautobot[bot] commented 3 years ago

mchirico, Thanks for opening this issue!

ZoeChiri commented 3 years ago

func subset(seq1 []int, seq2 []int ) bool {

        /* write code here */
    1index :=0
    2index := 0
    for 1index < len(seq1) && 2index < len(seq2){
         if seq1[1index] == seq2[2index] {
             2index +=1
         }
         1index +=1
             }
             return 1index == len(seq1)
             }