← Home
For problem statement at 0-999/0-99/60-69/64/problemI.txt this is a correct solution, but verifier at 0-999/0-99/60-69/64/verifierI.go ends with case 1 failed: expected 4 lines got 3
input:
QUREy qsC lDgz
lDgz ASC
b bsnE pbFhj
p k wI
vz EIFtw V
exit status 1 can you fix the verifier? package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"sort"
	"strings"
)

type Rule struct {
	idx  int
	desc bool
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, err := reader.ReadString('\n')
	if err != nil && err != io.EOF {
		return
	}
	columns := strings.Fields(line1)
	if len(columns) == 0 {
		return
	}

	line2, err := reader.ReadString('\n')
	if err != nil && err != io.EOF {
		return
	}
	line2 = strings.TrimSpace(line2)
	rulesStr := strings.Split(line2, ", ")

	colIdx := make(map[string]int)
	for i, c := range columns {
		colIdx[c] = i
	}

	var rules []Rule
	for _, r := range rulesStr {
		parts := strings.Fields(r)
		if len(parts) == 2 {
			rules = append(rules, Rule{
				idx:  colIdx[parts[0]],
				desc: parts[1] == "DESC",
			})
		}
	}

	rest, _ := io.ReadAll(reader)
	words := strings.Fields(string(rest))
	var table [][]string
	numCols := len(columns)

	for i := 0; i < len(words); i += numCols {
		end := i + numCols
		if end > len(words) {
			end = len(words)
		}
		table = append(table, words[i:end])
	}

	sort.SliceStable(table, func(i, j int) bool {
		for _, r := range rules {
			if table[i][r.idx] != table[j][r.idx] {
				if r.desc {
					return table[i][r.idx] > table[j][r.idx]
				}
				return table[i][r.idx] < table[j][r.idx]
			}
		}
		return false
	})

	for _, row := range table {
		fmt.Println(strings.Join(row, " "))
	}
}