Browse Source

Exercism problems

master
Lachlan Jacob 5 years ago
parent
commit
56afbe9dc5

+ 6
- 2
.gitignore View File

@@ -1,3 +1,7 @@

.fake
.ionide
.ionide
.config
.exercism
bin/
obj/
.vscode/

+ 4
- 0
exercism/fsharp/accumulate/Accumulate.fs View File

@@ -0,0 +1,4 @@
module Accumulate

let accumulate (func: 'a -> 'b) (input: 'a list): 'b list =
input |> List.map func

+ 21
- 0
exercism/fsharp/accumulate/Accumulate.fsproj View File

@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Compile Include="Accumulate.fs" />
<Compile Include="AccumulateTests.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="FsUnit.xUnit" Version="3.8.1" />
</ItemGroup>

</Project>

+ 42
- 0
exercism/fsharp/accumulate/AccumulateTests.fs View File

@@ -0,0 +1,42 @@
// This file was created manually and its version is 2.0.0

module AccumulateTest

open System
open Xunit
open FsUnit.Xunit
open Accumulate

let reverse (str:string) = new string(str.ToCharArray() |> Array.rev)

[<Fact>]
let ``Empty accumulation produces empty accumulation`` () =
accumulate (fun x -> x + 1) List.empty |> should be Empty

[<Fact>]
let ``Identity accumulation returns unmodified list`` () =
accumulate id [1; 2; 3] |> should equal [1; 2; 3]

[<Fact>]
let ``Accumulate squares`` () =
accumulate (fun x -> x * x) [1; 2; 3] |> should equal [1; 4; 9]

[<Fact>]
let ``Accumulate upcases`` () =
accumulate (fun (x:string) -> x.ToUpper()) ["hello"; "world"]
|> should equal ["HELLO"; "WORLD"]

[<Fact>]
let ``Accumulate reversed strings`` () =
accumulate reverse (List.ofArray ("the quick brown fox etc".Split(' ')))
|> should equal (List.ofArray("eht kciuq nworb xof cte".Split(' ')))

[<Fact>]
let ``Accumulate within accumulate`` () =
accumulate (fun (x:string) -> String.concat " " (accumulate (fun y -> x + y) ["1"; "2"; "3"])) ["a"; "b"; "c"]
|> should equal ["a1 a2 a3"; "b1 b2 b3"; "c1 c2 c3"]

[<Fact>]
let ``Accumulate large data set without stack overflow`` () =
accumulate id [1..100000]
|> should equal [1..100000]

+ 52
- 0
exercism/fsharp/accumulate/README.md View File

@@ -0,0 +1,52 @@
# Accumulate

Implement the `accumulate` operation, which, given a collection and an
operation to perform on each element of the collection, returns a new
collection containing the result of applying that operation to each element of
the input collection.

Given the collection of numbers:

- 1, 2, 3, 4, 5

And the operation:

- square a number (`x => x * x`)

Your code should be able to produce the collection of squares:

- 1, 4, 9, 16, 25

Check out the test suite to see the expected function signature.

## Restrictions

Keep your hands off that collect/map/fmap/whatchamacallit functionality
provided by your standard library!
Solve this one yourself using other basic tools instead.

## Hints
For this exercise the following F# feature comes in handy:
- [Tail recursion](https://blogs.msdn.microsoft.com/fsharpteam/2011/07/08/tail-calls-in-f/) Prevent stack overflows with large input by using tail recursion. While there are no test cases checking explicitly for this, using tail recursion leads to a more performant solution. Another good resource on tail recursion is [this blog post](http://blog.ploeh.dk/2015/12/22/tail-recurse/).
- [Type inference](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/generics/#implicitly-generic-constructs) The F# compiler can automatically infer types, which means you often don't have to add any types at all. For more information on how this works and its advantages, see [this page](https://fsharpforfunandprofit.com/posts/type-inference/).


## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

## Autoformatting the code

F# source code can be formatted with the [Fantomas](https://github.com/fsprojects/fantomas) tool.

After installing it with `dotnet tool restore`, run `dotnet fantomas .` to format code within the current directory.

## Further information

For more detailed information about the F# track, including how to get help if
you're having trouble, please visit the exercism.io [F# language page](http://exercism.io/languages/fsharp/resources).

## Source

Conversation with James Edward Gray II [https://twitter.com/jeg2](https://twitter.com/jeg2)


+ 12
- 0
exercism/fsharp/collatz-conjecture/CollatzConjecture.fs View File

@@ -0,0 +1,12 @@
module CollatzConjecture

let rec countSteps counter n =
match n with
| 1 -> counter
| _ when n % 2 = 0 -> countSteps (counter + 1) (n / 2)
| _ -> countSteps (counter + 1) (n * 3 + 1)

let steps (number: int): int option =
match number with
| _ when number < 1 -> None
| _ -> Some(countSteps 0 number)

+ 21
- 0
exercism/fsharp/collatz-conjecture/CollatzConjecture.fsproj View File

@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Compile Include="CollatzConjecture.fs" />
<Compile Include="CollatzConjectureTests.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="FsUnit.xUnit" Version="3.8.1" />
</ItemGroup>

</Project>

+ 33
- 0
exercism/fsharp/collatz-conjecture/CollatzConjectureTests.fs View File

@@ -0,0 +1,33 @@
// This file was auto-generated based on version 1.2.1 of the canonical data.

module CollatzConjectureTests

open FsUnit.Xunit
open Xunit

open CollatzConjecture

[<Fact>]
let ``Zero steps for one`` () =
steps 1 |> should equal (Some 0)

[<Fact>]
let ``Divide if even`` () =
steps 16 |> should equal (Some 4)

[<Fact>]
let ``Even and odd steps`` () =
steps 12 |> should equal (Some 9)

[<Fact>]
let ``Large number of even and odd steps`` () =
steps 1000000 |> should equal (Some 152)

[<Fact>]
let ``Zero is an error`` () =
steps 0 |> should equal None

[<Fact>]
let ``Negative value is an error`` () =
steps -15 |> should equal None


+ 47
- 0
exercism/fsharp/collatz-conjecture/README.md View File

@@ -0,0 +1,47 @@
# Collatz Conjecture

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.

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

## Autoformatting the code

F# source code can be formatted with the [Fantomas](https://github.com/fsprojects/fantomas) tool.

After installing it with `dotnet tool restore`, run `dotnet fantomas .` to format code within the current directory.

## Further information

For more detailed information about the F# track, including how to get help if
you're having trouble, please visit the exercism.io [F# language page](http://exercism.io/languages/fsharp/resources).

## Source

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


+ 3
- 0
exercism/fsharp/hello-world/HelloWorld.fs View File

@@ -0,0 +1,3 @@
module HelloWorld

let hello: string = "Hello, World!"

+ 21
- 0
exercism/fsharp/hello-world/HelloWorld.fsproj View File

@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Compile Include="HelloWorld.fs" />
<Compile Include="HelloWorldTests.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="FsUnit.xUnit" Version="3.8.1" />
</ItemGroup>

</Project>

+ 13
- 0
exercism/fsharp/hello-world/HelloWorldTests.fs View File

@@ -0,0 +1,13 @@
// This file was auto-generated based on version 1.1.0 of the canonical data.

module HelloWorldTests

open FsUnit.Xunit
open Xunit

open HelloWorld

[<Fact>]
let ``Say Hi!`` () =
hello |> should equal "Hello, World!"


+ 35
- 0
exercism/fsharp/hello-world/README.md View File

@@ -0,0 +1,35 @@
# Hello World

The classical introductory exercise. Just say "Hello, World!".

["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is
the traditional first program for beginning programming in a new language
or environment.

The objectives are simple:

- Write a function that returns the string "Hello, World!".
- Run the test suite and make sure that it succeeds.
- Submit your solution and check it at the website.

If everything goes well, you will be ready to fetch your first real exercise.

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

## Autoformatting the code

F# source code can be formatted with the [Fantomas](https://github.com/fsprojects/fantomas) tool.

After installing it with `dotnet tool restore`, run `dotnet fantomas .` to format code within the current directory.

## Further information

For more detailed information about the F# track, including how to get help if
you're having trouble, please visit the exercism.io [F# language page](http://exercism.io/languages/fsharp/resources).

## Source

This is an exercise to introduce users to using Exercism [http://en.wikipedia.org/wiki/%22Hello,_world!%22_program](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program)


+ 3
- 0
exercism/fsharp/leap/Leap.fs View File

@@ -0,0 +1,3 @@
module Leap

let leapYear (year: int): bool = year % 4 = 0 && (year % 100 <> 0 || year % 400 = 0)

+ 21
- 0
exercism/fsharp/leap/Leap.fsproj View File

@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Compile Include="Leap.fs" />
<Compile Include="LeapTests.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="FsUnit.xUnit" Version="3.8.1" />
</ItemGroup>

</Project>

+ 45
- 0
exercism/fsharp/leap/LeapTests.fs View File

@@ -0,0 +1,45 @@
// This file was auto-generated based on version 1.6.0 of the canonical data.

module LeapTests

open FsUnit.Xunit
open Xunit

open Leap

[<Fact>]
let ``Year not divisible by 4 in common year`` () =
leapYear 2015 |> should equal false

[<Fact>]
let ``Year divisible by 2, not divisible by 4 in common year`` () =
leapYear 1970 |> should equal false

[<Fact>]
let ``Year divisible by 4, not divisible by 100 in leap year`` () =
leapYear 1996 |> should equal true

[<Fact>]
let ``Year divisible by 4 and 5 is still a leap year`` () =
leapYear 1960 |> should equal true

[<Fact>]
let ``Year divisible by 100, not divisible by 400 in common year`` () =
leapYear 2100 |> should equal false

[<Fact>]
let ``Year divisible by 100 but not by 3 is still not a leap year`` () =
leapYear 1900 |> should equal false

[<Fact>]
let ``Year divisible by 400 in leap year`` () =
leapYear 2000 |> should equal true

[<Fact>]
let ``Year divisible by 400 but not by 125 is still a leap year`` () =
leapYear 2400 |> should equal true

[<Fact>]
let ``Year divisible by 200, not divisible by 400 in common year`` () =
leapYear 1800 |> should equal false


+ 49
- 0
exercism/fsharp/leap/README.md View File

@@ -0,0 +1,49 @@
# Leap

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

## Notes

The DateTime class in F# provides a built-in [IsLeapYear](https://msdn.microsoft.com/en-us/library/system.datetime.isleapyear(v=vs.110).aspx?cs-save-lang=1&cs-lang=fsharp) method
which you should pretend doesn't exist for the purposes of implementing this exercise.

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

## Autoformatting the code

F# source code can be formatted with the [Fantomas](https://github.com/fsprojects/fantomas) tool.

After installing it with `dotnet tool restore`, run `dotnet fantomas .` to format code within the current directory.

## Further information

For more detailed information about the F# track, including how to get help if
you're having trouble, please visit the exercism.io [F# language page](http://exercism.io/languages/fsharp/resources).

## Source

JavaRanch Cattle Drive, exercise 3 [http://www.javaranch.com/leap.jsp](http://www.javaranch.com/leap.jsp)


+ 20
- 0
exercism/fsharp/queen-attack/QueenAttack.fs View File

@@ -0,0 +1,20 @@
module QueenAttack

let allowed pos = pos > -1 && pos < 8

let create (position: int * int) =
match position with
| (r, c) when allowed r && allowed c -> true
| _ -> false

let difference a b = abs (a - b)

let diagonallyAttacked (a: int * int) (b: int * int) =
let cmpARow = a |> fst |> difference
let cmpACol = a |> snd |> difference
(b |> fst |> cmpARow) = (b |> snd |> cmpACol)

let canAttack (queen1: int * int) (queen2: int * int) =
(=) (queen1 |> fst) (queen2 |> fst) ||
(=) (queen1 |> snd) (queen2 |> snd) ||
diagonallyAttacked queen1 queen2

+ 21
- 0
exercism/fsharp/queen-attack/QueenAttack.fsproj View File

@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Compile Include="QueenAttack.fs" />
<Compile Include="QueenAttackTests.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="FsUnit.xUnit" Version="3.8.1" />
</ItemGroup>

</Project>

+ 71
- 0
exercism/fsharp/queen-attack/QueenAttackTests.fs View File

@@ -0,0 +1,71 @@
// This file was auto-generated based on version 2.3.0 of the canonical data.

module QueenAttackTests

open FsUnit.Xunit
open Xunit

open QueenAttack

[<Fact>]
let ``Queen with a valid position`` () =
create (2, 2) |> should equal true

[<Fact>]
let ``Queen must have positive row`` () =
create (-2, 2) |> should equal false

[<Fact>]
let ``Queen must have row on board`` () =
create (8, 4) |> should equal false

[<Fact>]
let ``Queen must have positive column`` () =
create (2, -2) |> should equal false

[<Fact>]
let ``Queen must have column on board`` () =
create (4, 8) |> should equal false

[<Fact>]
let ``Can not attack`` () =
let whiteQueen = (2, 4)
let blackQueen = (6, 6)
canAttack blackQueen whiteQueen |> should equal false

[<Fact>]
let ``Can attack on same row`` () =
let whiteQueen = (2, 4)
let blackQueen = (2, 6)
canAttack blackQueen whiteQueen |> should equal true

[<Fact>]
let ``Can attack on same column`` () =
let whiteQueen = (4, 5)
let blackQueen = (2, 5)
canAttack blackQueen whiteQueen |> should equal true

[<Fact>]
let ``Can attack on first diagonal`` () =
let whiteQueen = (2, 2)
let blackQueen = (0, 4)
canAttack blackQueen whiteQueen |> should equal true

[<Fact>]
let ``Can attack on second diagonal`` () =
let whiteQueen = (2, 2)
let blackQueen = (3, 1)
canAttack blackQueen whiteQueen |> should equal true

[<Fact>]
let ``Can attack on third diagonal`` () =
let whiteQueen = (2, 2)
let blackQueen = (1, 1)
canAttack blackQueen whiteQueen |> should equal true

[<Fact>]
let ``Can attack on fourth diagonal`` () =
let whiteQueen = (1, 7)
let blackQueen = (0, 6)
canAttack blackQueen whiteQueen |> should equal true


+ 47
- 0
exercism/fsharp/queen-attack/README.md View File

@@ -0,0 +1,47 @@
# Queen Attack

Given the position of two queens on a chess board, indicate whether or not they
are positioned so that they can attack each other.

In the game of chess, a queen can attack pieces which are on the same
row, column, or diagonal.

A chessboard can be represented by an 8 by 8 array.

So if you're told the white queen is at (2, 3) and the black queen at
(5, 6), then you'd know you've got a set-up like so:

```text
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ W _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ B _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
```

You'd also be able to answer whether the queens can attack each other.
In this case, that answer would be yes, they can, because both pieces
share a diagonal.

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

## Autoformatting the code

F# source code can be formatted with the [Fantomas](https://github.com/fsprojects/fantomas) tool.

After installing it with `dotnet tool restore`, run `dotnet fantomas .` to format code within the current directory.

## Further information

For more detailed information about the F# track, including how to get help if
you're having trouble, please visit the exercism.io [F# language page](http://exercism.io/languages/fsharp/resources).

## Source

J Dalbey's Programming Practice problems [http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html](http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html)


+ 39
- 0
exercism/fsharp/raindrops/README.md View File

@@ -0,0 +1,39 @@
# Raindrops

Your task is to convert a number into a string that contains raindrop sounds corresponding to certain potential factors. A factor is a number that evenly divides into another number, leaving no remainder. The simplest way to test if a one number is a factor of another is to use the [modulo operation](https://en.wikipedia.org/wiki/Modulo_operation).

The rules of `raindrops` are that if a given number:

- has 3 as a factor, add 'Pling' to the result.
- has 5 as a factor, add 'Plang' to the result.
- has 7 as a factor, add 'Plong' to the result.
- _does not_ have any of 3, 5, or 7 as a factor, the result should be the digits of the number.

## Examples

- 28 has 7 as a factor, but not 3 or 5, so the result would be "Plong".
- 30 has both 3 and 5 as factors, but not 7, so the result would be "PlingPlang".
- 34 is not factored by 3, 5, or 7, so the result would be "34".

## Hints
- Think of this in a generic way. If you're familiar with the (fizz buzz)[https://en.wikipedia.org/wiki/Fizz_buzz] problem this is similar except there are three conditions instead of two. How would you implement this knowing that one day we might want to extend to four, five, or even ten types of raindrops?

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

## Autoformatting the code

F# source code can be formatted with the [Fantomas](https://github.com/fsprojects/fantomas) tool.

After installing it with `dotnet tool restore`, run `dotnet fantomas .` to format code within the current directory.

## Further information

For more detailed information about the F# track, including how to get help if
you're having trouble, please visit the exercism.io [F# language page](http://exercism.io/languages/fsharp/resources).

## Source

A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division. [https://en.wikipedia.org/wiki/Fizz_buzz](https://en.wikipedia.org/wiki/Fizz_buzz)


+ 9
- 0
exercism/fsharp/raindrops/Raindrops.fs View File

@@ -0,0 +1,9 @@
module Raindrops

type ResultState = (bool * string)

let applyStep factor str n rs = if n % factor = 0 then (true, ((rs |> snd) + str)) else rs

let convert (number: int): string =
let result = (false, "") |> applyStep 3 "Pling" number |> applyStep 5 "Plang" number |> applyStep 7 "Plong" number
if result |> fst then result |> snd else string number

+ 21
- 0
exercism/fsharp/raindrops/Raindrops.fsproj View File

@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Compile Include="Raindrops.fs" />
<Compile Include="RaindropsTests.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="FsUnit.xUnit" Version="3.8.1" />
</ItemGroup>

</Project>

+ 81
- 0
exercism/fsharp/raindrops/RaindropsTests.fs View File

@@ -0,0 +1,81 @@
// This file was auto-generated based on version 1.1.0 of the canonical data.

module RaindropsTests

open FsUnit.Xunit
open Xunit

open Raindrops

[<Fact>]
let ``The sound for 1 is 1`` () =
convert 1 |> should equal "1"

[<Fact>]
let ``The sound for 3 is Pling`` () =
convert 3 |> should equal "Pling"

[<Fact>]
let ``The sound for 5 is Plang`` () =
convert 5 |> should equal "Plang"

[<Fact>]
let ``The sound for 7 is Plong`` () =
convert 7 |> should equal "Plong"

[<Fact>]
let ``The sound for 6 is Pling as it has a factor 3`` () =
convert 6 |> should equal "Pling"

[<Fact>]
let ``2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base`` () =
convert 8 |> should equal "8"

[<Fact>]
let ``The sound for 9 is Pling as it has a factor 3`` () =
convert 9 |> should equal "Pling"

[<Fact>]
let ``The sound for 10 is Plang as it has a factor 5`` () =
convert 10 |> should equal "Plang"

[<Fact>]
let ``The sound for 14 is Plong as it has a factor of 7`` () =
convert 14 |> should equal "Plong"

[<Fact>]
let ``The sound for 15 is PlingPlang as it has factors 3 and 5`` () =
convert 15 |> should equal "PlingPlang"

[<Fact>]
let ``The sound for 21 is PlingPlong as it has factors 3 and 7`` () =
convert 21 |> should equal "PlingPlong"

[<Fact>]
let ``The sound for 25 is Plang as it has a factor 5`` () =
convert 25 |> should equal "Plang"

[<Fact>]
let ``The sound for 27 is Pling as it has a factor 3`` () =
convert 27 |> should equal "Pling"

[<Fact>]
let ``The sound for 35 is PlangPlong as it has factors 5 and 7`` () =
convert 35 |> should equal "PlangPlong"

[<Fact>]
let ``The sound for 49 is Plong as it has a factor 7`` () =
convert 49 |> should equal "Plong"

[<Fact>]
let ``The sound for 52 is 52`` () =
convert 52 |> should equal "52"

[<Fact>]
let ``The sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7`` () =
convert 105 |> should equal "PlingPlangPlong"

[<Fact>]
let ``The sound for 3125 is Plang as it has a factor 5`` () =
convert 3125 |> should equal "Plang"


+ 46
- 0
exercism/fsharp/two-fer/README.md View File

@@ -0,0 +1,46 @@
# Two Fer

`Two-fer` or `2-fer` is short for two for one. One for you and one for me.

Given a name, return a string with the message:

```text
One for X, one for me.
```

Where X is the given name.

However, if the name is missing, return the string:

```text
One for you, one for me.
```

Here are some examples:

|Name |String to return
|:-------|:------------------
|Alice |One for Alice, one for me.
|Bob |One for Bob, one for me.
| |One for you, one for me.
|Zaphod |One for Zaphod, one for me.

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

## Autoformatting the code

F# source code can be formatted with the [Fantomas](https://github.com/fsprojects/fantomas) tool.

After installing it with `dotnet tool restore`, run `dotnet fantomas .` to format code within the current directory.

## Further information

For more detailed information about the F# track, including how to get help if
you're having trouble, please visit the exercism.io [F# language page](http://exercism.io/languages/fsharp/resources).

## Source

[https://github.com/exercism/problem-specifications/issues/757](https://github.com/exercism/problem-specifications/issues/757)


+ 6
- 0
exercism/fsharp/two-fer/TwoFer.fs View File

@@ -0,0 +1,6 @@
module TwoFer

let twoFer (input: string option): string =
match input with
| Some(name) -> "One for " + name + ", one for me."
| None -> "One for you, one for me."

+ 15
- 0
exercism/fsharp/two-fer/TwoFer.fsproj View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="TwoFer.fs" />
<Compile Include="TwoFerTests.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="FsUnit.xUnit" Version="3.8.1" />
</ItemGroup>
</Project>

+ 21
- 0
exercism/fsharp/two-fer/TwoFerTests.fs View File

@@ -0,0 +1,21 @@
// This file was auto-generated based on version 1.2.0 of the canonical data.

module TwoFerTests

open FsUnit.Xunit
open Xunit

open TwoFer

[<Fact>]
let ``No name given`` () =
twoFer None |> should equal "One for you, one for me."

[<Fact>]
let ``A name given`` () =
twoFer (Some "Alice") |> should equal "One for Alice, one for me."

[<Fact>]
let ``Another name given`` () =
twoFer (Some "Bob") |> should equal "One for Bob, one for me."


Loading…
Cancel
Save