LeetCode 520. Detect Capital (LaTeX)

Given a word, you need to judge whether the usage of capitals in it is right or not.

We define the usage of capitals in a word to be right when one of the following cases holds:

  1. All letters in this word are capitals, like “USA”.
  2. All letters in this word are not capitals, like “leetcode”.
  3. Only the first letter in this word is capital, like “Google”.

Otherwise, we define that this word doesn’t use capitals in a right way.

Example

Example 1:

Input: "USA"
Output: True

Example 2:

Input: "FlaG"
Output: False

Notes

The input will be a non-empty word consisting of uppercase and lowercase latin letters.

Solution

\documentclass{article}
\usepackage[a3paper, landscape]{geometry}
\usepackage{newtxtext, newtxmath}
\usepackage{amsmath}
\usepackage{datetime2}
\usepackage{expl3}

\begin{document}

\ExplSyntaxOn

\bool_new:N \g_result_bool

\cs_new:Npn \get_charcode:n #1 {
    \int_eval:n {`#1}
}

\cs_new:Npn \is_upper:n #1 {
    \int_compare:nTF {64 < \get_charcode:n{#1} < 91} {\c_true_bool} {\c_false_bool} 
}

\cs_new:Npn \is_lower:n #1 {
    \int_compare:nTF {96 < \get_charcode:n{#1} < 123} {\c_true_bool} {\c_false_bool} 
}

\cs_new:Npn \detect_cap_use:n #1 {
    \str_gset:Nn \g_tmpa_str {#1}
    \par input:~\str_use:N \g_tmpa_str
    \int_compare:nNnTF {\str_count:N \g_tmpa_str} = {1} {
        \par true~because~length~is~1
        \bool_gset_true:N \g_result_bool
    } {
        % get tail
        \str_gset:Nx \g_tmpb_str {\str_tail:N \g_tmpa_str}
        \tl_gclear:N \g_tmpa_tl
        % see if tail is all lower
        \str_map_variable:NNn \g_tmpb_str \l_tmpa_str {
            \tl_gput_right:Nx \g_tmpa_tl {\exp_args:NV \is_lower:n \l_tmpa_str}
        }
        \bool_if:nTF {\exp_args:NV \bool_lazy_all_p:n \g_tmpa_tl} {
            \par true~because~tail~is~all~lower
            \bool_gset_true:N \g_result_bool
        } {
            % see if entire string is all upper
            \tl_gclear:N \g_tmpa_tl
            \str_map_variable:NNn \g_tmpa_str \l_tmpa_str {
                \tl_gput_right:Nx \g_tmpa_tl {\exp_args:NV \is_upper:n \l_tmpa_str}
            }
            \bool_if:nTF {\exp_args:NV \bool_lazy_all_p:n \g_tmpa_tl} {
                \par true~because~entire~string~is~upper
                \bool_gset_true:N \g_result_bool
            } {
                \par invalid~case!
                \bool_gset_false:N \g_result_bool
            }
        }
    }
}

\cs_new:Npn \bool_use:N #1 {
    \bool_if:nTF {#1} {true} {false}
}

\detect_cap_use:n {USA}
\par \textbf{result: ~\bool_use:N \g_result_bool}
\detect_cap_use:n {FlaG}
\par \textbf{result: ~\bool_use:N \g_result_bool}
\detect_cap_use:n {Flag}
\par \textbf{result: ~\bool_use:N \g_result_bool}
\detect_cap_use:n {abcde}
\par \textbf{result: ~\bool_use:N \g_result_bool}

\ExplSyntaxOff

\par\DTMNow

\end{document} 

Output

input: USA
true because entire string is upper
result: true
input: FlaG
invalid case!
result: false
input: Flag
true because tail is all lower
result: true
input: abcde
true because tail is all lower
result: true
2020-06-14 03:12:16-04:00