Bladeren bron

A few more exercises done

master
Lachlan Jacob 5 jaren geleden
bovenliggende
commit
8e1727f8da

+ 8
- 0
exercism/fsharp/hamming/Hamming.fs Bestand weergeven

@@ -0,0 +1,8 @@
module Hamming

let hamming s1 s2 =
let pairs = Seq.zip s1 s2
Seq.fold (fun a x -> if (x |> fst) <> (x |> snd) then (a + 1) else a) 0 pairs

let distance (strand1: string) (strand2: string): int option =
if String.length strand1 <> String.length strand2 then None else Some(hamming strand1 strand2)

+ 21
- 0
exercism/fsharp/hamming/Hamming.fsproj Bestand weergeven

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

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

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

<ItemGroup>
<Compile Include="Hamming.fs" />
<Compile Include="HammingTests.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/hamming/HammingTests.fs Bestand weergeven

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

module HammingTests

open FsUnit.Xunit
open Xunit

open Hamming

[<Fact>]
let ``Empty strands`` () =
distance "" "" |> should equal (Some 0)

[<Fact>]
let ``Single letter identical strands`` () =
distance "A" "A" |> should equal (Some 0)

[<Fact>]
let ``Single letter different strands`` () =
distance "G" "T" |> should equal (Some 1)

[<Fact>]
let ``Long identical strands`` () =
distance "GGACTGAAATCTG" "GGACTGAAATCTG" |> should equal (Some 0)

[<Fact>]
let ``Long different strands`` () =
distance "GGACGGATTCTG" "AGGACGGATTCT" |> should equal (Some 9)

[<Fact>]
let ``Disallow first strand longer`` () =
distance "AATG" "AAA" |> should equal None

[<Fact>]
let ``Disallow second strand longer`` () =
distance "ATA" "AGTG" |> should equal None

[<Fact>]
let ``Disallow left empty strand`` () =
distance "" "G" |> should equal None

[<Fact>]
let ``Disallow right empty strand`` () =
distance "G" "" |> should equal None


+ 44
- 0
exercism/fsharp/hamming/README.md Bestand weergeven

@@ -0,0 +1,44 @@
# Hamming

Calculate the Hamming Distance between two DNA strands.

Your body is made up of cells that contain DNA. Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells. In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime!

When cells divide, their DNA replicates too. Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information. If we compare two strands of DNA and count the differences between them we can see how many mistakes occurred. This is known as the "Hamming Distance".

We read DNA using the letters C,A,G and T. Two strands might look like this:

GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^ ^ ^ ^^

They have 7 differences, and therefore the Hamming Distance is 7.

The Hamming Distance is useful for lots of things in science, not just biology, so it's a nice phrase to be familiar with :)

# Implementation notes

The Hamming distance is only defined for sequences of equal length, so
an attempt to calculate it between sequences of different lengths should
not work. The general handling of this situation (e.g., raising an
exception vs returning a special value) may differ between languages.

## 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

The Calculating Point Mutations problem at Rosalind [http://rosalind.info/problems/hamm/](http://rosalind.info/problems/hamm/)


+ 42
- 0
exercism/fsharp/space-age/README.md Bestand weergeven

@@ -0,0 +1,42 @@
# Space Age

Given an age in seconds, calculate how old someone would be on:

- Mercury: orbital period 0.2408467 Earth years
- Venus: orbital period 0.61519726 Earth years
- Earth: orbital period 1.0 Earth years, 365.25 Earth days, or 31557600 seconds
- Mars: orbital period 1.8808158 Earth years
- Jupiter: orbital period 11.862615 Earth years
- Saturn: orbital period 29.447498 Earth years
- Uranus: orbital period 84.016846 Earth years
- Neptune: orbital period 164.79132 Earth years

So if you were told someone were 1,000,000,000 seconds old, you should
be able to say that they're 31.69 Earth-years old.

If you're wondering why Pluto didn't make the cut, go watch [this
youtube video](http://www.youtube.com/watch?v=Z_2gbGXzFbs).

## Hints
- Try to focus on minimizing the amount of code duplication. If you find yourself doing a lot of copy/paste take a step back and think about how the code can be refactored
- [Pattern matching](https://fsharpforfunandprofit.com/posts/match-expression/) is more idiomatic than using dictionaries to translate values

## 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

Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. [http://pine.fm/LearnToProgram/?Chapter=01](http://pine.fm/LearnToProgram/?Chapter=01)


+ 24
- 0
exercism/fsharp/space-age/SpaceAge.fs Bestand weergeven

@@ -0,0 +1,24 @@
module SpaceAge

type Planet =
| Mercury
| Venus
| Earth
| Mars
| Jupiter
| Saturn
| Uranus
| Neptune

let age (planet: Planet) (seconds: int64): float =
(/) (float (seconds))
((match planet with
| Mercury -> 0.2408467
| Venus -> 0.61519726
| Mars -> 1.8808158
| Jupiter -> 11.862615
| Saturn -> 29.447498
| Uranus -> 84.016846
| Neptune -> 164.79132
| _ -> 1.0)
* 31557600.0)

+ 21
- 0
exercism/fsharp/space-age/SpaceAge.fsproj Bestand weergeven

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

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

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

<ItemGroup>
<Compile Include="SpaceAge.fs" />
<Compile Include="SpaceAgeTests.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>

+ 41
- 0
exercism/fsharp/space-age/SpaceAgeTests.fs Bestand weergeven

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

module SpaceAgeTests

open FsUnit.Xunit
open Xunit

open SpaceAge

[<Fact>]
let ``Age on Earth`` () =
age Earth 1000000000L |> should (equalWithin 0.01) 31.69

[<Fact>]
let ``Age on Mercury`` () =
age Mercury 2134835688L |> should (equalWithin 0.01) 280.88

[<Fact>]
let ``Age on Venus`` () =
age Venus 189839836L |> should (equalWithin 0.01) 9.78

[<Fact>]
let ``Age on Mars`` () =
age Mars 2129871239L |> should (equalWithin 0.01) 35.88

[<Fact>]
let ``Age on Jupiter`` () =
age Jupiter 901876382L |> should (equalWithin 0.01) 2.41

[<Fact>]
let ``Age on Saturn`` () =
age Saturn 2000000000L |> should (equalWithin 0.01) 2.15

[<Fact>]
let ``Age on Uranus`` () =
age Uranus 1210123456L |> should (equalWithin 0.01) 0.46

[<Fact>]
let ``Age on Neptune`` () =
age Neptune 1821023456L |> should (equalWithin 0.01) 0.35


Laden…
Annuleren
Opslaan