]> git.zndr.dk Git - codility.git/commitdiff
Initial commit
authorJannik ZANDER <jzander@grundfos.com>
Thu, 3 Nov 2016 22:46:20 +0000 (23:46 +0100)
committerJannik ZANDER <jzander@grundfos.com>
Thu, 3 Nov 2016 22:46:20 +0000 (23:46 +0100)
26 files changed:
CMakeLists.txt [new file with mode: 0644]
googletest [new submodule]
solution1/CMakeLists.txt [new file with mode: 0644]
solution1/solution.c [new file with mode: 0644]
solution1/solution.h [new file with mode: 0644]
solution1/unittest.cpp [new file with mode: 0644]
solution2/CMakeLists.txt [new file with mode: 0644]
solution2/solution.c [new file with mode: 0644]
solution2/solution.h [new file with mode: 0644]
solution2/unittest.cpp [new file with mode: 0644]
solution3/CMakeLists.txt [new file with mode: 0644]
solution3/solution.c [new file with mode: 0644]
solution3/solution.h [new file with mode: 0644]
solution3/unittest.cpp [new file with mode: 0644]
solution4/CMakeLists.txt [new file with mode: 0644]
solution4/solution.c [new file with mode: 0644]
solution4/solution.h [new file with mode: 0644]
solution4/unitTest.cpp [new file with mode: 0644]
solution5/CMakeLists.txt [new file with mode: 0644]
solution5/solution.c [new file with mode: 0644]
solution5/solution.h [new file with mode: 0644]
solution5/unitTest.cpp [new file with mode: 0644]
solution6/CMakeLists.txt [new file with mode: 0644]
solution6/solution.c [new file with mode: 0644]
solution6/solution.h [new file with mode: 0644]
solution6/unittest.cpp [new file with mode: 0644]

diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644 (file)
index 0000000..be43dad
--- /dev/null
@@ -0,0 +1,22 @@
+cmake_minimum_required(VERSION 2.6)
+project(basic_test)
+
+################################
+# GTest
+################################
+# Prevent GoogleTest from overriding our compiler/linker options
+# when building with Visual Studio
+set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
+add_subdirectory(googletest)
+enable_testing()
+include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
+################################
+# Unit Tests
+################################
+# Add test cpp file
+add_subdirectory( solution1 )
+add_subdirectory( solution2 )
+add_subdirectory( solution3 )
+add_subdirectory( solution4 )
+add_subdirectory( solution5 )
+add_subdirectory( solution6 )
diff --git a/googletest b/googletest
new file mode 160000 (submodule)
index 0000000..3447fc3
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit 3447fc31b4eea1fbcb86fa0e2f5d9ed9f38776bf
diff --git a/solution1/CMakeLists.txt b/solution1/CMakeLists.txt
new file mode 100644 (file)
index 0000000..dd8e229
--- /dev/null
@@ -0,0 +1,5 @@
+cmake_minimum_required (VERSION 2.6)
+add_executable(solution1 solution.c solution.h unittest.cpp)
+add_test(solution1 solution1)
+target_link_libraries(solution1 gtest_main)
+add_custom_command(TARGET solution1 POST_BUILD COMMAND solution1)
diff --git a/solution1/solution.c b/solution1/solution.c
new file mode 100644 (file)
index 0000000..1bdf3d0
--- /dev/null
@@ -0,0 +1,16 @@
+#include "solution.h"
+
+int solution(int A, int B)
+{
+  int i;
+  int count = 0;
+  for (i = 0; i <= 100; i++)
+  {
+    if (A <= i*i && i*i <= B)
+    {
+      count++;
+    }
+  }
+  return count;
+}
+
diff --git a/solution1/solution.h b/solution1/solution.h
new file mode 100644 (file)
index 0000000..df89de0
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef SOLUTION_H_
+#define SOLUTION_H_
+
+
+int solution(int A, int B);
+
+#endif // SOLUTION_H_
diff --git a/solution1/unittest.cpp b/solution1/unittest.cpp
new file mode 100644 (file)
index 0000000..53fcd66
--- /dev/null
@@ -0,0 +1,11 @@
+#include "gtest/gtest.h"
+extern "C" {
+#include "solution.h"
+}
+
+
+TEST(SolutionTestSuite, returnCorrectResult)
+{
+  ASSERT_EQ(3, solution(4,17));
+  ASSERT_EQ(5, solution(-4,17));
+}
diff --git a/solution2/CMakeLists.txt b/solution2/CMakeLists.txt
new file mode 100644 (file)
index 0000000..b8b6dfb
--- /dev/null
@@ -0,0 +1,5 @@
+cmake_minimum_required (VERSION 2.6)
+add_executable(solution2 solution.c solution.h unittest.cpp)
+add_test(solution2 solution2)
+target_link_libraries(solution2 gtest_main)
+add_custom_command(TARGET solution2 POST_BUILD COMMAND solution2)
diff --git a/solution2/solution.c b/solution2/solution.c
new file mode 100644 (file)
index 0000000..7301690
--- /dev/null
@@ -0,0 +1,60 @@
+#include "solution.h"
+
+int solution_with_bug(int *A, int A_length)
+{
+    int n = A_length;
+    int result = 0;
+    int i;
+    for (i = 0; i < n - 1; i++) {
+        if (A[i] == A[i + 1])
+            result = result + 1;
+    }
+    int r = 0;
+    for (i = 0; i < n; i++) {
+        int count = 0;
+        if (i > 0) {
+            if (A[i - 1] != A[i])
+                count = count + 1;
+            else
+                count = count - 1;
+        }
+        if (i < n - 1) {
+            if (A[i + 1] != A[i])
+                count = count + 1;
+            else
+                count = count - 1;
+        }
+        if (count > r)
+            r = count;
+    }
+    return result + r;
+}
+
+int solution(int *A, int A_length) {
+    int n = A_length;
+    int result = 1;
+    int i;
+    for (i = 0; i < n - 1; i++) {
+      if (A[i] == A[i + 1] && A[i] == 1)
+        result = result + 1;
+    }
+    int r = 0;
+    for (i = 0; i < n; i++) {
+        int count = 0;
+        if (i > 0) {
+            if (A[i - 1] != A[i])
+                count = count + 1;
+            else
+                count = count - 1;
+        }
+        if (i < n - 1) {
+            if (A[i + 1] != A[i])
+                count = count + 1;
+            else
+                count = count - 1;
+        }
+        if (count > r)
+            r = count;
+    }
+    return result + r;
+}
diff --git a/solution2/solution.h b/solution2/solution.h
new file mode 100644 (file)
index 0000000..b8ca84b
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef SOLUTION_H_
+#define SOLUTION_H_
+
+int solution(int *A, int A_length);
+
+#endif // SOLUTION_H_
diff --git a/solution2/unittest.cpp b/solution2/unittest.cpp
new file mode 100644 (file)
index 0000000..0f11933
--- /dev/null
@@ -0,0 +1,65 @@
+#include "gtest/gtest.h"
+extern "C" {
+#include "solution.h"
+}
+
+
+TEST(SolutionTestSuite, returnCorrectResult)
+{
+  int A[1000];
+
+  A[0] = 1;
+  A[1] = 1;
+  A[2] = 0;
+  A[3] = 1;
+  A[4] = 0;
+  A[5] = 0;
+
+  ASSERT_EQ(4, solution(A,6));
+
+  A[0] = 1;
+  A[1] = 1;
+  A[2] = 0;
+  A[3] = 1;
+  A[4] = 0;
+  A[5] = 0;
+  A[6] = 0;
+#if 0
+  ASSERT_EQ(5, solution(A,7));
+
+   A[0] = 1;
+   A[1] = 1;
+
+  ASSERT_EQ(2, solution(A,2));
+
+   A[0] = 1;
+   A[1] = 1;
+   A[2] = 0;
+
+  ASSERT_EQ(3, solution(A,3));
+
+   A[0] = 1;
+   A[1] = 1;
+   A[2] = 0;
+   A[3] = 1;
+
+  ASSERT_EQ(4, solution(A,4));
+
+  A[0] = 1;
+  A[1] = 1;
+  A[2] = 0;
+  A[3] = 1;
+  A[4] = 0;
+
+  ASSERT_EQ(4, solution(A,5));
+
+  A[0] = 1;
+  A[1] = 1;
+  A[2] = 0;
+  A[3] = 1;
+  A[4] = 0;
+  A[5] = 0;
+
+  ASSERT_EQ(4, solution(A,6));
+#endif
+}
diff --git a/solution3/CMakeLists.txt b/solution3/CMakeLists.txt
new file mode 100644 (file)
index 0000000..5dff733
--- /dev/null
@@ -0,0 +1,5 @@
+cmake_minimum_required (VERSION 2.6)
+add_executable(solution3 solution.c solution.h unittest.cpp)
+add_test(solution1 solution3)
+target_link_libraries(solution3 gtest_main)
+add_custom_command(TARGET solution3 POST_BUILD COMMAND solution3)
diff --git a/solution3/solution.c b/solution3/solution.c
new file mode 100644 (file)
index 0000000..085b3d6
--- /dev/null
@@ -0,0 +1,40 @@
+#include "solution.h"
+
+struct Results solution(int A[], int M)
+{
+    struct Results result;
+    int i;
+    int X = 0;
+    int sign = 0;
+    for (i = 0; i < M; i++)
+    {
+      if (i & 0x1)
+      {
+        X -= A[i]*(1<<i);
+      }
+      else
+      {
+        X += A[i]*(1<<i);
+      }
+    }
+    result.N = X + 1;
+    result.B = A;
+
+    for (i = 0; i <= 100; i++)
+    {
+      if (i & 0x1)
+      {
+        sign = -1;
+      }
+      else
+      {
+        sign = 1;
+      }
+      if (X + sign*(1 << i))
+      {
+      }
+    }
+
+    return result;
+}
+
diff --git a/solution3/solution.h b/solution3/solution.h
new file mode 100644 (file)
index 0000000..3a670e7
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef SOLUTION_H_
+#define SOLUTION_H_
+
+
+struct Results {
+  int * B;
+  int N;
+};
+
+struct Results solution(int A[], int M);
+
+#endif // SOLUTION_H_
diff --git a/solution3/unittest.cpp b/solution3/unittest.cpp
new file mode 100644 (file)
index 0000000..82f3e2c
--- /dev/null
@@ -0,0 +1,22 @@
+#include "gtest/gtest.h"
+extern "C" {
+#include "solution.h"
+}
+TEST(SolutionTestSuite, returnCorrectResult)
+{
+  struct Results res;
+  int A[1000];
+  A[0] = 1;
+  A[1] = 0;
+  A[2] = 1;
+  // X = 5
+
+  res = solution(A, 3);
+
+  ASSERT_EQ(6, res.N);
+//  ASSERT_EQ(0, res.B[0]);
+//  ASSERT_EQ(1, res.B[1]);
+//  ASSERT_EQ(0, res.B[3]);
+//  ASSERT_EQ(1, res.B[4]);
+//  ASSERT_EQ(1, res.B[5]);
+}
diff --git a/solution4/CMakeLists.txt b/solution4/CMakeLists.txt
new file mode 100644 (file)
index 0000000..ed249d4
--- /dev/null
@@ -0,0 +1,5 @@
+cmake_minimum_required (VERSION 2.6)
+add_executable(solution4 solution.c solution.h unittest.cpp)
+add_test(solution4 solution4)
+target_link_libraries(solution4 gtest_main)
+add_custom_command(TARGET solution4 POST_BUILD COMMAND solution4)
diff --git a/solution4/solution.c b/solution4/solution.c
new file mode 100644 (file)
index 0000000..86c5c5e
--- /dev/null
@@ -0,0 +1,33 @@
+#include "solution.h"
+
+int solution(int A, int B)
+{
+  int res = -1;
+  int i, j;
+  int digitsA[11] = { 0 };
+  int digitsB[11] = { 0 };
+
+  for (i = 0; i < 10; i++)
+  {
+    digitsA[i] = A % 10;
+    A = A / 10;
+    digitsB[i] = B % 10;
+    B = B / 10;
+  }
+  j = 0;
+  for (i = 0; i < 10;i++)
+  {
+    if (digitsA[j] == digitsB[i] && digitsB[i] != 0)
+    {
+      j++;
+      res = i;
+    }
+    if (digitsA[j] == 0 && digitsB[i] == 0)
+    {
+      return res - j;
+    }
+  }
+
+  return res;
+}
+
diff --git a/solution4/solution.h b/solution4/solution.h
new file mode 100644 (file)
index 0000000..df89de0
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef SOLUTION_H_
+#define SOLUTION_H_
+
+
+int solution(int A, int B);
+
+#endif // SOLUTION_H_
diff --git a/solution4/unitTest.cpp b/solution4/unitTest.cpp
new file mode 100644 (file)
index 0000000..d43149b
--- /dev/null
@@ -0,0 +1,13 @@
+#include "gtest/gtest.h"
+extern "C" {
+#include "solution.h"
+}
+
+TEST(SolutionTest, SomePosition)
+{
+  int A = 53;
+  int B = 1953786;
+
+  EXPECT_EQ(2, solution(A,B));
+}
+
diff --git a/solution5/CMakeLists.txt b/solution5/CMakeLists.txt
new file mode 100644 (file)
index 0000000..c24c5d8
--- /dev/null
@@ -0,0 +1,5 @@
+cmake_minimum_required (VERSION 2.6)
+add_executable(solution5 solution.c solution.h unittest.cpp)
+add_test(solution5 solution5)
+target_link_libraries(solution5 gtest_main)
+add_custom_command(TARGET solution5 POST_BUILD COMMAND solution5)
diff --git a/solution5/solution.c b/solution5/solution.c
new file mode 100644 (file)
index 0000000..41c46fa
--- /dev/null
@@ -0,0 +1,54 @@
+#include "solution.h"
+
+int solution(int A, int B)
+{
+  int res = -1;
+  int i, j, k, l;
+  int digitsA[10] = { 0 };
+  int digitsB[10] = { 0 };
+  int digitsC[10] = { 0 };
+
+  for (i = 0; i < 9; i++)
+  {
+    digitsA[9-i] = A % 10;
+    A = A / 10;
+    digitsB[9-i] = B % 10;
+    B = B / 10;
+  }
+  j = 9;
+  k = 9;
+  for (i = 0; i < 9; i++)
+  {
+    if (digitsA[i] != 0 && j == 9)
+    {
+      j = i;
+    }
+    if (digitsB[i] != 0 && k == 9)
+    {
+      k = i;
+    }
+  }
+  l = 0;
+  for (i = 0; i < 9; i++)
+  {
+    if (j < 10)
+    {
+      digitsC[l++] = digitsA[j];
+      j++;
+    }
+    if (k < 10)
+    {
+      digitsC[l++] = digitsB[k];
+      k++;
+    }
+  }
+  res = 0;
+  for (i = 0; i < l; i++)
+  {
+    res *= 10;
+    res += digitsC[i];
+  }
+
+  return res;
+}
+
diff --git a/solution5/solution.h b/solution5/solution.h
new file mode 100644 (file)
index 0000000..df89de0
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef SOLUTION_H_
+#define SOLUTION_H_
+
+
+int solution(int A, int B);
+
+#endif // SOLUTION_H_
diff --git a/solution5/unitTest.cpp b/solution5/unitTest.cpp
new file mode 100644 (file)
index 0000000..5ada3e7
--- /dev/null
@@ -0,0 +1,13 @@
+#include "gtest/gtest.h"
+extern "C" {
+#include "solution.h"
+}
+
+TEST(SolutionTest, ZipSome)
+{
+  EXPECT_EQ(1526, solution(12,56));
+  EXPECT_EQ(5162, solution(56,12));
+  EXPECT_EQ(16273845, solution(12345,678));
+  EXPECT_EQ(16273890, solution(123,67890));
+  EXPECT_EQ(10234, solution(1234,0));
+}
diff --git a/solution6/CMakeLists.txt b/solution6/CMakeLists.txt
new file mode 100644 (file)
index 0000000..97ae236
--- /dev/null
@@ -0,0 +1,5 @@
+cmake_minimum_required (VERSION 2.6)
+add_executable(solution6 unittest.cpp solution.c solution.h)
+add_test(solution6 solution6)
+target_link_libraries(solution6 gtest_main)
+add_custom_command(TARGET solution6 POST_BUILD COMMAND solution6)
diff --git a/solution6/solution.c b/solution6/solution.c
new file mode 100644 (file)
index 0000000..6bd0100
--- /dev/null
@@ -0,0 +1,62 @@
+#include "Solution.h"
+
+int solution(char *S)
+{
+  int stack[10];
+  int top = 0;
+  int i;
+  char *ch=S;
+  int num1;
+  int num2;
+  int digits[8];
+  int num_digits = 0;
+
+  while (*ch != 0)
+  {
+    if (*ch >= '0' && *ch <= '9')
+    {
+      digits[num_digits++]= *ch - '0';
+    }
+    else if (*ch == ' ' && num_digits)
+    {
+      num1 = 0;
+      for (i = 0; i < num_digits; i++)
+      {
+        num1 *= 10;
+        num1 += digits[i];
+      }
+      num_digits = 0;
+      stack[top++] = num1;
+    }
+    else if (*ch == 'D' && *(ch+1)=='U' && *(ch+2)=='P')
+    {
+      if (top < 1) return -1;
+      num1 = stack[top-1];
+      stack[top++] = num1;
+    }
+    else if (*ch == 'P' && *(ch+1)=='O' && *(ch+2)=='P')
+    {
+      if (top < 1) return -1;
+      num1 = stack[--top];
+    }
+    else if (*ch=='+')
+    {
+      if (top < 2) return -1;
+      num1 = stack[--top];
+      num2 = stack[--top];
+      stack[top++] = num1 + num2;
+    }
+    else if (*ch=='-')
+    {
+      if (top < 2) return -1;
+      num1 = stack[--top];
+      num2 = stack[--top];
+      if (num1 - num2 < 0) return -1;
+      stack[top++] = num1 - num2;
+    }
+    ch++;
+  }
+
+
+  return stack[top-1];
+}
diff --git a/solution6/solution.h b/solution6/solution.h
new file mode 100644 (file)
index 0000000..84ad726
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef SOLUTION_H_
+#define SOLUTION_H_
+
+int solution(char *S);
+
+#endif // SOLUTION_H_
diff --git a/solution6/unittest.cpp b/solution6/unittest.cpp
new file mode 100644 (file)
index 0000000..8063d85
--- /dev/null
@@ -0,0 +1,14 @@
+#include "gtest/gtest.h"
+extern "C" {
+#include "solution.h"
+}
+
+TEST(SolutionTest, SomeOperations)
+{
+  char S1[]= "13 DUP 4 POP 5 DUP + DUP + -";
+  char S2[] = "5 6 + -";
+//  char S2[] ="3 DUP 5 - -";
+
+  EXPECT_EQ(7, solution(S1));
+  EXPECT_EQ(-1, solution(S2));
+}