0
- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
import java.util.Arrays;
import java.util.Optional;
public class AntiVirus{
private int scanIntensity = 0;
//this method is ready for you.
public void setScanIntensity(int level){
scanIntensity = level;
}
//write this method.
public String scanFile(File file,VirusDB database){
String[] signature = database.getSignatures(scanIntensity);
String fileData = file.getData().toLowerCase();
Optional<String> res = Arrays.stream(signature)
.map(s -> s.toLowerCase())
.filter(fileData::contains).findAny();
String scan = " is safe";
if(res.isPresent()) {
System.out.println(res.get());
System.out.println("scan: " + scan);
scan = " is not safe";
System.out.println("scan after: " + scan);
}
return file.getName() + scan;
}
}
Не понимаю, почему не работает.
Задача
https://www.codewars.com/kata/5b13027eedd62c5216000001
Test Results:
AVTest
checkRandomFiles
Log
dos
scan: is safe
scan after: is not safe
dos
scan: is safe
scan after: is not safe
dos
scan: is safe
scan after: is not safe
dos
scan: is safe
scan after: is not safe
dos
scan: is safe
scan after: is not safe
dos
scan: is safe
scan after: is not safe
dos
scan: is safe
scan after: is not safe
dos
scan: is safe
scan after: is not safe
dos
scan: is safe
scan after: is not safe
expected:<f4wpzFoQD is [not ]safe> but was:<f4wpzFoQD is []safe>
Stack Trace
Completed in 476ms
checkSameFilesWithDifferentIntensitySett ings
Log
virus
scan: is safe
scan after: is not safe
expected:<file1 is [not ]safe> but was:<file1 is []safe>
Stack Trace
Completed in 1ms
Completed in 496ms
imrnccc,
14 Сентября 2021
0
- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
public static boolean isMagicSquare(int[][] a) {
boolean isMagic = true;
boolean isSquare = true;
//square? checking here.
for(int i = 0; i < a.length; i++) {
if(a.length != a[i].length) {
isSquare = false;
}
}
if(isSquare) {
int sum = 0;
int nextSum = 0;
//first row
for(int i = 0; i < a.length; i++) {
sum += a[0][i];
}
//rows
for(int i = 1; i < a.length; i++) {
for(int j = 0; j < a.length; j++) {
nextSum += a[i][j];
}
if(nextSum != sum) {
isMagic = false;
break;
} else {
nextSum = 0;
}
}
//columns
if(isMagic) {
for(int i = 0; i < a.length; i++) {
for(int j = 0; j < a.length; j++) {
nextSum += a[j][i];
}
if(nextSum != sum) {
isMagic = false;
break;
} else {
nextSum = 0;
}
}
//diagonals
if(isMagic) {
for(int i = 0; i < a.length; i++) {
nextSum += a[i][i];
}
if(nextSum != sum) {
isMagic = false;
} else {
nextSum = 0;
}
if(isMagic) {
int j = a.length - 1;
for(int i = 0; i < a.length; i++) {
nextSum += a[i][j];
if(j > 0) {
j--;
}
}
if(nextSum != sum) {
isMagic = false;
}
}
}
}
} else {
isMagic = false;
}
return isMagic;
}
Write a method called isMagicSquare that accepts a two-dimensional array of integers as a parameter and returns true if it is a magic square. A square matrix is a magic square if it is square in shape (same number of rows as columns, and every row the same length), and all of its row, column, and diagonal sums are equal. For example, [[2, 7, 6], [9, 5, 1], [4, 3, 8]] is a magic square because all eight of the sums are exactly 15.
(https://practiceit.cs.washington.edu/problem/view/bjp3/chapter7/e20%2DisMagicSquare)
Работает, но код нечитаемый. Как сократить? Понятия не имею.
imrnccc,
28 Марта 2021