From d883fc21c99b1bb34333cc88fc91934ae616e820 Mon Sep 17 00:00:00 2001 From: Gulliby-development <20404543+Gulliby@users.noreply.github.com> Date: Wed, 21 Oct 2026 06:13:26 +0200 Subject: [PATCH] Add run notation for shapes Wherever a shape is read, a token may be written PARTxCOUNT for a part repeated COUNT times, so 4,3x2,1 is 4,3,3,1. Size is checked with big integers before the shape is spelled out. Tests and README updated. --- README.md | 2 + internal/cli/runs_test.go | 42 ++++++++++ internal/shape/runs.go | 30 +++++++ internal/shape/runs_test.go | 162 ++++++++++++++++++++++++++++++++++++ internal/shape/shape.go | 50 ++++++----- 5 files changed, 266 insertions(+), 20 deletions(-) create mode 100644 internal/cli/runs_test.go create mode 100644 internal/shape/runs.go create mode 100644 internal/shape/runs_test.go diff --git a/README.md b/README.md index 412ff23..4265ac9 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,8 @@ For `rsk`, provide permutation entries as arguments, or use `-` as the sole argu A shape is written as comma-separated, positive, nonincreasing parts, for example `5,3,3,1`. The notation `0` denotes the empty shape. Parts must be canonical integers: malformed notation such as a leading-zero part (`03`), a trailing comma, or an empty shape string is rejected. +A run of equal parts may be written `PARTxCOUNT` with the lowercase letter `x`: `4,3x2,1` is `4,3,3,1`, `2x3` is `2,2,2` and `3x1` is `3`. PART and COUNT are canonical integers, COUNT runs from 1 to 5000, and the total size limit is unchanged. Every command that takes a shape accepts the run notation and prints the parts spelled out; `0` stays the only spelling of the empty shape, so `0x1` is rejected as a zero part. + A tableau is written with slash-separated rows and comma-separated entries, for example `1,3,5/2,4/6`; `0` denotes the empty tableau. Standard tableaux contain each integer from 1 through the tableau's size exactly once, with entries increasing from left to right in every row and from top to bottom in every column. Empty rows, malformed entries and tableaux that fail these conditions are rejected. Counts and ranks are exact integers. Shape and tableau commands accept `0` for their empty object; its printed form is also `0`. diff --git a/internal/cli/runs_test.go b/internal/cli/runs_test.go new file mode 100644 index 0000000..111ecac --- /dev/null +++ b/internal/cli/runs_test.go @@ -0,0 +1,42 @@ +package cli_test + +import "testing" + +func TestRunNotationSevenCommands(t *testing.T) { + requireCommandSuccess(t, []string{"rank", "4,3x2,1"}, + "shape 4,3,3,1\nsize 11\nrank 32\nof 56\n") + requireCommandSuccess(t, []string{"conjugate", "3x2"}, + "shape 3,3\nsize 6\nconjugate 2,2,2\nself no\nrow ###\nrow ###\n") + requireCommandSuccess(t, []string{"core", "3", "4x2,1"}, + "shape 4,4,1\nk 3\ncore 0\nweight 3\nquotient 0=2\nquotient 1=0\nquotient 2=1\n") + requireCommandSuccess(t, []string{"compare", "3x2", "4,1,1"}, + "a 3,3\nb 4,1,1\nsize 6\nsums-a 3 6 6\nsums-b 4 5 6\nrelation incomparable\na-ahead 2\nb-ahead 1\n") + requireCommandSuccess(t, []string{"hooks", "2x2,1"}, + "shape 2,2,1\nsize 5\nhook 4 2\nhook 3 1\nhook 1\nsyt 5\n") + requireCommandSuccess(t, []string{"neighbors", "2x2"}, + "shape 2,2\nsize 4\nadds 2\nadd 3,2\nadd 2,2,1\nremoves 1\nremove 2,1\n") + requireCommandSuccess(t, []string{"syt", "2x2"}, + "shape 2,2\nsize 4\ncount 2\nindex 0\nshown 2\ntableau 1,3/2,4\ntableau 1,2/3,4\n") +} + +func TestRunNotationRunOfOne(t *testing.T) { + requireCommandSuccess(t, []string{"rank", "3x1"}, + "shape 3\nsize 3\nrank 0\nof 3\n") + requireCommandSuccess(t, []string{"rank", "2,1x1"}, + "shape 2,1\nsize 3\nrank 1\nof 3\n") +} + +func TestRunNotationErrors(t *testing.T) { + requireDispatchError(t, []string{"rank", "3x"}, nil, + "invalid shape \"3x\" (expected parts such as 5,3,1, or 0 for the empty shape)", 0) + requireDispatchError(t, []string{"rank", "3x0"}, nil, + "invalid count \"0\" in \"3x0\" (expected 1 to 5000)", 0) + requireDispatchError(t, []string{"hooks", "3x2,4"}, nil, + "parts of \"3x2,4\" must not increase (part 3 is 4, after 3)", 0) + requireDispatchError(t, []string{"conjugate", "2x5001"}, nil, + "invalid count \"5001\" in \"2x5001\" (expected 1 to 5000)", 0) + requireDispatchError(t, []string{"core", "3", "5000x2"}, nil, + "shape \"5000x2\" has size 10000 (above 5000)", 0) + requireDispatchError(t, []string{"rank", "1x5000"}, nil, + "size 5000 is above 400 (too large for rank)", 0) +} diff --git a/internal/shape/runs.go b/internal/shape/runs.go new file mode 100644 index 0000000..46e2632 --- /dev/null +++ b/internal/shape/runs.go @@ -0,0 +1,30 @@ +package shape + +import ( + "strings" + + "github.com/Gulliby-development/youngkit/internal/base" +) + +// run is a part repeated count times, with the texts it was read from. +type run struct { + part, count int64 + partText, countText string +} + +// readRun reads one token PART or PARTxCOUNT; ok is false on a syntax error. +func readRun(token string) (run, bool) { + partText, countText, found := strings.Cut(token, "x") + if !found { + countText = "1" + } + part, err := base.ParseInt("part", partText, 0, 9999999999) + if err != nil { + return run{}, false + } + count, err := base.ParseInt("count", countText, 0, 9999999999) + if err != nil { + return run{}, false + } + return run{part: part, count: count, partText: partText, countText: countText}, true +} diff --git a/internal/shape/runs_test.go b/internal/shape/runs_test.go new file mode 100644 index 0000000..c28c3e1 --- /dev/null +++ b/internal/shape/runs_test.go @@ -0,0 +1,162 @@ +package shape + +import ( + "errors" + "math/rand" + "sort" + "strconv" + "strings" + "testing" + "testing/quick" + + "github.com/Gulliby-development/youngkit/internal/base" +) + +const runsSyntaxMessage = " (expected parts such as 5,3,1, or 0 for the empty shape)" + +func TestParseRunsExpand(t *testing.T) { + for _, test := range []struct { + text string + want Shape + }{ + {text: "3x2", want: Shape{3, 3}}, + {text: "4,3x2,1", want: Shape{4, 3, 3, 1}}, + {text: "2x3", want: Shape{2, 2, 2}}, + {text: "3x1", want: Shape{3}}, + {text: "2,1x1", want: Shape{2, 1}}, + {text: "2x2,2,1x2", want: Shape{2, 2, 2, 1, 1}}, + } { + got, err := Parse(test.text) + if err != nil { + t.Fatalf("Parse(%q): %v", test.text, err) + } + requireShapeEqual(t, got, test.want) + } + + // Round trip: a run text and its spelled-out text parse to the same shape. + property := func(seed int64) bool { + rng := rand.New(rand.NewSource(seed)) + n := 1 + rng.Intn(10) + parts := make([]int, n) + for i := range parts { + parts[i] = 1 + rng.Intn(20) + } + sort.Sort(sort.Reverse(sort.IntSlice(parts))) + var runText, spelled []string + for _, part := range parts { + count := 1 + rng.Intn(20) + runText = append(runText, strconv.Itoa(part)+"x"+strconv.Itoa(count)) + for j := 0; j < count; j++ { + spelled = append(spelled, strconv.Itoa(part)) + } + } + spelledText := strings.Join(spelled, ",") + fromRuns, err := Parse(strings.Join(runText, ",")) + if err != nil { + t.Logf("runs %v: %v", runText, err) + return false + } + fromSpelled, err := Parse(spelledText) + if err != nil { + t.Logf("spelled %q: %v", spelledText, err) + return false + } + if len(fromRuns) != len(fromSpelled) { + return false + } + for i := range fromRuns { + if fromRuns[i] != fromSpelled[i] { + return false + } + } + return fromRuns.String() == spelledText + } + config := &quick.Config{MaxCount: 200, Rand: rand.New(rand.NewSource(20240607))} + if err := quick.Check(property, config); err != nil { + t.Fatal(err) + } +} + +func TestParseRunsAtSizeLimit(t *testing.T) { + got, err := Parse("2500x2") + if err != nil { + t.Fatal(err) + } + requireShapeEqual(t, got, Shape{2500, 2500}) +} + +func TestParseRunsRejectsMalformedTokens(t *testing.T) { + for _, text := range []string{"3x", "x2", "x", "3xx", "3x2x2", "3x02", "3X2", "3x+2", "3x1.5", "3x2,"} { + _, err := Parse(text) + // A malformed PARTxCOUNT token yields a *base.Error whose Msg is the syntax message. + var baseErr *base.Error + if !errors.As(err, &baseErr) { + t.Fatalf("Parse(%q): want *base.Error, got %v", text, err) + } + if want := "invalid shape " + strconv.Quote(text) + runsSyntaxMessage; baseErr.Msg != want { + t.Fatalf("Parse(%q) Msg = %q, want %q", text, baseErr.Msg, want) + } + requireShapeError(t, err, "invalid shape "+strconv.Quote(text)+runsSyntaxMessage) + } +} + +func TestParseRunsRejectsOutOfRange(t *testing.T) { + for _, test := range []struct { + text string + want string + }{ + {text: "0x2", want: `invalid part "0" in "0x2" (expected 1 to 5000)`}, + {text: "7000x2", want: `invalid part "7000" in "7000x2" (expected 1 to 5000)`}, + {text: "3x0", want: `invalid count "0" in "3x0" (expected 1 to 5000)`}, + {text: "2x5001", want: `invalid count "5001" in "2x5001" (expected 1 to 5000)`}, + {text: "0x0", want: `invalid part "0" in "0x0" (expected 1 to 5000)`}, + {text: "3x0,0x2", want: `invalid count "0" in "3x0,0x2" (expected 1 to 5000)`}, + } { + _, err := Parse(test.text) + requireShapeError(t, err, test.want) + } +} + +func TestParseRunsSyntaxPrecedesRange(t *testing.T) { + _, err := Parse("3x0,y") + requireShapeError(t, err, `invalid shape "3x0,y"`+runsSyntaxMessage) +} + +func TestParseRunsEmptyShape(t *testing.T) { + got, err := Parse("0") + if err != nil { + t.Fatal(err) + } + requireShapeEqual(t, got, Shape{}) + _, err = Parse("0x1") + requireShapeError(t, err, `invalid part "0" in "0x1" (expected 1 to 5000)`) +} + +func TestParseRunsOrderingPosition(t *testing.T) { + for _, test := range []struct { + text string + want string + }{ + {text: "3x2,4", want: `parts of "3x2,4" must not increase (part 3 is 4, after 3)`}, + {text: "2,3x2", want: `parts of "2,3x2" must not increase (part 2 is 3, after 2)`}, + {text: "4x2,3x3,4", want: `parts of "4x2,3x3,4" must not increase (part 6 is 4, after 3)`}, + {text: "1x5000,2", want: `parts of "1x5000,2" must not increase (part 5001 is 2, after 1)`}, + } { + _, err := Parse(test.text) + requireShapeError(t, err, test.want) + } +} + +func TestParseRunsSizeBeforeExpanding(t *testing.T) { + for _, test := range []struct { + text string + want string + }{ + {text: "5000x2", want: `shape "5000x2" has size 10000 (above 5000)`}, + {text: "2500x2,1", want: `shape "2500x2,1" has size 5001 (above 5000)`}, + {text: "5000x5000", want: `shape "5000x5000" has size 25000000 (above 5000)`}, + } { + _, err := Parse(test.text) + requireShapeError(t, err, test.want) + } +} diff --git a/internal/shape/shape.go b/internal/shape/shape.go index f0e13bf..167be6b 100644 --- a/internal/shape/shape.go +++ b/internal/shape/shape.go @@ -11,53 +11,63 @@ import ( // Shape is a partition written as positive, nonincreasing parts. type Shape []int -// Parse parses a shape, validating token syntax, part ranges, ordering, and size -// in that order. +// Parse parses a shape, validating token syntax, part and count ranges, +// ordering, and size in that order. A token may be a run PARTxCOUNT. func Parse(text string) (Shape, error) { - parts := strings.Split(text, ",") - values := make([]int64, len(parts)) + tokens := strings.Split(text, ",") + runs := make([]run, len(tokens)) // Validate every token's syntax before checking any semantic constraints. - for i, part := range parts { - value, err := base.ParseInt("part", part, 0, 9999999999) - if err != nil { + for i, token := range tokens { + r, ok := readRun(token) + if !ok { return nil, base.Fail("invalid shape " + base.Show(text) + " (expected parts such as 5,3,1, or 0 for the empty shape)") } - values[i] = value + runs[i] = r } if text == "0" { return Shape{}, nil } - for i, value := range values { - if value < 1 || value > 5000 { - return nil, base.Fail("invalid part " + base.Show(parts[i]) + + for _, r := range runs { + if r.part < 1 || r.part > 5000 { + return nil, base.Fail("invalid part " + base.Show(r.partText) + + " in " + base.Show(text) + " (expected 1 to 5000)") + } + if r.count < 1 || r.count > 5000 { + return nil, base.Fail("invalid count " + base.Show(r.countText) + " in " + base.Show(text) + " (expected 1 to 5000)") } } - for i := 1; i < len(values); i++ { - if values[i] > values[i-1] { + position := int64(0) + for i, r := range runs { + if i > 0 && r.part > runs[i-1].part { return nil, base.Fail("parts of " + base.Show(text) + - " must not increase (part " + strconv.Itoa(i+1) + " is " + - parts[i] + ", after " + parts[i-1] + ")") + " must not increase (part " + strconv.FormatInt(position+1, 10) + + " is " + r.partText + ", after " + runs[i-1].partText + ")") } + position += r.count } var total big.Int - for _, value := range values { - total.Add(&total, big.NewInt(value)) + for _, r := range runs { + var product big.Int + product.Mul(big.NewInt(r.part), big.NewInt(r.count)) + total.Add(&total, &product) } if total.Cmp(big.NewInt(5000)) > 0 { return nil, base.Fail("shape " + base.Show(text) + " has size " + total.String() + " (above 5000)") } - result := make(Shape, len(values)) - for i, value := range values { - result[i] = int(value) + result := Shape{} + for _, r := range runs { + for j := int64(0); j < r.count; j++ { + result = append(result, int(r.part)) + } } return result, nil }