Goodiny777

Targilim 7.1-7.5

Feb 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.21 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class testerChoice {
  5.  
  6. //Functions======================================================================================================
  7.     //targil 7.1
  8.     //We send links of arrays so we change them also in the main
  9.     public static void twoToOne(String[] arg1, String[] arg2, String[] arg3, int ln){
  10.         for(int i = 0; i<ln; i+=1){
  11.             //if a length of both strings is good put them into third array, if not put empty string
  12.             if(arg1[i].length()+arg2[i].length()<10) {
  13.                 arg3[i] = arg1[i] + arg2[i];
  14.             }else{
  15.                 arg3[i]="";
  16.             }
  17.         }
  18.     }
  19.  
  20.     //targil 7.2
  21.     public static int howMuchLessWords(String[] arg1, int ln, int num, Boolean[] arg2 ){
  22.         int temp=0;
  23.         for(int i = 0; i<ln;i+=1){
  24.             if(arg1[i].length()<num){
  25.                 arg2[i]=true;
  26.                 temp +=1;
  27.             }else{
  28.                 arg2[i]=false;
  29.             }
  30.         }
  31.         return temp;
  32.     }
  33.  //targil 7.3
  34.     public static String getFirstLetters(String str) {
  35.         String[] words_arr = str.split(" "); //array of words
  36.         String temp_word; //to get only one word from array
  37.         String temp_ot = "";//to get first letter from the word. String cause .substring returns string format
  38.         String return_str = "";//string to return in the end
  39.         if (words_arr.length == 0) { // if we have only one word in the basic string
  40.             return "nos";
  41.         }
  42.         for (int i = 0; i < words_arr.length; i += 1) {
  43.             temp_word = words_arr[i]; //get word from array
  44.             temp_ot = temp_word.substring(0, 1); //get first letter with a less O(t)
  45.             //put the first letter in case when return string is empty and if not find matches in the string
  46.             if (return_str.length() <= 0) {
  47.                 return_str += temp_ot;
  48.             } else {
  49.                 //check if we have a char in the our result string
  50.                 if (!return_str.contains(temp_ot.toUpperCase()) && !return_str.contains(temp_ot.toLowerCase())) {
  51.                     return_str += temp_ot;
  52.                 }
  53.             }
  54.         }
  55.         return return_str;
  56.     }
  57. //Targil 7.4
  58.     public static int [] wordsAndChars(String str) {
  59.         //separete all sentences and get their lenght
  60.         String[] tempArr = str.split("[!.]+");
  61.         int rightLength = tempArr.length;
  62.         //if we DON'T! have "." or "!" in the end of the string we decrement length cause the last sentence illegal
  63.         if (!str.substring(str.length() - 1, str.length()).contains(".") && !str.substring(str.length() - 1, str.length()).contains("!")) {
  64.             rightLength -= 1;
  65.         }
  66.         //declare result array of type Integer with right length
  67.         int[] res = new int[rightLength];
  68.         //make loop just for legal sentences
  69.         for (int i = 0; i < rightLength; i += 1) {
  70.             if (tempArr[i].substring(0, 1).contains(" ")) {
  71.                 res[i] = tempArr[i].length() - 1;
  72.             } else {
  73.                 res[i] = tempArr[i].length();
  74.             }
  75.         }
  76.         return res;
  77.     }
  78.  
  79.   //Targil 7.5
  80.     public static boolean isKaprekarXXX(int num, int[] parts) {
  81.         int power = num * num;
  82.         //Sum of risha and sipa should be equal to number
  83.         int risha = power; //risha is a first part of splited power of number.  We start risha from our number and cut it untill we have good risha and sipa or untill risha equals 0
  84.         int sipa = 0;// sipa is the second part of splited number
  85.         int place =1; //special increament to put our sipa in right order(from left to right)
  86.         while ((risha + sipa != num)&&(risha!=0)) {
  87.             sipa +=risha % 10*place;
  88.             risha /= 10; //cut risha
  89.             place*=10; //increament to get a right place
  90.         }
  91.         if (risha == 0) {
  92.             return false;
  93.         } else {
  94.             parts[0] = risha;
  95.             parts[1] = sipa;
  96.             return true;
  97.         }
  98.     }
  99.  
  100.     public static boolean isStringKaprekar(String num, String[] parts){
  101.         String power = ""+Integer.parseInt(num)*Integer.parseInt(num); //Use * for power
  102.         String risha = power;
  103.         String sipa = "";
  104.         String reverse="";
  105.         while(/*(compare risha+sipa!=num?)&&*/(risha.length()!=0)){
  106.             reverse+=risha.substring(risha.length()-1,risha.length());
  107.             risha=risha.substring(0, risha.length() - 1);
  108.             sipa =new StringBuffer(reverse).reverse().toString();
  109.         }
  110.         return true;
  111.     }
  112.  
  113. //main(teke of comments to work with main)================================================================================
  114. public static void main(String[] args) {
  115.         Scanner scan = new Scanner(System.in);
  116.         /*
  117.         //targil 7.1
  118.         String[] text1 = {"hi", "good", "abc"};
  119.         String[] text2 = {"hihi", "morning", "def"};
  120.         String[] text3 = new String[3];
  121.         twoToOne(text1,text2, text3, text1.length());
  122.         for(int i = 0; i<text3.length; i+=1) {
  123.             System.out.println(text3[i]);
  124.         }
  125.         //targil 7.2
  126.         String[] texter = {"hi", "good", "abjdnvndkvndkkldvc", "bubuhdfjfjf", "tudidu", "blabl", "bobr"};
  127.         int rand_num = new Random().nextInt(15)+5;
  128.         Boolean[] truer = new Boolean[texter.length];
  129.         int res = howMuchLessWords(texter,texter.length, rand_num, truer);
  130.         System.out.println("Amount of less: "+ res+" rand_num="+rand_num);
  131.         for(int i=0; i<truer.length;i+=1) {
  132.             if (truer[i]){
  133.                 System.out.println(i);
  134.             }
  135.         }
  136.  
  137.         //targil 7.3
  138.         String work = "Mama mila Ramu vo mila Dvore";
  139.         String res_7_3 = getFirstLetters(work);
  140.         System.out.println("result: "+res_7_3);
  141.  
  142.          //Targil 7.4
  143.         //for the test
  144.         String bdika = "Hi! This is a lovely day. Please join me for breakfast.";// [2,20,28]
  145.         String bdika2 = "Hi! This is a lovely day. Please join me for breakfast";// [2,20]
  146.         String bdika3 = " Hi ! This is a lovely day. Please join me for breakfast.";//[3,20,28]
  147.         int[] resArr = wordsAndChars(bdika);
  148.         for (int i : resArr) {
  149.             System.out.print(i + " ");
  150.         }
  151.         resArr = wordsAndChars(bdika2);
  152.         System.out.println();
  153.         for (int i : resArr) {
  154.             System.out.print(i + " ");
  155.         }
  156.         System.out.println();
  157.         resArr = wordsAndChars(bdika3);
  158.         for (int i : resArr) {
  159.             System.out.print(i + " ");
  160.         }
  161.  
  162.         //Targil 7.5
  163.         int [] res = new int [2];
  164.         String [] res2 = new String[2];
  165.         String st_num;
  166.         for(int i=0; i<10000;i+=1) {
  167.             boolean kap = isKaprekarXXX(i, res);
  168.             if (kap) {
  169.                 System.out.println("Kaprekar: "+i + " risha: " + res[0] + " sipa: " + res[1]);
  170.             }
  171.         }
  172.  
  173.         boolean skap = isStringKaprekar("2728",res2);
  174.         for(int i=0; i<10;i+=1) {
  175.             st_num = ""+i;
  176.             skap = isStringKaprekar(st_num,res2);
  177.             if(skap){
  178.                 System.out.println("Kaprekar: "+i + " risha: " + res2[0] + " sipa: " + res2[1]);
  179.             }
  180.         }
  181. */
Advertisement
Add Comment
Please, Sign In to add comment