algo

Just a bunch of algorithm implementations
Log | Files | Refs | README

commit bcc45b6749fa2b252a36570115e473094499a107
parent 3e07fb9d0605209c1215aa2b87e2e6b0bac0dfa3
Author: zerous Naveen Narayanan <zerous@nocebo.space>
Date:   Sun, 10 Mar 2019 02:02:39 +0100

A couple of Codility tasks

Diffstat:
Ad.c | 43+++++++++++++++++++++++++++++++++++++++++++
Agrq.c | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amat.c | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ampt.c | 33+++++++++++++++++++++++++++++++++
Apc.c | 31+++++++++++++++++++++++++++++++
5 files changed, 242 insertions(+), 0 deletions(-)

diff --git a/d.c b/d.c @@ -0,0 +1,43 @@ +#include <stdio.h> +#include <stdlib.h> +/* Distinct */ +int +comp(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} + +int +solution(int *A, int N) +{ + int prev, res; + + prev = 0; + res = 0; + + qsort(A, N, sizeof(int), comp); + + for (int i = 0; i < N; i++) { + if (i == 0) { + prev = A[i]; + res++; + } + else if (A[i] != prev) { + res++; + prev = A[i]; + } + } + + return res; +} + +int +main(void) +{ + int a[] = {2, 1, 1, 2, 3, 1}; + + printf("%d", solution(a, 6)); + + return 0; +} + diff --git a/grq.c b/grq.c @@ -0,0 +1,75 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +/* GenomiRangeQuery */ +typedef struct Results Results; +struct Results { + int *A; + int M; +}; + +Results solution(char *, int *, int *, int); + +int +main(void) +{ + char s[] = "CAGCCTA"; + int P[] = {2, 5, 0}; + int Q[] = {4, 5, 6}; + int M = 3; + + Results res = solution(s, P, Q, M); + for (int i = 0; i < res.M; i++) + printf("%d", res.A[i]); + + free(res.A); + + return 0; +} + +Results +solution(char *S, int P[], int Q[], int M) +{ + Results r; + + r.A = malloc(M * sizeof(int)); + for (int i = 0; i < M; i++) + r.A[i] = 4; + r.M = M; + + for (int i = 0; i < M; i++) { + int p = P[i]; + int q = Q[i]; + int t; + + for (int j = p; j <= q; j++) { + switch (S[j]) { + case 'A': + t = 1; + if (r.A[i] > t) + r.A[i] = t; + break; + case 'C': + t = 2; + if (r.A[i] > t) + r.A[i] = t; + break; + case 'G': + t = 3; + if (r.A[i] > t) + r.A[i] = t; + break; + case 'T': + t = 4; + if (r.A[i] > t) + r.A[i] = t; + break; + default: + printf("wrong sequence\n"); + return r; + } + } + } + + return r; +} diff --git a/mat.c b/mat.c @@ -0,0 +1,60 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +/* MinAvgTwoSlice */ + +int solution(int *, int); + +int +main(void) +{ + int A[] = {4, 2, 2, 5, 1, 5, 8}; + + printf("%d\n", solution(A, 7)); + + return 0; +} + +int +solution(int A[], int N) +{ + int nelem, res; + double *R = malloc(N * sizeof(double)); + double k, t; + for (int i = 0; i < N; i++) + R[i] = 0; + + for (int i = 0; i < N; i++) + { + t = A[i]; + nelem = 1; + for (int j = i+1; j < N; j++) + { + t += A[j]; + ++nelem; + k = (double) t/nelem; + if (j == i+1 || R[i] > k) { + R[i] = k; + } + } + } + + for (int i = 0; i < (N - 1); i++) { + if (i == 0) { + k = R[i]; + res = i; + } + else if (R[i] < k) { + k = R[i]; + res = i; + } + else if (R[i] == k) { + if (i < res) + res = i; + } + } + + free(R); + + return res; +} diff --git a/mpt.c b/mpt.c @@ -0,0 +1,33 @@ +#include <stdio.h> + +int solution(int *, int N); + +int +main(void) +{ + int A[] = {-3, 1, 2, -2, 5, 6}; + + printf("%d", solution(A, 6)); + + return 0; +} + +int +solution(int *A, int N) +{ + int res; + + res = A[0] * A[1] * A[2]; + for (int i = 0; i < N; i++) { + for (int j = i+1; j < N; j++) { + for (int k = j+1; k < N; k++) { + if ((A[i] * A[j] * A[k]) > res) { + res = (A[i] * A[j] * A[k]); + } + } + } + } + + return res; +} + diff --git a/pc.c b/pc.c @@ -0,0 +1,31 @@ +#include <stdio.h> +/* codility lessons - passing cars */ + +int solution(int A[], int N); + +int +main(void) +{ + int a[] = {0, 1, 0, 0, 1, 1, 1, 0, 1}; + + printf("%d\n", solution(a, 9)); + + return 0; +} + +int +solution(int A[], int N) +{ + int pc; + + pc = 0; + + for (int i = 0; i < N; i++) + for(int j = i+1; j < N; j++) + if (A[i] == 0 && A[j] == 1) { + pc++; + if (pc > 1000000000) + return -1; + } + return pc; +}