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

Haskell collatz conjecture #188

Merged
merged 11 commits into from
Aug 3, 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
7 changes: 4 additions & 3 deletions .github/workflows/haskell.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
---
#
# .github/workflows/haskell.yml
#
---
name: Haskell Workflow
on: # yamllint disable-line rule:truthy
pull_request:

jobs:
stage1:
name: Change Check
Expand All @@ -31,7 +32,7 @@ jobs:
# Check if the files are present in the changed file list (added, modified, deleted) then output to stdout.
HAS_DIFF=false
printf "=== Which Haskell files changed? ===\n"
if printf "%s\n" "${GIT_DIFF}" | grep -E '^(.*[.](hs|cabal)|.*/stack[.]yaml[.].*|.github/workflows/haskell.yml)$'; then
if printf "%s\n" "${GIT_DIFF}" | grep -E '^(haskell/.*[.](hs|cabal)|haskell/.*/stack[.]yaml[.].*|.github/workflows/haskell.yml)$'; then
HAS_DIFF=true
fi
printf "\n"
Expand Down Expand Up @@ -94,6 +95,6 @@ jobs:
./for_each hlint src --report
- name: Testing with coverage
id: haskell-test-coverage
run: |
run: |-
cd ./haskell
./for_each stack test --coverage
5 changes: 5 additions & 0 deletions haskell/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@
- [space-age](./space-age/README.md)
- [bob](./bob/README.md)
- [triangle](./triangle/README.md)
- [collatz-conjecture](./collatz-conjecture/README.md)
- [hamming](./hamming/README.md)
- [protein-translation](./protein-translation/README.md)
- [robot-simulator](./robot-simulator/README.md)
- [yacht](./yacht/README.md)
30 changes: 30 additions & 0 deletions haskell/collatz-conjecture/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"authors": [],
"contributors": [
"guygastineau",
"iHiD",
"navossoc",
"petertseng",
"ppartarr",
"sshine",
"tejasbubane"
],
"files": {
"solution": [
"src/CollatzConjecture.hs",
"package.yaml"
],
"test": [
"test/Tests.hs"
],
"example": [
".meta/examples/success-standard/src/CollatzConjecture.hs"
],
"invalidator": [
"stack.yaml"
]
},
"blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.",
"source": "An unsolved problem in mathematics named after mathematician Lothar Collatz",
"source_url": "https://en.wikipedia.org/wiki/3x_%2B_1_problem"
}
1 change: 1 addition & 0 deletions haskell/collatz-conjecture/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"haskell","exercise":"collatz-conjecture","id":"17d391e3c4b6400cba19f838366e68b9","url":"https://exercism.org/tracks/haskell/exercises/collatz-conjecture","handle":"vpayno","is_requester":true,"auto_approve":false}
86 changes: 86 additions & 0 deletions haskell/collatz-conjecture/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Help

## Running the tests

To run the test suite, execute the following command:

```bash
stack test
```

#### If you get an error message like this...

```
No .cabal file found in directory
```

or

```
RedownloadInvalidResponse Request {
...
}
"/home/username/.stack/build-plan/lts-xx.yy.yaml" (Response {responseStatus = Status {statusCode = 404, statusMessage = "Not Found"},
```

You are probably running an old stack version and need
to upgrade it. Try running:

```bash
stack upgrade
```

Or see other options for upgrading at [Stack documentation](https://docs.haskellstack.org/en/stable/install_and_upgrade/#upgrade).

#### Otherwise, if you get an error message like this...

```
No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...
```

Just do as it says and it will download and install
the correct compiler version:

```bash
stack setup
```

If you want to play with your solution in GHCi, just run the command:

```bash
stack ghci
```

## Submitting your solution

You can submit your solution using the `exercism submit src/CollatzConjecture.hs package.yaml` 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 [Haskell track's documentation](https://exercism.org/docs/tracks/haskell)
- [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.

## Getting Started

Please refer to the [installation](https://exercism.io/tracks/haskell/installation)
and [learning](https://exercism.io/tracks/haskell/learning) help pages.

## Feedback, Issues, Pull Requests

The [exercism/haskell](https://github.com/exercism/haskell) repository on
GitHub is the home for all of the Haskell exercises.

If you have feedback about an exercise, or want to help implementing a new
one, head over there and create an issue. We'll do our best to help you!
53 changes: 53 additions & 0 deletions haskell/collatz-conjecture/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Collatz Conjecture

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

## Instructions

The Collatz Conjecture or 3x+1 problem can be summarized as follows:

Take any positive integer n. If n is even, divide n by 2 to get n / 2. If n is
odd, multiply n by 3 and add 1 to get 3n + 1. Repeat the process indefinitely.
The conjecture states that no matter which number you start with, you will
always reach 1 eventually.

Given a number n, return the number of steps required to reach 1.

## Examples

Starting with n = 12, the steps would be as follows:

0. 12
1. 6
2. 3
3. 10
4. 5
5. 16
6. 8
7. 4
8. 2
9. 1

Resulting in 9 steps. So for input n = 12, the return value would be 9.

## Source

### Contributed to by

- @guygastineau
- @iHiD
- @navossoc
- @petertseng
- @ppartarr
- @sshine
- @tejasbubane

### Based on

An unsolved problem in mathematics named after mathematician Lothar Collatz - https://en.wikipedia.org/wiki/3x_%2B_1_problem

### My Solution

- [my solution](./src/CollatzConjecture.hs)
- [run-tests](./run-tests-haskell.txt)
34 changes: 34 additions & 0 deletions haskell/collatz-conjecture/collatz-conjecture.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.35.1.
--
-- see: https://github.com/sol/hpack

name: collatz-conjecture
version: 1.2.1.4
build-type: Simple

library
exposed-modules:
CollatzConjecture
other-modules:
Paths_collatz_conjecture
hs-source-dirs:
src
ghc-options: -Wall
build-depends:
base
default-language: Haskell2010

test-suite test
type: exitcode-stdio-1.0
main-is: Tests.hs
other-modules:
Paths_collatz_conjecture
hs-source-dirs:
test
build-depends:
base
, collatz-conjecture
, hspec
default-language: Haskell2010
21 changes: 21 additions & 0 deletions haskell/collatz-conjecture/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: collatz-conjecture
version: 1.2.1.4

dependencies:
- base

library:
exposed-modules: CollatzConjecture
source-dirs: src
ghc-options: -Wall
# dependencies:
# - foo # List here the packages you
# - bar # want to use in your solution.

tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- collatz-conjecture
- hspec
Loading