Skip to content

Commit f20420e

Browse files
committed
completed 2090 - k radius subarray averages in c
1 parent 7e82f34 commit f20420e

File tree

11 files changed

+4734
-0
lines changed

11 files changed

+4734
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# General
2+
*.log
3+
*.tmp
4+
5+
# IntelliJ IDEA Files
6+
.idea/
7+
*.iml
8+
*.iws
9+
*.ipr
10+
11+
# clangd cache
12+
**/.cache/
13+
14+
# CLion files
15+
cmake-build-debug/
16+
**/cmake-build-debug/
17+
**/build/
18+
compile_commands.json
19+
20+
21+
# Eclipse Files
22+
.classpath
23+
.project
24+
.settings/
25+
26+
# NetBeans Files
27+
nbproject/private/
28+
build/
29+
nbbuild/
30+
dist/
31+
nbdist/
32+
.nb-gradle/
33+
34+
# VS Code
35+
.vscode/
36+
37+
# Compiled Go binaries
38+
/bin/
39+
/pkg/
40+
41+
# Output of the go coverage tool
42+
*.out
43+
44+
# Python
45+
__pycache__/
46+
/__pycache__/
47+
*.pyc
48+
venv/
49+
.env
50+
51+
# Rust
52+
/target/
53+
target/
54+
**/target/
55+
**/*.rs.bk
56+
Cargo.lock
57+
58+
# Windows image file caches
59+
Thumbs.db
60+
ehthumbs.db
61+
62+
# Folder config file
63+
Desktop.ini
64+
65+
# Recycle Bin used on file shares
66+
$RECYCLE.BIN/
67+
68+
# Mac desktop service store files
69+
.DS_Store
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
cmake_minimum_required(VERSION 3.29)
2+
project(c_dsa_templ C)
3+
4+
set(CMAKE_C_STANDARD 17)
5+
set(CMAKE_C_STANDARD_REQUIRED ON)
6+
7+
# Include directories
8+
include_directories(include)
9+
10+
# Add your source files
11+
add_library(solution STATIC src/solution.c include/solution.h)
12+
13+
# Add Unity from the vendor folder
14+
add_library(unity STATIC vendor/unity.c vendor/unity.h)
15+
16+
# Add test executable
17+
add_executable(test_solution test/test_solution.c)
18+
19+
# Link Unity and solution to the test executable
20+
target_link_libraries(test_solution unity solution)
21+
22+
# Include directories
23+
target_include_directories(test_solution PRIVATE
24+
${CMAKE_CURRENT_SOURCE_DIR}
25+
${CMAKE_CURRENT_SOURCE_DIR}/vendor
26+
)
27+
28+
# Add a custom target to run tests
29+
add_custom_target(test
30+
COMMAND test_solution
31+
DEPENDS test_solution
32+
)
33+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2024, Jeremy Novak All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6+
7+
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8+
9+
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10+
11+
Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
12+
13+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# C DSA Template
2+
3+
This template is created to quickly stand up a new project for C that includes
4+
Unity Tests and CMake for builds.
5+
6+
## Usage
7+
8+
`git clone https://github.com/jgndev/c_dsa_templ your_dir_name`
9+
10+
11+
## Project Setup
12+
13+
Run project setup to configure the project and create `compile-commands.json` for LSP support.
14+
15+
## Running Tests
16+
17+
Add your tests to `test/test_solution.c`
18+
19+
Then you will `cd` into the `build` directory and run `make`.
20+
21+
```bash
22+
cd build && \
23+
make && \
24+
./test_solution
25+
```
26+
27+
## 2090. K Radius Subarray Averages
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#include <stddef.h>
4+
#include <stdlib.h>
5+
6+
int* getAverages(int* nums, int numsSize, int k, int* returnSize);
7+
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
5+
# Constants
6+
readonly SCRIPT_NAME="${0##*/}"
7+
readonly BUILD_DIR="build"
8+
9+
# Color codes for output
10+
readonly RED='\033[0;31m'
11+
readonly GREEN='\033[0;32m'
12+
readonly NC='\033[0m' # No Color
13+
14+
# Function to print error messages
15+
print_error() {
16+
printf "${RED}Error: %s${NC}\n" "$1" >&2
17+
}
18+
19+
# Function to print success messages
20+
print_success() {
21+
printf "${GREEN}Success: %s${NC}\n" "$1"
22+
}
23+
24+
# Function to check if a command exists
25+
command_exists() {
26+
command -v "$1" &>/dev/null
27+
}
28+
29+
# Main function
30+
main() {
31+
# Check if CMake is installed
32+
if ! command_exists cmake; then
33+
print_error "CMake is not installed. Please install CMake and try again."
34+
exit 1
35+
fi
36+
37+
# Create build directory if it doesn't exist
38+
mkdir -p "${BUILD_DIR}"
39+
40+
# Generate build files and compile_commands.json
41+
echo "Generating build files and compile_commands.json..."
42+
if ! cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_C_STANDARD=17 -B "${BUILD_DIR}" .; then
43+
print_error "Failed to generate build files."
44+
exit 1
45+
fi
46+
print_success "Build files and compile_commands.json generated."
47+
48+
# Build the project
49+
echo "Building the project..."
50+
if ! cmake --build "${BUILD_DIR}"; then
51+
print_error "Failed to build the project."
52+
exit 1
53+
fi
54+
print_success "Project built successfully."
55+
56+
# Run the tests
57+
echo "Running tests..."
58+
if ! (cd "${BUILD_DIR}" && ./test_solution); then
59+
print_error "Some tests failed."
60+
else
61+
print_success "All tests passed."
62+
fi
63+
64+
# Symlink compile_commands.json to project root
65+
echo "Creating symlink for compile_commands.json..."
66+
if [[ -L compile_commands.json ]]; then
67+
rm compile_commands.json
68+
elif [[ -e compile_commands.json ]]; then
69+
print_error "A file named compile_commands.json already exists and is not a symlink."
70+
exit 1
71+
fi
72+
if ! ln -s "${BUILD_DIR}/compile_commands.json" .; then
73+
print_error "Failed to create symlink for compile_commands.json."
74+
exit 1
75+
fi
76+
print_success "Symlink for compile_commands.json created in project root."
77+
78+
echo "All tasks completed successfully!"
79+
echo "You can now use compile_commands.json for your LSP."
80+
echo "The project is set up to use C17 standard."
81+
echo "To run tests again, use 'ctest' in the ${BUILD_DIR} directory."
82+
}
83+
84+
# Execute main function
85+
main "$@"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "solution.h"
2+
3+
int *getAverages(int *nums, int numsSize, int k, int *returnSize) {
4+
if (k == 0) {
5+
*returnSize = numsSize;
6+
return nums;
7+
}
8+
9+
int windowSize = 2 * k + 1;
10+
int *averages = (int *) malloc(numsSize * sizeof(int));
11+
*returnSize = numsSize;
12+
13+
for (int i = 0; i < numsSize; ++i) {
14+
averages[i] = -1;
15+
}
16+
17+
if (windowSize > numsSize) {
18+
return averages;
19+
}
20+
21+
long long windowSum = 0;
22+
for (int i = 0; i < windowSize; ++i) {
23+
windowSum += nums[i];
24+
}
25+
26+
averages[k] = (int) (windowSum / windowSize);
27+
28+
for (int i = windowSize; i < numsSize; ++i) {
29+
windowSum = windowSum - nums[i - windowSize] + nums[i];
30+
averages[i - k] = (int) (windowSum / windowSize);
31+
averages[i] = -1; // Set the current element to -1
32+
}
33+
34+
return averages;
35+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include "solution.h"
2+
#include "unity.h"
3+
4+
void setUp(void) {
5+
// This function runs before each test
6+
}
7+
8+
void tearDown(void) {
9+
// This function runs after each test
10+
}
11+
12+
// BEGIN TEST IMPLEMENTATIONS
13+
14+
void test_case_one(void) {
15+
int nums[] = {1, 3, 2, 6, 2};
16+
int numsSize = 5;
17+
int k = 1;
18+
int returnSize;
19+
int expected[] = {-1, 2, 3, 3, -1};
20+
21+
int* result = getAverages(nums, numsSize, k, &returnSize);
22+
23+
TEST_ASSERT_EQUAL_INT(numsSize, returnSize);
24+
TEST_ASSERT_EQUAL_INT_ARRAY(result, expected, numsSize);
25+
26+
free(result);
27+
}
28+
29+
void test_case_two(void) {
30+
int nums[] = {7, 4, 3, 9, 1, 8, 5, 2, 6};
31+
int numsSize = 9;
32+
int k = 3;
33+
int returnSize;
34+
int expected[] = {-1, -1, -1, 5, 4, 4, -1, -1, -1};
35+
36+
int* result = getAverages(nums, numsSize, k, &returnSize);
37+
38+
TEST_ASSERT_EQUAL_INT(numsSize, returnSize);
39+
TEST_ASSERT_EQUAL_INT_ARRAY(result, expected, numsSize);
40+
41+
free(result);
42+
}
43+
44+
45+
void test_case_three(void) {
46+
int nums[] = {5, 2, 8, 1, 9};
47+
int numsSize = 5;
48+
int k = 0;
49+
int returnSize;
50+
51+
int* result = getAverages(nums, numsSize, k, &returnSize);
52+
53+
TEST_ASSERT_EQUAL_INT(numsSize, returnSize);
54+
TEST_ASSERT_EQUAL_INT_ARRAY(nums, result, numsSize);
55+
56+
// Don't free result here as it's the same as nums
57+
}
58+
59+
void test_case_four(void) {
60+
int nums[] = {1, 2, 3, 4, 5};
61+
int numsSize = 5;
62+
int k = 3;
63+
int returnSize;
64+
int expected[] = {-1, -1, -1, -1, -1};
65+
66+
int* result = getAverages(nums, numsSize, k, &returnSize);
67+
68+
TEST_ASSERT_EQUAL_INT(numsSize, returnSize);
69+
TEST_ASSERT_EQUAL_INT_ARRAY(expected, result, numsSize);
70+
71+
free(result);
72+
}
73+
74+
// END TEST IMPLEMENTATIONS
75+
76+
int main(void) {
77+
UNITY_BEGIN();
78+
RUN_TEST(test_case_one);
79+
RUN_TEST(test_case_two);
80+
RUN_TEST(test_case_three);
81+
RUN_TEST(test_case_four);
82+
return UNITY_END();
83+
}

0 commit comments

Comments
 (0)