Решение на Markdown от Мария Терзиева

Обратно към всички решения

Към профила на Мария Терзиева

Резултати

  • 0 точки от тестове
  • 0 бонус точки
  • 0 точки общо
  • 0 успешни тест(а)
  • 7 неуспешни тест(а)

Код

package main
import (
"fmt"
"regexp"
"strconv"
"strings"
)
type MarkdownParser struct {
Text string
}
func NewMarkdownParser(text string) *MarkdownParser {
mp := new(MarkdownParser)
mp.Text = text
return mp
}
func removeLeadingAndTrailingSymbols(symbols []string, src string) string {
reLeading := strings.Join([]string{symbols[0], `|(\n|\r)`, symbols[0]}, "")
reTrailing := strings.Join([]string{`(\n|\r)[`, symbols[1], `]+`}, "")
leading := regexp.MustCompile(reLeading).Split(src, 2)
trailing := regexp.MustCompile(reTrailing).Split(leading[len(leading)-1], 2)
return strings.TrimSpace(trailing[0])
}
func headersList(symbols []string, re string, src string) []string {
unformated := regexp.MustCompile(re).FindAllString(src, -1)
var formated = make([]string, len(unformated))
for index, value := range unformated {
formated[index] = removeLeadingAndTrailingSymbols(symbols, value)
}
return formated
}
func (mp *MarkdownParser) partOfTextWithSubheaders(prefix string, header string) string {
subprefixLen := strconv.Itoa(len(prefix) + 1)
re := strings.Join([]string{prefix, `[ ]*`, header, `[ ]*#?([^#]|#{`, subprefixLen, `,})+(`, prefix, `|\z)`}, "")
if prefix == "#" {
re = strings.Join([]string{prefix, `[ ]*`, header, `[ ]*#?([^#]|#{`, subprefixLen, `,})+(`, prefix, `|(\n|\r)[=]+|\z)`}, "")
secondRE := strings.Join([]string{header, `[ ]*(\r|\n|\r\n)[=]+(.|\n)+((\n|\r)[=]+|(\n|\r)#|\z)`}, "")
re = strings.Join([]string{re, secondRE}, "|")
}
if prefix == "##" {
re = strings.Join([]string{prefix, `[ ]*`, header, `[ ]*#?([^#]|#{`, subprefixLen, `,})+(`, prefix, `|(\n|\r)+[-]+|\z)`}, "")
secondRE := strings.Join([]string{header, `[ ]*(\r|\n|\r\n)[-]+(.|\n)+((\n|\r)[-]+|(\n|\r)## |\z)`}, "")
re = strings.Join([]string{re, secondRE}, "|")
}
return regexp.MustCompile(re).FindString(mp.Text)
}
func (mp *MarkdownParser) Headers() []string {
return headersList([]string{`#`, `=`}, `(\n|\r|\A)#[^#\n\r]+|(.)+(\n|\r)[=]+`, mp.Text)
}
func (mp *MarkdownParser) SubHeadersOf(header string) []string {
partOfText := mp.partOfTextWithSubheaders("#", header)
return headersList([]string{`##`, `-`}, `(\n|\r|\A)##[^#\r\n]+|(.)+(\n|\r)[-]+`, partOfText)
}
//for H2-H6
func (mp *MarkdownParser) subHeaders(prefix string, header string) []string {
partOfText := mp.partOfTextWithSubheaders(prefix, header)
return headersList([]string{prefix + `#`, ` `}, `(\n|\r|\A)`+prefix+`#[^#\r\n]+`, partOfText)
}
func (mp *MarkdownParser) SubHeaders3(header string) []string {
result := mp.subHeaders("##", header)
return result
}
func (mp *MarkdownParser) SubHeaders4(header string) []string {
return mp.subHeaders("###", header)
}
func (mp *MarkdownParser) SubHeaders5(header string) []string {
return mp.subHeaders("####", header)
}
func (mp *MarkdownParser) SubHeaders6(header string) []string {
return mp.subHeaders("#####", header)
}
func (mp *MarkdownParser) PhoneNumbers() []string {
re := regexp.MustCompile(`(\+|\()?\d+([ ()-]*\d+)+`)
return re.FindAllString(mp.Text, -1)
}
func (mp *MarkdownParser) Links() []string {
re := regexp.MustCompile(`\[[^\]]+\]\((?P<link>[^)]+)\)`)
matches := re.FindAllString(mp.Text, -1)
for index, value := range matches {
matches[index] = re.ReplaceAllString(value, `${link}`)
}
return matches
}
func (mp *MarkdownParser) Emails() []string {
re := regexp.MustCompile(`[a-zA-Z\d]+([-+._][a-zA-Z\d]+){0,200}@[a-zA-Z\d]+([-.][a-zA-Z\d]+)*\.[a-zA-Z]{2,}`)
return re.FindAllString(mp.Text, -1)
}
func (mp *MarkdownParser) GenerateTableOfContents() string {
H1 := mp.Headers()
var str string
for i := 0; i < len(H1); i++ {
str = fmt.Sprintf("%s%d.", str, i+1)
str = fmt.Sprintf("%s %s\n", str, H1[i])
H2 := mp.SubHeadersOf(H1[i])
for j := 0; j < len(H2); j++ {
str = fmt.Sprintf("%s%d.%d", str, i+1, j+1)
str = fmt.Sprintf("%s %s\n", str, H2[j])
H3 := mp.SubHeaders3(H2[j])
for k := 0; k < len(H3); k++ {
str = fmt.Sprintf("%s%d.%d.%d", str, i+1, j+1, k+1)
str = fmt.Sprintf("%s %s\n", str, H3[k])
H4 := mp.SubHeaders4(H3[k])
for l := 0; l < len(H4); l++ {
str = fmt.Sprintf("%s%d.%d.%d.%d", str, i+1, j+1, k+1, l+1)
str = fmt.Sprintf("%s %s\n", str, H4[l])
H5 := mp.SubHeaders5(H4[l])
for m := 0; m < len(H5); m++ {
str = fmt.Sprintf("%s%d.%d.%d.%d.%d", str, i+1, j+1, k+1, l+1, m+1)
str = fmt.Sprintf("%s %s\n", str, H5[m])
H6 := mp.SubHeaders6(H5[m])
for n := 0; n < len(H6); n++ {
str = fmt.Sprintf("%s%d.%d.%d.%d.%d.%d", str, i+1, j+1, k+1, l+1, m+1, n+1)
str = fmt.Sprintf("%s %s\n", str, H6[n])
}
}
}
}
}
}
return str
}
func (mp *MarkdownParser) Names() []string {
re := regexp.MustCompile(`\b[A-ZА-Я][a-zа-я]+(( | - |-)[A-ZА-Я][a-zа-я]+)+`)
return re.FindAllString(mp.Text, -1)
}

Лог от изпълнението

--- FAIL: TestHeaders (0.00 seconds)
	solution_test.go:27: Not equal:
		  []string{"One", "Two", ""}
		  []string{"One", "Two", "# Three # Four"}
FAIL
exit status 1
FAIL	_/tmp/d20140106-32701-wjftyt	0.012s
--- FAIL: TestSubHeadersOf (0.00 seconds)
	solution_test.go:56: Not equal:
		  []string{"Three-four"}
		  []string(nil)
FAIL
exit status 1
FAIL	_/tmp/d20140106-32701-wjftyt	0.013s
--- FAIL: TestNames (0.00 seconds)
	solution_test.go:72: Not equal:
		  []string{"Beginning Of Line", "He Used", "Mozilla Firefox"}
		  []string{"Of Line", "Иван Петров", "Mozilla Firefox"}
FAIL
exit status 1
FAIL	_/tmp/d20140106-32701-wjftyt	0.012s
--- FAIL: TestPhoneNumbers (0.00 seconds)
	solution_test.go:86: Not equal:
		  []string{"0889123456", "0 (889) 123", "456", "+45-(31"}
		  []string{"0889123456", "0 (889) 123", "456", "+45-(31)"}
FAIL
exit status 1
FAIL	_/tmp/d20140106-32701-wjftyt	0.012s
--- FAIL: TestLinks (0.00 seconds)
	solution_test.go:98: Not equal:
		  []string(nil)
		  []string{"http://somelink.com:230", "https://www.google.bg/search?q=4531&ie=utf-8&oe=utf-8&rls=org.mozilla:en-US:official&client=%20firefox-a&gws_rd=asd&ei=some#somefragment"}
FAIL
exit status 1
FAIL	_/tmp/d20140106-32701-wjftyt	0.012s
--- FAIL: TestEmails (0.00 seconds)
	solution_test.go:107: Not equal:
		  []string{"validMail12@foobar.com", "invalidmail@google.com", "toolongmailhereaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@gmail.com", "12mail@gmail.com"}
		  []string{"validMail12@foobar.com", "12mail@gmail.com"}
FAIL
exit status 1
FAIL	_/tmp/d20140106-32701-wjftyt	0.014s
--- FAIL: TestTableOfContents (0.01 seconds)
	solution_test.go:165: Not equal:
		  []string{"1. One", "1.1 Three", "1.2 Five", "2. Two", "2.1 Three", "2.2 Five", "3. Four", "3.1 Five"}
		  []string{"1. One", "2. Two", "2.1 Three", "3. Four", "3.1 Five"}
FAIL
exit status 1
FAIL	_/tmp/d20140106-32701-wjftyt	0.017s

История (3 версии и 3 коментара)

Мария обнови решението на 03.12.2013 01:36 (преди над 4 години)

+package main
+
+import (
+ "regexp"
+ "strconv"
+ "strings"
+)
+
+type MarkdownParser struct {
+ Text string
+}
+
+func NewMarkdownParser(text string) *MarkdownParser {
+ mp := new(MarkdownParser)
+ mp.Text = text
+ return mp
+}
+
+func removeLeadingAndTrailingSymbols(symbols []string, src string) string {
+ reLeading := strings.Join([]string{symbols[0], `|\n`, symbols[0]}, "")
+ reTrailing := strings.Join([]string{`\n[`, symbols[1], `]+`}, "")
+ leading := regexp.MustCompile(reLeading).Split(src, 2)
+ trailing := regexp.MustCompile(reTrailing).Split(leading[len(leading)-1], 2)
+ return strings.TrimSpace(trailing[0])
+}
+
+func headersList(symbols []string, re string, src string) []string {
+ unformated := regexp.MustCompile(re).FindAllString(src, -1)
+ var formated = make([]string, len(unformated))
+ for index, value := range unformated {
+ formated[index] = removeLeadingAndTrailingSymbols(symbols, value)
+ }
+ return formated
+}
+
+func (mp *MarkdownParser) partOfTextWithSubheaders(prefix string, header string) string {
+ subprefixLen := strconv.Itoa(len(prefix) + 1)
+ re := strings.Join([]string{prefix, `[ ]*`, header, `[ ]*#?([^#]|#{`, subprefixLen, `,})+(`, prefix, `|\z)`}, "")
+ if prefix == "#" {
+ secondRE := strings.Join([]string{header, `[ ]*\n[=]+[^=]+([=]+|\z)`}, "")
+ re = strings.Join([]string{re, secondRE}, "|")
+ }
+ return regexp.MustCompile(re).FindString(mp.Text)
+}
+
+func (mp *MarkdownParser) Headers() []string {
+ return headersList([]string{`#`, `=`}, `(\n|\A)#[^#\n]+|(.)+\n[=]+`, mp.Text)
+}
+
+func (mp *MarkdownParser) SubHeadersOf(header string) []string {
+ partOfText := mp.partOfTextWithSubheaders("#", header)
+ return headersList([]string{`##`, `-`}, `(\n|\A)##[^#\n]+|(.)+\n[-]+`, partOfText)
+}
+
+func (mp *MarkdownParser) PhoneNumbers() []string {
+ re := regexp.MustCompile(`(\+|\()?\d+([ ()-]*\d+)+`)
+ return re.FindAllString(mp.Text, -1)
+}
+
+func (mp *MarkdownParser) Links() []string {
+ re := regexp.MustCompile(`\[[^\]]+\]\((?P<link>[^)]+)\)`)
+ matches := re.FindAllString(mp.Text, -1)
+ for index, value := range matches {
+ matches[index] = re.ReplaceAllString(value, `${link}`)
+ }
+ return matches
+}
+
+func (mp *MarkdownParser) Emails() []string {
+ re := regexp.MustCompile(`[a-zA-Z\d]+([-+._][a-zA-Z\d]+){0,200}@[a-zA-Z\d]+([-.][a-zA-Z\d]+)*\.[a-zA-Z]{2,}`)
+ return re.FindAllString(mp.Text, -1)
+}
+
+func (mp *MarkdownParser) GenerateTableOfContents() string {
+ return ""
+}
+
+func (mp *MarkdownParser) Names() []string {
+ return nil
+}

Мария обнови решението на 03.12.2013 02:22 (преди над 4 години)

package main
import (
"regexp"
"strconv"
"strings"
)
type MarkdownParser struct {
Text string
}
func NewMarkdownParser(text string) *MarkdownParser {
mp := new(MarkdownParser)
mp.Text = text
return mp
}
func removeLeadingAndTrailingSymbols(symbols []string, src string) string {
reLeading := strings.Join([]string{symbols[0], `|\n`, symbols[0]}, "")
- reTrailing := strings.Join([]string{`\n[`, symbols[1], `]+`}, "")
+ reTrailing := strings.Join([]string{`\r\n[`, symbols[1], `]+`}, "")
leading := regexp.MustCompile(reLeading).Split(src, 2)
trailing := regexp.MustCompile(reTrailing).Split(leading[len(leading)-1], 2)
return strings.TrimSpace(trailing[0])
}
func headersList(symbols []string, re string, src string) []string {
unformated := regexp.MustCompile(re).FindAllString(src, -1)
var formated = make([]string, len(unformated))
for index, value := range unformated {
formated[index] = removeLeadingAndTrailingSymbols(symbols, value)
}
return formated
}
func (mp *MarkdownParser) partOfTextWithSubheaders(prefix string, header string) string {
subprefixLen := strconv.Itoa(len(prefix) + 1)
re := strings.Join([]string{prefix, `[ ]*`, header, `[ ]*#?([^#]|#{`, subprefixLen, `,})+(`, prefix, `|\z)`}, "")
if prefix == "#" {
- secondRE := strings.Join([]string{header, `[ ]*\n[=]+[^=]+([=]+|\z)`}, "")
+ secondRE := strings.Join([]string{header, `[ ]*(\r\n)+[=]+[^=]+([=]+|\z)`}, "")
re = strings.Join([]string{re, secondRE}, "|")
}
return regexp.MustCompile(re).FindString(mp.Text)
}
func (mp *MarkdownParser) Headers() []string {
- return headersList([]string{`#`, `=`}, `(\n|\A)#[^#\n]+|(.)+\n[=]+`, mp.Text)
+ return headersList([]string{`#`, `=`}, `(\n|\A)#[^#\r]+|(.)+\r\n[=]+`, mp.Text)
}
func (mp *MarkdownParser) SubHeadersOf(header string) []string {
partOfText := mp.partOfTextWithSubheaders("#", header)
- return headersList([]string{`##`, `-`}, `(\n|\A)##[^#\n]+|(.)+\n[-]+`, partOfText)
+ return headersList([]string{`##`, `-`}, `(\n|\A)##[^#\r]+|(.)+\r\n[-]+`, partOfText)
}
func (mp *MarkdownParser) PhoneNumbers() []string {
re := regexp.MustCompile(`(\+|\()?\d+([ ()-]*\d+)+`)
return re.FindAllString(mp.Text, -1)
}
func (mp *MarkdownParser) Links() []string {
re := regexp.MustCompile(`\[[^\]]+\]\((?P<link>[^)]+)\)`)
matches := re.FindAllString(mp.Text, -1)
for index, value := range matches {
matches[index] = re.ReplaceAllString(value, `${link}`)
}
return matches
}
func (mp *MarkdownParser) Emails() []string {
re := regexp.MustCompile(`[a-zA-Z\d]+([-+._][a-zA-Z\d]+){0,200}@[a-zA-Z\d]+([-.][a-zA-Z\d]+)*\.[a-zA-Z]{2,}`)
return re.FindAllString(mp.Text, -1)
}
func (mp *MarkdownParser) GenerateTableOfContents() string {
return ""
}
func (mp *MarkdownParser) Names() []string {
return nil
}

От ключово значение е да накараш цялото нещо да работи, ако му се подава изходът от тази функция на конструктура ти:

func loadTheReadme() string {
    resp, err := http.Get("https://raw.github.com/fmi/go-homework/master/2013/03/README.md")
    if err != nil {
        return ""
    }
    defer resp.Body.Close()
    content, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return ""
    }
    return string(content)
}

Мария обнови решението на 03.12.2013 14:47 (преди над 4 години)

package main
import (
+ "fmt"
"regexp"
"strconv"
"strings"
)
type MarkdownParser struct {
Text string
}
func NewMarkdownParser(text string) *MarkdownParser {
mp := new(MarkdownParser)
mp.Text = text
return mp
}
func removeLeadingAndTrailingSymbols(symbols []string, src string) string {
- reLeading := strings.Join([]string{symbols[0], `|\n`, symbols[0]}, "")
- reTrailing := strings.Join([]string{`\r\n[`, symbols[1], `]+`}, "")
+ reLeading := strings.Join([]string{symbols[0], `|(\n|\r)`, symbols[0]}, "")
+ reTrailing := strings.Join([]string{`(\n|\r)[`, symbols[1], `]+`}, "")
leading := regexp.MustCompile(reLeading).Split(src, 2)
trailing := regexp.MustCompile(reTrailing).Split(leading[len(leading)-1], 2)
return strings.TrimSpace(trailing[0])
}
func headersList(symbols []string, re string, src string) []string {
unformated := regexp.MustCompile(re).FindAllString(src, -1)
var formated = make([]string, len(unformated))
for index, value := range unformated {
formated[index] = removeLeadingAndTrailingSymbols(symbols, value)
}
return formated
}
func (mp *MarkdownParser) partOfTextWithSubheaders(prefix string, header string) string {
subprefixLen := strconv.Itoa(len(prefix) + 1)
re := strings.Join([]string{prefix, `[ ]*`, header, `[ ]*#?([^#]|#{`, subprefixLen, `,})+(`, prefix, `|\z)`}, "")
if prefix == "#" {
- secondRE := strings.Join([]string{header, `[ ]*(\r\n)+[=]+[^=]+([=]+|\z)`}, "")
+ re = strings.Join([]string{prefix, `[ ]*`, header, `[ ]*#?([^#]|#{`, subprefixLen, `,})+(`, prefix, `|(\n|\r)[=]+|\z)`}, "")
+ secondRE := strings.Join([]string{header, `[ ]*(\r|\n|\r\n)[=]+(.|\n)+((\n|\r)[=]+|(\n|\r)#|\z)`}, "")
re = strings.Join([]string{re, secondRE}, "|")
}
+ if prefix == "##" {
+ re = strings.Join([]string{prefix, `[ ]*`, header, `[ ]*#?([^#]|#{`, subprefixLen, `,})+(`, prefix, `|(\n|\r)+[-]+|\z)`}, "")
+ secondRE := strings.Join([]string{header, `[ ]*(\r|\n|\r\n)[-]+(.|\n)+((\n|\r)[-]+|(\n|\r)## |\z)`}, "")
+ re = strings.Join([]string{re, secondRE}, "|")
+ }
return regexp.MustCompile(re).FindString(mp.Text)
}
func (mp *MarkdownParser) Headers() []string {
- return headersList([]string{`#`, `=`}, `(\n|\A)#[^#\r]+|(.)+\r\n[=]+`, mp.Text)
+ return headersList([]string{`#`, `=`}, `(\n|\r|\A)#[^#\n\r]+|(.)+(\n|\r)[=]+`, mp.Text)
}
func (mp *MarkdownParser) SubHeadersOf(header string) []string {
partOfText := mp.partOfTextWithSubheaders("#", header)
- return headersList([]string{`##`, `-`}, `(\n|\A)##[^#\r]+|(.)+\r\n[-]+`, partOfText)
+ return headersList([]string{`##`, `-`}, `(\n|\r|\A)##[^#\r\n]+|(.)+(\n|\r)[-]+`, partOfText)
}
+//for H2-H6
+func (mp *MarkdownParser) subHeaders(prefix string, header string) []string {
+ partOfText := mp.partOfTextWithSubheaders(prefix, header)
+ return headersList([]string{prefix + `#`, ` `}, `(\n|\r|\A)`+prefix+`#[^#\r\n]+`, partOfText)
+}
+
+func (mp *MarkdownParser) SubHeaders3(header string) []string {
+ result := mp.subHeaders("##", header)
+ return result
+}
+
+func (mp *MarkdownParser) SubHeaders4(header string) []string {
+ return mp.subHeaders("###", header)
+}
+
+func (mp *MarkdownParser) SubHeaders5(header string) []string {
+ return mp.subHeaders("####", header)
+}
+
+func (mp *MarkdownParser) SubHeaders6(header string) []string {
+ return mp.subHeaders("#####", header)
+}
+
func (mp *MarkdownParser) PhoneNumbers() []string {
re := regexp.MustCompile(`(\+|\()?\d+([ ()-]*\d+)+`)
return re.FindAllString(mp.Text, -1)
}
func (mp *MarkdownParser) Links() []string {
re := regexp.MustCompile(`\[[^\]]+\]\((?P<link>[^)]+)\)`)
matches := re.FindAllString(mp.Text, -1)
for index, value := range matches {
matches[index] = re.ReplaceAllString(value, `${link}`)
}
return matches
}
func (mp *MarkdownParser) Emails() []string {
re := regexp.MustCompile(`[a-zA-Z\d]+([-+._][a-zA-Z\d]+){0,200}@[a-zA-Z\d]+([-.][a-zA-Z\d]+)*\.[a-zA-Z]{2,}`)
return re.FindAllString(mp.Text, -1)
}
func (mp *MarkdownParser) GenerateTableOfContents() string {
- return ""
+ H1 := mp.Headers()
+ var str string
+ for i := 0; i < len(H1); i++ {
+ str = fmt.Sprintf("%s%d.", str, i+1)
+ str = fmt.Sprintf("%s %s\n", str, H1[i])
+ H2 := mp.SubHeadersOf(H1[i])
+ for j := 0; j < len(H2); j++ {
+ str = fmt.Sprintf("%s%d.%d", str, i+1, j+1)
+ str = fmt.Sprintf("%s %s\n", str, H2[j])
+ H3 := mp.SubHeaders3(H2[j])
+ for k := 0; k < len(H3); k++ {
+ str = fmt.Sprintf("%s%d.%d.%d", str, i+1, j+1, k+1)
+ str = fmt.Sprintf("%s %s\n", str, H3[k])
+ H4 := mp.SubHeaders4(H3[k])
+ for l := 0; l < len(H4); l++ {
+ str = fmt.Sprintf("%s%d.%d.%d.%d", str, i+1, j+1, k+1, l+1)
+ str = fmt.Sprintf("%s %s\n", str, H4[l])
+ H5 := mp.SubHeaders5(H4[l])
+ for m := 0; m < len(H5); m++ {
+ str = fmt.Sprintf("%s%d.%d.%d.%d.%d", str, i+1, j+1, k+1, l+1, m+1)
+ str = fmt.Sprintf("%s %s\n", str, H5[m])
+ H6 := mp.SubHeaders6(H5[m])
+ for n := 0; n < len(H6); n++ {
+ str = fmt.Sprintf("%s%d.%d.%d.%d.%d.%d", str, i+1, j+1, k+1, l+1, m+1, n+1)
+ str = fmt.Sprintf("%s %s\n", str, H6[n])
+ }
+ }
+ }
+ }
+ }
+ }
+ return str
}
func (mp *MarkdownParser) Names() []string {
- return nil
+ re := regexp.MustCompile(`\b[A-ZА-Я][a-zа-я]+(( | - |-)[A-ZА-Я][a-zа-я]+)+`)
+ return re.FindAllString(mp.Text, -1)
}