Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise 5.10 #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
},
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": ["arg1", "arg2"],
"environment": [{ "name": "squid", "value": "clam" }],
"cwd": "${workspaceFolder}"
}
]
}
68 changes: 68 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"files.associations": {
"*.dev": "dockerfile",
"array": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"map": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"regex": "cpp",
"set": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"ostream": "cpp",
"shared_mutex": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"valarray": "cpp",
"bit": "cpp"
}
}
44 changes: 44 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-pthread",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "cppbuild",
"label": "C/C++: cpp build active file",
"command": "/usr/bin/cpp",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": "build",
"detail": "compiler: /usr/bin/cpp"
}
]
}
Binary file added epi_judge_cpp/int_as_array_multiply
Binary file not shown.
28 changes: 27 additions & 1 deletion epi_judge_cpp/int_as_array_multiply.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
#include <vector>
#include <math.h>
#include <algorithm>

#include "test_framework/generic_test.h"
using std::vector;
vector<int> Multiply(vector<int> num1, vector<int> num2) {
// TODO - you fill in here.
return {};


//Step 1: Sign or unsigned integer
int sign = (num1.front() < 0) ^ (num2.front() < 0) ? -1 : 1;
//Step 2: Make negative int to +ve by abs
num1.front() = std::abs(num1.front()), num2.front() = std::abs(num2.front());
//Step 3: Making 2 loop
vector<int> result(num1.size() + num2.size(),0);
for(int i = num1.size() - 1; i >= 0; i--){
for(int j = num2.size() - 1; j>=0; j--){
result[i+j+1] += num1[i] * num2[j];
result[i+j] += result[i+j+1] / 10;
result[i+j+1] %= 10;
}
}

//Step 4: Remove starting zero
vector<int>::iterator removeStatingZero = std::find_if_not(begin(result), end(result), [](int a){ return a == 0; });
result = {removeStatingZero, end(result)};
if(result.empty()){
return {0};
}
result.front() *= sign;
//Step 5: Return the result
return result;
}

int main(int argc, char* argv[]) {
Expand Down
18 changes: 18 additions & 0 deletions epi_judge_cpp/is_number_palindromic.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
#include "test_framework/generic_test.h"
bool IsPalindromeNumber(int x) {
// TODO - you fill in here.
//0 - Check if number is negative
if(x <= 0) {
return x == 0;
}
//1. Count the number if digit
int num_of_digit = static_cast<int>(floor(log10(x))) + 1;
//2. Create a mask
int mask = static_cast<int>(pow(10, num_of_digit - 1));
//3. Check as palindram by checking first and last digit
for(int i = 0; i < num_of_digit / 2; i++){
if(x/mask != x%10){
return false;
}
x %= mask;
x /= 10;
mask/=100;

}
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions problem_mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ problem_mapping = {
"passed": 0,
"total": 10000
},
"Python: closest_int_same_weight.py": {
"Python: closest_int_same_weight.py": {
"passed": 0,
"total": 10000
}
Expand Down Expand Up @@ -128,7 +128,7 @@ problem_mapping = {
},
"4.09 Check if a decimal integer is a palindrome": {
"C++: is_number_palindromic.cc": {
"passed": 0,
"passed": 20000,
"total": 20000
},
"Java: IsNumberPalindromic.java": {
Expand Down Expand Up @@ -214,7 +214,7 @@ problem_mapping = {
},
"5.03 Multiply two arbitrary-precision integers": {
"C++: int_as_array_multiply.cc": {
"passed": 0,
"passed": 1000,
"total": 1000
},
"Java: IntAsArrayMultiply.java": {
Expand Down