aboutsummaryrefslogtreecommitdiff
path: root/challenge-085/jeongoon/go/ch-1.go
blob: b64fcb186e5644916c53d41ed7aeba86c2c58653 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main

/* tested with:
 * go run ch-1.go 1.1 0.8 0.4 0.2 2.3 1.1 0.4 0.9 4.3
 * 1 as 1.0 < (0.200 + 0.400 + 0.800) < 2.0
 */

import (
	"os"
	"fmt"
	"strconv"
	"strings"
	"sort"
)

func usage () {
	fmt.Println( "Usage: go run ch-1.go <real numbers> ..." );
}

type combiIter struct {
	dlen int // data length
	nsel int // number of selection
	ncsr int // next cursor
	room []int
	pos  []int
}

// adapted from challenge-083/jeongoon/ch-2.go
// this combinations implentation keep the status into combiIter
// and give next combination when calling nextIndices()
func lazyCombinationsIndex( M int, N int ) *combiIter {
	iter := &combiIter{
		M,
		N,
		N-1,
		make([]int, N),
		make([]int, N),
	}

	initRoomSize := M - N

	for i := 0; i < (N-1); i++ {
		iter.room[i] = initRoomSize
		iter.pos[i]  = i
	}
	// note: we'll increase when nextIdices() called for the first time.
	iter.room[N-1] = initRoomSize + 1
	iter.pos[N-1]  = N - 2;

	return iter
}

func (ci *combiIter) nextIndices() []int {
	var new_case []int

	if ci.room[ci.ncsr] > 0 {
		ci.room[ci.ncsr]--
		ci.pos[ci.ncsr]++
		new_case = make([]int, ci.nsel)
		copy(new_case, ci.pos)
		return new_case
	} else {
		cursor_moved := false
		for i := ci.ncsr; i > 0; i-- {
			if ci.room[i-1] > 0 {
				ci.ncsr = i - 1;
				cursor_moved = true
				break
			}
		}
		if cursor_moved {
			new_room := ci.room[ci.ncsr] - 1
			base_pos := ci.pos[ci.ncsr];
			for p, i := 1, ci.ncsr; i < ci.nsel; i++ {
				ci.room[i] = new_room
				ci.pos[i] = base_pos + p
				p++
			}
			new_case = make([]int, ci.nsel)
			copy(new_case, ci.pos)
			ci.ncsr = ci.nsel - 1
		} else {
			new_case = []int{}
		}
	}
	return new_case
}

func parseStringsToFloat64s( args []string ) []float64 {
	var result []float64
	for i, maybe_f := range args {
		f, err := strconv.ParseFloat(maybe_f, 64)
		if err == nil {
			result = append(result, f)
		} else {
			fmt.Fprintf(os.Stderr,
				"wrong float value: [%d] [%s]\n", i, maybe_f)
		}
	}
	return result
}

func tidyList( flist []float64 ) []float64 {
	var result []float64
	sorted := flist  // copy
	sort.Float64s(sorted) // sort (in place)

	// only numbers < 2.0
	for _, f := range(sorted) {
		if f < 2.0 {
			result = append(result, f)
		}
	}
	return result
}

func main() {
	if len(os.Args[1:]) < 3 {
		usage();
		os.Exit(1);
	}

	flist_ := parseStringsToFloat64s(os.Args[1:])
	flist  := tidyList( flist_ )

	it := lazyCombinationsIndex(len(flist), 3)
	var foundTripletStrs []string
	for {
		tripletIndices := it.nextIndices();
		if len(tripletIndices) == 0 {
			break
		}

		sum := 0.0;
		for _, idx := range( tripletIndices ) {
			sum += flist[idx]
		}
		if sum > 1.0 && sum < 2.0 {
			for _, idx := range( tripletIndices ) {
				fStr := strconv.
					FormatFloat(flist[idx], 'f', 3, 64)
				foundTripletStrs = append(foundTripletStrs,fStr)
			}
			break
		}
	}

	if len(foundTripletStrs) == 0 {
		fmt.Println("0");
		os.Exit(2);
	} else {
		fmt.Println("1 as 1.0 < (" +
			strings.Join(foundTripletStrs, " + ") +
			") < 2.0")
		os.Exit(0);
	}
}