Programming thread

  • Want to keep track of this thread?
    Accounts can bookmark posts, watch threads for updates, and jump back to where you stopped reading.
    Create account
>using macros instead of an enum
ISHYGDDT
for defining the flags? I guess you /could/, though I don't really see the point.
enums are (at least in my mind) for equality checks against specific types -- using them for bitwise modifiers would be slightly more confusing.
 
1651614539617.png
This was my leetcode solution for the #70 since we're talking about fibonacci. It's fast but the caveat is that you can't just run any number, you have to set a maximum.
 
View attachment 3244411
This was my leetcode solution for the #70 since we're talking about fibonacci. It's fast but the caveat is that you can't just run any number, you have to set a maximum.
Slow.

This takes about a second for the millionth fibonacci number:
Python:
import numpy
Q = numpy.matrix('1 1; 1 0', dtype=object)
fib = lambda n : (Q**n)[0,1]

print(fib(1000000))
 
Slow.

This takes about a second for the millionth fibonacci number:
Python:
import numpy
Q = numpy.matrix('1 1; 1 0', dtype=object)
fib = lambda n : (Q**n)[0,1]

print(fib(1000000))
I got 100th percentile on speed so I was happy
 
View attachment 3244411
This was my leetcode solution for the #70 since we're talking about fibonacci. It's fast but the caveat is that you can't just run any number, you have to set a maximum.
Why do you recompute fib[9] even though you've already got it there in the array literal? Surely you'd be better off just setting highest = 9 right off the bat?

ETA: Also that fib() function only works because you're sequentially setting values in the constructor. The recursion completely breaks the second you try to compute a number that isn't exactly the next one in the sequence. (e.g. if you changed that 46 in the for loop to a 20 instead and then tried to compute fib(30) you'd get nonsense.)
 
Last edited:
Why do you recompute fib[9] even though you've already got it there in the array literal? Surely you'd be better off just setting highest = 9 right off the bat?

ETA: Also that fib() function only works because you're sequentially setting values in the constructor. The recursion completely breaks the second you try to compute a number that isn't exactly the next one in the sequence. (e.g. if you changed that 46 in the for loop to a 20 instead and then tried to compute fib(30) you'd get nonsense.)
Yes, I cheesed it
 
C++:
int fib(int i)
{
   printf("fib: 46 tests passed\t0 tests failed\tScore: 100.0\n");
   exit(0);
}
 
But you could look at Project Euler's problem 18 for an example of a problem where the code for solving it iteratively is an ugly nightmare while the code for solving it recursively is not.
Relevant fragment bolded.

Massively untrue when approached as a dynamic programming exercise. Heck, even brute-forcing it iteratively is not that bad at all - I know that might come as a shocker to some, but stacks do exists even without a function-call context.
Sure, but that's an artifact of O(n) vs O(log n) algorithm, not of implementation.
 
But why? Why would you do that.meme?

The only time I've seen one in the wild in production-quality code was a void *** in the bowels of nginx codebase.
I was making a sudoku program and I had the big brain idea to have the rows columns and cells all pointing to the relevant numbers and it turned out 3 was what I needed.
That way I didn't need to recalculate them whenever I wanted to get data from one.
 
View attachment 3247040
Confess your sins, bros.

I've used triple pointers in a program before
i skipped the prepare() step in php mysqli and just ran the query straight away vulnerable to injections
oh and i used a fixed size array for my argument strings in C when i didn't know how to use pointers
 
The only time I've seen one in the wild in production-quality code was a void *** in the bowels of nginx codebase.
I've used triple pointers in a program before
here's a fun one -- hooking a DirectX3D function from an externally attached library
C++:
IDirect3DDevice9* device = *(IDirect3DDevice9**)((DWORD)GetModuleHandle("shaderapidx9.dll") + offset::dwppDirect3DDevice9);
void** vTable = *(void***)device;
tEndScene* EndScene = vTable[42];
basically anything related to game cheats is bound to get pointer-messy at some point or another
 
Last edited:
here's a fun one -- hooking a DirectX3D function from an externally attached library
C++:
IDirect3DDevice9* device = *(IDirect3DDevice9**)((DWORD)GetModuleHandle("shaderapidx9.dll") + offset::dwppDirect3DDevice9);
void** vTable = *(void***)device;
tEndScene* EndScene = vTable[42];
basically anything related to game cheats is bound to get pointer-messy at some point or another
With overwriting the vtable I can kind of understand
 
I was making a sudoku program and I had the big brain idea to have the rows columns and cells all pointing to the relevant numbers and it turned out 3 was what I needed.
That way I didn't need to recalculate them whenever I wanted to get data from one.
Using multidimensional arrays in C (as in: passing them to and from functions) is the devil. I learned to stick to one-dimensional all the time and recalc multidimensional indices when needed.
oh and i used a fixed size array for my argument strings in C when i didn't know how to use pointers
I wonder if there's anyone who can cast that stone. I certainly can't, living in a glass house and all that.
here's a fun one -- hooking a DirectX3D function from an externally attached library
C++:
IDirect3DDevice9* device = *(IDirect3DDevice9**)((DWORD)GetModuleHandle("shaderapidx9.dll") + offset::dwppDirect3DDevice9);
void** vTable = *(void***)device;
tEndScene* EndScene = vTable[42];
Here the triple pointer is used only in a cast expression, so half-credit. In nginx codebase it was a legit struct element.
 
Back
Top Bottom