Skip to content

Instantly share code, notes, and snippets.

@dyoo
Last active December 22, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyoo/6407562 to your computer and use it in GitHub Desktop.
Save dyoo/6407562 to your computer and use it in GitHub Desktop.
More play with Go. A question from reading Barry Schwartz's "The Paradox of Choice": how many English words start with 't', and how many have 't' as the third letter?
package main
// A question from reading Barry Schwartz's "The Paradox of Choice".
//
// How many English words start with "t"?
//
// How many English words have "t" as the third letter?
import (
"bufio"
"fmt"
"os"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
count1, count2 := 0, 0
for scanner.Scan() {
s := scanner.Text()
if len(s) > 0 && (s[0] == 't' || s[0] == 'T') {
count1++
}
if len(s) > 2 && (s[2] == 't' || s[2] == 'T') {
count2++
}
}
fmt.Println(count1)
fmt.Println(count2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment