Advertisement
Guest User

TIRA 5.4

a guest
Feb 18th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1.  
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6. public static int reitinPituus(String reitti) {
  7.  
  8. HashSet <Integer> pisteet = new HashSet<>();
  9.  
  10. int pituus = reitti.length();
  11.  
  12. int x = 0;
  13. int y = 0;
  14.  
  15. Piste piste1 = new Piste(x, y);
  16. int hashluku1 = piste1.hashCode();
  17. pisteet.add(hashluku1);
  18.  
  19. int askeleet = 0;
  20.  
  21.  
  22. for (int i = 0; i < pituus; i++) {
  23.  
  24. if (reitti.charAt(i) == 'O') {
  25. x += 1;
  26. } else if (reitti.charAt(i) == 'V') {
  27. x -= 1;
  28. } else if (reitti.charAt(i) == 'A') {
  29. y -= 1;
  30. } else {
  31. y += 1;
  32. }
  33.  
  34. Piste piste = new Piste(x, y);
  35. int hashluku = piste.hashCode();
  36.  
  37. if (pisteet.contains(hashluku)) {
  38.  
  39. askeleet = i + 1;
  40. break;
  41.  
  42. }
  43.  
  44. pisteet.add(hashluku);
  45.  
  46. }
  47.  
  48. return askeleet;
  49. }
  50.  
  51. public static void main(String[] args) {
  52.  
  53. System.out.println(reitinPituus("YYVVAAOO"));
  54. System.out.println(reitinPituus("YVAOYVAO"));
  55. System.out.println(reitinPituus("YYYYYYYY"));
  56. System.out.println(reitinPituus("OYVVAOOO"));
  57.  
  58. System.out.println(reitinPituus("YYYAAA"));
  59.  
  60. }
  61. }
  62.  
  63.  
  64.  
  65.  
  66. public class Piste {
  67.  
  68. private int x;
  69. private int y;
  70.  
  71. public Piste (int x, int y) {
  72.  
  73. this.x = x;
  74. this.y = y;
  75. }
  76.  
  77. public int getPisteX() {
  78.  
  79. return this.x;
  80. }
  81.  
  82. public int getPisteY() {
  83.  
  84. return this.y;
  85. }
  86.  
  87. public String toString() {
  88.  
  89. return this.x + " ja " + this.y;
  90. }
  91.  
  92. public boolean equals(Object olio) {
  93.  
  94. if (olio == null) {
  95. return false;
  96. }
  97.  
  98. if (getClass() != olio.getClass()) {
  99. return false;
  100. }
  101.  
  102. Piste verrattava = (Piste) olio;
  103.  
  104. if (this.x != verrattava.x) {
  105. return false;
  106. }
  107.  
  108. if (this.y != verrattava.y) {
  109. return false;
  110. }
  111.  
  112. return true;
  113. }
  114.  
  115. @Override
  116. public int hashCode() {
  117. int hash = 3;
  118. hash = 37 * hash + this.x;
  119. hash = 37 * hash + this.y;
  120. return hash;
  121. }
  122. }
  123.  
  124.  
  125.  
  126. TEST PACKAGES
  127.  
  128. import org.junit.Test;
  129.  
  130. import static org.junit.Assert.*;
  131. import fi.helsinki.cs.tmc.edutestutils.Points;
  132. import fi.helsinki.cs.tmc.edutestutils.timing.CpuStopwatch;
  133. import java.util.Arrays;
  134.  
  135. @Points("5.4")
  136. public class MainTest {
  137. public void pieniTesti(String reitti, int tulos) {
  138. int uusi = Main.reitinPituus(reitti);
  139. assertTrue("Reitin " + reitti + " pituuden tulisi olla " + tulos +
  140. ", mutta metodisi antaa " + uusi + ".",
  141. tulos == uusi);
  142. }
  143.  
  144. public void suuriTesti(String reitti, int tulos) {
  145. CpuStopwatch kello=new CpuStopwatch(CpuStopwatch.Mode.USER);
  146. int uusi = Main.reitinPituus(reitti);
  147. assertTrue("Metodisi toimii väärin suurella syötteellä.",
  148. tulos == uusi);
  149. double aika=kello.getElapsedTime();
  150. assertTrue("Metodisi kuluttaa liikaa aikaa: "+kello+"s, kun aikaraja on 1s.",
  151. aika<=1);
  152. }
  153.  
  154. @Test(timeout=5000)
  155. public void esimerkit() {
  156. pieniTesti("YYVVAAOO", 8);
  157. pieniTesti("YVAOYVAO", 4);
  158. pieniTesti("YYYYYYYY", 0);
  159. pieniTesti("OYVVAOOO", 6);
  160. }
  161.  
  162. @Test(timeout=5000)
  163. public void pienet() {
  164. pieniTesti("Y", 0);
  165. pieniTesti("A", 0);
  166. pieniTesti("V", 0);
  167. pieniTesti("O", 0);
  168.  
  169. pieniTesti("YY", 0);
  170. pieniTesti("AA", 0);
  171. pieniTesti("VV", 0);
  172. pieniTesti("OO", 0);
  173.  
  174. pieniTesti("YA", 2);
  175. pieniTesti("AY", 2);
  176. pieniTesti("VO", 2);
  177. pieniTesti("OV", 2);
  178.  
  179. pieniTesti("YV", 0);
  180. pieniTesti("AV", 0);
  181. pieniTesti("OY", 0);
  182. pieniTesti("OA", 0);
  183.  
  184. pieniTesti("VYOA", 4);
  185. pieniTesti("OAVY", 4);
  186. pieniTesti("AOYV", 4);
  187. pieniTesti("AVYO", 4);
  188.  
  189. pieniTesti("YYYAAA", 4);
  190. pieniTesti("YYYVVV", 0);
  191.  
  192. pieniTesti("YYOOAVVVAO", 7);
  193. pieniTesti("OOYYOAOYYY", 0);
  194. pieniTesti("OOYYOAAVVV", 8);
  195. }
  196.  
  197. @Test(timeout=5000)
  198. public void suuri1() {
  199. int n = 100000;
  200. char[] mjono = new char[n];
  201. for (int i = 0; i < n; i++) mjono[i] = 'A';
  202. suuriTesti(new String(mjono), 0);
  203. }
  204.  
  205. @Test(timeout=5000)
  206. public void suuri2() {
  207. int n = 100000;
  208. char[] mjono = new char[n];
  209. for (int i = 0; i < n/2; i++) mjono[i] = 'A';
  210. for (int i = n/2; i < n; i++) mjono[i] = 'Y';
  211. suuriTesti(new String(mjono), n/2+1);
  212. }
  213.  
  214. @Test(timeout=5000)
  215. public void suuri3() {
  216. int n = 100000;
  217. char[] mjono = new char[n];
  218. for (int i = 0; i < n/4; i++) mjono[i] = 'A';
  219. for (int i = n/4; i < n/2; i++) mjono[i] = 'O';
  220. for (int i = n/2; i < n/2+n/4; i++) mjono[i] = 'Y';
  221. for (int i = n/2+n/4; i < n; i++) mjono[i] = 'V';
  222. suuriTesti(new String(mjono), n);
  223. }
  224.  
  225. @Test(timeout=5000)
  226. public void suuri4() {
  227. int n = 100000;
  228. char[] mjono = new char[n];
  229. long x = 12345;
  230. long a = 798732191;
  231. long b = 921339221;
  232. for (int i = 0; i < n/3; i++) {
  233. x = (x*a)%b;
  234. if (x%2 == 0) mjono[i] = 'O';
  235. if (x%2 == 1) mjono[i] = 'Y';
  236. }
  237. mjono[n/3] = 'O';
  238. for (int i = n/3+1; i < n/2; i++) {
  239. mjono[i] = 'A';
  240. }
  241. mjono[n/2] = 'V';
  242. for (int i = n/2+1; i < n; i++) {
  243. x = (x*a)%b;
  244. if (x%2 == 0) mjono[i] = 'V';
  245. if (x%2 == 1) mjono[i] = 'Y';
  246. }
  247. suuriTesti(new String(mjono), 66746);
  248. }
  249.  
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement