Skip to content

Instantly share code, notes, and snippets.

@vfreex
Last active January 12, 2018 08:47
Show Gist options
  • Save vfreex/702c2089fa11c265f9e3ee4467cee62f to your computer and use it in GitHub Desktop.
Save vfreex/702c2089fa11c265f9e3ee4467cee62f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sort"
)
type Rating struct {
score int
count int
}
type Hotel struct {
id int
rating float64
}
type HotelSlice []Hotel
func (h HotelSlice) Less(i, j int) bool {
if h[i].rating > h[j].rating {
return true
}
if h[i].rating == h[j].rating {
return h[i].id < h[j].id
}
return false
}
func (h HotelSlice) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h HotelSlice) Len() int {
return len(h)
}
func main() {
var lines int
fmt.Scanf("%d\n", &lines)
hotelRatings := make(map[int]*Rating)
for i := 0; i < lines; i++ {
var id, score int
fmt.Scanf("%d %d\n", &id, &score)
if hotelRatings[id] == nil {
hotelRatings[id] = &Rating{score: score, count: 1}
} else {
hotelRatings[id].score += score
hotelRatings[id].count++
}
}
hotels := make([]Hotel, 0)
for id, rating := range hotelRatings {
hotels = append(hotels, Hotel{id, float64(rating.score / rating.count)})
}
sort.Sort(HotelSlice(hotels))
for _, id := range hotels {
fmt.Println(id)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment