Skip to content

Instantly share code, notes, and snippets.

@physacco
Created December 22, 2015 19:52
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 physacco/afcbf944fc98b019fd12 to your computer and use it in GitHub Desktop.
Save physacco/afcbf944fc98b019fd12 to your computer and use it in GitHub Desktop.
My solutions to paiza POH 7 in D language
import std.stdio;
long factorial(int n) {
long fac = 1;
foreach (i; 1..n+1) {
fac *= i;
}
return fac;
}
void main() {
int n;
readf(" %d", &n);
writeln(factorial(n));
}
import std.stdio;
void read_square_matrix(int n, ref int[][] mat) {
foreach (i; 0..n) {
int[] row;
foreach (j; 0..n) {
int e;
readf(" %d", &e);
row ~= e;
}
mat ~= row;
}
}
bool isMatch(const int[][] nn, int m, const int[][] mm, int y, int x) {
foreach (i; 0..m) {
foreach (j; 0..m) {
if (nn[y + i][x + j] != mm[i][j]) {
return false;
}
}
}
return true;
}
int[2] search(int n, const int[][] nn, int m, const int[][] mm) {
int e = n - m + 1;
foreach (y; 0..e) {
foreach (x; 0..e) {
if (isMatch(nn, m, mm, y, x)) {
return [y, x];
}
}
}
return [-1, -1];
}
void main() {
int n;
readf(" %d", &n);
int[][] nn;
read_square_matrix(n, nn);
int m;
readf(" %d", &m);
int[][] mm;
read_square_matrix(m, mm);
int[2] pos = search(n, nn, m, mm);
writeln(pos[0], " ", pos[1]);
}
import std.stdio;
import std.algorithm;
int minInterval(const int[] a, int max) {
int min = max;
foreach (i; 1..a.length) {
int interval = a[i] - a[i - 1];
if (interval < min) {
min = interval;
}
}
return min;
}
void main() {
int x, y, z, n;
readf(" %d %d %d %d", &x, &y, &z, &n);
int[] a1 = [0, x];
int[] a2 = [0, y];
foreach (_; 0..n) {
int a, b;
readf(" %d %d", &a, &b);
if (a == 0) {
a1 ~= b;
} else {
a2 ~= b;
}
}
sort(a1);
sort(a2);
int min1 = minInterval(a1, x);
int min2 = minInterval(a2, y);
int minCube = min1 * min2 * z;
writeln(minCube);
}
import std.stdio;
import std.bigint;
//# rebase_n(144, 2) => [9, 4]
// i.e. 144 = 9 * (2 ** 4)
int[2] rebase_n(int x, int n) {
int[2] res = [x, 0];
while (res[0] % n == 0) {
res[1] += 1;
res[0] /= n;
}
return res;
}
// Return [c, b, a] that x = c * (5 ** b) * (2 ** a)
int[3] rebase_5_2(int x) {
int[2] a1 = rebase_n(x, 2);
int[2] a2 = rebase_n(a1[0], 5);
return [a2[0], a2[1], a1[1]];
}
// pow(b, k)
BigInt pow(int b, int k) {
BigInt res = 1;
foreach (_; 0..k) {
res *= b;
}
return res;
}
long calc(int n, int k) {
BigInt mod = pow(10, k);
BigInt[3] acc = [BigInt(1), BigInt(0), BigInt(0)];
foreach (i; 1..n+1) {
int[3] a1 = rebase_5_2(i);
acc[0] = (acc[0] * a1[0]) % mod;
acc[1] += a1[1];
acc[2] += a1[2];
}
int twos = (acc[2] - acc[1]).toInt();
foreach (i; 0..twos) {
acc[0] = (acc[0] * 2) % mod;
}
return acc[0].toLong();
}
void main() {
int n;
readf(" %d", &n);
writeln(calc(n, 9));
}
import std.stdio;
import std.conv;
import std.string;
void main() {
int n;
readf(" %d", &n);
int[] a1, a2;
a1.length = n;
a2.length = n;
int m1;
readf(" %d", &m1);
foreach (i; 0..m1) {
int e;
readf(" %d", &e);
a1[e - 1] = 1;
}
int m2;
readf(" %d", &m2);
foreach (i; 0..m2) {
int e;
readf(" %d", &e);
a2[e - 1] = 1;
}
int[] a3;
foreach (i; 0..n) {
if (a2[i] == 1 && a1[i] != 1) {
a3 ~= i + 1;
}
}
if (a3.length > 0) {
writeln(join(to!(string[])(a3), " "));
} else {
writeln("None");
}
}
import std.stdio;
void main() {
int n, m, r;
readf(" %d", &n);
readf(" %d", &m);
r = (m % n == 0) ? (m / n) : (m / n + 1);
char[] output;
foreach (i; 0..r) {
if (i % 2 == 0) {
foreach (_; 0..n) {
output ~= 'R';
}
} else {
foreach (_; 0..n) {
output ~= 'W';
}
}
}
writeln(output[0..m]);
}
import std.stdio;
void main() {
int count;
readf(" %d", &count);
while (count > 0) {
write("Ann");
--count;
}
}
import std.stdio;
void main() {
int a, b;
readf(" %d %d", &a, &b);
writeln(a + b);
}
import std.stdio;
import std.algorithm;
void main() {
int yes = 0, no = 0;
foreach (_; 0..5) {
auto line = readln();
line.startsWith("yes") ? ++yes : ++no;
}
if (yes > no) {
writeln("yes");
} else {
writeln("no");
}
}
import std.stdio;
void main() {
int n;
readf(" %d", &n);
foreach_reverse (i; 0..n+1) {
auto format = (i == 0) ? "%d!!\n" :"%d\n";
writef(format, i);
}
}
import std.stdio;
void main() {
double a, b, c, d, e, f;
readf(" %f %f", &a, &b);
readf(" %f %f", &c, &d);
e = a / b;
f = c / d;
(e > f) ? writeln(1) : writeln(2);
}
import std.stdio;
import std.conv;
import std.string;
import std.algorithm;
void main() {
int n = to!int(strip(readln()));
string[] words;
foreach (_; 0..n) {
words ~= strip(readln());
}
writeln(joiner(words, "_"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment