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

live20 loppu

parent 7f2762f3
No related branches found
No related tags found
No related merge requests found
Showing
with 392 additions and 0 deletions
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/modules.xml
/.idea.live19.iml
/projectSettingsUpdater.xml
/contentModel.xml
# 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.Text;
using System.Linq;
using System.Collections.Generic;
/// @author Omanimi
/// @version 06.11.2023
/// <summary>
///
/// </summary>
public class Rekursio
{
/// <summary>
///
/// </summary>
public static void Main()
{
long tulos = Kertoma(4); // 52! = 8 10¨68
// long tulosf = KertomaFor(4);
System.Console.WriteLine(tulos);
}
/// <example>
/// <pre name="test">
/// Kertoma(0) === 1L;
/// Kertoma(1) === 1L;
/// Kertoma(2) === 2L;
/// Kertoma(5) === 120L;
/// Kertoma(6) === 720L;
/// </pre>
/// </example>
public static long Kertoma(long n)
{
if (n <= 1L) return 1L;
return n * Kertoma(n - 1);
}
/// <example>
/// <pre name="test">
/// KertomaFor(0) === 1L;
/// KertomaFor(1) === 1L;
/// KertomaFor(2) === 2L;
/// KertomaFor(5) === 120L;
/// KertomaFor(6) === 720L;
/// </pre>
/// </example>
public static long KertomaFor(long n)
{
long tulos = 1L;
for (int i = 1; i <= n; i++) tulos *= i;
return tulos;
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ExternalConsole>true</ExternalConsole>
</PropertyGroup>
</Project>
// ReSharper disable all
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using NUnit.Framework;
using static Rekursio;
[TestFixture]
[DefaultFloatingPointTolerance(0.000001)]
public class TestRekursio
{
[Test]
public void TestKertoma25()
{
Assert.AreEqual( 1L, Kertoma(0) , "in method Kertoma, line 26");
Assert.AreEqual( 1L, Kertoma(1) , "in method Kertoma, line 27");
Assert.AreEqual( 2L, Kertoma(2) , "in method Kertoma, line 28");
Assert.AreEqual( 120L, Kertoma(5) , "in method Kertoma, line 29");
Assert.AreEqual( 720L, Kertoma(6) , "in method Kertoma, line 30");
}
[Test]
public void TestKertomaFor41()
{
Assert.AreEqual( 1L, KertomaFor(0) , "in method KertomaFor, line 42");
Assert.AreEqual( 1L, KertomaFor(1) , "in method KertomaFor, line 43");
Assert.AreEqual( 2L, KertomaFor(2) , "in method KertomaFor, line 44");
Assert.AreEqual( 120L, KertomaFor(5) , "in method KertomaFor, line 45");
Assert.AreEqual( 720L, KertomaFor(6) , "in method KertomaFor, line 46");
}
}
<?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="..\Rekursio\Rekursio.csproj"/>
</ItemGroup>
</Project>

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rekursio", "Rekursio\Rekursio.csproj", "{874F3A3C-FC86-4A53-B0F9-BDA00AA96F4D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RekursioTest", "RekursioTest\RekursioTest.csproj", "{814FF6BD-D07E-4CC7-861D-AD17131F5E9C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{874F3A3C-FC86-4A53-B0F9-BDA00AA96F4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{874F3A3C-FC86-4A53-B0F9-BDA00AA96F4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{874F3A3C-FC86-4A53-B0F9-BDA00AA96F4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{874F3A3C-FC86-4A53-B0F9-BDA00AA96F4D}.Release|Any CPU.Build.0 = Release|Any CPU
{814FF6BD-D07E-4CC7-861D-AD17131F5E9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{814FF6BD-D07E-4CC7-861D-AD17131F5E9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{814FF6BD-D07E-4CC7-861D-AD17131F5E9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{814FF6BD-D07E-4CC7-861D-AD17131F5E9C}.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}") = "Rekursio", "Rekursio\Rekursio.csproj", "{874F3A3C-FC86-4A53-B0F9-BDA00AA96F4D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{874F3A3C-FC86-4A53-B0F9-BDA00AA96F4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{874F3A3C-FC86-4A53-B0F9-BDA00AA96F4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{874F3A3C-FC86-4A53-B0F9-BDA00AA96F4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{874F3A3C-FC86-4A53-B0F9-BDA00AA96F4D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/contentModel.xml
/projectSettingsUpdater.xml
/modules.xml
/.idea.live20.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;
/// @author Vesa Lappalainen
/// @version 10.11.2020
/// <summary>
/// Tutkitaan poikkeuksia
/// </summary>
public class Poikkeus
{
/// <summary>
/// Esimerkkejä poikkeuksista
/// </summary>
public static void Main()
{
int[] luvut = { 2, 4, 1 };
/*
luvut = new int[3]; // täynnä 0:ia
luvut[0] = 2;
luvut[1] = 4;
luvut[2] = 1;
*/
int a1 = 3, a2 = 4, a3 = 1, a4 = 7;
try
{
int s = Summa(luvut, 0, 4);
Console.WriteLine(s);
Console.WriteLine(10 / a1);
Console.WriteLine(10 / a2);
Console.WriteLine(10 / a3);
Console.WriteLine(10 / a4);
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Jaoit nollalla aasi! " + ex.Message);
}
/*
catch (IndexOutOfRangeException ex)
{
Console.WriteLine(ex.Message);
}
*/
}
/// <summary>
/// Lasketaan lukujen summa taulukon osajoukosta
/// </summary>
/// <param name="luvut">taulukko, jonka lukujen summa lasketaan</param>
/// <param name="alku">indeksi josta summaaminen aloitetaan</param>
/// <param name="loppu">loppuindeksi joka otetaan mukaan, -1 = taulukon loppuun</param>
/// <returns>taulukon osajoukon summa</returns>
/// <example>
/// <pre name="test">
/// int[] luvut = { 2, 4, 1 };
/// Summa(luvut) === 7;
/// Summa(luvut,0,2) === 7;
/// Summa(luvut,1,2) === 5;
/// Summa(luvut,1) === 5;
/// Summa(luvut,0,1) === 6;
/// Summa(luvut,1,1) === 4;
/// Summa(luvut,1,0) === 0;
/// Summa(luvut,0,100) === 7; #THROWS IndexOutOfRangeException
/// Summa(luvut,-4,100) === 7; #THROWS IndexOutOfRangeException
/// </pre>
/// </example>
public static int Summa(int[] luvut, int alku = 0, int loppu = -1)
{
int tulos = 0;
if (loppu == -1) loppu = luvut.Length - 1;
// Seuraavat voisi olla oikeasti järkeviä, mutta tässä harjoitellaan
// sitä, että poikkeus päästetään kutsuvaan ohjelmaan.
// if (loppu >= luvut.Length) loppu = luvut.Length - 1;
// if (alku < 0) alku = 0;
for (int i = alku; i <= loppu; i++)
{
int luku = luvut[i];
tulos += luku; // tulos = tulos + luku;
}
return tulos;
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ExternalConsole>true</ExternalConsole>
</PropertyGroup>
</Project>
using System;
using System.Collections.Generic;
/// @author Omanimi
/// @version 07.11.2023
/// <summary>
///
/// </summary>
public class Poistot
{
/// <summary>
///
/// </summary>
public static void Main()
{
List<string> sanat = new List<string>(){ "", "kissa", "kissa", "koira", "kana", "susi", "ankka" };
// PoistaSanat(sanat, "kissa");
string poistettava = "kissa";
//sanat.RemoveAll(sana => sana == "kissa");
Console.WriteLine(String.Join(", ", sanat));
poistettava = "koira";
sanat.RemoveAll(delegate(string sana) { if (sana.Length == 0) return false; return sana[0] == 'k';});
Console.WriteLine(String.Join(", ", sanat));
}
public static bool AlkaaKlla(string sana)
{
if (sana.Length == 0) return false;
return sana[0] == 'k';
}
public static List<string> PoistaSanat(List<string> sanat, string poistettava)
{
while (sanat.Remove(poistettava)) ;
return sanat;
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ExternalConsole>true</ExternalConsole>
</PropertyGroup>
</Project>

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Poistot", "Poistot\Poistot.csproj", "{7FCEEA19-68B4-4A6C-8E5F-233154B9A213}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Poikkeus", "Poikkeus\Poikkeus.csproj", "{E0C65815-A9AC-457C-B38F-F6E5C13882B6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7FCEEA19-68B4-4A6C-8E5F-233154B9A213}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7FCEEA19-68B4-4A6C-8E5F-233154B9A213}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FCEEA19-68B4-4A6C-8E5F-233154B9A213}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FCEEA19-68B4-4A6C-8E5F-233154B9A213}.Release|Any CPU.Build.0 = Release|Any CPU
{E0C65815-A9AC-457C-B38F-F6E5C13882B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E0C65815-A9AC-457C-B38F-F6E5C13882B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E0C65815-A9AC-457C-B38F-F6E5C13882B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E0C65815-A9AC-457C-B38F-F6E5C13882B6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/modules.xml
/contentModel.xml
/projectSettingsUpdater.xml
/.idea.luento19.iml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
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