playground updated

This commit is contained in:
Hizenberg469
2024-11-30 23:25:28 +05:30
parent 9415c7f0c7
commit b46c486580
20 changed files with 837 additions and 566 deletions

View File

View File

View File

59
playground/Makefile Normal file
View File

@@ -0,0 +1,59 @@
BUILD_DIR = build
OBJS_DIR = $(BUILD_DIR)/objs
SOLUTION_BINARY = $(BUILD_DIR)/solution
BRUTEFORCE_BINARY = $(BUILD_DIR)/$(notdir Stress_Testing/bruteForce)
GENERATOR_BINARY = $(BUILD_DIR)/$(notdir Stress_Testing/generator)
CODEDIRS = . ./Stress_Testing
INCDIRS = .
CXX = g++
DEPFLAGS = -MP -MMD
CXXFLAGS = -Wall -Wextra -g \
$(foreach D,$(INCDIRS), -I$(D)) \
$(DEPFLAGS)
CXXFILES = $(foreach D, \
$(CODEDIRS), \
$(wildcard $(D)/*.cpp))
OBJECTS = $(patsubst %.cpp, $(OBJS_DIR)/%.o, $(notdir $(CXXFILES)))
DEPFILES = $(patsubst %.cpp, $(OBJS_DIR)/%.d, $(notdir $(CXXFILES)))
all: $(SOLUTION_BINARY) \
$(GENERATOR_BINARY) \
$(BRUTEFORCE_BINARY)
$(SOLUTION_BINARY): $(OBJS_DIR)/solution.o | $(OBJS_DIR)
$(CXX) -o $@ $<
$(GENERATOR_BINARY): $(OBJS_DIR)/generator.o | $(OBJS_DIR)
$(CXX) -o $@ $<
$(BRUTEFORCE_BINARY): $(OBJS_DIR)/bruteForce.o | $(OBJS_DIR)
$(CXX) -o $@ $<
$(OBJS_DIR)/%.o: %.cpp | $(OBJS_DIR)
$(CXX) $(CXXFLAGS) -c -o $@ $<
$(OBJS_DIR)/%.o: Stress_Testing/%.cpp | $(OBJS_DIR)
$(CXX) $(CXXFLAGS) -c -o $@ $<
clean:
rm -f $(SOLUTION_BINARY) \
$(BRUTEFORCE_BINARY) \
$(GENERATOR_BINARY) \
$(OBJECTS) \
$(DEPFILES)
help:
@echo "make clean: To remove executable, objects and dependency files."
@echo "make $(SOLUTION_BINARY): To build $(SOLUTION_BINARY) only."
@echo "make all: To build for Stress_Testing."
-include $(DEPFILES)
.PHONY: clean help all

View File

@@ -0,0 +1,42 @@
#include <bits/stdc++.h>
using namespace std;
const int N = int(2e5) + 9;
int n, k, h;
int need = int(1e9);
int cnt[N];
int main() {
scanf("%d %d", &n, &k);
for(int i = 0; i < n; ++i){
int x;
scanf("%d", &x);
h = max(h, x);
need = min(need, x);
++cnt[x];
}
int pos = N - 1;
int res = 0;
long long sum = 0;
int c = 0;
while(true){
long long x = sum - c * 1LL * (pos - 1);
if(x > k){
++res;
h = pos;
sum = pos * 1LL * c;
}
--pos;
if(pos == need) break;
c += cnt[pos];
sum += cnt[pos] * 1LL * pos;
}
if(h != need) ++res;
cout << res << endl;
}

View File

@@ -0,0 +1,31 @@
#include <bits/stdc++.h>
using namespace std;
int rnd(int a,int b){
return a + rand() % ( b - a + 1 );
}
int main(int argc, char* argv[]){
int seed = atoi(argv[1]);
srand(seed);
int n = rnd(1,20);
int k = rnd(20, 40);
cout << n << ' ' << k << '\n';
int val = 0;
for(int i = 0 ; i < n ; i++ ){
val = rnd(0,50);
cout << val << ' ';
}
cout << '\n';
return 0;
}

83
playground/run.sh Normal file
View File

@@ -0,0 +1,83 @@
#!/bin/bash
#Creating .txt files
TESTCASE_FILE=IOFiles/testcase.txt
OUTPUT_FILE=IOFiles/output.txt
CORRECT_ANSWER_FILE=IOFiles/correct_answer.txt
if ! [ -f $TESTCASE_FILE ]; then
echo "No $TESTCASE_FILE detected"
exit 1
fi
if ! [ -f $OUTPUT_FILE ]; then
echo "No $OUTPUT_FILE detected"
exit 1
fi
if ! [ -f $CORRECT_ANSWER_FILE ]; then
echo "No $CORRECT_ANSWER_FILE detected"
exit 1
fi
#Executables.
SOLUTION=build/solution
make $SOLUTION
# Read and split test cases and answers based on empty lines
echo "Running test cases..."
pass_count=0
fail_count=0
# Separate test cases and expected outputs by empty lines
mapfile -t test_cases < <(awk -v RS= '1' "$TESTCASE_FILE")
mapfile -t expected_outputs < <(awk -v RS= '1' "$CORRECT_ANSWER_FILE")
# Ensure number of testcase is same as number of output.
if [ ${#test_cases[@]} -ne ${#expected_outputs[@]} ]; then
echo "The TestCase and answer count should be same."
exit 2
fi
for (( test_num = 0 ; test_num < ${#test_cases[@]} ; test_num++))
do
test_case=${test_cases[test_num]}
expected_output=${expected_outputs[test_num]}
if ! echo "$test_case" | ./$SOLUTION > "$OUTPUT_FILE"; then
echo -e "\033[0;31mTest #$(($test_num+1)): Execution failed\033[0m"
echo "solution executable didn't executed successfully"
let fail_count++
continue
fi
test_output=$(<"$OUTPUT_FILE")
if diff -q -Z <(echo "$test_output") <(echo "$expected_output") > /dev/null; then
echo -e "\033[0;32mTest #$(($test_num+1)): PASS\033[0m"
let pass_count++
else
echo -e "\033[0;31mTest #$(($test_num+1)): FAIL\033[0m"
echo "Test case:"
echo $test_case
echo "Your output:"
echo $test_output
echo "Correct output:"
echo $expected_output
let fail_count++
fi
done
#Sumary
echo -e "\033[0;32mPassed test: $pass_count\033[0m"
echo -e "\033[0;31mFailed test: $fail_count\033[0m"
# Exit with appropriate status
if [ $fail_count -eq 0 ]; then
exit 0
else
exit 3
fi

9
playground/solution.cpp Normal file
View File

@@ -0,0 +1,9 @@
#include <iostream>
using namespace std;
int main(){
int a,b;
cin >> a >> b;
cout << a << ' ' << b << '\n';
}

47
playground/stress.sh Normal file
View File

@@ -0,0 +1,47 @@
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Provide the limit of stress testing"
echo "$0 'test case limit'"
echo "$0 -1 (for unlimited)"
exit 1
fi
#Required tools
SOLUTION=build/solution
GENERATOR=build/generator
BRUTEFORCE=build/bruteForce
#Build necessary things
make all
#Files
TESTCASE_FILE=IOFiles/testcase.txt
OUTPUT_FILE=IOFiles/output.txt
CORRECT_ANSWER_FILE=IOFiles/correct_answer.txt
pass_count=0
while (( 1 ))
do
./generator > $TESTCASE_FILE
./SOLUTION < $TESTCASE_FILE > $OUTPUT_FILE
./BRUTEFORCE < $TESTCASE_FILE > $CORRECT_ANSWER_FILE
if diff -q -Z $OUTPUT_FILE $CORRECT_ANSWER_FILE >> /dev/null; then
echo -e "\033[0;31mTest #$(($pass_count+1)): FAIL\033[0m"
echo "Test case:"
cat $TESTCASE_FILE
echo "Your output:"
cat $OUTPUT_FILE
echo "Correct output:"
cat $CORRECT_ANSWER_FILE
break
fi
echo -e "\033[0;32mPassed test: #$(($pass_count+1))\033[0m"
if [ $1 -ne -1 ] || [ $pass_count -lt $1 ]; then
break
fi
done