1. Куча / Говнокод #27544

    0

    1. 1
    IT Оффтоп #105

    #75: https://govnokod.ru/27166 https://govnokod.xyz/_27166
    #76: https://govnokod.ru/27168 https://govnokod.xyz/_27168
    #77: https://govnokod.ru/27186 https://govnokod.xyz/_27186
    #78: https://govnokod.ru/27219 https://govnokod.xyz/_27219
    #79: https://govnokod.ru/27254 https://govnokod.xyz/_27254
    #80: https://govnokod.ru/27270 https://govnokod.xyz/_27270
    #81: https://govnokod.ru/27280 https://govnokod.xyz/_27280
    #82: https://govnokod.ru/27284 https://govnokod.xyz/_27284
    #83: https://govnokod.ru/27296 https://govnokod.xyz/_27296
    #84: https://govnokod.ru/27336 https://govnokod.xyz/_27336
    #85: https://govnokod.ru/27381 https://govnokod.xyz/_27381
    #86: https://govnokod.ru/27405 https://govnokod.xyz/_27405
    #87: https://govnokod.ru/27429 https://govnokod.xyz/_27429
    #88: https://govnokod.ru/27432 https://govnokod.xyz/_27432
    #89: https://govnokod.ru/27435 https://govnokod.xyz/_27435
    #90: https://govnokod.ru/27439 https://govnokod.xyz/_27439
    #91: https://govnokod.ru/27449 https://govnokod.xyz/_27449
    #92: https://govnokod.ru/27460 https://govnokod.xyz/_27460
    #93: https://govnokod.ru/27463 https://govnokod.xyz/_27463
    #94: https://govnokod.ru/27466 https://govnokod.xyz/_27466
    #95: https://govnokod.ru/27473 https://govnokod.xyz/_27473
    #96: https://govnokod.ru/27478 https://govnokod.xyz/_27478
    #97: https://govnokod.ru/27484 https://govnokod.xyz/_27484
    #98: https://govnokod.ru/27495 https://govnokod.xyz/_27495
    #99: https://govnokod.ru/27504 https://govnokod.xyz/_27504
    #100: https://govnokod.ru/27508 https://govnokod.xyz/_27508
    #101: https://govnokod.ru/27511 https://govnokod.xyz/_27511
    #102: https://govnokod.ru/27518 https://govnokod.xyz/_27518
    #103: https://govnokod.ru/27526 https://govnokod.xyz/_27526
    #104: https://govnokod.ru/27534 https://govnokod.xyz/_27534

    nepeKamHblu_nemyx, 01 Августа 2021

    Комментарии (2460)
  2. JavaScript / Говнокод #27543

    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
    namespace Ifaces {
        interface IFoo {
            foo(): number;
            bar(x: number): string;
            twoArg(x: number, y: number): number;
            baz: string;
        }
    
        class A implements IFoo {
            constructor() {
                this.baz = "Q" + "A";
            }
            foo(): number {
                return 12;
            }
            bar(v: number) {
                return v.toString();
            }
            twoArg(x: number, y: number) {
                return x;
            }
            baz: string;
        }
        class B extends A {
            foo(): number {
                return 13;
            }
        }
    
        function foo(f: IFoo) {
            return "" + f.foo() + f.baz + f.bar(42);
        }
    
        export function run() {
            print("Ifaces.run");
            let a = new A();
            assert("" + foo(a) + "X" == "12.QA42.X");
            assert((a as IFoo).twoArg(1, 2) == 1, "t");
            a = new B();
            assert("" + foo(a) + "X" == "13.QA42.X", "b");
            let q = a as IFoo;
            q.baz = "Z";
            assert("" + foo(q) + "X" == "13.Z42.X", "x");
            print("Ifaces.runDONE");
        }
    }
    
    function main() {
        Ifaces.run();
        print("done.");
    }

    хотел оставить вас в покое. но не смог. Ловите - fields в интерфейсах

    ASD_77, 01 Августа 2021

    Комментарии (161)
  3. Python / Говнокод #27542

    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
    class Bagor:
    	r = []
    	
    	def __init__(self, val):
    		self.r.append(val)
    		
    	def get(self):
    		return self.r[0]
    		
    kakoi = Bagor(1)
    bagor = Bagor(2)
    print([kakoi.get(), bagor.get()])

    https://ideone.com/K7tADi

    3_dar, 29 Июля 2021

    Комментарии (26)
  4. Си / Говнокод #27541

    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
    /* https://fstarlang.github.io/lowstar/html/Introduction.html#the-essence-of-low
    
    Consider the following very simple program:
    
    module Intro
    
    module P = LowStar.Printf
    module C = LowStar.Comment
    module B = LowStar.Buffer
    
    open FStar.HyperStack.ST
    open LowStar.BufferOps
    
    let main (): St Int32.t =
      push_frame ();
      let b: B.buffer UInt32.t = B.alloca 0ul 8ul in
      b.(0ul) <- 255ul;
      C.comment "Calls to printf are desugared via meta-programming";
      let s = "from Low*!" in
      P.(printf "Hello from %s\nbuffer contents: %xul\n"
        s 8ul b done);
      pop_frame ();
      0l
    
    ....
    
    Once compiled by the KreMLin compiler, we obtain the following C code:
    */
    
    int32_t main()
    {
      uint32_t b[8U] = { 0U };
      b[0U] = (uint32_t)255U;
      /* Calls to printf are desugared via meta-programming */
      Prims_string s = "from Low*!";
      LowStar_Printf_print_string("Hello from ");
      LowStar_Printf_print_string(s);
      LowStar_Printf_print_string("\nbuffer contents: ");
      LowStar_Printf_print_lmbuffer_u32((uint32_t)8U, (uint32_t *)b);
      LowStar_Printf_print_string("\n");
      return (int32_t)0;
    }

    Какая-то компилируемая в сишку хренотень с завтипами, разрабатываемая в Microsoft Research

    j123123, 29 Июля 2021

    Комментарии (66)
  5. JavaScript / Говнокод #27540

    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
    namespace InstanceOf {
    
        class Foo {
            x: number
            y: string
            bar() {
                return this.x
            }
        }
    
        class Bar extends Foo { }
        class Baz extends Foo { }
        class Bar2 extends Bar { }
        class Bar3 extends Bar { }
    
        export function run() {
            print("InstanceOf")
    
            assert(new Bar2() instanceof Foo, "if")
            assert(new Bar2() instanceof Bar, "ib")
            assert(new Bar2() instanceof Bar2, "ib2")
            assert(new Bar3() instanceof Bar, "ib")
            assert(!(new Bar2() instanceof Baz), "!ib")
            assert(!(new Foo() instanceof Baz), "!ib2")
            assert(!(new Foo() instanceof Bar), "!ib2")
    
            (new Foo()).bar();
            (new Bar3()).bar();
        }
    }
    
    function main()
    {
      InstanceOf.run()
      print("done");
    }

    Возрадуйтесь братья и сестры. я вам принес зачатки RTTI :) и узрите этот дампик во очию.

    ASD_77, 29 Июля 2021

    Комментарии (79)
  6. Куча / Говнокод #-27539

    +1

    1. 1
    Именно поэтому я за «PHP» #4

    #1: https://govnokod.ru/26462 https://govnokod.xyz/_26462
    #2: https://govnokod.ru/26827 https://govnokod.xyz/_26827
    #3: https://govnokod.ru/26832 https://govnokod.xyz/_26832

    nepeKamHblu_nemyx, 28 Июля 2021

    Комментарии (9875)
  7. JavaScript / Говнокод #27538

    +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
    namespace Ifaces {
        interface IFoo {
            foo(): number;
        }
    }
    
    class Cls1 implements Ifaces.IFoo
    {
    	foo(): number
    	{
    		print("Hello");
    		return 1;
    	}
    }
    
    function main()
    {
    	const cls1 = new Cls1();
    	cls1.foo();
    
    	const ifoo: Ifaces.IFoo = cls1;
    	ifoo.foo();
    }

    Алилуя. я вам интерфейсы принес... узрите теперь дампик

    ASD_77, 27 Июля 2021

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

    −1

    1. 1
    Немного лирики в ветку

    День за днем из года в год
    Мы херачим говнокод
    Не испытывая стресс
    Мы шатаем 1С

    И работаем мы чисто
    По заветам программистов:
    "CTRL+C, CTRL+V
    И гоните мне лавэ!"

    DrAku1a, 26 Июля 2021

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

    +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
    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
    class Solution {
    public:
        std::vector<std::vector<int>> diagonalSort(std::vector<std::vector<int>> & mat) {
            if (!mat.size()) return mat;
            
            const size_t rl = mat[0].size();
            const size_t cl = mat.size();
            
            sort(mat, rl, cl, 0, 0);
            for (size_t i = 1; i < rl; ++i) {
                sort(mat, rl, cl, 0, i);
            }
            for (size_t i = 1; i < cl; ++i) {
                sort(mat, rl, cl, i, 0);
            }
            
            return mat;
        }
    private:
        void sort(std::vector<std::vector<int>> & mat, size_t rl, size_t cl, size_t i, size_t j) {
            const size_t len = std::min(rl - j, cl - i);
            const size_t endj = j + len;
            const size_t endi = i + len;
            std::sort(diag_iter<false>{&mat, i, j}, diag_iter<false>{&mat, endi, endj});
        }
        
        template <bool isConst>
        class diag_iter {
            std::vector<std::vector<int>> *base;
            size_t i, j;
            using T = int;
        public:
            using iterator_category = std::forward_iterator_tag;
            using difference_type   = std::ptrdiff_t;
            using value_type        = T;
            using pointer           = T*;
            using reference         = typename std::conditional<isConst, const T&, T&>::type;
            diag_iter(std::vector<std::vector<int>> *base, size_t i, size_t j) : base(base), i(i), j(j) { }
            diag_iter(const diag_iter&) = default;
            diag_iter& operator=(const diag_iter&) = default;
            ~diag_iter() = default;
            reference operator*() const { return (*base)[i][j]; }
            diag_iter& operator++() { i++; j++; return *this; }
            friend bool operator== (const diag_iter& a, const diag_iter& b) { return a.i == b.i && a.j == b.j; };
            friend bool operator!= (const diag_iter& a, const diag_iter& b) { return !(a == b); };
            pointer operator->() const { return &(this->operator*()); }
            diag_iter operator++(int) { diag_iter tmp = *this; ++(*this); return tmp; }
            diag_iter() = default;
            diag_iter& operator--() { i--; j--; return *this; }
            diag_iter operator--(int) { diag_iter tmp = *this; --(*this); return tmp; }
            diag_iter& operator+=(difference_type n) { i += n; j += n; return *this; }
            friend diag_iter operator+(diag_iter it, difference_type n) { return it += n; }
            diag_iter& operator-=(difference_type n) { i -= n; j -= n; return *this; }
            diag_iter operator-(difference_type n) const { return diag_iter(*this) -= n; }
            friend difference_type operator-(const diag_iter& a, const diag_iter& b) { return (b.j * b.base->size() + b.i) - (a.j * a.base->size() + a.i); }
            reference operator[](difference_type n) const { return *(*this + n); }
            friend bool operator<(const diag_iter& a, const diag_iter& b) { return b - a > 0; }
            friend bool operator>(const diag_iter& a, const diag_iter& b) { return b < a; }
            friend bool operator>=(const diag_iter& a, const diag_iter& b) { return !(a < b); }
            friend bool operator<=(const diag_iter& a, const diag_iter& b) { return !(a > b); }
        };
    };

    https://leetcode.com/problems/sort-the-matrix-diagonally/

    Сортировка через итераторы оказалась примерно в три раза медленнее, чем через копирование в вектор, сортировку его и копирование обратно.

    grillow1337, 25 Июля 2021

    Комментарии (18)
  10. Си / Говнокод #27535

    −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
    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
    void* execute_thread(void* arg)
    {
        int i;
        int interval;
    
        //Период контроля времени задаётся с точностью в 10мс.
        //Контролировать в данной реализации таймера точность в 1мс не имеет смысла,
        //так как это почти не возможно и, как правило, не требуется,
        //а крутить проверку таймеров с такой частотой только "пожерать" ресурсы процессора.
    
        struct timespec sleep_period = {0,9999999}; //Период, почти 10 мс
    
        do {
            for(i=0;i<n_timers;i++){
                if(timers[i]->enable == false){
                    //Если таймер не активный, то присваиваем ему начальное значение
                    clock_gettime(CLOCK_REALTIME, &timers[i]->time_before);
                }
            }
            //Засыпаем на 10мс
            nanosleep(&sleep_period , NULL);
    
            for(i=0;i<n_timers;i++){
                if(timers[i]->enable == true){
                    //Получаем текущее значение времени.
                    clock_gettime(CLOCK_REALTIME, &timers[i]->time_after);
                    //Вычисляем прошедшее время ожидания
                    interval = ((timers[i]->time_after.tv_sec-timers[i]->time_before.tv_sec)*1000000000 
                                +timers[i]->time_after.tv_nsec-timers[i]->time_before.tv_nsec)/1000000; 
                    //Проверяем условие, если ОК, то обновляем время и формируем событие
                    if(interval >= timers[i]->interval){
                        clock_gettime(CLOCK_REALTIME, &timers[i]->time_before);
                        timers[i]->listener->on_time(timers[i]);
                    }
                }
            }
         } while (terminate == false);
    }

    https://habr.com/ru/post/569392/
    > Объектно-ориентированное программирование на Си без плюсов. Часть 2. Таймер

    PolinaAksenova, 24 Июля 2021

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