Skip to content

Commit 1370f64

Browse files
committed
completed 1732 - find the highest altitude in go, c and c++
1 parent 23082f7 commit 1370f64

File tree

23 files changed

+5112
-0
lines changed

23 files changed

+5112
-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.

c/find_the_largest_altitude/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
## 1732. Find the Highest Altitude
28+
29+
There is a biker going on a road trip. The road trip consists of n + 1 points at different altitudes. The biker starts his trip on point 0 with altitude equal 0.
30+
31+
You are given an integer array gain of length n where gain[i] is the net gain in altitude between points i​​​​​​ and i + 1 for all (0 <= i < n). Return the highest altitude of a point.
32+
33+
34+
35+
Example 1:
36+
37+
Input: gain = [-5,1,5,0,-7]
38+
Output: 1
39+
Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.
40+
41+
42+
Example 2:
43+
44+
Input: gain = [-4,-3,-2,-1,4,3,2]
45+
Output: 0
46+
Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.
47+
48+
49+
Constraints:
50+
51+
n == gain.length
52+
1 <= n <= 100
53+
-100 <= gain[i] <= 100
54+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
int largestAltitude(int *gain, int gainSize);
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "solution.h"
2+
3+
int max(int a, int b) {
4+
if (a > b) {
5+
return a;
6+
}
7+
8+
return b;
9+
}
10+
11+
int largestAltitude(int *gain, int gainSize) {
12+
int high = 0, curr = 0;
13+
14+
for (int i = 0; i < gainSize; ++i) {
15+
curr += gain[i];
16+
17+
high = max(high, curr);
18+
}
19+
20+
return high;
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 gain[] = {-5, 1, 5, 0, -7};
16+
int gainSize = 5;
17+
int expected = 1;
18+
19+
int result = largestAltitude(gain, gainSize);
20+
21+
TEST_ASSERT_EQUAL_INT(result, expected);
22+
}
23+
24+
void test_case_two(void) {
25+
int gain[] = {-4, -3, -2, -1, 4, 3, 2};
26+
int gainSize = 7;
27+
int expected = 0;
28+
29+
int result = largestAltitude(gain, gainSize);
30+
31+
TEST_ASSERT_EQUAL_INT(result, expected);
32+
}
33+
34+
35+
// END TEST IMPLEMENTATIONS
36+
37+
int main(void) {
38+
UNITY_BEGIN();
39+
RUN_TEST(test_case_one);
40+
RUN_TEST(test_case_two);
41+
return UNITY_END();
42+
}

0 commit comments

Comments
 (0)