Skip to content

Commit

Permalink
fix cpp generator and add problems
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalmishraa committed Sep 6, 2024
1 parent d56bd2f commit c37df64
Show file tree
Hide file tree
Showing 70 changed files with 368 additions and 238 deletions.
218 changes: 111 additions & 107 deletions apps/boilerplate-generator/src/GenerateFullBoilerplate/generateFullCpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,122 +10,126 @@ export default function generateFullCpp({
outputFields
}: IGenerator): string {
const makeType = new MakeType()
const inputReads = inputFields
.map((field, index) => {
if (field.type.startsWith("list<")) {
return `int size_${field.name};\n istringstream(lines[${index}]) >> size_${field.name};\n ${makeType.ToCpp(field.type)} ${field.name}(size_${field.name});\n if(size_${field.name} != 0) {\n \tistringstream iss(lines[${index + 1}]);\n \tfor (int i=0; i < size_${field.name}; i++) iss >> ${field.name}[i];\n }`;
} else {
return `${makeType.ToCpp(field.type)} ${field.name};\n istringstream(lines[${index}]) >> ${field.name};`;
}
})
.join("\n ");
let lineIndex = 0;
const inputReads = inputFields.map((field) => {
if (field.type.startsWith("list<")) {
const sizeRead = `int size_${field.name};\n istringstream(lines[${lineIndex++}]) >> size_${field.name};`;
const arrayDeclaration = `${makeType.ToCpp(field.type)} ${field.name}(size_${field.name});`;
const arrayRead = `if (size_${field.name} > 0) {\n istringstream iss(lines[${lineIndex++}]);\n for (int i = 0; i < size_${field.name}; i++) {\n iss >> ${field.name}[i];\n }\n }`;
return `${sizeRead}\n ${arrayDeclaration}\n ${arrayRead}`;
} else {
return `${makeType.ToCpp(field.type)} ${field.name};\n istringstream(lines[${lineIndex++}]) >> ${field.name};`;
}
});

const inputReadsString = inputFields.length > 0 ? inputReads.join('\n') : "";
const outputType = outputFields[0].type;
const functionCall = `${outputType} result = ${functionName}(${inputFields.map((field) => field.name).join(", ")});`;
const outputWrite = `cout << result << endl;`;

return ` #ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
return `#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#include <exception>
#include <stdexcept>
#endif
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#include <exception>
#include <stdexcept>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;
##USER_CODE_HERE##
##USER_CODE_HERE##
int main() {
ifstream file("${process.env.PV_DIR_PATH}/${problemName.replace(" ", "-")}/tests/inputs/##INPUT_FILE_INDEX##.txt");
vector<string> lines;
string line;
while (getline(file, line)) lines.push_back(line);
int main() {
ifstream file("${process.env.PV_DIR_PATH}/${problemName.replace(" ", "-")}/tests/inputs/##INPUT_FILE_INDEX##.txt");
vector<string> lines;
string line;
while (getline(file, line)) lines.push_back(line);
file.close();
${inputReads}
${functionCall}
${outputWrite}
return 0;
}
`;
file.close();
${inputReadsString}
${functionCall}
${outputWrite}
return 0;
}
`;
}
2 changes: 1 addition & 1 deletion apps/compiler/src/utils/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function getCommnad(language: string) {

switch (language) {
case 'cpp':
command = 'g++ ./box/yourcode.cpp -o ./box/yourcode && ./box/yourcode';
command = 'g++ -std=c++17 ./box/yourcode.cpp -o ./box/yourcode && ./box/yourcode';
break;
case 'python':
command = 'python3 ./box/yourcode.py';
Expand Down
96 changes: 96 additions & 0 deletions apps/problems/Balanced-Arrays/Problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
## Problem: Balanced Arrays

### Description

Given an array of integers, you are allowed to remove at most one element. After potentially removing one element, determine the maximum sum of elements where the sum of the elements on the left of a certain index is equal to the sum of the elements on the right of that index. If no such equilibrium can be achieved, even after a removal, return -1.

### Constraints
- The array `arr` consists of integers.
- The size of `arr` is in the range `[3, 10^5]`.
- Each element in the array is in the range `[1, 10^9]`.

### Output
- Return the maximum sum of the array (excluding the removed element if any) where an equilibrium index exists after potentially removing one element. If no such equilibrium can be found, return `-1`.


### Test Case 1
**Input:**
```
[6, 1, 6, 3]
```
**Explanation:**
Without removing any elements, no equilibrium index exists. By removing the last element `3`, the array becomes `[6, 1, 6]`. We can achieve an equilibrium at index `1` (zero-based index), with `6` on both sides. The sum on either side is `6`.

The other valid partitions (just the partition not the paritions with the sum equal) of `[6,1,6,3]` are :
- `[]` and `[1,6,3]`
- `[6]` and `[6,3]`
- `[6,1]` and `[3]`
- `[6,1,6]` and `[]`


**Output:**
```
6
```

### Test Case 2
**Input:**
```
[3, 3, 5, 3, 3]
```
**Explanation:**
Without removing any elements, the array has an equilibrium index at `2` (zero-based index), with `[3, 3]` on the left and `[3, 3]` on the right, both summing to `6`. No need to remove any elements as this already provides a valid equilibrium.

**Output:**
```
6
```

### Test Case 3
**Input:**
```
[2, 1, 1, 2]
```
**Explanation:**
This array needs the removal of either of the 1's both give the same answer.
After removing the first 1 the array becomes `[2,1,2]`
Then we can partition it using the `1` at the `1st` index
The sums of the parts `[2]` on the left and `[2]` on the right (excluding the middle `1`) are both equal to `2`.

**Output:**
```
2
```

### Test Case 4
**Input:**
```
[10, 20, 10, 5, 15, 5, 10]
```
**Explanation:**
Removing the element `5` at index `3` results in the array `[10, 20, 10, 15, 5, 10]`. No equilibrium can be found in this modified array that yields a sum larger than any other configuration. Without any removal, equilibrium occurs with the index `2`, but the sum to each side (excluding index `2`) doesn't exceed `30` from other possible configurations.

**Output:**
```
30
```

### Test Case 5
**Input:**
```
[1, 2, 3]
```
**Explanation:**
There's no possible way to remove an element and achieve equilibrium in this array. With or without removal, no index meets the requirement where both sides are equal.

**Output:**
```
-1
```

### Note
- If there is no solution you have to return `-1` else you will get a Wrong Answer Verdict
- The sum of an empty parition `[]` is considered to be `0`
- The solution should aim to operate in a time complexity that allows for efficient processing, ideally better than `O(n^2)`, considering potential array sizes.


18 changes: 18 additions & 0 deletions apps/problems/Balanced-Arrays/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
g++ solutions/ans.cpp -o solutions/ans

# Create the output directory if it doesn't exist
mkdir -p tests/output

# Loop through all the input files
for input_file in tests/input/*.txt; do
# Extract the filename without the extension
base_name=$(basename -- "$input_file" .txt)

# Run the compiled program with input and generate the output
./solutions/ans < "$input_file" > "tests/output/$base_name.txt"
if [ $? -ne 0 ]; then
echo "Error processing file: $input_file"
fi
done

echo "All tests have been processed."
1 change: 1 addition & 0 deletions apps/problems/Balanced-Arrays/tags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ARRAYS
6 changes: 6 additions & 0 deletions apps/problems/Balanced-Arrays/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Problem Name: "Balanced Arrays"
Function Name: solve
Input Structure:
Input Field: list<int> A
Output Structure:
Output Field: int result
2 changes: 2 additions & 0 deletions apps/problems/Balanced-Arrays/tests/inputs/0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
4
6 1 6 3
2 changes: 2 additions & 0 deletions apps/problems/Balanced-Arrays/tests/inputs/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
5
3 3 5 3 3
2 changes: 2 additions & 0 deletions apps/problems/Balanced-Arrays/tests/inputs/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
4
2 1 1 2
2 changes: 2 additions & 0 deletions apps/problems/Balanced-Arrays/tests/inputs/3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
7
10 20 10 5 15 5 10
2 changes: 2 additions & 0 deletions apps/problems/Balanced-Arrays/tests/inputs/4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
3
1 2 3
2 changes: 2 additions & 0 deletions apps/problems/Balanced-Arrays/tests/inputs/5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
3
1 1 1
2 changes: 2 additions & 0 deletions apps/problems/Balanced-Arrays/tests/inputs/6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
3
1 3 1
1 change: 1 addition & 0 deletions apps/problems/Balanced-Arrays/tests/outputs/0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6
Loading

0 comments on commit c37df64

Please sign in to comment.