1. Java / Говнокод #27664

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 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

    Комментарии (10)
  2. Java / Говнокод #27663

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    private static final Map<Integer, FieldInfo> HEAD_FIELDS = new ImmutableMap.Builder<Integer, FieldInfo>()
    			.put(5, new FieldInfo("DOC.CODEOKATO", FieldInfoType.STRING))
    //			.put(7, new FieldInfo("DOC.NAMECMO", FieldInfoType.STRING))
    			.put(8, new FieldInfo("DOC.CODECMO", FieldInfoType.STRING))
    //			.put(9, new FieldInfo("DOC.NAMEMEDORG", FieldInfoType.STRING))
    			.put(10, new FieldInfo("DOC.CODEMO", FieldInfoType.STRING))
    			.put(14, new FieldInfo("DOC.INFINSPERSON_BIRTHDAY", FieldInfoType.DATE))
    			.put(12, new FieldInfo("DOC.INFINSPERSON_NUMHEALINS", FieldInfoType.STRING))
    			.put(15, new FieldInfo("DOC.INFINSPERSON_GENDER", FieldInfoType.STRING))
    			.put(16, new FieldInfo("DOC.INFINSPERSON_DATESTRMED", FieldInfoType.DATE))
    			.put(17, new FieldInfo("DOC.INFINSPERSON_DATEFINMED", FieldInfoType.DATE))
    			.put(13, new FieldInfo("DOC.INFINSPERSON_NMEDICCARD", FieldInfoType.STRING))
    			.put(18, new FieldInfo("DOC.INFINSPERSON_CODEMKB", FieldInfoType.STRING))
    			.put(23 - 1, new FieldInfo("DOC.INFINSPERSON_SEVCOVID19", FieldInfoType.STRING))
    			.put(24 - 1, new FieldInfo("DOC.INFINSPERSON_SEVCOVID191", FieldInfoType.STRING))
    			.put(25 - 1, new FieldInfo("DOC.INFINSPERSON_SEVCOVID192", FieldInfoType.STRING))
    			.build();

    Коллега изменил мой код, пока я болел.

    zloer, 14 Сентября 2021

    Комментарии (21)
  3. Java / Говнокод #27661

    +2

    1. 1
    https://codeforwin.org/2018/05/10-cool-bitwise-operator-hacks-and-tricks.html

    10 cool bitwise operator hacks and tricks every programmer must know

    Right shift (>>) operator is equivalent to division by 2
    Want to divide a number by 2 quicky. Here you go, use bitwise right shift operator to divide an integer by 2. Each right shift operation reduces the number (operand) to its half.


    Просто напомню, что Jawa-петушки вручную заменяют деление/умножение на 2 на сдвиг, потому что анскильный компилятор так не умеет.

    OCETuHCKuu_nemyx, 12 Сентября 2021

    Комментарии (87)
  4. Java / Говнокод #27577

    +2

    1. 1
    wrapOnException(() -> file.writeTo(env.getFiler()));

    https://www.youtube.com/watch?v=nCkpzqqog4k

    3_dar, 17 Августа 2021

    Комментарии (5)
  5. Java / Говнокод #27574

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    package test.sandbox
    
    object Main {
      def foo(implicit a: Int): Int = a * 2
    
      def main(args: Array[String]): Unit = {
        {
          import Test._
          val result = foo
    
          println(s"Result1 = $result") // Result1 = 42
        }
        {
          implicit val x = 16
          println(s"Result2 = $foo")  // Result2 = 32
        }
      }
    }
    
    object Test {
      implicit val x: Int = 21
    }

    "Scala" — сахарная. (*^‿^*)

    PolinaAksenova, 16 Августа 2021

    Комментарии (19)
  6. Java / Говнокод #27487

    +2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    public static int booleanToInt(Boolean value) {
    
            int res;
    
            if (value == null) {
    
                res = 0;
    
            } else {
                if (value) {
                    res = 1;
                } else {
                    res = 0;
                }
            }
    
            return res;
        }

    Добро пожаловать к нам в индийскую школу программирования!

    nekkiy, 28 Июня 2021

    Комментарии (34)
  7. Java / Говнокод #27470

    0

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    /**
      * Gets a customer
      *
      * @param customerId customer's id
      * @return Customer 
      */
    Customer getCustomer(int customerId) {
          . . .
    }

    В джава-джава-городе, на джава-джава-улице, живёт джавамен и пишет JAVADOC *шокирующий писк*

    JloJle4Ka, 16 Июня 2021

    Комментарии (18)
  8. Java / Говнокод #27367

    +1

    1. 1
    there are java.io.FileNotFoundException and java.nio.file.NoSuchFileException . Both are subclasses of IOException, neither of them is a subclass of the opposite.

    DypHuu_niBEHb, 20 Апреля 2021

    Комментарии (10)
  9. Java / Говнокод #27360

    +2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    package org.trishinfotech.builder;
    
    public class Car {
    
        private String chassis;
        private String body;
        private String paint;
        private String interior;
        
        public Car() {
            super();
        }
    
        public Car(String chassis, String body, String paint, String interior) {
            this();
            this.chassis = chassis;
            this.body = body;
            this.paint = paint;
            this.interior = interior;
        }
    
        public String getChassis() {
            return chassis;
        }
    
    	public void setChassis(String chassis) {
            this.chassis = chassis;
    
        }
    
        public String getBody() {
            return body;
        }
    
        public void setBody(String body) {
            this.body = body;
        }
    
        public String getPaint() {
            return paint;
        }
    
        public void setPaint(String paint) {
            this.paint = paint;
        }
    		public String getInterior() {
            return interior;
        }
    
        public void setInterior(String interior) {
            this.interior = interior;
        }
    
        public boolean doQualityCheck() {
            return (chassis != null && !chassis.trim().isEmpty()) && (body != null && !body.trim().isEmpty())
                    && (paint != null && !paint.trim().isEmpty()) && (interior != null && !interior.trim().isEmpty());
        }
    
        @Override
        public String toString() {
            // StringBuilder class also uses Builder Design Pattern with implementation of java.lang.Appendable interface
            StringBuilder builder = new StringBuilder();
            builder.append("Car [chassis=").append(chassis).append(", body=").append(body).append(", paint=").append(paint)
            return builder.toString();
        }
    
    }

    https://habr.com/ru/company/otus/blog/552412/
    Паттерн проектирования Builder (Строитель) в Java

    PolinaAksenova, 15 Апреля 2021

    Комментарии (109)
  10. Java / Говнокод #27325

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 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

    Комментарии (12)