Advertisement
Zizalik

Kodutöö Rugam 3 Recursive

Apr 11th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package praktika3_op;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. /**
  12. *
  13. * @author t166056
  14. */
  15. public class Praktika3_Op {
  16. public static List <Integer> Andmed=new ArrayList<>();//Meie Arraylist, deklareeritud siin, aga ei ole kindel miks ma seda deklareersin. Seda ei kasutata
  17. /**
  18. * @param args the command line arguments
  19. */
  20. public static void main(String[] args) {
  21. System.out.println("Index: " + indexOf("Teetooline",'n',0));//Prindime ekraanile oma teksti IndexOf funktsiooniga, esimene on meil String sõna. Otsime n tähte, tagastatakse väärtus
  22. String word="isa";//Meie sõna muutujaga word antud talle väärtus Stringiline Isa
  23. System.out.println("LastIndex: "+ LastIndexOf(word,'a',word.length()-1));// Samamoodi lastindexOf funktsioon Word sõna on meie string otsime a tähte, ning -1 sellepärast, et tagant poolt ette hakatakse lugema
  24. int groupSize =5;//Grupi suurus
  25. int teamSize = 3;//Mitu tiimi
  26. char start = 'A';//Algus
  27. String sequence = "";//Tühi stringiline sisu
  28. makeTeam(sequence,start,groupSize,teamSize);// Anname talle
  29.  
  30. }
  31. public static int indexOf(String word, char searchLetter, int positsion){//Meie funktsioon
  32. //int positsion=0;
  33.  
  34. if(word==null || word.equals("") || positsion > word.length()){// Antakse tingimused.
  35. return -1;// Kui need tingimused on mitte funktsioneerivad, tagastatakse meile väärtus -1 ehk puudub
  36. }
  37. if(searchLetter==word.charAt(positsion)){// Tingimus
  38. return positsion+1;// Võetakse positsioon ja lisatakse talle 1 juurde, et saada kätte meie antud õiget arvu. Kuna lugemist alustatakse 0st tuleb liita 1 juurde
  39. }
  40. else
  41. {
  42. return indexOf(word,searchLetter,positsion+1);//Tagastatakse tulemus
  43. }
  44.  
  45.  
  46. }
  47. public static int LastIndexOf(String word, char searchLetter, int positsion){
  48.  
  49.  
  50. if(word==null || word.equals("") || positsion < 0){//Tingimus
  51. return -1;
  52. }
  53. if(searchLetter==word.charAt(positsion)){//Tingimus
  54. return positsion+1;
  55. }
  56. else
  57. {
  58. return LastIndexOf(word,searchLetter,positsion-1);//Lihtsalt vastupidi
  59. }
  60. }
  61.  
  62. public static void makeTeam(String sequence,char start, int n, int k)
  63. {
  64. if (n==0||k==0||k>n) {//Tingimused
  65. if (k==0) // this condition will make sure that only complete
  66. // sequences of k elements will be printed
  67. System.out.println(sequence);//Prindime välja sagedused
  68. } else {
  69. makeTeam(sequence+start,(char)(start+1),n-1,k-1);
  70. makeTeam(sequence,(char)(start+1),n-1,k);
  71. }
  72. }
  73. }
  74.  
  75. /*public static List<List<Integer>> funktsioon1(List<Integer> sisestus)
  76. {
  77. return samm(sisestus, sisestus.size(), new ArrayList<>());
  78. }
  79. public static List <List<Integer>> samm(List <Integer> sisestus, int a ,List <List<Integer>> vastus)
  80. {
  81. if(a==0)
  82. {
  83. return vastus;
  84. }
  85. if(vastus.size()==0)
  86. {
  87. for(Integer i: sisestus)
  88. {
  89. ArrayList<Integer> uusList = new ArrayList<>();
  90. uusList.add(i);
  91. vastus.add(uusList);
  92. }
  93. return samm(sisestus, a ,vastus);
  94. }
  95.  
  96. List<List<Integer>> uuemVastus = new ArrayList<>();
  97.  
  98. for(List<Integer> uusList : vastus)
  99. {
  100. for(Integer i : sisestus)
  101. {
  102. List<Integer>Arraynr1=new ArrayList<>();
  103. Arraynr1.addAll(uusList);
  104. Arraynr1.add(i);
  105. uuemVastus.add(Arraynr1);
  106. }
  107. }
  108. return samm(sisestus, a ,uuemVastus);
  109. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement