k.go 782 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package kata
  2. import "strings"
  3. func Reverse(s string) string {
  4. rs := []rune(s)
  5. for i, j := 0, len(rs)-1; i < j; i, j = i+1, j-1 {
  6. rs[i], rs[j] = rs[j], rs[i]
  7. }
  8. return string(rs)
  9. }
  10. const NL = "\n"
  11. // VertMirror reverses each line in the multiline string.
  12. func VertMirror(s string) string {
  13. ss := strings.Split(s, NL)
  14. rs := make([]string, len(ss))
  15. for i, row := range ss {
  16. rs[i] = Reverse(row)
  17. }
  18. rev := strings.Join(rs, NL)
  19. return rev
  20. }
  21. // HorMirror reverses the line order in the multiline strings.
  22. func HorMirror(s string) string {
  23. ss := strings.Split(s, NL)
  24. for i, j := 0, len(ss)-1; i < j; i, j = i+1, j-1 {
  25. ss[i], ss[j] = ss[j], ss[i]
  26. }
  27. return strings.Join(ss, NL)
  28. }
  29. type FParam func(string) string
  30. func Oper(f FParam, x string) string {
  31. return f(x)
  32. }