-
-
Notifications
You must be signed in to change notification settings - Fork 182
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
Add spiral-matrix #942
Merged
Merged
Add spiral-matrix #942
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
63c100f
Add spiral-matrix
ryanplusplus fc54fbc
Add TEST_IGNORE() for all tests except the first
ryanplusplus eaf6aa1
Merge branch 'main' of https://github.com/exercism/c into spiral-matrix
ryanplusplus 6db9b2d
Merge branch 'main' of https://github.com/exercism/c into spiral-matrix
ryanplusplus 574b898
Use updated UNITY_BEGIN/END macro calls
ryanplusplus 42b5215
Merge branch 'main' of https://github.com/exercism/c into spiral-matrix
ryanplusplus 508851e
spiral_matrix -> spiral
ryanplusplus 0926e88
Provide type in the skeleton header
ryanplusplus 18875d8
Use Unity's array matcher to improve output
ryanplusplus 8632957
Add contributor
ryanplusplus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Instructions | ||
|
||
Given the size, return a square matrix of numbers in spiral order. | ||
|
||
The matrix should be filled with natural numbers, starting from 1 in the top-left corner, increasing in an inward, clockwise spiral order, like these examples: | ||
|
||
## Examples | ||
|
||
### Spiral matrix of size 3 | ||
|
||
```text | ||
1 2 3 | ||
8 9 4 | ||
7 6 5 | ||
``` | ||
|
||
### Spiral matrix of size 4 | ||
|
||
```text | ||
1 2 3 4 | ||
12 13 14 5 | ||
11 16 15 6 | ||
10 9 8 7 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": ["ryanplusplus"], | ||
"files": { | ||
"solution": [ | ||
"spiral_matrix.c", | ||
"spiral_matrix.h" | ||
], | ||
"test": [ | ||
"test_spiral_matrix.c" | ||
], | ||
"example": [ | ||
".meta/example.c", | ||
".meta/example.h" | ||
] | ||
}, | ||
"blurb": "Given the size, return a square matrix of numbers in spiral order.", | ||
"source": "Reddit r/dailyprogrammer challenge #320 [Easy] Spiral Ascension.", | ||
"source_url": "https://web.archive.org/web/20230607064729/https://old.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <stdlib.h> | ||
#include "spiral_matrix.h" | ||
|
||
spiral_matrix_t *spiral_matrix_create(int size) | ||
{ | ||
spiral_matrix_t *spiral_matrix = calloc(1, sizeof(spiral_matrix_t)); | ||
spiral_matrix->size = size; | ||
|
||
if (size == 0) { | ||
return spiral_matrix; | ||
} | ||
|
||
spiral_matrix->matrix = calloc(size, sizeof(int *)); | ||
for (int i = 0; i < size; i++) { | ||
spiral_matrix->matrix[i] = calloc(size, sizeof(int)); | ||
} | ||
|
||
int x = 0; | ||
int y = 0; | ||
int dx = 1; | ||
int dy = 0; | ||
|
||
for (int i = 1; i <= size * size; i++) { | ||
wolf99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
spiral_matrix->matrix[y][x] = i; | ||
if (x + dx >= size || x + dx < 0 || y + dy >= size || y + dy < 0 || | ||
spiral_matrix->matrix[y + dy][x + dx] != 0) { | ||
int new_dx = -dy; | ||
int new_dy = dx; | ||
dx = new_dx; | ||
dy = new_dy; | ||
} | ||
x += dx; | ||
y += dy; | ||
} | ||
|
||
return spiral_matrix; | ||
} | ||
|
||
void spiral_matrix_destroy(spiral_matrix_t *spiral_matrix) | ||
{ | ||
for (int i = 0; i < spiral_matrix->size; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
free(spiral_matrix->matrix[i]); | ||
} | ||
|
||
if (spiral_matrix->size > 0) { | ||
free(spiral_matrix->matrix); | ||
} | ||
|
||
free(spiral_matrix); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef SPIRAL_MATRIX_H | ||
#define SPIRAL_MATRIX_H | ||
|
||
typedef struct { | ||
int size; | ||
int **matrix; | ||
} spiral_matrix_t; | ||
|
||
spiral_matrix_t *spiral_matrix_create(int size); | ||
void spiral_matrix_destroy(spiral_matrix_t *spiral_matrix); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[8f584201-b446-4bc9-b132-811c8edd9040] | ||
description = "empty spiral" | ||
|
||
[e40ae5f3-e2c9-4639-8116-8a119d632ab2] | ||
description = "trivial spiral" | ||
|
||
[cf05e42d-eb78-4098-a36e-cdaf0991bc48] | ||
description = "spiral of size 2" | ||
|
||
[1c475667-c896-4c23-82e2-e033929de939] | ||
description = "spiral of size 3" | ||
|
||
[05ccbc48-d891-44f5-9137-f4ce462a759d] | ||
description = "spiral of size 4" | ||
|
||
[f4d2165b-1738-4e0c-bed0-c459045ae50d] | ||
description = "spiral of size 5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
### If you wish to use extra libraries (math.h for instance), | ||
### add their flags here (-lm in our case) in the "LIBS" variable. | ||
|
||
LIBS = -lm | ||
|
||
### | ||
CFLAGS = -std=c99 | ||
CFLAGS += -g | ||
CFLAGS += -Wall | ||
CFLAGS += -Wextra | ||
CFLAGS += -pedantic | ||
CFLAGS += -Werror | ||
CFLAGS += -Wmissing-declarations | ||
CFLAGS += -DUNITY_SUPPORT_64 -DUNITY_OUTPUT_COLOR | ||
|
||
ASANFLAGS = -fsanitize=address | ||
ASANFLAGS += -fno-common | ||
ASANFLAGS += -fno-omit-frame-pointer | ||
|
||
.PHONY: test | ||
test: tests.out | ||
@./tests.out | ||
|
||
.PHONY: memcheck | ||
memcheck: ./*.c ./*.h | ||
@echo Compiling $@ | ||
@$(CC) $(ASANFLAGS) $(CFLAGS) test-framework/unity.c ./*.c -o memcheck.out $(LIBS) | ||
@./memcheck.out | ||
@echo "Memory check passed" | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -rf *.o *.out *.out.dSYM | ||
|
||
tests.out: ./*.c ./*.h | ||
@echo Compiling $@ | ||
@$(CC) $(CFLAGS) test-framework/unity.c ./*.c -o tests.out $(LIBS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "spiral_matrix.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#ifndef SPIRAL_MATRIX_H | ||
#define SPIRAL_MATRIX_H | ||
|
||
ryanplusplus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nit:
Consider refactoring type and variable names from
spiral_matrix
tospiral
so that we then have a nicespiral->matrix
during use?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, good idea