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

F# hw 6.2 #13

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ run
.gcda
.gcno

FirstSteps/FirstSteps/bin/*
FirstSteps/FirstSteps/obj/*
**/bin/*
**/obj/*
27 changes: 27 additions & 0 deletions lastSteps/Tests/Tests.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="prog.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FsCheck" Version="3.0.0-alpha3" />
<PackageReference Include="FsUnit" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\lastSteps\lastSteps.fsproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="FSharp.Core" Version="4.3.4" />
</ItemGroup>

</Project>
75 changes: 75 additions & 0 deletions lastSteps/Tests/prog.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
open NUnit.Framework
open FsUnit
open hw

let rec permutations list taken =
seq { if Set.count taken = List.length list then yield [] else
for l in list do
if not (Set.contains l taken) then
for perm in permutations list (Set.add l taken) do
yield l::perm }

let testIterator (x:List<int>) =
let bst = BinaryTree<int>()
for t in x do bst.Insert t;
let mutable t = 0
for it in bst do
it |> should equal t
t <- t + 1;
t |> should equal x.Length

let testFind (x:List<int>) =
let bst = BinaryTree<int>()
for t in x do bst.Insert t;
for i in -1..x.Length do
(bst.At i ) |> should equal (not (i < 0 || i >= x.Length))

let testErase (x:List<int>) =
for k in -1..x.Length do
let bst = BinaryTree<int>()
for t in x do bst.Insert t
bst.Erase k
let mutable t = 0;
for p in bst do
if t = k then t <- t + 1
t |> should equal p
t <- t + 1

let randomTest (n) =
let bst = BinaryTree<int>()
let mutable st = Set.empty
for i in 1..n do
let el = rnd.Next()
if rnd.Next() % 2 = 0 then
if Set.contains el st then
bst.At el |> should be True
else
bst.At el |> should be False
bst.Insert el
bst.At el |> should be True
else
let sz = bst.Size()
if Set.contains el st then
bst.At el |> should be True
bst.Erase el
bst.At el |> should be False
bst.Size() |> should equal (sz - 1)
else
bst.At el |> should be False
bst.Erase el
bst.At el |> should be False
bst.Size() |> should equal sz

[<Test>]
let ``test`` () =
let n = 8
for x in permutations (List.init n id) Set.empty do
testIterator x

for x in permutations (List.init n id) Set.empty do
testFind x

for x in permutations (List.init (n - 1) id) Set.empty do
testErase x

Choose a reason for hiding this comment

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

Один большой тест не прокатит

for i in 1..100 do
randomTest(100)
31 changes: 31 additions & 0 deletions lastSteps/lastSteps.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2027
MinimumVisualStudioVersion = 10.0.40219.1
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "lastSteps", "lastSteps\lastSteps.fsproj", "{0D1A3D07-30A9-421F-9367-6AD852CF97D5}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Tests", "Tests\Tests.fsproj", "{DDA2F794-D1CC-4046-9237-3B2C1F7D7867}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0D1A3D07-30A9-421F-9367-6AD852CF97D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0D1A3D07-30A9-421F-9367-6AD852CF97D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0D1A3D07-30A9-421F-9367-6AD852CF97D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0D1A3D07-30A9-421F-9367-6AD852CF97D5}.Release|Any CPU.Build.0 = Release|Any CPU
{DDA2F794-D1CC-4046-9237-3B2C1F7D7867}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DDA2F794-D1CC-4046-9237-3B2C1F7D7867}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DDA2F794-D1CC-4046-9237-3B2C1F7D7867}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DDA2F794-D1CC-4046-9237-3B2C1F7D7867}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1507EBBF-D6FE-4AEA-B727-11CB0FEC1811}
EndGlobalSection
EndGlobal
16 changes: 16 additions & 0 deletions lastSteps/lastSteps/lastSteps.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="prog.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="FSharp.Core" Version="4.3.4" />
</ItemGroup>

</Project>
105 changes: 105 additions & 0 deletions lastSteps/lastSteps/prog.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Learn more about F# at http://fsharp.org
module hw

open System
open System.Collections
open System.Collections.Generic
open System.IO

let read = bigint.Parse << Console.ReadLine
let readInt = Int32.Parse << Console.ReadLine
let readStr = Console.ReadLine
let rnd = System.Random()

type Tree<'T> =
| Tree of 'T * Tree<'T> * Tree<'T>
| Leaf

/// <summary>
/// Implementation of simple binary tree
/// </summary>
type BinaryTree<'T when 'T : comparison>() =
let mutable root = Leaf
let mutable size = 0

let iterator () =
let rec handle (node : Tree<'T>) =
seq {
match node with
| Tree(curValue, left, right) ->
yield! handle left
yield curValue
yield! handle right
| Leaf -> yield! Seq.empty
}
handle root

interface IEnumerable<'T> with

member this.GetEnumerator(): IEnumerator<'T> =
iterator().GetEnumerator()

member this.GetEnumerator(): IEnumerator =
iterator().GetEnumerator() :> IEnumerator

member v.Empty () = size = 0

member v.Size () = size

/// <summary>
/// Inserts an element into the tree.
/// </summary>
member v.Insert (value : 'T) =
let rec handle (value : 'T) (node : Tree<'T>) =
match node with
| Leaf -> Tree(value, Leaf, Leaf)
| Tree(curValue, left, right) when value < curValue -> Tree(curValue, handle value left, right)
| Tree(curValue, left, right) -> Tree(curValue, left, handle value right)
root <- handle value root
size <- size + 1

/// <summary>
/// Removes an element from the tree, if exists.
/// </summary>
member v.Erase (value : 'T) =
let rec cutLeft node =
match node with
| Tree(value, Leaf, right) -> (value, right)
| Tree(value, left, right) ->
let (cutted, newChild) = cutLeft left
(cutted, Tree(value, newChild, right))

let rec handle value node =
match node with
| Leaf ->
size <- size + 1;
Leaf
| Tree(curValue, left, right) when value < curValue -> Tree(curValue, handle value left, right)
| Tree(curValue, left, right) when curValue < value -> Tree(curValue, left, handle value right)
| Tree(_, left, right) ->
if left = Leaf then right
elif right = Leaf then left
else
let (cutted, newChild) = cutLeft right
Tree(cutted, left, newChild)

root <- handle value root
size <- size - 1

/// <summary>
/// Checks for the presence of an element in the tree.
/// </summary>
member v.At (value : 'T) =
let rec handle (value : 'T) (node : Tree<'T>) =
match node with
| Tree(curValue, left, right) when value < curValue -> handle value left
| Tree(curValue, left, right) when curValue < value -> handle value right
| Leaf -> false
| _ -> true
handle value root

(*_*)
[<EntryPoint>]
let main argv =

0 // return an integer exit code