summaryrefslogtreecommitdiff
path: root/main.go
blob: d475ac32083c07bba16ee6106642a5f361e2f38e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// http://www.spoj.com/problems/HOMO/

package main

import (
	"bufio"
	"io"
	"os"
	"strconv"
	"strings"
)

type List map[int]int

func (l List) Insert(n int) {
	l[n]++
}

func (l List) Delete(n int) {
	if l[n]--; l[n] <= 0 {
		delete(l, n)
	}
}

func (l List) IsHomo() bool {
	var top int
	for _, v := range l {
		if v > top {
			top = v
		}
	}
	return top > 1
}

func (l List) IsHetero() bool {
	return len(l) > 1
}

func Split(s string) (string, int) {
	i := strings.Index(s, " ")
	n, _ := strconv.Atoi(s[i+1:])
	return s[:i], n
}

func (l List) String() string {
	homo, hetero := l.IsHomo(), l.IsHetero()
	switch {
	case homo && hetero:
		return "both"
	case hetero:
		return "hetero"
	case homo:
		return "homo"
	default:
		return "neither"
	}
}

func Homo(r io.Reader, w io.Writer) {
	scanner := bufio.NewScanner(r)
	scanner.Scan() // eat # of cases

	l := make(List)

	for scanner.Scan() {
		s, n := Split(scanner.Text())
		switch s {
		case "insert":
			l.Insert(n)
		case "delete":
			l.Delete(n)
		}
		io.WriteString(w, l.String())
		io.WriteString(w, "\n")
	}
}

func main() {
	Homo(os.Stdin, os.Stdout)
}