Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jg 102 mycollection #98

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.hillel.elementary.javageeks.mycollection;

import java.util.LinkedList;

public class MyList {

public static LinkedList actMatrix(int[][] arrOne,int[][] arrTwo,String argument){
LinkedList<Integer> listFist = matrixToList(arrOne);
LinkedList<Integer> listSecond = matrixToList(arrTwo);

LinkedList listResult = actList(listFist, listSecond,argument);
return listResult;

}

private static LinkedList matrixToList(int[][] arr){
LinkedList list = new LinkedList();
for (int i = 0;i < arr.length;i++){
for (int k = 0;k < arr[0].length;k++){
if(arr[i][k] != 0){
list.add(arr[i][k]);
}
}
}
return list;
}

private static LinkedList actList(LinkedList<Integer> listOne,LinkedList<Integer> listTwo,String argument){
LinkedList list = new LinkedList();
int minSize = 0;
if(listOne.size() <= listTwo.size()){
minSize = listOne.size();
}
else {
minSize = listTwo.size();
}

if(argument == "sum") {
for (int i = 0; i < minSize; i++) {
list.add(listOne.get(i) + listTwo.get(i));
}
return list;
}
if(argument == "multiply"){
for(int i = 0;i < minSize;i++){
list.add(listOne.get(i) * listTwo.get(i));
}
return list;
}
return list;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.hillel.elementary.javageeks.mycollection;

import org.junit.jupiter.api.Test;

import java.util.LinkedList;

import static org.junit.jupiter.api.Assertions.*;

class MyListTest {
@Test
void shouldsumMatrix(){
int [][] matrixOne = {{0,2,0},{1,0,0},{0,0,3}};
int [][] matrixTwo = {{0,2,0,1},{1,0,0,2},{0,0,3,0}};
Integer[] num = {4,2,4};
assertArrayEquals(num,MyList.actMatrix(matrixOne, matrixTwo,"sum").toArray());
}
@Test
void shouldmultyplyMatrix(){
int [][] matrixOne = {{0,2,0},{1,0,0},{0,0,3}};
int [][] matrixTwo = {{0,2,0,1},{1,0,0,2},{0,0,3,0}};
Integer[] num = {4, 1, 3};
assertArrayEquals(num,MyList.actMatrix(matrixOne, matrixTwo,"multiply").toArray());
}

}