From 0433c6344ccc286b5e66b9ae92617da849a251e1 Mon Sep 17 00:00:00 2001 From: Meutel Date: Fri, 7 Jul 2017 16:55:55 +0200 Subject: [PATCH] Improve longest word (punctuation) --- longest-word/main.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/longest-word/main.go b/longest-word/main.go index 0d88b67..b932b42 100644 --- a/longest-word/main.go +++ b/longest-word/main.go @@ -7,6 +7,8 @@ import "os" import "bufio" import "strings" +var PUNCTUATION = []string{".", ",", "\"", "—", "-", "/"} + func longest(r io.Reader) string { lw := "" s := bufio.NewScanner(r) @@ -14,14 +16,12 @@ func longest(r io.Reader) string { for s.Scan() { word := s.Text() word = strings.ToLower(word) - word = strings.Replace(word, ".", "", -1) - word = strings.Replace(word, ",", "", -1) - word = strings.Replace(word, "\"", "", -1) - for _, word := range strings.Split(word, "—") { + for _, c := range PUNCTUATION { + word = strings.Replace(word, c, " ", -1) + } + for _, word := range strings.Fields(word) { if len(word) > len(lw) { - if !strings.HasPrefix(word, "http://") { - lw = word - } + lw = word } } }