Skip to content
Snippets Groups Projects
Commit e0273f8c authored by Vesa Lappalainen's avatar Vesa Lappalainen :bicyclist:
Browse files

pohjien muokkausta

parent e5f05b99
No related branches found
No related tags found
No related merge requests found
Showing
with 333 additions and 97 deletions
......@@ -52,7 +52,7 @@ public class PuuRekursio
// Pääohjelma, jossa luodaan puun solmut ja aloitetaan tulostaminen.
public static void Main(String[] args)
public static void Main(string[] args)
{
Solmu s2 = new Solmu(2, new Solmu(1), new Solmu(3));
Solmu s6 = new Solmu(6, new Solmu(5), new Solmu(7));
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PPTeht2
namespace PPTeht2d;
/// @author: Simo Haatainen
/// @version: 14.11.2011
///
/// <summary>
/// Harjoitellaan muuttujien tyyppejä
/// </summary>
public class Muuttujia
{
/// @author: Simo Haatainen
/// @version: 14.11.2011
///
/// <summary>
/// Harjoitellaan muuttujien tyyppejä
/// </summary>
public class Muuttujia
{
public static void Main(string[] args)
{
/*
muuttuvaMerkkijono = new
muuttuvaMerkkijono.Append("Tämä on jokin merkkijono, jota pystyy muokkaamaan");
merkkijono = "Tämä on tavallinen merkkijono";
merkki = merkkijono[0];
kokonaisLuku = 4;
jakoJaannos = kokonaisLuku % 2;
osaMaara = kokonaisLuku / 3;
kokonaisLukuTaulukko = new { kokonaisLuku, 2, 4, 6 };
merkkijonoTaulukko = new { "kissa", "koira", "kala" };
*/
}
}
public static void Main(string[] args)
{
/*
muuttuvaMerkkijono = new
muuttuvaMerkkijono.Append("Tämä on jokin merkkijono, jota pystyy muokkaamaan");
merkkijono = "Tämä on tavallinen merkkijono";
merkki = merkkijono[0];
kokonaisLuku = 4;
jakoJaannos = kokonaisLuku % 2;
osaMaara = kokonaisLuku / 3;
kokonaisLukuTaulukko = new { kokonaisLuku, 2, 4, 6 };
merkkijonoTaulukko = new { "kissa", "koira", "kala" };
*/
}
}
......@@ -13,6 +13,7 @@ public class Robot
/// </summary>
public static void Main()
{
// ReSharper disable once UnusedVariable
RobotCommander rc = new RobotCommander();
}
......
......@@ -11,9 +11,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PPTeht1", "PPTeht1\PPTeht1.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PPTeht2b", "PPTeht2b\PPTeht2b.csproj", "{40C9CE62-8683-47DA-B9F9-5B575A72E194}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PPTeht2d", "PPTeht2c\PPTeht2d.csproj", "{77516ECA-8C05-4624-ABA0-3D4616E0D95B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PPTeht2Test", "PPTeht2Test\PPTeht2Test.csproj", "{CE612254-98E0-482D-B1EB-C938CC2E4ED0}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PPTeht2d", "PPTeht2d\PPTeht2d.csproj", "{77516ECA-8C05-4624-ABA0-3D4616E0D95B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
......@@ -41,10 +39,6 @@ Global
{77516ECA-8C05-4624-ABA0-3D4616E0D95B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{77516ECA-8C05-4624-ABA0-3D4616E0D95B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{77516ECA-8C05-4624-ABA0-3D4616E0D95B}.Release|Any CPU.Build.0 = Release|Any CPU
{CE612254-98E0-482D-B1EB-C938CC2E4ED0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CE612254-98E0-482D-B1EB-C938CC2E4ED0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE612254-98E0-482D-B1EB-C938CC2E4ED0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE612254-98E0-482D-B1EB-C938CC2E4ED0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace Demo9;
......@@ -15,9 +11,9 @@ public static class Program
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
public static void Main()
{
using (var game = new Demo9.Kuva())
game.Run();
using var game = new Demo9.Kuva();
game.Run();
}
}
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace Demo9;
/// <summary>
/// The main class.
......@@ -15,9 +9,9 @@ public static class Program
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
public static void Main()
{
using (var game = new Demo9.Palapeli())
game.Run();
using var game = new Demo9.Palapeli();
game.Run();
}
}
#region Using Statements
using System;
using System;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace PortaatYlos
namespace PortaatYlos;
/// <summary>
/// The main class.
/// </summary>
public static class Program
{
/// <summary>
/// The main class.
/// The main entry point for the application.
/// </summary>
public static class Program
[STAThread]
public static void Main()
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
using var game = new PortaatYlos();
game.Run();
}
using var game = new PortaatYlos();
game.Run();
}
}
\ No newline at end of file
}
#region Using Statements
using System;
using System;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace PortaatYlosAlas
namespace PortaatYlosAlas;
/// <summary>
/// The main class.
/// </summary>
public static class Program
{
/// <summary>
/// The main class.
/// The main entry point for the application.
/// </summary>
public static class Program
[STAThread]
public static void Main()
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
using var game = new PortaatYlosAlas();
game.Run();
}
using var game = new PortaatYlosAlas();
game.Run();
}
}
\ No newline at end of file
}
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/modules.xml
/contentModel.xml
/projectSettingsUpdater.xml
/.idea.Konsolimalleja.iml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
</component>
</project>
\ No newline at end of file
using System;
using System.Collections.Generic;
/// @author vesal
/// @version 04.11.2023
/// <summary>
/// Knapsack problem. See
/// - https://en.wikipedia.org/wiki/Knapsack_problem
/// - https://www.youtube.com/watch?v=hagBB17_hvg
/// - https://www.youtube.com/watch?v=cJ21moQpofY
/// TODO: Implement dynamic version
/// </summary>
public class KnapsackProblem
{
public record struct Item(string name, int weight, int value);
/// <summary>
/// Try functions
/// </summary>
public static void Main()
{
Item[] items = {
new Item("micro", 8, 50),
new Item("drone", 2, 150),
new Item("monitor", 6, 210),
new Item("kettle", 1, 30)
};
(long bestValue, List<Item> used) = KnapsackBruteForce(items, 10);
Console.Write($"Best value {bestValue}: ");
foreach (Item item in used) Console.Write($"{item.name} {item.weight} kg, ");
(bestValue, used) = KnapsackDynamic(items, 10);
Console.Write($"Best value {bestValue}: ");
foreach (Item item in used) Console.Write($"{item.name} {item.weight} kg, ");
}
// ReSharper disable InvalidXmlDocComment
/// <summary>
/// Solve Knapsack 0/1 problem with brute force.
/// O(n 2^n)), max 64 items.
/// </summary>
/// <param name="items">Items to fit</param>
/// <param name="maxCapacity">capacity of knapsack</param>
/// <returns>(bestValue, list of used items)</returns>
/// <example>
/// <pre name="test">
/// #EQUALSMARKER2="==>"
/// #import System.Linq
/// Item[] items = {
/// new Item("micro", 8, 50),
/// new Item("drone", 2, 150),
/// new Item("monitor", 6, 210),
/// new Item("kettle", 1, 30)
/// };
///
/// (long bestValue, List<Item> used) = KnapsackDynamic(items, 10);
/// bestValue === 390;
/// used.Select(item => item.name) =J= "drone, monitor, kettle";
/// (bestValue, used) = KnapsackDynamic(items, 8);
/// bestValue === 360;
/// used.Select(item => item.name) =J= "drone, monitor";
/// (bestValue, used) = KnapsackDynamic(items, 3);
/// bestValue === 180;
/// used.Select(item => item.name) =J= "drone, kettle";
/// </pre>
/// </example>
public static (long, List<Item>) KnapsackDynamic(Item[] items, int maxCapacity)
{
// TODO: toteuta
List<Item> used = new List<Item>();
return (0, used);
}
// ReSharper disable InvalidXmlDocComment
/// <summary>
/// Solve Knapsack 0/1 problem with brute force.
/// O(n 2^n)), max 64 items.
/// </summary>
/// <param name="items">Items to fit</param>
/// <param name="maxCapacity">capacity of knapsack</param>
/// <returns>(bestValue, list of used items)</returns>
/// <example>
/// <pre name="test">
/// #EQUALSMARKER2="==>"
/// #import System.Linq
/// Item[] items = {
/// new Item("micro", 8, 50),
/// new Item("drone", 2, 150),
/// new Item("monitor", 6, 210),
/// new Item("kettle", 1, 30)
/// };
///
/// (long bestValue, List<Item> used) = KnapsackBruteForce(items, 10);
/// bestValue === 390;
/// used.Select(item => item.name) =J= "drone, monitor, kettle";
/// (bestValue, used) = KnapsackBruteForce(items, 8);
/// bestValue === 360;
/// used.Select(item => item.name) =J= "drone, monitor";
/// (bestValue, used) = KnapsackBruteForce(items, 3);
/// bestValue === 180;
/// used.Select(item => item.name) =J= "drone, kettle";
/// </pre>
/// </example>
public static (long, List<Item>) KnapsackBruteForce(Item[] items, int maxCapacity)
{
int n = items.Length;
uint nMax = (uint)Math.Pow(2, n);
long bestValue = 0;
ulong bestItems = 0;
for (ulong itemsMask = 1; itemsMask < nMax; itemsMask++)
{
ulong mask = 1;
long sumWeights = 0, sumValues = 0;
for (int i = 0; i < n && mask <= itemsMask; i++, mask <<=1)
{
if ((itemsMask & mask) == 0L) continue;
sumWeights += items[i].weight;
if (sumWeights > maxCapacity) break;
sumValues += items[i].value;
}
if (sumWeights > maxCapacity || bestValue >= sumValues) continue;
bestValue = sumValues;
bestItems = itemsMask;
}
List<Item> used = new List<Item>();
ulong m = 1;
for (int i = 0; i < n; i++, m <<= 1) if ((bestItems & m) != 0) used.Add(items[i]);
return (bestValue, used);
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ExternalConsole>true</ExternalConsole>
</PropertyGroup>
</Project>
#EQUALSMARKER2="==>"
\ No newline at end of file
// ReSharper disable all
using System;
using System.Collections.Generic;
using NUnit.Framework;
using static KnapsackProblem;
using System.Linq;
[TestFixture]
[DefaultFloatingPointTolerance(0.000001)]
public class TestKnapsackProblem
{
[Test]
public void Teststatic47()
{
Item[] items = {
new Item("micro", 8, 50),
new Item("drone", 2, 150),
new Item("monitor", 6, 210),
new Item("kettle", 1, 30)
};
(long bestValue, List<Item> used) = KnapsackBruteForce(items, 10);
Assert.AreEqual( 390, bestValue , "in method static, line 58");
Assert.AreEqual(( "drone, monitor, kettle").ToString(), String.Join(", ", used.Select(item => item.name) ), "in method static, line 59");
(bestValue, used) = KnapsackBruteForce(items, 8);
Assert.AreEqual( 360, bestValue , "in method static, line 61");
Assert.AreEqual(( "drone, monitor").ToString(), String.Join(", ", used.Select(item => item.name) ), "in method static, line 62");
(bestValue, used) = KnapsackBruteForce(items, 3);
Assert.AreEqual( 180, bestValue , "in method static, line 64");
Assert.AreEqual(( "drone, kettle").ToString(), String.Join(", ", used.Select(item => item.name) ), "in method static, line 65");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NUnit" Version="3.13.1"/>
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\KnapsackProblem\KnapsackProblem.csproj"/>
</ItemGroup>
</Project>

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KnapsackProblem", "KnapsackProblem\KnapsackProblem.csproj", "{41029A20-BE21-4E40-AF2F-E321BEB16B68}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KnapsackProblemTest", "KnapsackProblemTest\KnapsackProblemTest.csproj", "{522FBF6A-58AD-4B3D-8E86-D5BAA27A0EE8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{41029A20-BE21-4E40-AF2F-E321BEB16B68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{41029A20-BE21-4E40-AF2F-E321BEB16B68}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41029A20-BE21-4E40-AF2F-E321BEB16B68}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41029A20-BE21-4E40-AF2F-E321BEB16B68}.Release|Any CPU.Build.0 = Release|Any CPU
{522FBF6A-58AD-4B3D-8E86-D5BAA27A0EE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{522FBF6A-58AD-4B3D-8E86-D5BAA27A0EE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{522FBF6A-58AD-4B3D-8E86-D5BAA27A0EE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{522FBF6A-58AD-4B3D-8E86-D5BAA27A0EE8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KnapsackProblem", "KnapsackProblem\KnapsackProblem.csproj", "{41029A20-BE21-4E40-AF2F-E321BEB16B68}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{41029A20-BE21-4E40-AF2F-E321BEB16B68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{41029A20-BE21-4E40-AF2F-E321BEB16B68}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41029A20-BE21-4E40-AF2F-E321BEB16B68}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41029A20-BE21-4E40-AF2F-E321BEB16B68}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment