for(...) {
for(...) {
for(...) {
}
}
}
I WILL write this and there's nothing you can do about it.
![]() Ape Out Shirt $21.68 |
![]() Tip Your Landlord Shirt $21.68 |
![]() Ape Out Shirt $21.68 |
for(...) {
for(...) {
for(...) {
}
}
}
I WILL write this and there's nothing you can do about it.
![]() Ape Out Shirt $21.68 |
![]() Tip Your Landlord Shirt $21.68 |
![]() Ape Out Shirt $21.68 |
That's cool.
>2 lines of unreachable code in just 6 lines total
wont make it
All 6 lines are unreachable because that doesn't compile
retord
me printing an tensor
nakadashi
Does anyone have a better solution to iterate over a 3d array?
bfs dfs
for (int i = 0; i < width * depth * height; i++) {
arr[i] = ...
}
>when your array goes above 1291 per side
just use #define int unsigned long long
and now you can get to almost 3 million per side
Does this work? I know multidimensional arrays are all allocated together in memory, but I was wondering if you can index them like this or if the compiler will throw a fit.
compiler complains
I tested it and you can do it in C/C++ but you have to be clever and user pointer arithmetic.
i=5
j=6
k=7
int matrix[i][j][k];
for(int n = 0; n < i*j*k; i++) {
*(** matrix + n ) = .. ;
}
.. being what you want to do. Basically because its a pointer of pointer of pointer you dereference twice to get the base pointer use that to move to the value you want and then dereference the last pointer.
just make one big array and then convert the index to coordinates
This solution works better because it gives you the option to index via 3 points if you want it. Which might be what you want for other operations.
it's also the simplest, fastest and smallest
The array example isn't bigger its the same size just typed differently. Using the multidimensional array makes more indexing valid.
Why would it be the fastest? Wouldn't the nested loops take the same time?
It probably wouldn't, but that depends on the compiler. There might be more optimizations for a large array. The compiler might not realize that you are functionally doing the same thing with the pointer arithmetic example. A lot of modern compilers can do loop unrolling implicitly and allow for non dependent parallelization of loops. IE running multiple iterations of a loop at once using sperate cores.
This is a better solution to get an integer overflow, for sure.
>single for
>instantly O(n)
foreach (v in arr.SelectMany(x => x.SelectMany(x => x*~~
{
}
works only for [][][], not for [,,]
for [,,] just use Cast<T>()
disgusting...
so this is the power of worktards?
>allocating for each element
why though
map3 = ((.).(.)) (>>= id) (=<<)
>(.).(.)
reported to hr for creating an unsafe work environment and also being ugly
How is any of this shit more readable than 3 nested for loops? You just made OP's code worse, congratulations
True, at least everybody understand these needed for loops immediately. And got
This style is quite universal in many languages (aka, they look very similar).
This solution requires that nested loops can be flatten like this. But if you still need all the running variables x y z, then you're better off just using nested loops, unless you have compiler optimization or want to run it on gpu for example.
>(.)(.)
booba
for (…) for(…) for(…){
}
.&stuff() for |@arr
rape correction is needed for the O(n3) brat
coder:
while(true)
{
}
programmer:
for(;;)
{
}
chad:
loop:
goto loop;
Ayy lmao
Do you even know what 'ayy lmao' means, you underage redditor?
ailens noise
#define ever (;;)
for ever
{
}
where did the word code come from?
From the Latin word "codex"
🙁
that's for being a homosexual and not writing in C
I don't want nor need your stupid old confusing language sorry man
how is it confusing?
if something doesnt work in c++ it usually just doesnt work, unless you want to spend an hour either rewriting it or do something confusing
however, because of this there is almost always a good alternative in c++ which is even easier to use than the original
this is proven just by the number of ways you can print something to the console or receive user input from it
the only issue with this that i see is that it requires you to look up almost everything up, but thats what programming is, you take what others have learned and add it to your own program for further levels of abstraction, otherwise every programmer would have to go from printing hello world on the console to modern graphics on their own
>if something doesnt work in c++ it usually just doesnt work
That's the complete opposite of reality.
In C++ development, 90% of the time is spent fighting against the language itself rather than implementing the business logic of your problem.
have you ever considered that maybe you are just bad at programming?
works in c,c++,c# haven't tried any other
C# I might use in my future Unity video game 🙂
the other two though, I've heard nothing but bad things about, which is the label I also put obsessive snobby elitist cultists under.
>obsessive snobby elitist cultists
that's the best part
>``
Typical front end """dev"""
frick you "``" is best javascript feature
It's the same as prefixing every single string in C# with $ even though you're not doing interpolations most of the time
It looks stupid and makes whoever is reading your code expect a reason for it to be present
Backticks:
>allow injecting variables like ${this}
>allow using quotations AND apostrophes without escapes
Quote marks:
>...
Literally why would I ever not use backticks?
Explain. Is there some tradeoff I'm unaware of?
>why would I ever not use backticks?
Because it won't work on IE
IEs EoL is in like 8 days
Good, geriatrics and chinks frick off.
...Do I give a shit?
Anyone using backticks is probably working with something like babeljs in their stack.
Works in java.
Demon
#define ever ;;
for (ever) {
/* code here */
}
for me it's
while(true)
{
}
As long as you break with goto.
I do nested for loops all the time. Could you create a function that returns after 2 levels and then you iterate over that with another for loop just so it looks cleaner?
DAMN CODE
NEEDS LOOP CORRECTION
for(...) {for(...) {for(...) {}}}
Based widescreen chad.
OP is right. stop reinventing the wheel
ToT
from itertools import product
for i, j, k in product(I, J, K):
...
Python chads strike again.
what a waste of quads for a moron that can't read
Sorry you got confused.
>what even is this shit
The same thing as 3 nested for loops, except elegant.
>how do I break out of each loop
I, J and K can be any sort of iterable, not just lists.
>I, J and K can be any sort of iterable
I don't see that in the type signature.
itertools.product(*iterables,repeat=1)
Although I realised they are consumed before running the loops so you couldn't break out based on what happens in the body of the loop.
There's generally a better design pattern than breaking out of for loops though. That's kinda a hallmark of bad programmers.
I wasn't the "break out of loop" guy. I was the (sarcastic) Python-type signature guy. And you didn't help me at all. In fact, you added an optional parameter I didn't even know existed.
> Also, being able to break out of loops is not bad per se. I prefer explicit break options like "foldWhile/foldUntil" over "break/return".
what even is this shit? how do i break out of each loop? why is Python so moronic?
what does that even do?
Whatever it does, it does it very inefficiently.
for(...) {
break;
for(...) {
for(...) {
}
}
}
do
x <- ...
y <- ...
...
O(n^3)
Not him but if you have data that occupies cubic space then you're hard pressed to iterate it in any less than cubic time complexity
Just invent a brand new algorithm, bro.
I’m not saying anything about anything. Just doing the needful sir
k, if you had to iterate a matrix or object data structure that had a need for it so be it.
nakadashi
Python is pretty cool, I guess.
#!/usr/bin/python3
import numpy as np
def main():
array = np.random.randint(0, 10, size = (3,4,5))
array.shape
print(array)
for elem in np.nditer(array):
print(elem, end=' ')
print()
if __name__ == '__main__': main()
isn't the point of python that you don't need a main function?
Why would that be the point of python? The point of doing that is so the file can be used either as a script or as a module.
All of that can be run without it, yes.
no.
>array.shape
What does this do?
shape the array into a nice cube
It initializes as an ugly cube?
don't be mean to the cube
I'm trying but your pythonism is making it difficult. Why does it need reshaping? Why does the shape method look like a getter?
>Why does it need reshaping?
so it looks nice
circular logic = circular logic
uglyCube = cube `from` Python("apparently")
>not unrolling your loops
ngmi
What's the problem?
There is absolutely nothing wrong with nested loops. If you need to iterate over all possible combinations of three indices, you need to iterate over all possible combinations of three indices, no amount of crying about imaginary "good code" practices will change that.
Writing such an algorithm in literally any way is going to be worse. Nested loops are the most explicit and direct way to express this construct, there is no problem with nesting if your logic does in fact demand nested operations. Torvalds lied to you.
perl -e 'print for qq/op is a homosexualn/;'
What is actually wrong with nesting for loops?
How else am I supposed to go through every element in a structure?
You're supposed to use some design pattern or some shit.
Apparently, actually saying what you want is now considered rude in programming as well.
nothing
you don't need to do anything else. this way is just as good as any other
There are cases, where this is totally normal?
But if you imply to deliberately overengineer something, which can be programmed simpler, then.. well, do whatever you want. It can be fun to to do unnecessary stuff once in a while.
Yikes, why aren't you following best practices?
Okay. There are some possible uses cases for that pattern.
Like what?
i completely forgot that Wendy's used to roast people
I do this and I look like this.
Cute
how else am i supposed to generate word lists if not with nested for loops?
Parser combinators?
NooOoooOo!!! You should put the forloops in a function and then put that function inside a for loop and then make a function out of it!!
def israelitelover69():
for n in nog:...
def frickjannies():
for n in nig:
israelitelover68()
def frickyou():
#this is O(n) because it only uses 1 for loop
for c in cracka:
frickjannies()
If you have a O(N**3) problem, you need a O(N**3) solution.