TeX Alchemist Online

TeX のこと,フォントのこと,Mac のこと

TeX で乱数を使う (3) ~ 逆置換の活用

前回の記事で作成した randomshuffle パッケージは,n次対称群 $\mathfrak{S}_n$ の元 $\sigma$ をランダムに生成したとき,同時にその逆置換 $\sigma^{-1}$ も生成する仕様になっていました。逆置換はどういう場面で役立つのでしょうか。

例えば次のような,英語の試験でよくある語順整序問題とその解答を見てみます(問題ネタは2016年センター試験「英語」より)。

f:id:doraTeX:20190117014913p:plain

このような語順整序問題の生成と解答表示では,次のように,置換とその逆置換が両方作用しています。

f:id:doraTeX:20190117022232p:plain

まず,解答選択肢記号 ㋐~㋕ に応じて単語を順に割り当て,それをシャッフルすることで問題の語群を生成します。次に,解答においては,空欄番号 1~6 に当てはまる正解の単語記号 ㋐~㋕ を表示します。このとき,前半の「シャッフルされた語群の生成」と後半の「それを正しい順に並べた単語記号列の生成」では,ちょうど置換と逆置換の関係になっています。

これを利用して,ランダムにシャッフルされた語群を毎回自動生成し,その正解も自動出力するという文書を作ってみましょう。

【コード例】

%#!uplatex
\documentclass[uplatex]{jsarticle}
\usepackage{randomshuffle}
\makeatletter
\newcount\wordnum
\newcount\wordcount

% 選択肢の記号を ㋐, ㋑, …… で出力する
\def\MaruKata#1{\chardef\@labelchar=\expandafter\ucs\numexpr13007+#1\relax\textgt{\@labelchar}}

%% 語群のインプットと空欄出力
\def\語順整序#1{%
  \wordnum=\z@
  \@tfor\word:=#1\do{%
    \advance\wordnum\@ne
    \edef\thisword{word\the\wordnum}%
    \expandafter\@namedef\expandafter{\expandafter\thisword\expandafter}\expandafter{\word}% 単語を登録
    \space\framebox[1cm][c]{\texttt{\the\wordnum}}% 空欄出力
  }%
  \wordcount=\wordnum% 単語数を保存
  \randomshuffle[name=shuffled]{1}{\wordcount}% 語群をランダムシャッフル
}

%% シャッフルされた語群を出力
\def\語群出力{%
  \par\noindent\textgt{《語群》}\par\nopagebreak
  \wordnum=\z@
  \loop
    \advance\wordnum\@ne
    \def\thisword{\@nameuse{word\shuffled{\the\wordnum}}}% シャッフルされた選択肢の内容
    \makebox[\dimexpr\linewidth/\wordcount\relax][l]{\MaruKata{\wordnum}\kern1zw\relax\thisword}% 選択肢を出力
  \ifnum\wordcount>\wordnum\repeat
}

%% 逆置換を用いて正解を出力
\def\解答出力{%
  \par\noindent\textgt{【解答】}\par\nopagebreak
  \wordnum=\z@
  \loop
    \advance\wordnum\@ne
    \def\answernum{\shuffledinverse{\the\wordnum}}% 正解選択肢の番号
    \texttt{\the\wordnum:} \MaruKata{\answernum}% 正解の記号を出力
    \qquad
  \ifnum\wordcount>\wordnum\repeat
}
\makeatother

\begin{document}

\section*{2016年センター英語 第2問B 問1}

Hotel clerk: Good evening, Mr.~and Mrs.~Gomez. How can I help you?\par
Mrs.~Gomez: Well, \語順整序{{we're}{wondering}{if}{you}{could}{tell}} us how to get to the theater.

\語群出力
\解答出力

\section*{2016年センター英語 第2問B 問2}

Student: Excuse me. I'd like to know what we will be discussing in next week's seminar.\par
Professor: I haven't decided yet, so \語順整序{{let}{me}{send}{you}{the details}{by}} email.

\語群出力
\解答出力

\section*{2016年センター英語 第2問B 問3}

Interviewer: How did you change after becoming the head of such a large company?\par
President: I \語順整序{{came to}{realize}{the}{need}{to}{manage}} my time more effectively.

\語群出力
\解答出力

\end{document}

【出力例】

コンパイルごとに毎回語群がランダムにシャッフルされ,それに応じた正解の記号列が出力されます。

f:id:doraTeX:20190117160851p:plain