11
Introduction
The LaTeX typesetting system uses macros for defining accents.
For example, the letter ê is produced by \hat{e}
.
In this challenge, your task is to implement an ASCII version of this functionality.
Input
Your input is a non-empty string of printable ASCII characters. It will not contain newlines.
Output
Your output is a string consisting of two lines.
The first line contains accents, and the second line the characters they belong to.
It is obtained from the input as follows (A
denotes an arbitrary character):
- Every
\bar{A}
is replaced byA
with_
on top of it. - Every
\dot{A}
is replaced byA
with.
on top of it. - Every
\hat{A}
is replaced byA
with^
on top of it. - For a -10% bonus: every
\tilde{A}
is replaced byA
with~
on top of it. - All other characters have a space above them.
For example, the input
Je suis pr\hat{e}t.
results in the output
^
Je suis pret.
Rules and scoring
You can assume that the characters \{}
only occur in the macros \bar{}
, \dot{}
and \hat{}
(and \tilde{}
if you go for the bonus).
All macro arguments are exact one character long, so \dot{foo}
and \dot{}
will not occur in the input.
The output can be a newline-separated string, or a list/pair of two strings.
Any amount of trailing and preceding whitespace is allowed, as long as the accents are in the correct places.
In particular, if there are no accents, the output can be a single string.
You can write a full program or a function. The lowest byte count (after bonuses) wins, and standard loopholes are disallowed.
Test cases
Without bonus:
Input:
No accents.
Output:
No accents.
Input:
Ch\hat{a}teau
Output:
^
Chateau
Input:
Som\bar{e} \dot{a}cc\hat{e}nts.
Output:
_ . ^
Some accents.
Input:
dot hat\dot{h}a\hat{t}\hat{ }x\bar{x}dot
Output:
. ^^ _
dot hathat xxdot
Input:
\hat{g}Hmi\hat{|}Su5Y(\dot{G}"\bar{$}id4\hat{j}gB\dot{n}#6AX'c\dot{[}\hat{)} 6\hat{[}T~_sR\hat{&}CEB
Output:
^ ^ . _ ^ . .^ ^ ^
gHmi|Su5Y(G"$id4jgBn#6AX'c[) 6[T~_sR&CEB
With bonus:
Input:
Ma\tilde{n}ana
Output:
~
Manana
Input:
\dot{L}Vz\dot{[}|M.\bar{#}0\hat{u}U^y!"\tilde{I} K.\bar{"}\hat{m}dT\tilde{$}F\bar{;}59$,/5\bar{'}K\tilde{v}R \tilde{E}X`
Output:
. . _ ^ ~ _^ ~ _ _ ~ ~
LVz[|M.#0uU^y!"I K."mdT$F;59$,/5'KvR EX`
I started to prototype this in Go but then I realised how much simpler Python would be...
– cat – 2015-11-28T03:36:27.5731Can we assume that each markup entry contains only one char? Or, in other words, is
\bar{foo}
a valid input? – Peter Taylor – 2015-11-28T07:43:06.930@PeterTaylor Yes, every macro argument is exactly one character long. I'll clarify that. – Zgarb – 2015-11-28T15:26:09.453