Combine different sort in Go - sorting

How can I combine sorters in Go? For example first I need sort by number of comments but if number of comments is null I need sort alphabetically.
This is what I have tried.
func sortArticles(articles []*Article) []*Article {
topArticlesSlice := make([]*Article, 0)
topArticlesSlice = append(topArticlesSlice, articles[:]...)
sort.SliceStable(topArticlesSlice, func(i, j int) bool {
var sortedByNumComments, areNumCommentsEquals, sortedByName bool
if topArticlesSlice[i].NumComments != nil && topArticlesSlice[j].NumComments != nil {
areNumCommentsEquals = *topArticlesSlice[i].NumComments == *topArticlesSlice[j].NumComments
sortedByNumComments = *topArticlesSlice[i].NumComments > *topArticlesSlice[j].NumComments
}
if areNumCommentsEquals {
if topArticlesSlice[i].Title != nil && topArticlesSlice[j].Title != nil {
sortedByName = *topArticlesSlice[i].Title == *topArticlesSlice[j].Title
return sortedByName
} else if topArticlesSlice[i].StoryTitle != nil && topArticlesSlice[j].StoryTitle != nil {
sortedByName = *topArticlesSlice[i].StoryTitle == *topArticlesSlice[j].StoryTitle
return sortedByName
}
return false
}
return sortedByNumComments
})
return topArticlesSlice
}
My structs (https://go.dev/play/p/27j-sFKaG2M)
type ArticleResponse struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
Total int `json:"total"`
TotalPages int `json:"total_pages"`
Articles []*Article `json:"data"`
}
type Article struct {
Title *string `json:"title"`
URL *string `json:"url"`
Author string `json:"author"`
NumComments *int `json:"num_comments"`
StoryID interface{} `json:"story_id"`
StoryTitle *string `json:"story_title"`
StoryURL *string `json:"story_url"`
ParentID *int `json:"parent_id"`
CreatedAt int `json:"created_at"`
}

Your compare function is far too complex. You need to refactor it into simpler more straightforward bits.
And, you haven't defined what your Article type looks like, so, for the purposes of example, I'm going to define it thus:
type Article struct {
NumComments *int
Title *string
}
Your basic ask is that you want to sort, first by the number of comments, and then (if the number of comments is nil) alphabetically by title, correct?
From your original code, it would appear that
NumComments is a pointer to int (*int), and
Title is a pointer to string (*string)
That means that each comparison has four cases that have to be dealt with:
X
Y
Action
non-nil
non-nil
Compare x and y (according to their underlying type)
non-nil
nil
How does nil compare with non-nil? (implementation detail)
nil
non-nil
How does nil compare with non-nil? (implementation detail)
nil
nil
two nils compare equal for the purposes of collation
For the purposes of this exercise, I'm going to declare that nil collates high with respect to non-nil (but nil collating low with respect to non-nil is equally valid. An implementation choice).
Comparing 2 *int values is easy:
func compareIntPtr(x *int, y *int) int {
var cc int
switch {
case x != nil && y != nil: cc = sign(*x - *y)
case x == nil && y == nil: cc = 0
case x == nil && y != nil: cc = +1
case x != nil && y == nil: cc = -1
}
return cc
}
func sign(n int) int {
var sign int
switch {
case n < 0: sign = -1
case n > 0: sign = +1
default: sign = 0
}
return sign
}
As is comparing two *string values:
import "strings"
.
.
.
func compareStringPtr(x *string, y *string) int {
var cc int
switch {
case x != nil && y != nil: cc = strings.Compare(*x, *y)
case x == nil && y == nil: cc = 0
case x == nil && y != nil: cc = +1
case x != nil && y == nil: cc = -1
}
return cc
}
Once you have those primitives, the comparer function for the sort is even simpler:
func sortArticles(articles []*Article) []*Article {
topArticlesSlice := make([]*Article, 0)
topArticlesSlice = append(topArticlesSlice, articles[:]...)
sort.SliceStable(topArticlesSlice, func(i, j int) bool {
x := *topArticlesSlice[i]
y := *topArticlesSlice[j]
// compare numbers of comments
cc := compareIntPtr(x.NumComments, y.NumComments)
// if equal, compare the titles
if cc == 0 {
cc = compareStringPtr(x.Title, y.Title)
}
// return `true` if `x` collates less than `y`, otherwise `false`
return cc < 0
})
return topArticlesSlice
}

package main
import (
"fmt"
"sort"
)
type Article struct {
Title string
NumComments int
}
func main() {
a1 := Article{"Dog", 3}
a2 := Article{"Tiger", 0}
a3 := Article{"Cat", 4}
a4 := Article{"Fish", 0}
a5 := Article{"Whale", 8}
articles := []Article{a1, a2, a3, a4, a5}
sort.Slice(articles, func(i, j int) bool {
if articles[i].NumComments == 0 && articles[j].NumComments == 0 {
return articles[i].Title < articles[j].Title
} else {
return articles[i].NumComments < articles[j].NumComments
}
})
fmt.Printf("articles: %v\n", articles)
}
Some of the type definitions are missing in your post. I have taken a simple strut example. I think this is what you may be looking for?

Assuming I'm understanding your requirement correctly you can use something like the following:
func sortArticles(articles []*Article) []*Article {
topArticlesSlice := append([]*Article{}, articles[:]...)
sort.SliceStable(topArticlesSlice, func(i, j int) bool {
if topArticlesSlice[i].NumComments != nil && topArticlesSlice[j].NumComments != nil &&
*topArticlesSlice[i].NumComments != *topArticlesSlice[j].NumComments {
return *topArticlesSlice[i].NumComments < *topArticlesSlice[j].NumComments
}
if topArticlesSlice[i].Title != nil && topArticlesSlice[j].Title != nil &&
*topArticlesSlice[i].Title != *topArticlesSlice[j].Title {
return *topArticlesSlice[i].Title < *topArticlesSlice[j].Title
} else if topArticlesSlice[i].StoryTitle != nil && topArticlesSlice[j].StoryTitle != nil {
return *topArticlesSlice[i].StoryTitle < *topArticlesSlice[j].StoryTitle
}
return false
})
return topArticlesSlice
}
Try this in the playground.

Related

panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x8 pc=0x48be5c] goroutine 1 [running]:

I am trying to implement addition of polynomials using linked list. The program adds the power 0 coefficients successfully but after the first traversal it panics. Here is the code I have written so far. after initializing temp1!= nil the loop iterates over else but doesn't enter if loop when powers are different and goes into panic state
package main
import (
"fmt"
)
type Node struct {
coefficient int
power int
next *Node
}
type list struct {
head *Node
count int
}
func main() {
list1 := &list{}
list1.addVariable(5, 2)
list1.addVariable(4, 1)
list1.addVariable(3, 0)
list1.print()
list2 := &list{}
list2.addVariable(4, 1)
list2.addVariable(2, 0)
list2.print()
list4 := addLists(list1, list2)
list4.print()
}
func (lst *list) print() {
temp := lst.head
for temp != nil {
fmt.Print(temp.coefficient, "x", "^", temp.power, "+")
temp = temp.next
}
fmt.Println("\n")
}
func (lst *list) addVariable(coefficient int, power int) {
lst.head = &Node{coefficient, power, lst.head}
lst.count++
}
func addLists(list1 *list, list2 *list) *list {
list3 := &list{}
temp1 := list1.head
temp2 := list2.head
fmt.Println("reached") // for debugging purposes
if list1.count > list2.count {
fmt.Println("\nreached 2") // for debugging purposes
for temp1 != nil {
fmt.Println("reached3") // for debugging purposes
if temp1.power != temp2.power {
fmt.Println("reached4") // for debugging purposes
list3.normalAdd(temp1, temp2)
temp1 = temp1.next
} else {
fmt.Println("reached5") // for debugging purposes
node4 := add(temp1, temp2)
list3.exlusiveAdd(node4)
temp1 = temp1.next
temp2 = temp2.next
}
}
}
return list3
}
func (lst *list) normalAdd(node6 *Node, node7 *Node) {
node6.next = lst.head
lst.head = node6
node7.next = lst.head
lst.head = node7
lst.count += 2
}
func (lst *list) exlusiveAdd(node5 *Node) {
node5.next = lst.head
lst.head = node5
lst.count++
}
func add(node1, node2 *Node) *Node {
node3 := &Node{}
node3.coefficient = node1.coefficient + node2.coefficient
node3.power = node1.power
return node3
}
Output when the program is run:
3x^0+4x^1+5x^2+
2x^0+4x^1+
reached
reached 2
reached3
reached5
reached3
reached5
reached3
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0xffffffff addr=0x0 pc=0xd48e6]
goroutine 1 [running]:
main.addLists(0x41c7a8, 0x41c7a0, 0x40c120, 0x1d)
/tmp/sandbox520212269/main.go:56 +0x186
main.main()
/tmp/sandbox520212269/main.go:28 +0x220
playground here
Update: Thanks for the solution but I have successfully written the code for above problem. here it is.
package main
import (
"fmt"
)
type Node struct {
coefficient int
power int
next *Node
}
type list struct {
head *Node
count int
}
func main() {
list1 := &list{}
list1.addVariable(5, 2)
list1.addVariable(4, 1)
list1.addVariable(3, 0)
list1.print()
list2 := &list{}
list2.addVariable(4, 1)
list2.addVariable(2, 0)
list2.print()
poly := addPoly(list1, list2)
poly.print()
}
func (lst *list) print() {
temp := lst.head
for temp != nil {
fmt.Print(temp.coefficient, "x", "^", temp.power, "+")
temp = temp.next
}
fmt.Println("\n")
}
func (lst *list) addVariable(coefficient int, power int) {
lst.head = &Node{coefficient, power, lst.head}
lst.count++
}
func addPoly(list_1 *list, list_2 *list) *list {
list_3 := &list{}
temp_1 := list_1.head
temp_2 := list_2.head
if list_1.count > list_2.count {
for temp_1 != nil && temp_2 != nil {
if temp_1.power == temp_2.power {
new_coefficient := temp_1.coefficient + temp_2.coefficient
new_power := temp_1.power
list_3.addVariable(new_coefficient, new_power)
temp_1 = temp_1.next
temp_2 = temp_2.next
} else if temp_1.power != temp_2.power {
list_3.addVariable(temp_1.coefficient, temp_1.power)
list_3.addVariable(temp_2.coefficient, temp_2.power)
temp_1 = temp_1.next
temp_2 = temp_2.next
}
}
}
for temp_1 != nil {
list_3.addVariable(temp_1.coefficient, temp_1.power)
temp_1 = temp_1.next
}
return list_3
}
The error occur because you tried to access .power property from nil object in code below. Both temp1 and temp2 are nil.
if temp1.power != temp2.power {
// ...
}
The .head property on list struct has pointer data type. The zero value of pointer data type is nil. In your code, both list1 and list2 has nil value on its .head property.
What you need to do: initialise the value of .head property explicitly during creation of both list1 and list2. Then the panic error will disappear and your code will work properly.
list1 := &list{head: new(Node)}
// ...
list2 := &list{head: new(Node)}
// ...
Playground: https://play.golang.org/p/vPK3pYKht3E
You have a lot of mistakes in your code like the first answer posted. Also, why is there a need to keep track of the number of terms? I would suggest you take a simpler approach.
type Node struct {
Coeff int
Power int
next *Node
}
// Add coeff^power to a polynomial
func (n *Node) AddTerm(coeff int, power int) {
for t := n; t != nil; t = t.next {
if t.Power == power {
t.Coeff += coeff
return
}
}
n.next = &Node{coeff, power, n.next}
}
// Add a polynomial m to the polynomial
func (n *Node) AddPolynomial(m *Node) {
for t := m; t != nil; t = t.next {
n.AddTerm(t.Coeff, t.Power)
}
}
// The string representation of the polynomial
func (n *Node) String() string {
buff := strings.Builder{}
for t := n; t != nil; t = t.next {
fmt.Fprintf(&buff, " + %d^%d", t.Coeff, t.Power)
}
return buff.String()[3:]
}
// Add two polynomials together creating a new one
func Add(a *Node, b *Node) *Node {
c := &Node{}
c.AddPolynomial(a)
c.AddPolynomial(b)
return c
}
func main() {
p1 := &Node{}
p1.AddTerm(5, 2)
p1.AddTerm(4, 1)
p1.AddTerm(3, 0)
fmt.Println(p1)
p2 := &Node{}
p2.AddTerm(4, 1)
p2.AddTerm(2, 0)
fmt.Println(p2)
p3 := Add(p1, p2)
fmt.Println(p3)
}
Playground
Just check your libraries. There are some redundant ones.

How to convert an int64 to int in Go?

In Go, what is the best strategy for converting int64 to int? I am having difficulty comparing the two
package main
import (
"math"
"strings"
"strconv"
)
type largestPrimeFactor struct {
N int
Result int
}
func main() {
base := largestPrimeFactor{N:13195}
max := math.Sqrt(float64(base.N))
maxStr := strconv.FormatFloat(max, 'E', 'G', 64)
maxShift := strings.Split(maxStr, ".")[0]
maxInt, err := strconv.ParseInt(maxShift, 10, 64)
if (err != nil) {
panic(err)
}
on this next line
for a := 2; a < maxInt; a++ {
if isPrime(a) {
if base.N % a == 0 {
base.Result = a
}
}
}
println(base)
}
func isPrime(n int) bool {
flag := false
max := math.Sqrt(float64(n))
maxStr := strconv.FormatFloat(max, 'E', 'G', 64)
maxShift := strings.Split(maxStr, ".")[0]
maxInt, err := strconv.ParseInt(maxShift, 10, 64)
if (err != nil) {
panic(err)
}
for a := 2; a < maxInt; a++ {
if (n % a == 0) {
flag := true
}
}
return flag
}
You convert them with a type "conversion"
var a int
var b int64
int64(a) < b
When comparing values, you always want to convert the smaller type to the larger. Converting the other way will possibly truncate the value:
var x int32 = 0
var y int64 = math.MaxInt32 + 1 // y == 2147483648
if x < int32(y) {
// this evaluates to false, because int32(y) is -2147483648
Or in your case to convert the maxInt int64 value to an int, you could use
for a := 2; a < int(maxInt); a++ {
which would fail to execute correctly if maxInt overflows the max value of the int type on your system.
I came here because of the title, "How to convert an int64 to int in Go?". The answer is,
int(int64Var)
It is correct to use the strconv package
strconv.FormatInt(int64Var, 10)

check for equality on slices without order

I am trying to find a solution to check for equality in 2 slices. Unfortanely, the answers I have found require values in the slice to be in the same order. For example, http://play.golang.org/p/yV0q1_u3xR evaluates equality to false.
I want a solution that lets []string{"a","b","c"} == []string{"b","a","c"} evaluate to true.
MORE EXAMPLES
[]string{"a","a","c"} == []string{"c","a","c"} >>> false
[]string{"z","z","x"} == []string{"x","z","z"} >>> true
Here is an alternate solution, though perhaps a bit verbose:
func sameStringSlice(x, y []string) bool {
if len(x) != len(y) {
return false
}
// create a map of string -> int
diff := make(map[string]int, len(x))
for _, _x := range x {
// 0 value for int is 0, so just increment a counter for the string
diff[_x]++
}
for _, _y := range y {
// If the string _y is not in diff bail out early
if _, ok := diff[_y]; !ok {
return false
}
diff[_y] -= 1
if diff[_y] == 0 {
delete(diff, _y)
}
}
return len(diff) == 0
}
Try it on the Go Playground
You can use cmp.Diff together with cmpopts.SortSlices:
less := func(a, b string) bool { return a < b }
equalIgnoreOrder := cmp.Diff(x, y, cmpopts.SortSlices(less)) == ""
Here is a full example that runs on the Go Playground:
package main
import (
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func main() {
x := []string{"a", "b", "c"}
y := []string{"a", "c", "b"}
less := func(a, b string) bool { return a < b }
equalIgnoreOrder := cmp.Diff(x, y, cmpopts.SortSlices(less)) == ""
fmt.Println(equalIgnoreOrder) // prints "true"
}
The other answers have better time complexity O(N) vs (O(N log(N)), that are in my answer, also my solution will take up more memory if elements in the slices are repeated frequently, but I wanted to add it because I think this is the most straight forward way to do it:
package main
import (
"fmt"
"sort"
"reflect"
)
func array_sorted_equal(a, b []string) bool {
if len(a) != len(b) {return false }
a_copy := make([]string, len(a))
b_copy := make([]string, len(b))
copy(a_copy, a)
copy(b_copy, b)
sort.Strings(a_copy)
sort.Strings(b_copy)
return reflect.DeepEqual(a_copy, b_copy)
}
func main() {
a := []string {"a", "a", "c"}
b := []string {"c", "a", "c"}
c := []string {"z","z","x"}
d := []string {"x","z","z"}
fmt.Println( array_sorted_equal(a, b))
fmt.Println( array_sorted_equal(c, d))
}
Result:
false
true
I would think the easiest way would be to map the elements in each array/slice to their number of occurrences, then compare the maps:
func main() {
x := []string{"a","b","c"}
y := []string{"c","b","a"}
xMap := make(map[string]int)
yMap := make(map[string]int)
for _, xElem := range x {
xMap[xElem]++
}
for _, yElem := range y {
yMap[yElem]++
}
for xMapKey, xMapVal := range xMap {
if yMap[xMapKey] != xMapVal {
return false
}
}
return true
}
You'll need to add some additional due dilligence, like short circuiting if your arrays/slices contain elements of different types or are of different length.
Generalizing the code of testify ElementsMatch, solution to compare any kind of objects (in the example []map[string]string):
https://play.golang.org/p/xUS2ngrUWUl
Like adrianlzt wrote in his answer, an implementation of assert.ElementsMatch from testify can be used to achieve that. But how about reusing actual testify module instead of copying that code when all you need is a bool result of the comparison? The implementation in testify is intended for tests code and usually takes testing.T argument.
It turns out that ElementsMatch can be quite easily used outside of testing code. All it takes is a dummy implementation of an interface with ErrorF method:
type dummyt struct{}
func (t dummyt) Errorf(string, ...interface{}) {}
func elementsMatch(listA, listB interface{}) bool {
return assert.ElementsMatch(dummyt{}, listA, listB)
}
Or test it on The Go Playground, which I've adapted from the adrianlzt's example.
Since I haven't got enough reputation to comment, I have to post yet another answer with a bit better code readability:
func AssertSameStringSlice(x, y []string) bool {
if len(x) != len(y) {
return false
}
itemAppearsTimes := make(map[string]int, len(x))
for _, i := range x {
itemAppearsTimes[i]++
}
for _, i := range y {
if _, ok := itemAppearsTimes[i]; !ok {
return false
}
itemAppearsTimes[i]--
if itemAppearsTimes[i] == 0 {
delete(itemAppearsTimes, i)
}
}
if len(itemAppearsTimes) == 0 {
return true
}
return false
}
The logic is the same as in this answer
I know its been answered but still I would like to add my answer. By following code here stretchr/testify we can have something like
func Elementsmatch(listA, listB []string) (string, bool) {
aLen := len(listA)
bLen := len(listB)
if aLen != bLen {
return fmt.Sprintf("Len of the lists don't match , len listA %v, len listB %v", aLen, bLen), false
}
visited := make([]bool, bLen)
for i := 0; i < aLen; i++ {
found := false
element := listA[i]
for j := 0; j < bLen; j++ {
if visited[j] {
continue
}
if element == listB[j] {
visited[j] = true
found = true
break
}
}
if !found {
return fmt.Sprintf("element %s appears more times in %s than in %s", element, listA, listB), false
}
}
return "", true
}
Now lets talk about performance of this solution compared to map based ones. Well it really depends on the size of the lists which you are comparing, If size of list is large (I would say greater than 20) then map approach is better else this would be sufficent.
Well on Go PlayGround it shows 0s always, but run this on local system and you can see the difference in time taken as size of list increases
So the solution I propose is, adding map based comparision from above solution
func Elementsmatch(listA, listB []string) (string, bool) {
aLen := len(listA)
bLen := len(listB)
if aLen != bLen {
return fmt.Sprintf("Len of the lists don't match , len listA %v, len listB %v", aLen, bLen), false
}
if aLen > 20 {
return elementsMatchByMap(listA, listB)
}else{
return elementsMatchByLoop(listA, listB)
}
}
func elementsMatchByLoop(listA, listB []string) (string, bool) {
aLen := len(listA)
bLen := len(listB)
visited := make([]bool, bLen)
for i := 0; i < aLen; i++ {
found := false
element := listA[i]
for j := 0; j < bLen; j++ {
if visited[j] {
continue
}
if element == listB[j] {
visited[j] = true
found = true
break
}
}
if !found {
return fmt.Sprintf("element %s appears more times in %s than in %s", element, listA, listB), false
}
}
return "", true
}
func elementsMatchByMap(x, y []string) (string, bool) {
// create a map of string -> int
diff := make(map[string]int, len(x))
for _, _x := range x {
// 0 value for int is 0, so just increment a counter for the string
diff[_x]++
}
for _, _y := range y {
// If the string _y is not in diff bail out early
if _, ok := diff[_y]; !ok {
return fmt.Sprintf(" %v is not present in list b", _y), false
}
diff[_y] -= 1
if diff[_y] == 0 {
delete(diff, _y)
}
}
if len(diff) == 0 {
return "", true
}
return "", false
}

Challenge of finding 3 pairs in array

The length L at the time of joining, when the length of the bar of the N (1 ≦ N ≦ 5000) is supplied from standard input, is the L by connecting three lengths among the N number of bars please write a program to find the total number of combinations of. However, and the length of the individual bars, length was pieced together (L) is a positive integer, is sufficient handle range in 32bit integer. In addition, it has all the length of the bar different things.
for example)
input:
15
5
8
4
10
3
2
output:
2 //{{2, 3, 10}, {3, 4, 8}}
example 2)
input :
35
10
13
12
17
10
4
18
3
11
5
7
output:
6 //{{4, 13, 18}, {5, 12, 18}, {5, 13, 17}, {7, 10, 18}, {7, 11, 17}, {10, 12, 13}}
and my answer is here
package main
import (
"fmt"
"sort"
)
func main() {
input_count := 0
var target int
var count int
var v int
var array []int
for read_count, _ := fmt.Scan(&v); read_count != 0; read_count, _ = fmt.Scan(&v) {
if 0 == input_count {
target = v
} else if 1 == input_count {
count = v
array = make([]int, count)
} else {
array[input_count-2] = v
}
input_count++
}
sort.Ints(array)
fmt.Println(Calculate(target, count, array))
}
func Except(pair []int, count int, array []int) []int {
except := make([]int, count-pair[2])
except_index := 0
on := false
for _, v := range array {
if on {
except[except_index] = v
except_index++
}
if v == pair[1] {
on = true
}
}
return except
}
func ListUp(target int, count int, array []int) [][]int {
max := array[count-1]
list := make([][]int, Fact(count-1))
list_index := 0
for i, h := range array {
if count > i+1 && target > h+array[i+1] {
for j, v := range array[i+1:] {
if count > i+j+1 && target <= max+h+v && target > h+v {
list[list_index] = []int{h, v, i + j + 1}
list_index++
}
}
}
}
return list
}
//func Calculate(target int, count int, array []int) [][]int {
func Calculate(target int, count int, array []int) int {
// var answers [][]int
answer_count := 0
for _, pair := range ListUp(target, count, array) {
if 3 == len(pair) {
pair_sum := pair[0] + pair[1]
if target-pair_sum >= array[0] {
for _, v := range Except(pair, count, array) {
if target == pair[0]+pair[1]+v {
// answers = append(answers, []int{pair[0], pair[1], v})
answer_count++
}
}
}
}
}
return answer_count
}
func Fact(n int) int {
if n == 0 {
return 0
}
return n + Fact(n-1)
}
Does anyone who can refactor the code?
and you should refactor it
if input
https://github.com/freddiefujiwara/horiemon-challenge-codeiq/blob/master/sample4.txt
then output
1571200
in 10 seconds
current status is here
time ./horiemon-challenge-codeiq < sample4.txt
1571200
real 6m56.584s
user 6m56.132s
sys 0m1.578s
very very slow.
Your time of almost seven minutes is very, very slow. Ten seconds is slow. One second is more reasonable, a tenth of a second is good. For example, using an O(N*N) algorithm,
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
)
func triples(l int, b []int) int {
t := 0
sort.Ints(b)
// for i < j < k, b[i] <= b[j] <= b[k]
for i := 0; i < len(b)-2; i++ {
x := b[i]
if x > l {
break
}
lx := l - x
j, k := i+1, len(b)-1
y := b[j]
z := b[k]
for j < k {
yz := y + z
switch {
case lx > yz:
j++
y = b[j]
case lx < yz:
k--
z = b[k]
default:
// l == b[i]+b[j]+b[k]
t++
j++
k--
y = b[j]
z = b[k]
}
}
}
return t
}
func readInput() (l int, b []int, err error) {
r := bufio.NewReader(os.Stdin)
for {
line, err := r.ReadString('\n')
line = strings.TrimSpace(line)
if err == nil && len(line) == 0 {
err = io.EOF
}
if err != nil {
if err == io.EOF {
break
}
return 0, nil, err
}
i, err := strconv.Atoi(string(line))
if err == nil && i < 0 {
err = errors.New("Nonpositive number: " + line)
}
if err != nil {
return 0, nil, err
}
b = append(b, i)
}
if len(b) > 0 {
l = b[0]
b = b[1:]
if len(b) > 1 {
n := b[0]
b = b[1:]
if n != len(b) {
err := errors.New("Invalid number of bars: " + strconv.Itoa(len(b)))
return 0, nil, err
}
}
}
return l, b, nil
}
func main() {
l, b, err := readInput()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
t := triples(l, b)
fmt.Println(t)
}
Output:
1571200
real 0m0.164s
user 0m0.161s
sys 0m0.004s
For comparison, your program,
Output:
1571200
real 9m24.384s
user 16m14.592s
sys 0m19.129s
ive tuned
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
)
type triple struct {
x, y, z int
}
func triples(l int, n []int, list bool) (nt int, t []triple) {
num_of_list := len(n)
for i := 0; i < num_of_list-2; i++ {
x := n[i]
if x > l {
break
}
for j := i + 1; j < num_of_list-1; j++ {
y := x + n[j]
if y > l {
break
}
pos := sort.SearchInts(n[j:], l-y)
if j < pos+j && pos+j < num_of_list && n[pos+j] == l-y {
nt++
}
}
}
return nt, t
}
func readInput() (l int, n []int, err error) {
r := bufio.NewReader(os.Stdin)
for {
line, err := r.ReadString('\n')
line = strings.TrimSpace(line)
if err == nil && len(line) == 0 {
err = io.EOF
}
if err != nil {
if err == io.EOF {
break
}
return 0, nil, err
}
i, err := strconv.Atoi(string(line))
if err == nil && i < 0 {
err = errors.New("Nonpositive number: " + line)
}
if err != nil {
return 0, nil, err
}
n = append(n, i)
}
if len(n) > 0 {
l = n[0]
n = n[1:]
}
sort.Ints(n)
for i := 1; i < len(n); i++ {
if n[i] == n[i-1] {
copy(n[i:], n[i+1:])
n = n[:len(n)-1]
}
}
return l, n, nil
}
func main() {
l, n, err := readInput()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
list := false
nt, t := triples(l, n, list)
fmt.Println(nt)
if list {
fmt.Println(t)
}
}

Golang: find first character in a String that doesn't repeat

I'm trying to write a function that returns the finds first character in a String that doesn't repeat, so far I have this:
package main
import (
"fmt"
"strings"
)
func check(s string) string {
ss := strings.Split(s, "")
smap := map[string]int{}
for i := 0; i < len(ss); i++ {
(smap[ss[i]])++
}
for k, v := range smap {
if v == 1 {
return k
}
}
return ""
}
func main() {
fmt.Println(check("nebuchadnezzer"))
}
Unfortunately in Go when you iterate a map there's no guarantee of the order so every time I run the code I get a different value, any pointers?
Using a map and 2 loops :
play
func check(s string) string {
m := make(map[rune]uint, len(s)) //preallocate the map size
for _, r := range s {
m[r]++
}
for _, r := range s {
if m[r] == 1 {
return string(r)
}
}
return ""
}
The benfit of this is using just 2 loops vs multiple loops if you're using strings.ContainsRune, strings.IndexRune (each function will have inner loops in them).
Efficient (in time and memory) algorithms for grabbing all or the first unique byte http://play.golang.org/p/ZGFepvEXFT:
func FirstUniqueByte(s string) (b byte, ok bool) {
occur := [256]byte{}
order := make([]byte, 0, 256)
for i := 0; i < len(s); i++ {
b = s[i]
switch occur[b] {
case 0:
occur[b] = 1
order = append(order, b)
case 1:
occur[b] = 2
}
}
for _, b = range order {
if occur[b] == 1 {
return b, true
}
}
return 0, false
}
As a bonus, the above function should never generate any garbage. Note that I changed your function signature to be a more idiomatic way to express what you're describing. If you need a func(string) string signature anyway, then the point is moot.
That can certainly be optimized, but one solution (which isn't using map) would be:
(playground example)
func check(s string) string {
unique := ""
for pos, c := range s {
if strings.ContainsRune(unique, c) {
unique = strings.Replace(unique, string(c), "", -1)
} else if strings.IndexRune(s, c) == pos {
unique = unique + string(c)
}
}
fmt.Println("All unique characters found: ", unique)
if len(unique) > 0 {
_, size := utf8.DecodeRuneInString(unique)
return unique[:size]
}
return ""
}
This is after the question "Find the first un-repeated character in a string"
krait suggested below that the function should:
return a string containing the first full rune, not just the first byte of the utf8 encoding of the first rune.

Resources