~saurabhs/binarysearchdotcom

a9769a669b7e0c9be9e58655b25cce39f3c50910 — Saurabh S 1 year, 1 month ago 848deca senpai
Add more solved problems
A ClockAngle/main.cpp => ClockAngle/main.cpp +9 -0
@@ 0,0 1,9 @@
#include <math.h>

int solve(int hour, int minutes) {
  hour %= 12;
  float hr_angle = (hour * 30) + (float)minutes / 2;
  float angle = fabs(hr_angle - (float)(minutes * 6));

  return angle < (360 - angle) ? (int)angle : (int)(360 - angle);
}
\ No newline at end of file

A ClockAngle/tests.cpp => ClockAngle/tests.cpp +17 -0
@@ 0,0 1,17 @@
#define CATCH_CONFIG_MAIN
#include "../catch2/catch.hpp"
#include "main.cpp"

TEST_CASE("Clock Angle", "[solve]") {
  REQUIRE(solve(3, 0) == 90);
  REQUIRE(solve(12, 0) == 0);
  REQUIRE(solve(12, 22) == 121);
  REQUIRE(solve(4, 22) == 1);
  REQUIRE(solve(7, 57) == 103);
  REQUIRE(solve(10, 0) == 60);
  REQUIRE(solve(6, 0) == 180);
  REQUIRE(solve(6, 1) == 174);
  REQUIRE(solve(6, 31) == 10);
  REQUIRE(solve(7, 0) == 150);
  REQUIRE(solve(22, 39) == 85);
}
\ No newline at end of file

A IntegerToRomanNumbers/main.cpp => IntegerToRomanNumbers/main.cpp +49 -0
@@ 0,0 1,49 @@
#include <map>
#include <string>

const std::map<int, std::string> rm = {{0, ""},    {1, "I"},   {5, "V"},
                                       {10, "X"},  {50, "L"},  {100, "C"},
                                       {500, "D"}, {1000, "M"}};

std::string get(int num, int order) {
  if (num == 0)
    return "";
  if (num == 1)
    return rm.at(num * order);
  if (num == 4)
    return rm.at(order) + rm.at(order * 5);
  if (num == 5)
    return rm.at(order * 5);
  if (num == 9)
    return rm.at(order) + rm.at(order * 10);

  const int d = num / 5;
  const int m = num % 5;

  auto repeat = [](std::string str, int count) -> std::string {
    std::string r{};
    for (int i = 0; i < count; i++) {
      r += str;
    }
    return r;
  };

  if (d == 0)
    return repeat(rm.at(order), m);

  return rm.at(5 * order) + repeat(rm.at(order), m);
}

std::string solve(int num) {

  std::string result{};
  int order = 1;
  while (num > 0) {
    const int m = num % 10;
    result = get(m, order) + result;
    order *= 10;
    num = num / 10;
  }

  return result;
}

A IntegerToRomanNumbers/tests.cpp => IntegerToRomanNumbers/tests.cpp +12 -0
@@ 0,0 1,12 @@
#define CATCH_CONFIG_MAIN
#include "../catch2/catch.hpp"
#include "main.cpp"

TEST_CASE("Integer -> Roman", "[solve]") {
  REQUIRE(solve(4) == "IV");
  REQUIRE(solve(12) == "XII");
  REQUIRE(solve(14) == "XIV");
  REQUIRE(solve(225) == "CCXXV");
  REQUIRE(solve(853) == "DCCCLIII");
  REQUIRE(solve(999) == "CMXCIX");
}
\ No newline at end of file

A Products/main.cpp => Products/main.cpp +19 -0
@@ 0,0 1,19 @@
#include <cstdio>
#include <vector>

std::vector<int> solve(std::vector<int> nums) {
  std::vector<int> result{};
  std::vector<int> suffix(nums.size());
  int s = 1;
  for (int len = nums.size(), i = len - 1; i >= 0; i--) {
    s *= nums[i];
    suffix[i] = s;
  }
  int prefix = 1;
  for (int i = 0, len = nums.size(); i < len - 1; i++) {
    result.push_back(prefix * suffix[i + 1]);
    prefix *= nums[i];
  }
  result.push_back(prefix);
  return result;
}

A Products/tests.cpp => Products/tests.cpp +21 -0
@@ 0,0 1,21 @@
#define CATCH_CONFIG_MAIN
#include "../catch2/catch.hpp"
#include "main.cpp"

TEST_CASE("Products 1-5", "[solve]") {
  std::vector<int> input = {1, 2, 3, 4, 5};
  std::vector<int> expected = {120, 60, 40, 30, 24};
  REQUIRE(expected == solve(input));
}

TEST_CASE("Products random", "[solve]") {
  std::vector<int> input = {4, 8, 3, 2, 7};
  std::vector<int> expected = {336, 168, 448, 672, 192};
  REQUIRE(expected == solve(input));
}

TEST_CASE("Products random even elements", "[solve]") {
  std::vector<int> input = {4, 8, 3, 2, 7, 10};
  std::vector<int> expected = {3360, 1680, 4480, 6720, 1920, 1344};
  REQUIRE(expected == solve(input));
}
\ No newline at end of file

A RomanNumbersToInteger/main.cpp => RomanNumbersToInteger/main.cpp +34 -0
@@ 0,0 1,34 @@
#include <map>
#include <string>

int solve(std::string numeral) {
  const std::map<char, int> m = {{'V', 5},   {'X', 10},  {'L', 50},
                                 {'C', 100}, {'D', 500}, {'M', 1000}};

  int result = 0;
  for (int len = numeral.length(), i = len - 1; i >= 0; i--) {
    const char c = numeral[i];
    if (c == 'I') {
      result++;
    } else if (c == 'V' || c == 'X') {
      result += m.at(c);
      if (i > 0 && numeral[i - 1] == 'I') {
        result--;
        i--;
      }
    } else if (c == 'L' || c == 'C') {
      result += m.at(c);
      if (i > 0 && numeral[i - 1] == 'X') {
        result -= 10;
        i--;
      }
    } else if (c == 'D' || c == 'M') {
      result += m.at(c);
      if (i > 0 && numeral[i - 1] == 'C') {
        result -= 100;
        i--;
      }
    }
  }
  return result;
}

A RomanNumbersToInteger/tests.cpp => RomanNumbersToInteger/tests.cpp +13 -0
@@ 0,0 1,13 @@
#define CATCH_CONFIG_MAIN
#include "../catch2/catch.hpp"
#include "main.cpp"

TEST_CASE("Roman -> Integer", "[solve]")
{
	REQUIRE(solve("DCCCLIII") == 853);
	REQUIRE(solve("IV") == 4);
	REQUIRE(solve("XII") == 12);
	REQUIRE(solve("XIV") == 14);
	REQUIRE(solve("CMXCIX") == 999);
	REQUIRE(solve("CCXXV") == 225);
}
\ No newline at end of file

A TheAuditer/main.cpp => TheAuditer/main.cpp +12 -0
@@ 0,0 1,12 @@
#include <stdio.h>
#include <string>

int solve(std::string s) {
  int result = 0;
  int pow = 1;
  for (int len = s.length(), i = len - 1; i >= 0; i--) {
    result += (s[i] - '@') * pow;
    pow *= 26;
  }
  return result;
}

A TheAuditer/tests.cpp => TheAuditer/tests.cpp +14 -0
@@ 0,0 1,14 @@
#define CATCH_CONFIG_MAIN
#include "../catch2/catch.hpp"
#include "main.cpp"

TEST_CASE("The Auditor") {
  REQUIRE(solve("A") == 1);
  REQUIRE(solve("B") == 2);
  REQUIRE(solve("S") == 19);
  REQUIRE(solve("Z") == 26);
  REQUIRE(solve("AA") == 27);
  REQUIRE(solve("AZ") == 52);
  REQUIRE(solve("80") == 80);
  REQUIRE(solve("GRA") == 5201);
}
\ No newline at end of file