2020 Day 21 Complete
This commit is contained in:
@@ -252,6 +252,15 @@ func IntSlicesAreEqual(s1 []int, s2 []int) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func StringSliceIndex(h []string, n string) int {
|
||||
for k, v := range h {
|
||||
if v == n {
|
||||
return k
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// StringSliceContains takes a string slice and a string and return true
|
||||
// if the string is in the slice
|
||||
func StringSliceContains(h []string, n string) bool {
|
||||
@@ -282,6 +291,46 @@ func AppendStrings(base string, group []string) []string {
|
||||
return group
|
||||
}
|
||||
|
||||
// UnionStringSlice returns a slice that combines list1 and list2
|
||||
func UnionStringSlice(list1, list2 []string) []string {
|
||||
for k := range list1 {
|
||||
if !StringSliceContains(list2, list1[k]) {
|
||||
list2 = append(list2, list1[k])
|
||||
}
|
||||
}
|
||||
return list2
|
||||
}
|
||||
|
||||
// IntersectStringSlice returns a slice that contains all elements that
|
||||
// both list1 and list2 contain
|
||||
func IntersectStringSlice(list1, list2 []string) []string {
|
||||
var ret []string
|
||||
for k := range list1 {
|
||||
if StringSliceContains(list2, list1[k]) {
|
||||
ret = append(ret, list1[k])
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// Unique removes all duplicates
|
||||
func UniqueStringSlice(list []string) []string {
|
||||
var ret []string
|
||||
for k := 0; k < len(list); k++ {
|
||||
var dupe bool
|
||||
for j := k; j < len(list); j++ {
|
||||
if list[k] == list[j] {
|
||||
dupe = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !dupe {
|
||||
ret = append(ret, list[k])
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// IsPrime takes a number and return true if that number is prime
|
||||
func IsPrime(value int) bool {
|
||||
for i := 2; i <= int(math.Floor(float64(value)/2)); i++ {
|
||||
|
Reference in New Issue
Block a user