Skip to content
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 10 commits into from
Feb 24, 2024
15 changes: 14 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@
"arrays",
"control_flow_loops",
"strings"
]
]
},
{
"slug": "pop-count",
Expand All @@ -1040,6 +1040,19 @@
"control_flow_if_statements",
"control_flow_loops"
]
},
{
"slug": "spiral-matrix",
"name": "Spiral Matrix",
"uuid": "2e5f7544-68fc-4993-9ee0-27b74b1176e7",
"practices": [],
"prerequisites": [],
"difficulty": 4,
"topics": [
"arrays",
"control_flow_loops",
"pointers"
]
}
]
},
Expand Down
24 changes: 24 additions & 0 deletions exercises/practice/spiral-matrix/.docs/instructions.md
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
```
19 changes: 19 additions & 0 deletions exercises/practice/spiral-matrix/.meta/config.json
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/"
}
50 changes: 50 additions & 0 deletions exercises/practice/spiral-matrix/.meta/example.c
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));
Copy link
Contributor

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 to spiral so that we then have a nice spiral->matrix during use?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, good idea

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++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size_t i maybe?

free(spiral_matrix->matrix[i]);
}

if (spiral_matrix->size > 0) {
free(spiral_matrix->matrix);
}

free(spiral_matrix);
}
12 changes: 12 additions & 0 deletions exercises/practice/spiral-matrix/.meta/example.h
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
28 changes: 28 additions & 0 deletions exercises/practice/spiral-matrix/.meta/tests.toml
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"
37 changes: 37 additions & 0 deletions exercises/practice/spiral-matrix/makefile
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)
1 change: 1 addition & 0 deletions exercises/practice/spiral-matrix/spiral_matrix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "spiral_matrix.h"
4 changes: 4 additions & 0 deletions exercises/practice/spiral-matrix/spiral_matrix.h
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
Loading