Skip to content

Instantly share code, notes, and snippets.

@theoremoon

theoremoon/app.d Secret

Last active May 17, 2019 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theoremoon/6661101f612cc612d29f48de6a0f4745 to your computer and use it in GitHub Desktop.
Save theoremoon/6661101f612cc612d29f48de6a0f4745 to your computer and use it in GitHub Desktop.
Hatena Summer Intern 2019 application quiz
import std.stdio, std.string, std.json, std.ascii, std.algorithm;
string rotN(string s, int n)
in {
assert (0 <= n && n <= 26);
}
out (result) {
assert(result.length == s.length);
}
do {
const auto N = uppercase.length;
char[] r = new char[](s.length);
foreach (i, c; s) {
if (uppercase.canFind(c)) {
r[i] = (c - 'A' + n) % N + 'A';
} else if (lowercase.canFind(c)) {
r[i] = (c - 'a' + n) % N + 'a';
} else {
r[i] = c;
}
}
return r.idup;
}
unittest {
import std.exception;
import core.exception: AssertError;
auto s = "Hello world ABC123 !\"#$%&'()~'";
assert(s == s.rotN(0).rotN(26-0));
assert(s == s.rotN(1).rotN(26-1));
assert(s == s.rotN(13).rotN(26-13));
assert(s == s.rotN(14).rotN(26-14));
assert(s == s.rotN(15).rotN(26-15));
assert(s == s.rotN(25).rotN(26-25));
assert(s == s.rotN(26));
assert("" == "".rotN(13));
assertThrown!AssertError(s.rotN(-1));
assertThrown!AssertError(s.rotN(27));
assertThrown!AssertError(s.rotN(100));
}
void main()
{
auto input = readln.strip;
auto json = JSONValue();
json["github"] = "theoldmoon0602";
json["twitter"] = "theoldmoon0602";
json["homepage"] = "https://furutsuki.hatenablog.com/";
json["answer"] = rotN(input, 13);
writeln(json.toPrettyString(JSONOptions.doNotEscapeSlashes));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment