initial commit
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
|
||||
</startup>
|
||||
</configuration>
|
||||
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
||||
|
||||
namespace magicSeries
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var n = Convert.ToInt32(args[0]);
|
||||
var series = new MinConflictsMagicSeries(n);
|
||||
var solution = series.Solve();
|
||||
|
||||
if (solution != null)
|
||||
{
|
||||
var s = string.Join(" ", solution.Select(i => i.ToString()));
|
||||
Console.WriteLine(n);
|
||||
Console.WriteLine(s);
|
||||
Debug.WriteLine(n);
|
||||
Debug.WriteLine(s);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("No solution");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class MinConflictsMagicSeries
|
||||
{
|
||||
private readonly int _n;
|
||||
|
||||
public MinConflictsMagicSeries(int n)
|
||||
{
|
||||
_n = n;
|
||||
}
|
||||
|
||||
public IList<int> Solve()
|
||||
{
|
||||
var series = new Series(_n);
|
||||
var random = new Random();
|
||||
while (true)
|
||||
{
|
||||
if (series.IsFeasible())
|
||||
return series.Values;
|
||||
// select the largest violation
|
||||
var updateValue =
|
||||
(from i in Enumerable.Range(0, series.Size)
|
||||
let value = series.Values[i]
|
||||
let occurrances = series.GetOccurrances(i)
|
||||
let violationDegree = Math.Abs(value - occurrances)
|
||||
select new {i, occurrances, violationDegree})
|
||||
.GroupBy(arg => arg.violationDegree)
|
||||
.OrderByDescending(arg => arg.Key)
|
||||
.First()
|
||||
.OrderBy(arg => random.Next())
|
||||
.First();
|
||||
|
||||
// adjust it to correct value
|
||||
series.UpdateValue(updateValue.i, updateValue.occurrances);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("magicSeries")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("magicSeries")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2013")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("c0f99f41-0030-4b13-af15-e4ba1d3eec88")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace magicSeries
|
||||
{
|
||||
[TestFixture]
|
||||
public class SeriesTests
|
||||
{
|
||||
[Test]
|
||||
public void UpdateValueTest()
|
||||
{
|
||||
var series = new Series(5);
|
||||
series.UpdateValue(0,2);
|
||||
series.UpdateValue(1,1);
|
||||
series.UpdateValue(2,2);
|
||||
|
||||
Assert.That(series.IsFeasible(), Is.True);
|
||||
|
||||
Console.WriteLine("Values: " + string.Join(" ", series.Values.Select(i => i.ToString())));
|
||||
Console.WriteLine("Occurr: " + string.Join(" ", series.Occurrances.Select(i => i.ToString())));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace magicSeries
|
||||
{
|
||||
public class Series
|
||||
{
|
||||
public readonly int[] Values;
|
||||
|
||||
public readonly int[] Occurrances;
|
||||
|
||||
public int Size { get { return Values.Length; } }
|
||||
|
||||
public Series(int size)
|
||||
{
|
||||
Values = new int[size];
|
||||
Occurrances = new int[size + 1];
|
||||
Occurrances[0] = size;
|
||||
}
|
||||
|
||||
public int GetOccurrances(int i)
|
||||
{
|
||||
return Occurrances[i];
|
||||
}
|
||||
|
||||
public bool IsFeasible()
|
||||
{
|
||||
for (var n = 0; n < Values.Count(); n++)
|
||||
if (Values[n] != Occurrances[n])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void UpdateValue(int n, int newValue)
|
||||
{
|
||||
Occurrances[Values[n]]--;
|
||||
Occurrances[newValue]++;
|
||||
Values[n] = newValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{B8C7F1E4-92F4-41AC-83F2-2BFCC13A6820}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>magicSeries</RootNamespace>
|
||||
<AssemblyName>magicSeries</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>..\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="nunit.framework">
|
||||
<HintPath>packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Series.cs" />
|
||||
<Compile Include="Series.Tests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
@@ -0,0 +1,20 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 2012
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "magicSeries", "magicSeries.csproj", "{B8C7F1E4-92F4-41AC-83F2-2BFCC13A6820}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{B8C7F1E4-92F4-41AC-83F2-2BFCC13A6820}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B8C7F1E4-92F4-41AC-83F2-2BFCC13A6820}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B8C7F1E4-92F4-41AC-83F2-2BFCC13A6820}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B8C7F1E4-92F4-41AC-83F2-2BFCC13A6820}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="NUnit" version="2.6.2" targetFramework="net45" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user