ch7ex10

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2022 License: GPL-3.0 Imports: 1 Imported by: 0

README

= Exercise 7.10
// Refs:
:chapter: 7
:exercise: 10
:url-base: https://github.com/fenegroni/TGPL-exercise-solutions
:workflow: workflows/Exercise {chapter}.{exercise}
:action: actions/workflows/ch{chapter}ex{exercise}.yml
:url-workflow: {url-base}/{workflow}
:url-action: {url-base}/{action}
:badge-exercise: image:{url-workflow}/badge.svg?branch=main[link={url-action}]

{badge-exercise}

The `sort.Interface` type can be adapted to other uses.
Write a function
`IsPalindrome(s sort.Interface) bool`
that
reports whether the sequence `s` is a palindrome,
in other words,
reversing the sequence would not change it.
Assume that the elements at indices `i` and `j` are equal
if `!s.Less(i, j) && !s.Less(j, i)`.

== Test

The implementation of `IsPalindrome` using `sort.Interface` is straightforward.

We test with two different types: `string` and `[]rune`.

=== Using `String`

Type `String` gives the builtin `string` type the `sort.Interface` methods we need.

It shows that to add an interface to a built in type like string we need to create an alias.

=== Using `RuneSlice`

`RuneSlice`, defined as an alias for `[]rune`, gives us the opportunity
to validate more than just prefectly symmetrical byte strings.

=== Using `CaseInsensitiveString`

The basic tests for `RuneSlice` don't look particularly promising,
but the adapter type `CaseInsensitiveString` shows the greater flexibility of converting
a utf8 `string` into a `RuneSlice` and adapting it to a case insensitive unicode string.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsPalindrome

func IsPalindrome(s sort.Interface) bool

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL