from typing import Sequence


def grade(seq:Sequence, *, descending=False) -> list:
	"""
	Returns an index permutation, such that:
		[seq[i] for i in grade(seq)] == sorted(seq)
	Cribbed from APL's grade_up/grade_down primitives.
	"""
	return sorted(range(len(seq)), key=seq.__getitem__, reverse=descending)