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

C leap #145

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
- [high-scores](./high-scores/README.md)
- [bob](./bob/README.md)
- [triangle](./triangle/README.md)
- [leap](./leap/README.md)
35 changes: 35 additions & 0 deletions c/leap/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"authors": [
"lpil"
],
"contributors": [
"bcc32",
"Gamecock",
"gea-migration",
"h-3-0",
"hintjens",
"JacobMikkelsen",
"kytrinyx",
"patricksjackson",
"QLaille",
"RealBarrettBrown",
"ryanplusplus",
"wolf99"
],
"files": {
"solution": [
"leap.c",
"leap.h"
],
"test": [
"test_leap.c"
],
"example": [
".meta/example.c",
".meta/example.h"
]
},
"blurb": "Given a year, report if it is a leap year.",
"source": "JavaRanch Cattle Drive, exercise 3",
"source_url": "http://www.javaranch.com/leap.jsp"
}
1 change: 1 addition & 0 deletions c/leap/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"c","exercise":"leap","id":"8925652346e9409fb291054adb377da1","url":"https://exercism.org/tracks/c/exercises/leap","handle":"vpayno","is_requester":true,"auto_approve":false}
63 changes: 63 additions & 0 deletions c/leap/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Help

## Running the tests

Get the first test compiling, linking and passing by following the [three rules of test-driven development][3-tdd-rules].

The included makefile can be used to create and run the tests using the `test` task.

```console
$ make test
```

Create just the functions you need to satisfy any compiler errors and get the test to fail.
Then write just enough code to get the test to pass.
Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

[3-tdd-rules]: https://blog.cleancoder.com/uncle-bob/2014/12/17/TheCyclesOfTDD.html

## Submitting your solution

You can submit your solution using the `exercism submit leap.c leap.h` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [C track's documentation](https://exercism.org/docs/tracks/c)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

Make sure you have read the [C track-specific documentation][c-track] on the Exercism site.
This covers the basic information on setting up the development environment expected by the exercises.

## Submitting Incomplete Solutions

If you are struggling with a particular exercise, it is possible to submit an incomplete solution so you can see how others have completed the exercise.

## Resources

To get help if having trouble, you can use the following resources:

- [StackOverflow][] can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
- [CPPReference][] can be used to look up information on C concepts, operators, types, standard library functions and more.
- [TutorialsPoint][] has similar content as CPPReference in its C programming section.
- [The C Programming][K&R] book by K&R is the original source of the language and is still useful today.

[c-track]: https://exercism.org/docs/tracks/c
[stackoverflow]: http://stackoverflow.com/questions/tagged/c
[cppreference]: https://en.cppreference.com/w/c
[tutorialspoint]: https://www.tutorialspoint.com/cprogramming/
[K&R]: https://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628/
57 changes: 57 additions & 0 deletions c/leap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Leap

Welcome to Leap on Exercism's C Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

Given a year, report if it is a leap year.

The tricky thing here is that a leap year in the Gregorian calendar occurs:

```text
on every year that is evenly divisible by 4
except every year that is evenly divisible by 100
unless the year is also evenly divisible by 400
```

For example, 1997 is not a leap year, but 1996 is.
1900 is not a leap year, but 2000 is.

## Notes

Though our exercise adopts some very simple rules, there is more to learn!

For a delightful, four minute explanation of the whole leap year phenomenon, go watch [this youtube video][video].

[video]: http://www.youtube.com/watch?v=xX96xng7sAE

## Source

### Created by

- @lpil

### Contributed to by

- @bcc32
- @Gamecock
- @gea-migration
- @h-3-0
- @hintjens
- @JacobMikkelsen
- @kytrinyx
- @patricksjackson
- @QLaille
- @RealBarrettBrown
- @ryanplusplus
- @wolf99

### Based on

JavaRanch Cattle Drive, exercise 3 - http://www.javaranch.com/leap.jsp

### My Solution

- [my solution](./leap.c)
- [run-tests](./run-tests-c.txt)
17 changes: 17 additions & 0 deletions c/leap/leap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "leap.h"

bool leap_year(int year) {
if (year % 400 == 0) {
return true;
}

if (year % 100 == 0) {
return false;
}

if (year % 4 == 0) {
return true;
}

return false;
}
8 changes: 8 additions & 0 deletions c/leap/leap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef LEAP_H
#define LEAP_H

#include <stdbool.h>

bool leap_year(int year);

#endif
37 changes: 37 additions & 0 deletions c/leap/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)
Loading