"Go isSuperset()"의 두 판 사이의 차이

 
(같은 사용자의 중간 판 하나는 보이지 않습니다)
74번째 줄: 74번째 줄:
==같이 보기==
==같이 보기==
* [[Go contains()]]
* [[Go contains()]]
* [[Go ElementsEqual()]]
* [[함수 issuperset()]]


[[분류: Go 슬라이스]]
[[분류: Go 슬라이스]]

2022년 7월 29일 (금) 17:04 기준 최신판

1 개요[ | ]

Go isSuperset()

2 isSupersetInt()[ | ]

package main

import "fmt"

func main() {
	a := []int{12, 2, 1, 11}
	b := []int{11, 2}

	fmt.Println(isSupersetInt(a, b))           // true
	fmt.Println(isSupersetInt(a, []int{1, 2})) // true

	fmt.Println(isSupersetInt(a, []int{1, 2, 3})) // false
}

func isSupersetInt(superset, subset []int) bool {
	for _, element := range subset {
		if !containsInt(superset, element) {
			return false
		}
	}
	return true
}

func containsInt(slice []int, x int) bool {
	for _, element := range slice {
		if element == x {
			return true
		}
	}
	return false
}

3 isSupersetString()[ | ]

package main

import "fmt"

func main() {
	a := []string{"z", "b", "a", "y"}
	b := []string{"y", "b"}

	fmt.Println(isSupersetString(a, b))                  // true
	fmt.Println(isSupersetString(a, []string{"a", "b"})) // true

	fmt.Println(isSupersetString(a, []string{"a", "b", "c"})) // false
}

func isSupersetString(superset, subset []string) bool {
	for _, element := range subset {
		if !containsString(superset, element) {
			return false
		}
	}
	return true
}

func containsString(slice []string, x string) bool {
	for _, element := range slice {
		if element == x {
			return true
		}
	}
	return false
}

4 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}