Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Python Challenge -- Level 3 -- Native Solution

Code

import tango.io.Stdout;

void main () {
        char[] text = "";

        assert (text.length >= 7);

        bool islower (char letter) {
                return letter >= 'a' && letter <= 'z';
        }

        bool isupper (char letter) {
                return letter >= 'A' && letter <= 'Z';
        }

        for (size_t i = 3; i + 3 < text.length; i++)
                if (islower(text[i]) && isupper(text[i - 1]) && isupper(text[i - 2]) &&
                isupper(text[i - 3]) && isupper(text[i + 1]) && isupper(text[i + 2]) &&
                isupper(text[i + 3]))
                        if (i != 3 && isupper(text[i - 4]))
                                continue;
                        else if (i != text.length - 3 && isupper(text[i + 4]))
                                continue;
                        else
                                Stdout.print("" ~ text[i]);
        Stdout.flush();
}

Explanation

        char[] text = "";

The source text from the challenge page should be put between the quotes.

        assert (text.length >= 7);

The length of the text must be at least 7 to meet the requirements for the challenge.

        bool islower (char letter) {
                return letter >= 'a' && letter <= 'z';
        }

        bool isupper (char letter) {
                return letter >= 'A' && letter <= 'Z';
        }

Nested functions that can be used to check whether a letter is uppercase or lowercase. While these aren't necessary, it reduces the redundant code in the rest of the solution.

        for (size_t i = 3; i + 3 < text.length; i++)

Do the following code only for lines that can have 3 letters on each side. This is necessary to prevent buffer overflows when doing the checks.

                if (islower(text[i]) && isupper(text[i - 1]) && isupper(text[i - 2]) &&
                isupper(text[i - 3]) && isupper(text[i + 1]) && isupper(text[i + 2]) &&
                isupper(text[i + 3]))

Check that the letter is lowercase with at least three uppercase letters on each side.

                        if (i != 3 && isupper(text[i - 4]))
                                continue;
                        else if (i != text.length - 3 && isupper(text[i + 4]))
                                continue;

Check the "exactly three" part of the challenge. The check is not performed if there is no letter there.

                        else
                                Stdout.print("" ~ text[i]);
        Stdout.flush();

Print the character to the screen and flush upon completion.