Created
February 2, 2013 06:44
-
-
Save dallarosa/4696325 to your computer and use it in GitHub Desktop.
My solutions for the 2013 hacker cup qualification round in Go.
My original solution for balanced smiles is broken but i'm gonna fix it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"io/ioutil" | |
"strings" | |
"strconv" | |
"./stack" | |
) | |
func main() { | |
filename := os.Args[1] | |
bContents,_ := ioutil.ReadFile(filename) | |
sContents := string(bContents) | |
aContents := strings.Split(sContents,"\n") | |
caseNum,_ := strconv.Atoi(aContents[0]) | |
for cn,s := range aContents[1:caseNum + 1] { | |
result := "YES" | |
stack := new(stack.Stack) | |
wasCollon := false | |
for _,char := range s { | |
if char >= 'a' && char <= 'z' || char == ' ' { | |
continue | |
} | |
if char == ':' { | |
wasCollon = true | |
continue | |
} | |
if char == '(' { | |
if wasCollon { | |
wasCollon = false | |
continue | |
} else { | |
stack.Push(char) | |
} | |
} | |
if char == ')' { | |
if stack.Top() == '(' { | |
stack.Pop() | |
} else if wasCollon { | |
wasCollon = false | |
continue | |
} else { | |
result = "NO" | |
} | |
} | |
} | |
if (stack.Top() != nil) { | |
result = "NO" | |
} | |
fmt.Printf("Case #%d: %s\n", cn + 1, result) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"io/ioutil" | |
"strings" | |
"strconv" | |
"regexp" | |
"sort" | |
) | |
var puncRegex = regexp.MustCompile("[^A-Za-z0-9\n]+") | |
type ValSorter struct { | |
Keys []rune | |
Vals []int | |
} | |
func NewValSorter(m map[rune]int) *ValSorter { | |
vs := &ValSorter{ | |
Keys: make([]rune, 0, len(m)), | |
Vals: make([]int, 0, len(m)), | |
} | |
for k, v := range m { | |
vs.Keys = append(vs.Keys, k) | |
vs.Vals = append(vs.Vals, v) | |
} | |
return vs | |
} | |
func (vs *ValSorter) Sort() { | |
sort.Sort(vs) | |
} | |
func (vs *ValSorter) Len() int { return len(vs.Vals) } | |
func (vs *ValSorter) Less(i, j int) bool { return vs.Vals[i] > vs.Vals[j] } | |
func (vs *ValSorter) Swap(i, j int) { | |
vs.Vals[i], vs.Vals[j] = vs.Vals[j], vs.Vals[i] | |
vs.Keys[i], vs.Keys[j] = vs.Keys[j], vs.Keys[i] | |
} | |
func main () { | |
filename := os.Args[1] | |
bContents,_ := ioutil.ReadFile(filename) | |
sContents := strings.ToLower(string(bContents)) | |
sContents = puncRegex.ReplaceAllString(sContents,"") | |
aContents := strings.Split(sContents,"\n") | |
caseNum,_ := strconv.Atoi(aContents[0]) | |
for cn,s := range aContents[1:caseNum + 1] { | |
tokens := make(map[rune]int) | |
for _,char := range s { | |
tokens[char] = tokens[char] + 1 | |
} | |
vs := NewValSorter(tokens) | |
vs.Sort() | |
weight := 26 | |
sum := 0 | |
for i := 0; i < vs.Len(); i++ { | |
sum = sum + weight * vs.Vals[i] | |
weight-- | |
} | |
fmt.Printf("Case #%d: %d\n", cn + 1, sum) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment