Propose a solution to fix backticks in Telegram messages
There can be two types:
- singular backticks `
- triple backticks ```
Add backticks to the end of message:
`malformed => `malformed`
```code` => ```code```
package kata
import "strings"
func FixMarkdown(text string) string {
text = strings.TrimRight(text, "`")
tripleCnt := strings.Count(text, "```")
singleCnt := strings.Count(strings.Replace(text, "```", "", -1), "`")
if tripleCnt % 2 == 1 {
text += "```"
}
if singleCnt % 2 == 1 {
text += "`"
}
return text
}
// TODO: replace with your own tests (TDD). An example to get you started is included below.
// Ginkgo BDD Testing Framework <http://onsi.github.io/ginkgo/>
// Gomega Matcher Library <http://onsi.github.io/gomega/>
package kata_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "codewarrior/kata"
)
var _ = Describe("Test Example", func() {
It("Empty testcase and normal text", func() {
Expect(FixMarkdown("")).To(Equal(""))
Expect(FixMarkdown("This is a normal text")).To(Equal("This is a normal text"))
})
It("Normal markdown", func() {
Expect(FixMarkdown("Normal `message`")).To(Equal("Normal `message`"))
Expect(FixMarkdown("Triple ```message```")).To(Equal("Triple ```message```"))
})
It("Malformed triple markdown", func() {
Expect(FixMarkdown("Triple ```message")).To(Equal("Triple ```message```"))
Expect(FixMarkdown("Triple ```message`")).To(Equal("Triple ```message```"))
Expect(FixMarkdown("Triple ```message``")).To(Equal("Triple ```message```"))
Expect(FixMarkdown("Triple ```message``` and this ``` // leave code here")).To(Equal("Triple ```message``` and this ``` // leave code here```"))
})
It("Malformed single markdown", func() {
Expect(FixMarkdown("`message")).To(Equal("`message`"))
Expect(FixMarkdown("Inside `message` and leaving like `fdfd")).To(Equal("Inside `message` and leaving like `fdfd`"))
})
It("Malformed multiple markdown", func() {
Expect(FixMarkdown("`message` and ```code``` and `message (abrupt) and ```code`")).To(Equal("`message` and ```code``` and `message (abrupt) and ```code````"))
})
})
math magic
package kumite import "time" import "math" func CheckWorkHours(dateTime time.Time) bool { if math.Abs(float64(dateTime.Weekday()) - 3) == 3 { return false } hour := dateTime.Hour() return 8 <= hour && hour < 18 }
- package kumite
- import "time"
- import "math"
- func CheckWorkHours(dateTime time.Time) bool {
day := dateTime.Weekday()if day < time.Monday || day > time.Friday {- if math.Abs(float64(dateTime.Weekday()) - 3) == 3 {
- return false
- }
- hour := dateTime.Hour()
- return 8 <= hour && hour < 18
- }
package kata func Multiple3And5(n int) bool { return n - 15 * (n / 15) == 0 }
- package kata
- func Multiple3And5(n int) bool {
return n%3==0 && n%5==0- return n - 15 * (n / 15) == 0
- }
// TODO: replace with your own tests (TDD). An example to get you started is included below. // Ginkgo BDD Testing Framework <http://onsi.github.io/ginkgo/> // Gomega Matcher Library <http://onsi.github.io/gomega/> package kata_test import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" . "codewarrior/kata" ) func dotest(n int, exp bool) { var ans = Multiple3And5(n) Expect(ans).To(Equal(exp)) } var _ = Describe("Test Example", func() { It("should handle basic cases", func() { dotest(15, true) dotest(30, true) dotest(9, false) dotest(44, false) dotest(81, false) dotest(9015, true) }) })
- // TODO: replace with your own tests (TDD). An example to get you started is included below.
- // Ginkgo BDD Testing Framework <http://onsi.github.io/ginkgo/>
- // Gomega Matcher Library <http://onsi.github.io/gomega/>
- package kata_test
- import (
- . "github.com/onsi/ginkgo"
- . "github.com/onsi/gomega"
- . "codewarrior/kata"
- )
- func dotest(n int, exp bool) {
- var ans = Multiple3And5(n)
- Expect(ans).To(Equal(exp))
- }
- var _ = Describe("Test Example", func() {
- It("should handle basic cases", func() {
- dotest(15, true)
- dotest(30, true)
- dotest(9, false)
- dotest(44, false)
- dotest(81, false)
- dotest(9015, true)
- })
- })
no number usage
thoughts:
- somehow we need to get (2 * 5) ** 2 = 100
- anything ** 0 => 1
- we need to get 2 (so it is basically 1 + 1)
- need to get 5 with shortest way possible (i dont like toString().length, so it can be improved)
- maybe there is a way to get 2 in a short way
Considering leap years
package kata // CalcAgeOnMars calculates the age on Mars from on a age on Earth func CalcAgeOnMars(age int) int { return int(float64(age) * 365.25 / 686.97) }
- package kata
- // CalcAgeOnMars calculates the age on Mars from on a age on Earth
- func CalcAgeOnMars(age int) int {
return age * 365 / 687- return int(float64(age) * 365.25 / 686.97)
- }