1. 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)
  2. Pascal / Говнокод #27324

    +1

    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
    program square;
    
    var sep, input: string;
      i, j, n, m : integer;
      
    
    begin
      write('sep:');
      readln(sep);
      
      write('str:');
      readln(input);
      
      for i:=1 to length(input) do write(input[i], ' ');
      writeln();
      for i:=2 to length(input)-1 do begin
        write(input[i]);
        for j:=1 to length(input)*2-3 do write (sep);
        writeln(input[length(input) - i + 1]);
      end;
      for i:=0 to length(input)-1 do 
        write(input[length(input)-i], ' ');
    end.

    Написала на скучной лекции программку на телефоне, чтобы распечатывать

    Х У Й
    У У
    Й У Х


    и прочие интересности UwU

    Пользуйтесь на здоровье, лицензия GNU GPL V3!

    KoWe4Ka_l7porpaMMep, 28 Марта 2021

    Комментарии (18)
  3. PHP / Говнокод #27323

    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
    function isRegEx($test)
    {
        $notThisLine = error_get_last();
        $notThisLine = isset($notThisLine['line']) ? $notThisLine['line'] + 0 : 0;
        while (($lines = rand(1, 100)) == $notThisLine);
        eval(
            str_repeat("\n", $lines) . 
            '@preg_match(\'' . addslashes($test) . '\', \'\');'
        );
        $check = error_get_last();
        $check = isset($check['line']) ? $check['line'] + 0 : 0;
        return $check == $notThisLine;
    }

    "Test if a regular expression is a valid one in PHP"
    https://stackoverflow.com/a/15143479/6702274

    kezzyhko, 28 Марта 2021

    Комментарии (9)
  4. Куча / Говнокод #27322

    0

    1. 1
    Пиздец-оффтоп #15

    #1: https://govnokod.ru/26503 https://govnokod.xyz/_26503
    #2: https://govnokod.ru/26541 https://govnokod.xyz/_26541
    #3: https://govnokod.ru/26583 https://govnokod.xyz/_26583
    #4: https://govnokod.ru/26689 https://govnokod.xyz/_26689
    #5: https://govnokod.ru/26784 https://govnokod.xyz/_26784
    #5: https://govnokod.ru/26839 https://govnokod.xyz/_26839
    #6: https://govnokod.ru/26986 https://govnokod.xyz/_26986
    #7: https://govnokod.ru/27007 https://govnokod.xyz/_27007
    #8: https://govnokod.ru/27023 https://govnokod.xyz/_27023
    #9: https://govnokod.ru/27098 https://govnokod.xyz/_27098
    #10: https://govnokod.ru/27125 https://govnokod.xyz/_27125
    #11: https://govnokod.ru/27129 https://govnokod.xyz/_27129
    #12: https://govnokod.ru/27184 https://govnokod.xyz/_27184
    #13: https://govnokod.ru/27286 https://govnokod.xyz/_27286
    #14: https://govnokod.ru/27298 https://govnokod.xyz/_27298

    nepeKamHblu_nemyx, 27 Марта 2021

    Комментарии (3610)
  5. Python / Говнокод #27321

    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
    def luhn(self):
            all_sum = 0
            self.__card_number = str(self.__card_number)
            self.__card_number = list(self.__card_number)
            for element in range(len(self.__card_number)):
                if element % 2 == 0:
                    self.__card_number[element] = int(self.__card_number[element]) * 2
                    if self.__card_number[element] > 9:
                        number = self.__card_number[element]
                        self.__card_number[element] = number // 100 + number // 10 % 10 + number % 10  # sum digits of number
                self.__card_number[element] = int(self.__card_number[element])
                all_sum += self.__card_number[element]
            checksum = 0
            while checksum < 10:
                if all_sum % 10 == 0:
                    checksum = str(checksum)
                    break
                else:
                    all_sum += 1
                    checksum += 1
            self.i = str(self.i)
            self.i = list(self.i)
            self.i.append(checksum)
            self.__card_number = self.i
     
            self.__card_number = "".join(self.__card_number)

    Для преокта нужен был алгоритм луна, чтобы создать в конце контрольную сумму. При написании проебался, что мне card_number нужно просто добавить 1 цифру, а не менять его и по этому просто добавил костыль в виде i.

    warzon131, 27 Марта 2021

    Комментарии (0)
  6. PHP / Говнокод #27320

    +1

    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
    $query = "
    	SELECT
    		`version`, `mods`, `plugins`, `keywords`
    	WHERE
    		`version` = {$version}";
    foreach (explode(",", $mods) as $mod)
    {
    	$query += "AND `mods` LIKE '{$mod}' OR `mods` LIKE '{$mod},%' OR `mods` LIKE '%,{$mod},%' OR `mods` LIKE '%,{$mod}'";
    }
    foreach (explode(",", $plugins) as $plugin)
    {
    	$query += "AND `plugins` LIKE '{$plugin}' OR `plugins` LIKE '{$plugin},%' OR `plugins` LIKE '%,{$plugin},%' OR `plugins` LIKE '%,{$plugin}'";
    }

    Говнокод чтобы прогнать массив с id записей внутри varchar по массиву с id записей из переменной (id,id,id)

    Dev1lroot, 27 Марта 2021

    Комментарии (11)
  7. Куча / Говнокод #-27319

    +1

    1. 1
    Просто оффтоп #17

    #1: https://govnokod.ru/20162 https://govnokod.xyz/_20162
    #2: https://govnokod.ru/25329 https://govnokod.xyz/_25329
    #3: https://govnokod.ru/25415 https://govnokod.xyz/_25415
    #4: (vanished) https://govnokod.xyz/_25472
    #5: https://govnokod.ru/25693 https://govnokod.xyz/_25693
    #6: (vanished) https://govnokod.xyz/_26649
    #7: https://govnokod.ru/26672 https://govnokod.xyz/_26672
    #8: https://govnokod.ru/26924 https://govnokod.xyz/_26924
    #9: https://govnokod.ru/27072 https://govnokod.xyz/_27072
    #10: https://govnokod.ru/27086 https://govnokod.xyz/_27086
    #11: https://govnokod.ru/27122 https://govnokod.xyz/_27122
    #12: https://govnokod.ru/27153 https://govnokod.xyz/_27153
    #13: https://govnokod.ru/27159 https://govnokod.xyz/_27159
    #14: https://govnokod.ru/27200 https://govnokod.xyz/_27200
    #15: https://govnokod.ru/27237 https://govnokod.xyz/_27237
    #16: https://govnokod.ru/27282 https://govnokod.xyz/_27282

    nepeKamHblu_nemyx, 26 Марта 2021

    Комментарии (6909)
  8. C# / Говнокод #27318

    +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
    using System.Device.Gpio;
    using System;
    using System.Threading;
    
    namespace Blinky
    {
    	public class Program
        {
            private static GpioController s_GpioController;
            public static void Main()
            {
                s_GpioController = new GpioController();
    
                // ESP32 DevKit: 4 is a valid GPIO pin in, some boards like Xiuxin ESP32 may require GPIO Pin 2 instead.
                GpioPin led = s_GpioController.OpenPin(4,PinMode.Output);
                led.Write(PinValue.Low);
    
                while (true)
                {
                    led.Toggle();
                    Thread.Sleep(125);
                    led.Toggle();
                    Thread.Sleep(125);
                    led.Toggle();
                    Thread.Sleep(125);
                    led.Toggle();
                    Thread.Sleep(525);
                }
            }        
        }
    }

    https://habr.com/ru/post/549012/: «.NET nanoFramework — платформа для разработки приложений на C# для микроконтроллеров».

    Ну все, последний оплот сишки пал, можно ее закапывать.

    PolinaAksenova, 25 Марта 2021

    Комментарии (26)
  9. C++ / Говнокод #27317

    +1

    1. 1
    pubimbue, ate и ios

    давайте ржать над крестостримами

    booratihno, 25 Марта 2021

    Комментарии (235)
  10. C++ / Говнокод #27316

    +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
    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
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    94. 94
    95. 95
    96. 96
    97. 97
    98. 98
    #include <array>
    #include <iostream>
    #include <string_view>
    #include <type_traits>
    
    std::string_view getMaxMargin();
    
    template<int bit_num>
    struct flag {
        friend constexpr int adl_flag(flag<bit_num>);
    };
    
    template<int bit_num>
    struct writer {
        friend constexpr int adl_flag(flag<bit_num>)
        {
            return bit_num;
        }
    
        static constexpr int value = bit_num;
    };
    
    template<int bit_num, int = adl_flag(flag<bit_num>{})>
    constexpr bool is_flag_set(int, flag<bit_num>)
    {
        return bit_num >= 0;
    }
    
    template<int bit_num>
    constexpr bool is_flag_set(float, flag<bit_num>)
    {
        return false;
    }
    
    template<size_t number, size_t bit_num>
    constexpr bool get_bit()
    {
        return (number & (1 << bit_num)) != 0;
    }
    
    #define flags_to_size()                     \
         ((is_flag_set<0>(0, flag<0>{}) << 0)   \
        | (is_flag_set<1>(0, flag<1>{}) << 1)   \
        | (is_flag_set<2>(0, flag<2>{}) << 2)   \
        | (is_flag_set<3>(0, flag<3>{}) << 3))
    
    template<bool test, typename T_true, typename T_false>
    struct meta_if {
        using type = T_true;
    };
    
    template<typename T_true, typename T_false>
    struct meta_if<false, T_true, T_false> {
        using type = T_false;
    };
    
    template<bool test, typename T_true, typename T_false>
    using meta_if_t = typename meta_if<test, T_true, T_false>::type;
    
    template<
        size_t desired_size,
        int = (0 +
               sizeof(meta_if_t<get_bit<desired_size, 0>(), writer<0>, int>) +
               sizeof(meta_if_t<get_bit<desired_size, 1>(), writer<1>, int>) +
               sizeof(meta_if_t<get_bit<desired_size, 2>(), writer<2>, int>) +
               sizeof(meta_if_t<get_bit<desired_size, 3>(), writer<3>, int>)
        )
    >
    constexpr size_t f()
    {
        return desired_size;
    }
    
    int main()
    {
        constexpr size_t a = f<1>();
        constexpr size_t b = f<6>();
        std::cout << "Max margin size: " << getMaxMargin().size() << std::endl;
    }
    
    constexpr size_t MARGIN_SIZE = flags_to_size();
    constexpr char MARGIN_CHAR = 'x';
    
    template<typename T, T... Args>
    constexpr auto getMarginStorageImpl(std::integer_sequence<T, Args...>)
    {
        return std::array<char, sizeof...(Args)>{(static_cast<void>(Args), MARGIN_CHAR)...};
    }
    constexpr auto getMarginStorage()
    {
        return getMarginStorageImpl(std::make_integer_sequence<int, MARGIN_SIZE>());
    }
    constexpr static auto marginStorage = getMarginStorage();;
    
    std::string_view getMaxMargin()
    {
        return std::string_view(marginStorage.data(), MARGIN_SIZE);
    }

    "Интересно, можно ли насфиначить такой шаблон, чтобы в пределах TU сгенерированные литералы были слайсами одного статического массива, длина которого выводилась бы автоматически."

    PolinaAksenova, 25 Марта 2021

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