Решение на Разлика в сумите от Моника Илиева

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

Към профила на Моника Илиева

Резултати

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

Код

package main
import "strings"
func countLines(str string) int {
numberOfLines := 0
for i := 0; i < len(str); i++ {
if str[i] == '\n' {
numberOfLines++
}
}
return numberOfLines
}
func ExtractDate(logContents string) string {
dates := ""
for i := 0; i < countLines(logContents); i++ {
date := strings.Fields(strings.Split(logContents, "\n")[i])
if len(date) > 0 {
dates += strings.Join(date[0:2], " ") + "\n"
}
}
return dates
}
func ExtractIPv4(logContents string) string {
ipv4Addresses := ""
for i := 0; i <= countLines(logContents); i++ {
IPv4 := strings.Fields(strings.Split(logContents, "\n")[i])
if len(IPv4) > 0 {
ipv4Addresses += strings.TrimSpace(strings.Join(IPv4[2:3], " ")) + "\n"
}
}
return ipv4Addresses
}
func ExtractString(logContents string) string {
str := ""
for i := 0; i < countLines(logContents); i++ {
strSlice := strings.Fields(strings.Split(logContents, "\n")[i])[3:]
for j := 0; j < len(strSlice); j++ {
str += strSlice[j] + " "
}
str = strings.TrimSpace(str)
str += "\n"
}
return str
}
func ExtractColumn(logContents string, column uint8) string {
if len(logContents) == 0 || logContents == "\n" {
return ""
}
switch column {
case 0:
return ExtractDate(logContents)
case 1:
return ExtractIPv4(logContents)
case 2:
return ExtractString(logContents)
}
return ""
}

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

PASS
ok  	_/tmp/d20151103-24541-1enfxy3	0.003s
PASS
ok  	_/tmp/d20151103-24541-1enfxy3	0.003s
PASS
ok  	_/tmp/d20151103-24541-1enfxy3	0.004s
PASS
ok  	_/tmp/d20151103-24541-1enfxy3	0.003s
--- FAIL: TestSpacesAtTheStartOrEndOfALine (0.00s)
	solution_test.go:260: Expected
		---
		2015-08-23 12:37:03
		2015-08-23 12:37:04
		2015-08-23 12:37:05
		2015-08-22 12:37:06
		0001-01-01 00:00:00
		
		---
		but found
		---
		2015-08-23 12:37:03
		2015-08-23 12:37:04
		2015-08-23 12:37:05
		2015-08-22 12:37:06
		
		---
	solution_test.go:260: Expected
		---
		As far as we can tell this is a DNS
		Yet another DNS, how quaint!
		There is definitely some trend here
		 Some spaces at the beginning of this line
		Nothing particularly interesting happened 
		
		---
		but found
		---
		As far as we can tell this is a DNS
		Yet another DNS, how quaint!
		There is definitely some trend here
		Some spaces at the beginning of this line
		
		---
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-1enfxy3	0.003s
--- FAIL: TestNoNewLineAtEndOfInput (0.00s)
	solution_test.go:260: Expected
		---
		As far as we can tell this is a DNS
		There is definitely some trend here
		
		---
		but found
		---
		As far as we can tell this is a DNS
		
		---
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-1enfxy3	0.003s
PASS
ok  	_/tmp/d20151103-24541-1enfxy3	0.003s
PASS
ok  	_/tmp/d20151103-24541-1enfxy3	0.003s
PASS
ok  	_/tmp/d20151103-24541-1enfxy3	0.003s
PASS
ok  	_/tmp/d20151103-24541-1enfxy3	0.003s
PASS
ok  	_/tmp/d20151103-24541-1enfxy3	0.003s
PASS
ok  	_/tmp/d20151103-24541-1enfxy3	0.003s
PASS
ok  	_/tmp/d20151103-24541-1enfxy3	0.003s
--- FAIL: TestWithSpaces (0.00s)
	solution_test.go:260: Expected
		---
		spaces   tabs		end
		
		---
		but found
		---
		
		---
FAIL
exit status 1
FAIL	_/tmp/d20151103-24541-1enfxy3	0.003s
PASS
ok  	_/tmp/d20151103-24541-1enfxy3	0.003s

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

Моника обнови решението на 30.10.2015 22:30 (преди над 2 години)

+package main
+
+import (
+ "regexp"
+ "strings"
+)
+
+const (
+ date string = "20\\d{2}(-|\\/)((0[1-9])|(1[0-2]))(-|\\/)((0[1-9])|" +
+ "([1-2][0-9])|(3[0-1]))(T|\\s)(([0-1][0-9])|(2[0-3])):" +
+ "([0-5][0-9]):([0-5][0-9])"
+ ipv4 string = "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})(\\.(25[0-5]" +
+ "|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})){3}"
+ newLine string = "\n"
+ emptyString string = ""
+)
+
+func ExtractDate(logContents string) string {
+ re := regexp.MustCompile(date)
+ return strings.Join(re.FindAllString(logContents, -1), newLine) + newLine
+}
+
+func ExtractIPv4(logContents string) string {
+ re := regexp.MustCompile(ipv4)
+ return strings.Join(re.FindAllString(logContents, -1), newLine) + newLine
+}
+
+func ExtractString(logContents string) string {
+ reDate := regexp.MustCompile(date + "\\s")
+ reIPv4 := regexp.MustCompile(ipv4 + "\\s")
+ str := reDate.ReplaceAllString(logContents, emptyString)
+ return reIPv4.ReplaceAllString(str, emptyString)
+}
+
+func ExtractColumn(logContents string, column uint8) string {
+ if len(logContents) == 0 || logContents == newLine {
+ return emptyString
+ }
+
+ switch column {
+ case 0:
+ return ExtractDate(logContents)
+ case 1:
+ return ExtractIPv4(logContents)
+ case 2:
+ return ExtractString(logContents)
+ }
+
+ return emptyString
+}

Моника обнови решението на 30.10.2015 22:41 (преди над 2 години)

package main
import (
- "regexp"
- "strings"
+ "regexp"
+ "strings"
)
const (
- date string = "20\\d{2}(-|\\/)((0[1-9])|(1[0-2]))(-|\\/)((0[1-9])|" +
+ date string = "20\\d{2}(-|\\/)((0[1-9])|(1[0-2]))(-|\\/)((0[1-9])|" +
"([1-2][0-9])|(3[0-1]))(T|\\s)(([0-1][0-9])|(2[0-3])):" +
"([0-5][0-9]):([0-5][0-9])"
- ipv4 string = "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})(\\.(25[0-5]" +
+ ipv4 string = "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})(\\.(25[0-5]" +
"|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})){3}"
- newLine string = "\n"
- emptyString string = ""
+ newLine string = "\n"
+ emptyString string = ""
)
func ExtractDate(logContents string) string {
- re := regexp.MustCompile(date)
- return strings.Join(re.FindAllString(logContents, -1), newLine) + newLine
+ re := regexp.MustCompile(date)
+ return strings.Join(re.FindAllString(logContents, -1), newLine) + newLine
}
func ExtractIPv4(logContents string) string {
- re := regexp.MustCompile(ipv4)
- return strings.Join(re.FindAllString(logContents, -1), newLine) + newLine
+ re := regexp.MustCompile(ipv4)
+ return strings.Join(re.FindAllString(logContents, -1), newLine) + newLine
}
func ExtractString(logContents string) string {
- reDate := regexp.MustCompile(date + "\\s")
- reIPv4 := regexp.MustCompile(ipv4 + "\\s")
- str := reDate.ReplaceAllString(logContents, emptyString)
- return reIPv4.ReplaceAllString(str, emptyString)
+ reDate := regexp.MustCompile(date + "\\s")
+ reIPv4 := regexp.MustCompile(ipv4 + "\\s")
+ str := reDate.ReplaceAllString(logContents, emptyString)
+ return reIPv4.ReplaceAllString(str, emptyString)
}
func ExtractColumn(logContents string, column uint8) string {
- if len(logContents) == 0 || logContents == newLine {
- return emptyString
- }
+ if len(logContents) == 0 || logContents == newLine {
+ return emptyString
+ }
- switch column {
- case 0:
- return ExtractDate(logContents)
- case 1:
- return ExtractIPv4(logContents)
- case 2:
- return ExtractString(logContents)
- }
+ switch column {
+ case 0:
+ return ExtractDate(logContents)
+ case 1:
+ return ExtractIPv4(logContents)
+ case 2:
+ return ExtractString(logContents)
+ }
- return emptyString
+ return emptyString
}

Моника обнови решението на 03.11.2015 12:27 (преди над 2 години)

package main
-import (
- "regexp"
- "strings"
-)
+import "strings"
-const (
- date string = "20\\d{2}(-|\\/)((0[1-9])|(1[0-2]))(-|\\/)((0[1-9])|" +
- "([1-2][0-9])|(3[0-1]))(T|\\s)(([0-1][0-9])|(2[0-3])):" +
- "([0-5][0-9]):([0-5][0-9])"
- ipv4 string = "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})(\\.(25[0-5]" +
- "|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})){3}"
- newLine string = "\n"
- emptyString string = ""
-)
+func countLines(str string) int {
+ numberOfLines := 0
+ for i := 0; i < len(str); i++ {
+ if str[i] == '\n' {
+ numberOfLines++
+ }
+ }
+ return numberOfLines
+}
func ExtractDate(logContents string) string {
- re := regexp.MustCompile(date)
- return strings.Join(re.FindAllString(logContents, -1), newLine) + newLine
+ dates := ""
+
+ for i := 0; i < countLines(logContents); i++ {
+ date := strings.Fields(strings.Split(logContents, "\n")[i])
+ if len(date) > 0 {
+ dates += strings.Join(date[0:2], " ") + "\n"
+ }
+ }
+ return dates
}
func ExtractIPv4(logContents string) string {
- re := regexp.MustCompile(ipv4)
- return strings.Join(re.FindAllString(logContents, -1), newLine) + newLine
+ ipv4Addresses := ""
+
+ for i := 0; i <= countLines(logContents); i++ {
+ IPv4 := strings.Fields(strings.Split(logContents, "\n")[i])
+ if len(IPv4) > 0 {
+ ipv4Addresses += strings.TrimSpace(strings.Join(IPv4[2:3], " ")) + "\n"
+ }
+ }
+ return ipv4Addresses
}
func ExtractString(logContents string) string {
- reDate := regexp.MustCompile(date + "\\s")
- reIPv4 := regexp.MustCompile(ipv4 + "\\s")
- str := reDate.ReplaceAllString(logContents, emptyString)
- return reIPv4.ReplaceAllString(str, emptyString)
+ str := ""
+ for i := 0; i < countLines(logContents); i++ {
+ strSlice := strings.Fields(strings.Split(logContents, "\n")[i])[3:]
+ for j := 0; j < len(strSlice); j++ {
+ str += strSlice[j] + " "
+ }
+ str = strings.TrimSpace(str)
+ str += "\n"
+ }
+ return str
}
func ExtractColumn(logContents string, column uint8) string {
- if len(logContents) == 0 || logContents == newLine {
- return emptyString
+ if len(logContents) == 0 || logContents == "\n" {
+ return ""
}
switch column {
- case 0:
- return ExtractDate(logContents)
- case 1:
- return ExtractIPv4(logContents)
- case 2:
- return ExtractString(logContents)
+ case 0:
+ return ExtractDate(logContents)
+ case 1:
+ return ExtractIPv4(logContents)
+ case 2:
+ return ExtractString(logContents)
}
- return emptyString
+ return ""
}