Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- public class testerChoice {
- //Functions======================================================================================================
- //targil 7.1
- //We send links of arrays so we change them also in the main
- public static void twoToOne(String[] arg1, String[] arg2, String[] arg3, int ln){
- for(int i = 0; i<ln; i+=1){
- //if a length of both strings is good put them into third array, if not put empty string
- if(arg1[i].length()+arg2[i].length()<10) {
- arg3[i] = arg1[i] + arg2[i];
- }else{
- arg3[i]="";
- }
- }
- }
- //targil 7.2
- public static int howMuchLessWords(String[] arg1, int ln, int num, Boolean[] arg2 ){
- int temp=0;
- for(int i = 0; i<ln;i+=1){
- if(arg1[i].length()<num){
- arg2[i]=true;
- temp +=1;
- }else{
- arg2[i]=false;
- }
- }
- return temp;
- }
- //targil 7.3
- public static String getFirstLetters(String str) {
- String[] words_arr = str.split(" "); //array of words
- String temp_word; //to get only one word from array
- String temp_ot = "";//to get first letter from the word. String cause .substring returns string format
- String return_str = "";//string to return in the end
- if (words_arr.length == 0) { // if we have only one word in the basic string
- return "nos";
- }
- for (int i = 0; i < words_arr.length; i += 1) {
- temp_word = words_arr[i]; //get word from array
- temp_ot = temp_word.substring(0, 1); //get first letter with a less O(t)
- //put the first letter in case when return string is empty and if not find matches in the string
- if (return_str.length() <= 0) {
- return_str += temp_ot;
- } else {
- //check if we have a char in the our result string
- if (!return_str.contains(temp_ot.toUpperCase()) && !return_str.contains(temp_ot.toLowerCase())) {
- return_str += temp_ot;
- }
- }
- }
- return return_str;
- }
- //Targil 7.4
- public static int [] wordsAndChars(String str) {
- //separete all sentences and get their lenght
- String[] tempArr = str.split("[!.]+");
- int rightLength = tempArr.length;
- //if we DON'T! have "." or "!" in the end of the string we decrement length cause the last sentence illegal
- if (!str.substring(str.length() - 1, str.length()).contains(".") && !str.substring(str.length() - 1, str.length()).contains("!")) {
- rightLength -= 1;
- }
- //declare result array of type Integer with right length
- int[] res = new int[rightLength];
- //make loop just for legal sentences
- for (int i = 0; i < rightLength; i += 1) {
- if (tempArr[i].substring(0, 1).contains(" ")) {
- res[i] = tempArr[i].length() - 1;
- } else {
- res[i] = tempArr[i].length();
- }
- }
- return res;
- }
- //Targil 7.5
- public static boolean isKaprekarXXX(int num, int[] parts) {
- int power = num * num;
- //Sum of risha and sipa should be equal to number
- 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
- int sipa = 0;// sipa is the second part of splited number
- int place =1; //special increament to put our sipa in right order(from left to right)
- while ((risha + sipa != num)&&(risha!=0)) {
- sipa +=risha % 10*place;
- risha /= 10; //cut risha
- place*=10; //increament to get a right place
- }
- if (risha == 0) {
- return false;
- } else {
- parts[0] = risha;
- parts[1] = sipa;
- return true;
- }
- }
- public static boolean isStringKaprekar(String num, String[] parts){
- String power = ""+Integer.parseInt(num)*Integer.parseInt(num); //Use * for power
- String risha = power;
- String sipa = "";
- String reverse="";
- while(/*(compare risha+sipa!=num?)&&*/(risha.length()!=0)){
- reverse+=risha.substring(risha.length()-1,risha.length());
- risha=risha.substring(0, risha.length() - 1);
- sipa =new StringBuffer(reverse).reverse().toString();
- }
- return true;
- }
- //main(teke of comments to work with main)================================================================================
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- /*
- //targil 7.1
- String[] text1 = {"hi", "good", "abc"};
- String[] text2 = {"hihi", "morning", "def"};
- String[] text3 = new String[3];
- twoToOne(text1,text2, text3, text1.length());
- for(int i = 0; i<text3.length; i+=1) {
- System.out.println(text3[i]);
- }
- //targil 7.2
- String[] texter = {"hi", "good", "abjdnvndkvndkkldvc", "bubuhdfjfjf", "tudidu", "blabl", "bobr"};
- int rand_num = new Random().nextInt(15)+5;
- Boolean[] truer = new Boolean[texter.length];
- int res = howMuchLessWords(texter,texter.length, rand_num, truer);
- System.out.println("Amount of less: "+ res+" rand_num="+rand_num);
- for(int i=0; i<truer.length;i+=1) {
- if (truer[i]){
- System.out.println(i);
- }
- }
- //targil 7.3
- String work = "Mama mila Ramu vo mila Dvore";
- String res_7_3 = getFirstLetters(work);
- System.out.println("result: "+res_7_3);
- //Targil 7.4
- //for the test
- String bdika = "Hi! This is a lovely day. Please join me for breakfast.";// [2,20,28]
- String bdika2 = "Hi! This is a lovely day. Please join me for breakfast";// [2,20]
- String bdika3 = " Hi ! This is a lovely day. Please join me for breakfast.";//[3,20,28]
- int[] resArr = wordsAndChars(bdika);
- for (int i : resArr) {
- System.out.print(i + " ");
- }
- resArr = wordsAndChars(bdika2);
- System.out.println();
- for (int i : resArr) {
- System.out.print(i + " ");
- }
- System.out.println();
- resArr = wordsAndChars(bdika3);
- for (int i : resArr) {
- System.out.print(i + " ");
- }
- //Targil 7.5
- int [] res = new int [2];
- String [] res2 = new String[2];
- String st_num;
- for(int i=0; i<10000;i+=1) {
- boolean kap = isKaprekarXXX(i, res);
- if (kap) {
- System.out.println("Kaprekar: "+i + " risha: " + res[0] + " sipa: " + res[1]);
- }
- }
- boolean skap = isStringKaprekar("2728",res2);
- for(int i=0; i<10;i+=1) {
- st_num = ""+i;
- skap = isStringKaprekar(st_num,res2);
- if(skap){
- System.out.println("Kaprekar: "+i + " risha: " + res2[0] + " sipa: " + res2[1]);
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment