You are on page 1of 8

1 ‫שאלה‬

public static void main(String[] args) {


String nums = "13490231";
System.out.println(stringPrime(nums));
}

public static int stringPrime(String str) {


int primes = 0;

for (int i = 0; i < str.length() - 1; i++) {


char digit1 = str.charAt(i);
char digit2 = str.charAt(i + 1);
String pair = Character.toString(digit1) +
Character.toString(digit2);
int number = Integer.parseInt(pair);
if(number<9) {
continue;
}
if (isPrime(number)) {
System.err.println(number+" prime");
primes++;
}
}

return primes;
}

public static boolean isPrime(int num) {


if (num <= 1) {
return false;
}

for (int i = 2; i <= Math.sqrt(num); i++) {


if (num % i == 0) {
return false; // not a prime number
}
}

return true; // no factors found, prime number


}
}
‫ב‬+‫א‬ -- 2 ‫שאלה‬ // mario
public static void main(String[] args) {
String str1 = "MANO";
String str2 = "ABCDE";
//----------------------------------------
/// different inputs for different cases and checks
/// --------------------------------------
String arr[] = {"MANO","AACDE","HELO"}; // false
String arr2[] = {"MANO","ABCDE","HELO"}; // true
System.out.println(isDiverseStr(str1, str2)); // true
System.err.println(isDiverseArr(arr)); // true

public static boolean isDiverseStr(String str1 ,String str2) {

int counter = 0;
for(int i = 0;i<str1.length();i++) {
for(int j = 0 ;j<str2.length();j++) {
if(str1.charAt(i)==str2.charAt(j)) {
counter++;
}
if(counter>1)
return false;
}
}
if(counter==1)
return true; // IS "GREAT"

return false;
}

public static boolean isDiverseArr(String [] arr) {

for(int i = 0;i<arr.length-1;i++) {
if(!isDiverseStr(arr[i], arr[i+1])) {
return false;
}
}
return true;
}
}
public static void main(String[] args) {

int [][]arr = {
{1,9,2},
{2,8,6},
{6,9,3}
};

System.out.println(largerValInRow(arr, 3));

System.out.println(vertArr(arr));
}

public static int largerValInRow(int [][] arr , int row) {

int maxInRow = 0;
row = row - 1;
for(int i = 0;i<arr[row].length;i++) {
if(arr[row][i]>maxInRow)
maxInRow = arr[row][i];
}

return maxInRow;
}
public static boolean vertArr(int[][] arr) {
int maxColumnIndex = 0;

for (int i = 1; i < arr[0].length; i++) {


if (arr[0][i] > arr[0][maxColumnIndex]) {
maxColumnIndex = i;
}
}

// Check if the maximum value in each row is


in the same column
for (int i = 1; i < arr.length; i++) {
if (largerValInRow(arr, i + 1) !=
arr[i][maxColumnIndex]) {
return false;
}
}

return true;
}

}
4 ‫שאלה‬

public static void main(String[] args) {

Random ran = new Random();

int countArr[] = new int[100];


int ranNum = 0;
for (int i = 0; i < 100; i++) {
ranNum = ran.nextInt(100); // Corrected
range from 99 to 100
System.out.println(ranNum);
countArr[ranNum]++;
}

int maxVal = 0;
int num = 0; // (the index is the number)
for (int i = 0; i < countArr.length; i++) {
if (countArr[i] > maxVal) {
maxVal = countArr[i];
num = i;
}
}
System.err.println(num);
}

}
5 ‫שאלה‬

public static void main(String[] args) {

String[] array = {"Hello", "World", "java",


"Programming","java","mming","world hello"};
System.out.println(mixingString(array));

}
public static int mixingString(String [] arr) {

Random ran = new Random();


int randomArrayInd = ran.nextInt(arr.length);

String chosenStr = arr[randomArrayInd];


int stringLength = chosenStr.length();

// random starting index for substring


int startIndex = ran.nextInt(stringLength);

// random ending index for substring


int endIndex = ran.nextInt(stringLength);
// make sure that the end Index is greater than or
equal to start Index
if (endIndex < startIndex) {
int temp = endIndex;
endIndex = startIndex;
startIndex = temp;
}

String substringFromStr =
chosenStr.substring(startIndex, endIndex + 1);
System.out.println(substringFromStr); // just to check
int count = 0;
for(int i = 0;i<arr.length;i++) {
if(arr[i].contains(substringFromStr))
count++;
}

return count-1 ; // don't count the string itself


}

You might also like