Sunteți pe pagina 1din 7

package homeworks;

import java.util.ArrayList;
import java.util.List;

public class HomeworkCollections {

public static void main (String[] args) {

//pentru prima problema

// int result = countPrimeNumbers();


// System.out.println("Number of prime numbers = " + result);

// System.out.println(countPrimeNumbers());

// int[] sir = {1,2,5,7,8,9,11};


// System.out.println(getPrimeNumbers(sir));

// System.out.println(arrayFact(sir));

// for (int i=0; i<arrayFact(sir).length; i ++) {


// System.out.println(arrayFact(sir)[i]);

// int[] sir = {1,2,3,4};


// for (int i=0; i<arrayPow(sir).length; i++) {
// System.out.println(arrayPow(sir)[i]);

// int[] sir = {1,2,3,4,5,6,7,8};


// pairNumbers (sir, 8);

//7. Am urmatoarele numere: 3,7,2,1,90,5,11,13,10.


// - Creeaza o lista cu aceste valori. (fie manual, fie dinamic
folosind un sir)

// int numere2[] = {3,7,2,1,90,5,11,13,10};

List<Integer> numere2 = new ArrayList<Integer>();


numere2.add(3);
numere2.add(7);
numere2.add(2);
numere2.add(1);
numere2.add(90);
numere2.add(5);
numere2.add(11);
numere2.add(13);
numere2.add(10);

}
}
// for (Integer nr : numere2) {
// System.out.println(nr);
//

// - Daca elementul este pe o pozitie care este nr prim, interschimbati


valoarea cu valoarea de dinainte

// private static boolean isPrime(int number) {


// for (int i = 2; i < number; i++) {
// if (number % i == 0) {
// return false;
// }
// }
// return true;
// }
// }

// for (int i = 2; i < numere2.size(); i++) {


// if (isPrime(i)) {
// int a = 0;
// a = numere2.get(i);
// numere2.set(i,numere2.get(i-1));
// numere2.set((i-1),a);
// }
// }
// System.out.println(numere2);

//metoda de la Codruta

// for(int i = 0;i<numere2.size();i++){
// if (isPrime(i)) {
// int aux = numere2.get(i);
// numere2.set (i,numere2.get(i-1));
// numere2.set((i-1),aux);
// }

// - Verifica daca elementul "12" este in lista

// System.out.println(numere2.contains(12));

// - Scoateti din lista elementele care sunt divizibile cu 10

// for (int i = 0; i < numere2.size(); i++)


//
// if (numere2.get(i) % 10 == 0) {
// numere2.remove(i);
// i--;
// }
// System.out.println(numere2);

// numere2.removeIf(a-> a % 10 == 0);
// System.out.println(numere2);

//6. Gasiti perechile de numere dintr-un sir ale caror suma e egala cu
un numar specificat

// int numere[] = { 1, 2, 3, 7, 8, 9, 4, 6};


// int sum = 10;
//
// // Consider all possible pairs and check their sums
// for (int i = 0; i < numere.length; i++)
// for (int j = i + 1; j < numere.length; j++)
// if ((numere[i] + numere[j]) == sum)
//
// System.out.println(Integer.toString(numere[i]) + " + " +
Integer.toString(numere[j]) + " egal " + sum);
// }
// }

//metoda de la Codruta
// public static void pairNumbers (int[] sir, int nr) {
// for (int i=0; i<sir.length; i++) {
// for (int j=i+1; j<sir.length; j++) {
// if (sir[i] + sir[j] == nr)
// System.out.println(sir[i] + "+" + sir[j]+ "==" +
nr);
// }
//
// }
// }
//}

//5. Gasiti elementele comune din 2 siruri. e.g. a=


{"vin","sarbatori","jordita"} b= {"floare,"pom","jordita"} --> jordita

// String[] a = {"vin","sarbatori","jordita"};
// String[] b = {"floare","pom","jordita", "vin"};
//
// for(int i = 0; i < a.length; i++){
// for(int j = 0; j < b.length; j++){
// if(a[i] == b[j]) {
//
// System.out.println(a[i]);
// }
// }
// }
// }

//metoda de la Codruta
// public static String getCommonWords (String[] s1, String[] s2) {
// String commonWords = "Common words: ";
//
// for (int i=0; i< s1.length; i++) {
// for (int j=0; j<s2.length; j++) {
// if (s1[i].equalsIgnoreCase(s2[j])) { //verifica
daca s1[i] este egal cu s2[j] ignorand literele mari
// commonWords = commonWords + s1[i]+""; // imi
adauga la un string daca ce e mai sus sunt egale
// }
// }
// }
// return commonWords;
// }
//}

//4. Returnati cuvantul care are cele mai multe vocale dintr-un
sir

// String[] lista = {"vin","sarbatori","jordita"};


// System.out.println(mostVowels(lista));
// }

// public static String mostVowels(String[] array) {


//
// String max = array[0];
// int numberOfMaxVowels = vowelsCount(array[0]);
//
// for (int i = 1; i < array.length; i++) {
// if (vowelsCount(array[i]) > numberOfMaxVowels) {
// max = array[i];
// numberOfMaxVowels = vowelsCount(array[i]);
// }
// }
// return max;
// }
//

// public static int vowelsCount(String cuvant) {


//
// int vowelsCount = 0;
// cuvant = cuvant.toLowerCase();
//
// for (int i = 0; i < cuvant.length(); i++) {
// char ch = cuvant.charAt(i);
//
// if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch
== 'u') {
// vowelsCount++;
// }
// }
// return vowelsCount;

// 3. Calculati a la puterea i, unde a e un element din sir si i este indexul


lui. e.g. [1,2,3] -> [1, 2, 9]

// public static void main (String[] args) {

// int[] array = {1,2,3,4};


//
// for (int i=0; i < array.length; i++) {
// array[i] = putere(array[i], i);
// System.out.println(array[i]);
// }
// }

// metoda de la Codruta
// public static int[] arrayPow (int[] sir) {
// int[] out = new int [sir.length];
// for (int i=0; i<sir.length; i++) {
// out[i] = putere(sir[i], i);
// }
// return out;
// }

// public static int putere (int baza, int exponent) {


//
// int power = 1;
// for (int i = 1;i<= exponent;i++)
// {
// power = power * baza;
// }
// return power;
// }
// }

//2. Calculati factorialul fiecarui element din sir.e.g. [1,2,3] -> [1,2,6]

// int sirNr[] = { 1, 2, 3, 4 };
//
// for (int i = 0; i < sirNr.length; i++) {
// sirNr[i] = factorial(sirNr[i]);
// System.out.println(sirNr[i]);
// }
// }
//metoda de la Codruta
// public static int[] arrayFact (int[] sir) {
// int[] out = new int[sir.length]; // new int de lungimea lui
//
// for (int i = 0; i < sir.length; i++) {
// out[i] = factorial(sir[i]);
// }
// return out;
// }

// }
//
// public static int factorial(int nr) {
// int fact = 1;
//
// for (int i = 1; i <= nr; i++) {
// fact = fact * i;
// }
// return fact;
// }

// public static int factorial(int nr) {


// int fact = 1;
//
// for (int i = nr; i > 1; i--) {
// fact = fact * i;
// }
// return fact;

//1. Calculati nr de elemente prime dintr-un sir

// private static int countPrimeNumbers() {


//
// int[] numere = { 1, 2, 3, 4, 5, 7, 11, 17 };
// int count = 0;
// for (int i = 0; i < numere.length; i++) {
// if (isPrime(i)) {
// count += 1;
// }
// }
// return count;
// }

// private static boolean isPrime(int number) {


// for (int i = 2; i < number; i++) {
// if (number % i == 0) {
// return false;
// }
// }
// return true;

//metoda de la Codruta

// private static boolean isPrime(int nr) {


// if (nr > 1) {
// for (int i = 2; i < nr / 2; i++) {
// if (nr % i == 0) {
// return false;
// }
// }
// } else {
// return false;
// }
// return true;
// }

// private static int getPrimeNumbers(int[] sir) {


//
// int nr = 0;
// for (int i = 0; i < sir.length; i++) {
// if (isPrime(sir[i])) {
// nr++;
// }
// }
// return nr;

S-ar putea să vă placă și