tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
106
Task/Power-set/Go/power-set.go
Normal file
106
Task/Power-set/Go/power-set.go
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// types needed to implement general purpose sets are element and set
|
||||
|
||||
// element is an interface, allowing different kinds of elements to be
|
||||
// implemented and stored in sets.
|
||||
type element interface {
|
||||
// an element must be disinguishable from other elements to satisfy
|
||||
// the mathematical definition of a set. a.eq(b) must give the same
|
||||
// result as b.eq(a).
|
||||
eq(element) bool
|
||||
// String result is used only for printable output. Given a, b where
|
||||
// a.eq(b), it is not required that a.String() == b.String().
|
||||
String() string
|
||||
}
|
||||
|
||||
// integer type satisfying element interface
|
||||
type intEle int
|
||||
|
||||
func (i intEle) eq(e element) bool {
|
||||
if j, ok := e.(intEle); ok {
|
||||
return i == j
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (i intEle) String() string {
|
||||
return strconv.Itoa(int(i))
|
||||
}
|
||||
|
||||
// set type implemented as a simple list. methods will be added to
|
||||
// make it satisfy the element interface, allowing sets of sets.
|
||||
type set []element
|
||||
|
||||
// uniqueness of elements can be ensured by using add method
|
||||
func (s *set) add(e element) {
|
||||
for _, ex := range *s {
|
||||
if e.eq(ex) {
|
||||
return
|
||||
}
|
||||
}
|
||||
*s = append(*s, e)
|
||||
}
|
||||
|
||||
// method to satify element interface
|
||||
func (s set) eq(e element) bool {
|
||||
t, ok := e.(set)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if len(s) != len(t) {
|
||||
return false
|
||||
}
|
||||
sLoop:
|
||||
for _, se := range s {
|
||||
for _, te := range t {
|
||||
if se.eq(te) {
|
||||
continue sLoop
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// method to satify element interface
|
||||
func (s set) String() string {
|
||||
r := "{"
|
||||
for _, e := range s {
|
||||
if len(r) > 1 {
|
||||
r += " "
|
||||
}
|
||||
r += fmt.Sprint(e)
|
||||
}
|
||||
return r + "}"
|
||||
}
|
||||
|
||||
// method required for task
|
||||
func (s set) powerSet() set {
|
||||
r := set{set{}}
|
||||
for _, es := range s {
|
||||
var u set
|
||||
for _, er := range r {
|
||||
u = append(u, append(er.(set), es))
|
||||
}
|
||||
r = append(r, u...)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func main() {
|
||||
var s set
|
||||
for _, i := range []int{1, 2, 2, 3, 4, 4, 4} {
|
||||
s.add(intEle(i))
|
||||
}
|
||||
fmt.Println(s)
|
||||
fmt.Println("length =", len(s))
|
||||
ps := s.powerSet()
|
||||
fmt.Println(ps)
|
||||
fmt.Println("length =", len(ps))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue