Demo 9 (ei tutkimusta)
Palauta viimeistään ma klo 11:59.
DEMOTILAISUUS ZOOMISSA MAANANTAINA.
Klikkaa tästä itsesi kokoukseen: https://jyufi.zoom.us/j/227987027 (alk. ma klo 12:10)
Your time usage this week (0.5 p.)
Submitting time usage grants 0.5 points. Use the Set Custom Points feature to set your points.
This includes all course-related work during the week (lectures, reading the course material, completing the exercises etc.)
Every student has the right and duty to assess the correctness of the points they receive. If your program does not work, Set custom points according to your best judgement. For example if you feel like you have achieved 50% of hte goals of the assignment you may set up 0.5 points for yourself. If you see your answer as being 90% correct but the automatic review gives you less points you can set 0.9 points for yourself.
PP5, perjantai 11.3.
PP-assignments (points are only given for those who attend the PP-group on Friday. The PP-assignments are given on Friday.) What are PP-assignments?
Task 1*
M: 15. Arrays.
Create a Function PisinNouseva
, which returns the length of the longest strictly increasing subset of numbers from an int
-array.
Note that if the subsequent numbers are equals, the subset is not strictly increasing. Test you function.
Note, that the function returns the lentgh of the set, namely a single integer.
Example: The longest strictly increasing subset of the following array 3, 3, 4, 4, 4, 0, 3, 4, 6, 6, 2
is 0, 3, 4, 6
. The lenght of the subset is 4, so function's return value in this incstance is 4.
You must not assume that the array would always be the one given above, but the function should work with any array of integers. You are free to ''test'' your functions with the given array using Comtest for example.
Pisteytys: 0.5 pistettä kun funktio kääntyy, 0.4 pistettä itse tehdyistä ja läpi menevistä testeistä, ja 0.1 pistettä dokumentaatiosivun luomisesta ja tämän sivun avaamisesta (Document-linkki vastauslaatikon alapuolella).
Halutaan siis etsiä alkuperäisestä jonosta t
pisin osajono, missä kaikki alkiot toteuttavat ehdon t[i] < t[i+1]
.
Tässä vielä toinen esimerkki ko. tehtävään:
Lukujonon
3, 5, 4, 4, 0, 1, 4, 6, 2
pisin aidosti kasvava osajono on
0, 1, 4, 6
ja tämä kyseinen osajono siis "alkaa" alkuperäisen jonon viidennestä alkiosta (luku 0) ja päättyy alkuperäisen jonon kahdeksanteen alkioon (luku 6).
Tip: If the algortihm is not crystal clear, practise using Tauno (below) or pen and paper.
Task 2*
Remember the box task earlier. Rewrite the program so that PiirraNelio
(DrawSquare)-subroutine receives Vector
-object instead of two real numbers (and size). This vector represents the bottom left corner of the box. The function draws the box and returns the coordinates of the top right corner. This makes it simple because the starting point is whatever the previous call returned.
Begin
-subroutine could look like this.
public override void Begin()
{
Vector piste = new Vector(0, 0);
piste = PiirraNelio(piste);
piste = PiirraNelio(piste);
piste = PiirraNelio(piste);
}
Add a small red circle to (0, 0). To ensure circle visibility you can add it to an upper layer.
Add(pallo, 1);
// Huomaa: luku 1 tarkoittaa tasoa numero 1.
// Add(pallo) lisäisi pallon tasolle 0
A picture example:

TODO: Ilmaise laatikon koko selkeämmin.
Task 3 (1-2 p.)
Basic part of the assignment (1 p.): Create the function OnkoSummaa
, which accepts two parameters: int
-array and int
-integer. The function returns a bool value based on the following: true if the integer is the sum of any two numbers of the array.
For example the call
int[] t = new int[] {1, 2, 3, 9};
OnkoSummaa(t, 8);
would return false
, because there are no two numbers inside the array which add up to 8.
int[] t = new int[] {1, 2, 4, 4};
OnkoSummaa(t, 8);
would return true
, since 4 + 4 = 8.
Bonus part (+ 1 p.): Copy/paste the code below to Visual Studio to test the speed of your function with an array size of 100,000 elements. If the time is below 50 milliseconds, you may assign another point for yourself.
Note: Due to randomness of the array the run time may occasionally go over 50ms which is alright. It is enough if the function runs below 50ms most of the time (8/10). However the runtime should not be thousands of milliseconds for such is the case in brute force algorithms. A working solution passess the test in included in the Main
-method.
using System;
using System.Collections.Generic;
using System.Diagnostics;
public class Summa
{
public static void Main()
{
int[] testi = new int[] {1,2,2,3,5};
bool testiLapi = !OnkoSummaa(testi,1) && !OnkoSummaa(testi,2) &&
OnkoSummaa(testi,4) && OnkoSummaa(testi,6);
Console.WriteLine(testiLapi ? "Testit läpäisty." :
"Testit epäonnistuivat, virheellinen toteutus!");
const int MONTAKO = 100000;
int[] luvut = LuoSatunnainenTaulukko(MONTAKO, 0, MONTAKO / 2);
Array.Sort(luvut);
Stopwatch sw = Stopwatch.StartNew();
bool loytyykoPari = OnkoSummaa(luvut, MONTAKO);
sw.Stop();
double ms = sw.Elapsed.TotalMilliseconds;
Console.WriteLine("Kesti: " + ms + " millisekuntia.");
}
private static int[] LuoSatunnainenTaulukko(int pituus, int min, int max)
{
Random r = new Random();
int[] luvut = new int[pituus];
for (int i = 0; i < luvut.Length; i++)
luvut[i] = r.Next(min, max);
return luvut;
}
}
Task 4
M: 23. Dynamic data sttructures.
Phase 1 (create a new function): Create a function PisinJono
(Longest String), which finds the longest string in a list of strings and returns it (as a string).
Phase 2 (create PoistaSanat
(remove words)-method): Print the longest string in the Main-mehtod, and then remove all instances of the string from the list. The purpose is to remove the strings using a loop and the Remove-method of a List-object. Make sure you understand the return type of the Remove
-method (bool
) as well as the documentation below:
Return Value: true
if item is successfully removed; otherwise, false
. This method also returns false
if item was not found in the List
(ks. MSDN)
This is also a great opportunity to practise using ComTest and to earn TDD-points by testing the functions.
Pisteytys: 0.9 pistettä kun funktio kääntyy, 1 piste oikeasta tulostuksesta ja 0.1 pistettä dokumentaatiosivun luomisesta ja tämän sivun avaamisesta (Document-linkki vastauslaatikon alapuolella).
Task 5 (1-2p.)
Preparations: Download Kotus' Finnish wordlist (choose it with the right mouse button and choose Save as...).
Create new Console Application -project and add the file you downloaded into the project: right mouse button over the project \(\rightarrow\) Add \(\rightarrow\) Existing Item \(\rightarrow\) Bottom right "All files" \(\rightarrow\) look for the .txt
-file you just downloaded. Once you have added the file to the project click it with the right mouse button \(\rightarrow\) Properties \(\rightarrow\) Copy to output directory -choose Copy if newer
.
You can push the data in a String
-array by writing the code below into Main
-method:
String[] sanat = File.ReadAllLines("kotus_sanat.txt");
Also add the following to the beginning of your code: using System.IO;
Task:
(part a) (1 p.) create a program which asks the user for a string. Then create a function which returns a list of words from the Kotus-list, which include all the characters given by the user. The program would then print out the words it found.
Copy SisaltaaKaikkiMerkit
(Includes all characters)-function (below) to your own program and use it. This function returns true
or false
depeding on whether a given (single) string includes all given characters.
Pisteytys: 0.9 pistettä kun funktio kääntyy ja 0.1 pistettä dokumentaatiosivun luomisesta ja tämän sivun avaamisesta (Document-linkki vastauslaatikon alapuolella).
/// <summary>
/// Funktiossa tutkitaan voidaanko annettu merkkijono
/// "upottaa toiseen merkkijonoon", eli
/// sisaltaako sana kaikki annetut merkit.
/// </summary>
/// <param name="sana">Sana, johon upotetaan</param>
/// <param name="merkit">Upotettava sana (eli tämän sanan
/// kirjaimet pitää löytyä ekasta sanasta)</param>
/// <returns>Onnistuko uptus</returns>
/// <example>
/// <pre name="test">
/// SananUpotus.SisaltaaKaikkiMerkit("antti", "antti") === true;
/// SananUpotus.SisaltaaKaikkiMerkit("antti", "at") === true;
/// SananUpotus.SisaltaaKaikkiMerkit("at", "antti") === false;
/// SananUpotus.SisaltaaKaikkiMerkit("anttijussi", "tia") === true;
/// SananUpotus.SisaltaaKaikkiMerkit("anttijussi", "") === true;
/// SananUpotus.SisaltaaKaikkiMerkit("anttijussi", "tiaa") === false;
/// SananUpotus.SisaltaaKaikkiMerkit("", "") === true;
/// SananUpotus.SisaltaaKaikkiMerkit("", "a") === false;
/// </pre>
/// </example>
public static bool SisaltaaKaikkiMerkit(String sana, String merkit)
{
StringBuilder merkitNyt = new StringBuilder(merkit);
StringBuilder sanaNyt = new StringBuilder(sana);
int i = 0;
while (i < merkit.Length)
{
char merkkiNyt = merkit[i];
// Tehdään seuraavaksi koodi jonka avulla
// löydetään merkin paikka StringBuilder-oliossa.
// Tämä vastaa String-olion IndexOf-metodia, jota
// ei valitettavasti löydy StringBuilderille.
int merkinPaikka = -1; // arvataan aluksi että merkkiä ei löydy.
int j = 0;
while (j < sanaNyt.Length)
{
if (sanaNyt[j] == merkkiNyt)
{
merkinPaikka = j;
break;
}
j++;
}
if (merkinPaikka >= 0)
{
sanaNyt = sanaNyt.Remove(merkinPaikka, 1);
}
else return false;
i++;
}
return true;
}
V1
This week there will be no new Ville-assignments. However you may do 5 loop/array-tasks that you have not done before or other tasks that you have not understood before. Remember Ville user manual.
TDD1
If you test at least two functions with an automatic test (ComTest), using Visual Studio or similar you can mark yourself one extra demo point. Tests cannot be the same as the ones you can copy from the TIM version. As an answer you will write which tasks and function/functions operations you have tested. You can also give feedback and improvement ideas for using ComTest.
If the ComTest does not work in the computer labs, try installing ComTest-VS-plugin. Then check to see that the ComTest path is correct: In Visual Studio choose Tools
\(\rightarrow\) ComTest
\(\rightarrow\) Options
. Check that Path to ComTest.jar
executable field has N:\bin\ComTest.jar
and that you have connected your computer to the N-drive. Ask the tutor for help if needed.
B1
Tee tehtävän 6 ohjelmasta interaktiivinen siten, että käyttäjä voi syöttää sanan kirjain kerrallaan, ja ohjelma näyttää tulokset heti. Katso video.
B2-3
Tee seuraava Console Application -peli.
Kaksi pelaajaa kilpailee siitä, kumpi saa noppaa heittämällä ensin 100 pistettä. Jokaisella vuorolla pelaaja heittää toistuvasti noppaa, kunnes saa joko numeron 1 tai pelaaja sanoo "pankki" jolloin hänen vuorollaan heitettyjen silmälukujen summa lisätään hänen kokonaispistemääräänsä.
Pelaaja kullakin heittokerralla joutuu siis tekemään seuraavan valinnan
- heitto - arvotaan nopan silmäluku 1-6, ja jos tulos on
- 1: pelaaja menettää kaikki omalla vuorollaan heittämänsä pisteet ja vuoro vaihtuu vastustajalle
- 2-6: silmäluku lisätään pelaajan "turn totaliin" ja pelaajan vuoro jatkuu
- pankki - pelaajan omalla vuorollaan heittämät pisteet (eli "turn total") lisätään hänen kokonaispistemääräänsä ja vuoro vaihtuu vastustajalle
B4
Tee algoritmi kokonaislukutaulukon sekoittamiseksi. Tee void-
aliohjelma, joka ottaa parametrina int
-taulukon ja sekoittaa sen alkiot. Testaa algoritmiasi taulukolla, johon on alustettu järjestyksessä luvut 1, 2, 3, ..., 52. Palauta toimiva ohjelmakoodi, jossa on hyvin kommentoituna mitä algoritmissa tehdään.
Lukuja 1, 2, 3, ..., 52 ei tietenkään kannata laittaa taulukkoon manuaalisesti, koska helpompiakin tapoja on jo opittu ;).
Huom: Tässä ei ole tarkoitus "keksiä" omaa algoritmia, vaan käyttää ideaa jostakin valmiista algoritmista sekoittamiseen. Hyvä algoritmi on esim: Fisher-Yates shuffle. Tästä on toteutus Jypelin RandomGen -luokan Shuffle-aliohjelmassa. Jos matkit tätä, niin vaihda T
:n tilalle int
ja listan tilalle int
-taulukko. Funktion alkuun pitää lisätä nyt Random rand = new Random();
jotta Random
-olio on olemassa. Jypelin esimerkissä se on luokan attribuutti.
G1-2
Tee aliohjelma ToistotonTaulukko
, joka saa parametrinaan taulukon, jossa on kokonaislukuja, ja palauttaa taulukon, jossa on alkuperäisen taulukon alkiot kukin vain yhden kerran järjestettynä niiden esiintymiskertojen määrän mukaan nousevaan järjestykseen. (1,5 p.) Täysiä pisteitä varten kirjoita aliohjelmalle myös testit. (0,5 p.)
Esim. taulukosta {1, 2, 3, 34, 34, 2, 1, 34, 1, 1, 1}
palautetaan {3, 2, 34, 1}
.
These are the current permissions for this document; please modify if needed. You can always modify these permissions from the manage page.