Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

gnomod.toml

module = "gno.land/p/nym-moultest123/x/daily/rot13/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186405
  max_deposit = "5000000ugnot"

rot13.gno

// Package rot13 ports Go's classic ROT13 example — the one used to teach
// strings.Map and io.Reader in the standard library docs — to gno as a reusable
// pure package. The core is a pure letter-rotation cipher over ASCII: ROT13
// shifts each letter 13 places, which (since the alphabet has 26 letters)
// makes ROT13 its own inverse. Caesar generalizes it to any shift.
//
// Everything here is pure strings/unicode logic: no state, no randomness,
// no clock.
//
// A live demo of this package is at
// [r/moul/x/daily/rot13demo](/r/nym-moultest123/x/daily/rot13demo/v0).
package rot13

import "strings"

// Rot13 applies the ROT13 substitution cipher, rotating ASCII letters by 13
// and leaving every other byte untouched. Because 13 is half of 26,
// Rot13(Rot13(s)) == s — the cipher is its own inverse. This mirrors the
// canonical strings.Map example from the Go docs.
func Rot13(s string) string {
	return strings.Map(rot13Rune, s)
}

// rot13Rune is the mapping function handed to strings.Map — the heart of the
// classic example.
func rot13Rune(r rune) rune {
	switch {
	case r >= 'a' && r <= 'z':
		return 'a' + (r-'a'+13)%26
	case r >= 'A' && r <= 'Z':
		return 'A' + (r-'A'+13)%26
	}
	return r
}

// Caesar generalizes ROT13 to an arbitrary shift. Negative and large shifts
// are normalized into [0,26). Only ASCII letters move; anything else passes
// through unchanged. Caesar(s, 13) is exactly Rot13(s).
func Caesar(s string, shift int) string {
	sh := rune(((shift % 26) + 26) % 26)
	return strings.Map(func(r rune) rune {
		switch {
		case r >= 'a' && r <= 'z':
			return 'a' + (r-'a'+sh)%26
		case r >= 'A' && r <= 'Z':
			return 'A' + (r-'A'+sh)%26
		}
		return r
	}, s)
}

rot13_test.gno

package rot13

import (
	"strconv"
	"testing"

	"gno.land/p/nt/uassert/v0"
)

func TestRot13(t *testing.T) {
	cases := []struct {
		in, want string
	}{
		{"", ""},
		{"Hello", "Uryyb"},
		{"Uryyb", "Hello"},                     // decode
		{"abcxyz", "nopklm"},                   // wraps within lowercase
		{"ABCXYZ", "NOPKLM"},                   // wraps within uppercase
		{"Hello, Gno! 123", "Uryyb, Tab! 123"}, // punctuation & digits untouched
	}
	for _, c := range cases {
		got := Rot13(c.in)
		uassert.Equal(t, c.want, got, "Rot13(%q)", c.in)
	}
}

func TestRot13IsItsOwnInverse(t *testing.T) {
	inputs := []string{"", "Hello, Gno!", "The quick brown fox", "MixedCASE 42!"}
	for _, in := range inputs {
		uassert.Equal(t, in, Rot13(Rot13(in)), "Rot13(Rot13(%q)) should round-trip", in)
	}
}

func TestCaesar(t *testing.T) {
	cases := []struct {
		in    string
		shift int
		want  string
	}{
		{"abc", 1, "bcd"},
		{"xyz", 3, "abc"},                  // wraps around the end
		{"ABC", 25, "ZAB"},                 // large shift, uppercase
		{"Hello", 0, "Hello"},              // no-op
		{"Hello", 26, "Hello"},             // full rotation
		{"bcd", -1, "abc"},                 // negative shift
		{"a-b c!", 1, "b-c d!"},            // non-letters untouched
		{"Hello, Gno!", 13, "Uryyb, Tab!"}, // shift 13 == ROT13
	}
	for _, c := range cases {
		got := Caesar(c.in, c.shift)
		uassert.Equal(t, c.want, got, "Caesar("+strconv.Quote(c.in)+", "+strconv.Itoa(c.shift)+")")
	}
}

func TestCaesarEqualsRot13At13(t *testing.T) {
	inputs := []string{"Hello, Gno!", "The quick brown fox jumps"}
	for _, in := range inputs {
		uassert.Equal(t, Rot13(in), Caesar(in, 13), "Caesar(%q,13) should equal Rot13", in)
	}
}