Skip to content
Snippets Groups Projects
Commit 7f2762f3 authored by Vesa Lappalainen's avatar Vesa Lappalainen :bicyclist:
Browse files
parents d8e00c6f e0273f8c
No related branches found
No related tags found
No related merge requests found
Showing
with 376 additions and 64 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
......
......@@ -148,7 +148,7 @@ public class Kuva : PhysicsGame
/// <summary>
/// Lasketaan konvoluutio uint[,] kuva-matriisille käytten painomatriisia.
/// Lasketaan konvoluutio uint[,] kuva-matriisille käyttäen painomatriisia.
/// Konvoluutio lasketaan kunkin pikselin kohdalla kullekin värille (R,B,G)
/// erikseen.
/// Reunat unohdetaan.
......@@ -158,17 +158,18 @@ public class Kuva : PhysicsGame
/// <returns>uusi kuva-matriisi konvoloituna</returns>
/// <example>
/// <pre name="test">
/// uint[,] kuva = { { 0x010905,0x55020202,0x7f030303,0x040404 },
/// { 0x010101,0x66020202,0x7f030803,0x050505 },
/// { 0x010102,0x77090909,0x7f030303,0x060606 } };
/// uint[,] kuva = { { 0x010905,0x55020202,0x7f030303,0x040404 },
/// { 0x010101,0x66020202,0x7f030803,0x050505 },
/// { 0x010102,0x77090909,0x7f030303,0x060606 },
/// { 0x010102,0x23111524,0x66778800,0x080808 } };
/// int[,] painot = {{1,0,0},{0,1,0},{0,0,2}};
///
/// uint[,] kuva2 = Muunna(kuva,painot);
/// kuva2[0,0] === 0U; // reunat jäävät mustiksi
/// kuva2[2,1] === 0U;
/// Color.GetBlue(kuva2[1,1]) === 3;
/// kuva2[1,2] === 0x7f040504U;
/// kuva2[1,1] === 0x66020403U;
/// kuva2[1,2] === 0x7f040504U;
/// kuva2[2,1] === 0x773E4602U;
/// kuva2[2,2] === 0x7F050505U;
/// </pre>
/// </example>
public static uint[,] Muunna(uint[,] kuva, int[,] painot)
......
#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();
}
}
using System;
namespace PortaatYlos;
/// <summary>
/// The main class.
/// </summary>
public static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main()
{
using var game = new PortaatYlos();
game.Run();
}
}
using Jypeli;
namespace PortaatYlos;
public class PortaatYlos : PhysicsGame
{
const double LAATIKON_KOKO = 80;
// BYCODEBEGIN
public override void Begin()
{
Level.Background.Color = Color.Black;
Vector piste = new Vector(0, 0);
// Poista rivien alusta kommenttimerkit
// piste = PiirraLaatikko(this, piste);
// piste = PiirraLaatikko(this, piste);
// piste = PiirraLaatikko(this, piste);
PhysicsObject pallo = new PhysicsObject(5, 5, Shape.Circle);
Add(pallo, 1);
pallo.Color = Color.Red;
Camera.ZoomToAllObjects(100);
}
/* poista tämä rivi ja vastaava kommentin lopetusrivi
/// <summary>
/// Aliohjelma piirtää ruutuun yhden neliön, jonka
/// sivun pituus on 80 ja vasemman alakulman koordinaatti p.
/// </summary>
/// <param name="peli">Peli, johon neliö piirretään</param>
/// <param name="p">Neliön vasemman alanurkan koordinaatti.</param>
/// <returns>Neliön oikean ylänurkan koordinaatti</returns>
public static ??? PiirraLaatikko(???)
{
double s = LAATIKON_KOKO;
GameObject nelio = new GameObject(s, s, Shape.Rectangle);
nelio.Position = ???
peli.Add(nelio);
???
}
*/
// BYCODEEND
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Jypeli.NET" Version="11.*"/>
<PackageReference Include="Jypeli.FarseerPhysics.NET" Version="2.*"/>
</ItemGroup>
</Project>
using System;
namespace PortaatYlosAlas;
/// <summary>
/// The main class.
/// </summary>
public static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main()
{
using var game = new PortaatYlosAlas();
game.Run();
}
}
using Jypeli;
namespace PortaatYlosAlas;
public class PortaatYlosAlas : PhysicsGame
{
const double LAATIKON_KOKO = 80;
// BYCODEBEGIN
public override void Begin()
{
Level.Background.Color = Color.Black;
Vector piste = new Vector(0, 0);
// Poista rivien alusta kommenttimerkit
// piste = PiirraLaatikko(this, piste);
// piste = PiirraLaatikko(this, piste);
// piste = PiirraLaatikko(this, piste);
// piste -= new Vector(0,LAATIKON_KOKO);
// piste = PiirraLaatikkoAlas(this,piste);
// piste = PiirraLaatikkoAlas(this,piste);
PhysicsObject pallo = new PhysicsObject(5, 5, Shape.Circle);
Add(pallo, 1);
pallo.Color = Color.Red;
Camera.ZoomToAllObjects(100);
}
// TODO: Kopioi tähän edellisen tehtävän PiirraLaatikko
// TODO: Jatka tekemällä PiirraLaatikkoAlas
// BYCODEEND
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Jypeli.NET" Version="11.*"/>
<PackageReference Include="Jypeli.FarseerPhysics.NET" Version="2.*"/>
</ItemGroup>
</Project>
......@@ -13,6 +13,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VarienVaihtoKonsolilla", "V
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PortaatYlosAlasKonsolilla", "PortaatYlosAlasKonsolilla\PortaatYlosAlasKonsolilla.csproj", "{CE21768D-76C1-40D6-B875-89051B14486C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PortaatYlos", "PortaatYlos\PortaatYlos.csproj", "{BBC7FA41-52CD-417C-9725-4649E3833118}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PortaatYlosAlas", "PortaatYlosAlas\PortaatYlosAlas.csproj", "{18EA707A-815F-4041-90DF-AD4C46730C56}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -39,6 +43,14 @@ Global
{CE21768D-76C1-40D6-B875-89051B14486C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE21768D-76C1-40D6-B875-89051B14486C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE21768D-76C1-40D6-B875-89051B14486C}.Release|Any CPU.Build.0 = Release|Any CPU
{BBC7FA41-52CD-417C-9725-4649E3833118}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BBC7FA41-52CD-417C-9725-4649E3833118}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BBC7FA41-52CD-417C-9725-4649E3833118}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BBC7FA41-52CD-417C-9725-4649E3833118}.Release|Any CPU.Build.0 = Release|Any CPU
{18EA707A-815F-4041-90DF-AD4C46730C56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{18EA707A-815F-4041-90DF-AD4C46730C56}.Debug|Any CPU.Build.0 = Debug|Any CPU
{18EA707A-815F-4041-90DF-AD4C46730C56}.Release|Any CPU.ActiveCfg = Release|Any CPU
{18EA707A-815F-4041-90DF-AD4C46730C56}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
# 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>
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