@@gtxg. bro manipulating atoms inside chips is so high level, I manipulate the quarks inside the electrons protons and neutrons inside atoms inside of chips
@@harrytsang1501 How is that more readable than it's equivalent in python? a_set = set() a_set.intesection(b) Or even better using proper set notation: a = {1,2,3,5} b = {2,4,5,8} print(a & b) # prints {2,5}
I’m just learning python and thought that would’ve been the solution, then when he started writing a far more robust code than I was prepared for I then thought to myself “ahh yes, I must have overlooked something - how foolish of me”
@@nathanaeltrimm2720 the video has overlooked duplicate values. The set approach is more consistent. It's slow af but who cares we are using python anyway
CS major here: Python is an interpreted language. That means the keyboard has to do extra work before the keystrokes can be send to the PC. That's why it is usually faster to type in C.
@@dingding4898 Hi! It's called the unpack operator. It's a very useful operator that can be useful in other contexts. For example: You can write [*...] instead of list(...) Or {*...} instead of set(...) But the unpack operator can be useful in a lot of other contexts. For example, if you want the first and last element, you will usually do something like: a,b=a_list[0], a_list[-1] But actually, you can write it in a shorter way with the unpack operator: a,*_,b=a_list And yes it works Python is a very rich and beautiful language. If you want to learn some tricks like that, you can read the documentation. But there is also a branch of programming which is called code golf, which consists in doing the smallest code possible. And so, you often learn new tricks. I created my own website about code golf, its called WeekGolf (week.golf) but there are other good websites like code golf stack exchange. Hope it helps :)
that's right, but what I dont get is the "map" function there, why parse each array item to string before showing on screen? it would just show the same thing
Your C code is O(n*m). You could sort the arrays first and then use two pointers to compare the sorted arrays and check for intersection. This would drop it down to O(n*logn + m*logm + m + n) which simplifies to O(n*log(n) + m*log(m)). This assumes an efficient sort algo like mergesort that offers O(xlogx)-. In case n>m, the final time complexity is O(n*log(n)).
Also, the C code being more explicit makes it clearer here what is happening in my opinion. The list comprehension + 'in' in Python hides how efficient this approach is. Is the interpreter smart enough to convert b into a hashset while checking to avoid slow linear search?
You mean allocing without free? I'll admit that it's bad, but it's not an error because the program ends quickly. When writing missile guidance software, their approach to freeing memory is "just wait until the ram is destroyed". Personally, I would have used alloca() to allocate an "out" buffer on the stack before passing it to intersect().
Your C code and Python code work differently if there can be duplicates in the arrays. If there are `n` of the same in a, and `m>0` of the same in b, then your Python code will give `n` duplicates, while your C code will give `n × m` duplicates, which might mean you go into unallocated memory This flaw in the C code could be very easily fixed by adding a `break;` after finding the first bi such that `a.arr[ai] == b.arr[bi]`. (i.e. after line 18)
Sir I respect you opinion but sir why would I just not do it the easy way. Why would I painful write 50 lines if I can just write 5. All and all that's my opinion :)
@@adil080_ the same reason if I would want my code to run 42x faster with the 5 lines than 50. C++ and C are meant for performance, python might be easier, but its to be noted that it's also slow as hell
@@keris3920 I said "doesn't mean you" programmed it to run faster. C often allows for some pretty dangerous code and can easily become very inefficient even when solving the same problem. Of course with the best implementation it's going to run faster.
@@DaJodad Sure, but you can write some really bad code in Python as well. A simple matrix multiplication in Python using for loops is more than 20,000x slower than the C implementation, and both of those implementations are O(n^3). My point is, the argument you're trying to make is not an argument for using Python. Writing the code in C, contrary to what you just said, is almost guaranteed to make your program much faster. As you mentioned, C can be dangerous because it's close to the hardware. This is the real reason someone should weigh when choosing the language. Sometimes python is inappropriate, and sometimes C is too cumbersome for the scope of your problem. But good C code will always be faster than good python code. Heck, even poor c code is often faster than good python code. Careful with the safety argument too, by the way. C has a compiler, which means it can catch a ton of bugs at compile-time. Python is interpreted and relies on runtime tests, which does make Python more dangerous to deploy to customers. Again, there are tradeoffs in all languages.
You know this is not the most efficient way right? you're doing in O(N * N) when you could do it in O(N). You would just put one of the arrays into a hashset, then loop through the array, every iteration for collision into the hashset, because the hashset is constant time lookup, assuming you are passing in values that have hashes, you are able to do this in O(N) in any language.
As someone who’s just started learning C, I’m gonna assume I unlock the inbuilt doom soundtrack in when I’ve learnt how to use 1 if statement without using stackoverflow
def intersect(a, b): return set(a).intersection(set(b)) There's no need to free up the memory the C code allocated in this case since it will just be freed by the OS when the program terminates. It'd only be a memory leak if the program continued running without the memory ever being cleaned up, e.g. if you ran the intersect function multiple times and none of the allocations were freed.
Unfortunately there are geniuses like you who do not free memory even in production code because "the OS will free it anyway". Libfontconfig comes to mind, where they allocate memory and store the pointer in global static variable, never freeing it. Now everyone need to add this allocation to whitelist of their Valgrind/ASan/whatever.
@@blueghost3649 It seems that you didn't pay attention while reading my comment. Unless you wrote some one-off script and then threw in to garbage bin, chances are that your piece of code will be used as a part of a bigger system. A piece of code in isolation can't know if it is allowed to cut corners by leaking owned resources - and that means it should always properly release those resources.
@@blueghost3649 it might be pointless, but it's incredibly important for large corporate code. If the user who's using the abstracted code doesn't realize that there's a memory leak further up the chain and then runs multiple children using that memory leak or exists for the duration of the program (which could take a while), or isn't seen and closed by the system during the duration, then the problem is now no longer negligible
@@abyrvalg_ problem statement was intersection of two arrays not two sets. so the question is should repeated values be left in the result or removed? it's undefined
Then in C one should apply qsort on both arrays and just then check for intersection (duplicates are easy to ignore in sorted arrays). That if you want the same time complexity.
use a hashmap. with the current impl it's O(n^2) since it contains nested loop. So unfortunately it would be slower than the python one when the input is large.
@@okkoheinio5139 By "n" I meant the total size of input. O(n^2) still stands here because big O denotes the worst case performance. Best case runtime happens when one of the inputs has size 1, which will take a linear (n-1) number of iterations to complete. The worst case happens when both of the input are of the same size, which would give a run time of 1/4(n^2) iterations. Thus the performance of this algorithm is O(n^2) after removing the coefficient from the worst case run time.
@@okkoheinio5139 If you set n and m to be the size of each arrays the complexity would be O(n*m), and if you set n to be the sum of the two array sizes, the complexity would be O(n^2). The result depends on how you define the input size. I should have been clear that I meant the latter case.
The C code is actually partially wrong... For example, arr1[] = { 8,3,1,5 }; arr2[] = { 5,3,4,6,9,3 }; returns 3 3 5 The idea is, if the element 3 appears once in arr1[], and the element 3 appears twice in arr2[], once your FOR loop finds a correspondent for an element in arr1[], it can find many more printing it repeatedly. So to fix this, once you find an element in intarr b, then leave the FOR loop searching for a correspondent, because you found one - so add a break; statement inside the IF statement. That's just to show how easy it is to write bugs in C code
@@jhonatasbonfim4917 you can sort the lists in O(n log n), then compare them in order in O(n), leading to a total of O(n log n). For large n this is much better than O(n^2)
But if you have arr1[] = {5, 3, 4, 6, 9, 3} and arr2[] = {8, 3, 1, 5} the result is wrong too, also if we use a flag to break the for loop... We have to save also the indexes of the used values...
@@AR-yd2nd All of C-python (the most common python implementation) is written in C. The difference is that python is meant to be general purpose and easy to use, whereas numpy is optimized for processing large arrays of numbers. That comes with certain restrictions/responsibilities on behalf of the programmer. For example, python integers have "arbitrary precision" to avoid overflowing. If a number becomes so large that it doesn't fit in the current datatype, python will automatically change the datatype to accomodate the new number. Numpy doesn't do that. Numpy arrays must contain one specific datatype (for example 64 bit integers). This means overflow is possible, but it also means numpy can implement certain optimizations by knowing the starting and ending datatype of all operations. Python lists are also optimized for dynamic sizing. They are good at updating how much memory they require when you add or remove elements. I don't believe numpy does that. Since the point of numpy is parallel computing, you're expected to create large arrays with a known size and then perform computations with them. You're not supposed to add new data to existing arrays. In fact, since numpy specializes in computations with large sets of data, it can actually be _slower_ than regular python if you're using very small arrays (or individual numpy numbers). It's important to know which tool is best for the job, and the best way to find that out is to test the execution time of different versions of the same code 🙂
@@farukyldrm8498 parts of NumPy are built on top of a standard API for linear algebra operations called BLAS and BLAS libraries make use of multiple threads to speed up some operations by default.
Ok. But python do the same job (or even more time consuming) on the lowest software layer. Python just have dedicated library to do this job. In C you need to write code which do the same, but on the lowest layer. It depends on destination of your functionality. If you need low-cost time consuming (c) or just do this job regardless costs (Python).
I'm a CS student and i still remembered that C is the second thing we learned after C++, don't understand shit and yet still wrote 100+ line of code for group project
In C++ it's super easy: using namespace std; vector a = { 8, 3, 1, 5 }; vector b = { 5, 3, 4, 6, 9 }; vector intersection; ranges::sort( a ); ranges::sort( b ); ranges::set_intersection( a, b, back_inserter( intersection ) ); for( auto n : intersection ) cout
You don't gotta loop twice, you'd have O(n*m) roughly O(n^2) complexity. You could instead create a hashing function and make a hashtable and run through both arrays once, checking whether it appears in the hash table, showing that they are the same values (resolving collisions would also have to be incorporated). This would give you O(n + m) roughly O(n) complexity at the disadvantage of space complexity which would be O(n + m) roughly O(n) rather than O(1), but sacrificing space for speed is pretty worthwhile.
@@freeshavaacadooo1095 I mean you're right - implementing a hash table is quite doable. But the amount of code you'd need to write to make it a) be efficient b) work with different types is cray cray
For those who dont understand why C was written faster than python: Actually it was a bot (programmed by the language of the vid's moment) that coded those files, and how we all know that C is faster than python, the C code was done faster. underrated vid
@@DaMonster As somewhat of an expert in not knowing enough about coding to actually understand what you meant but is learning c++ and is having fun doing it, my answer is this: Ok🗿
@@DaMonster sure, but in c++ you can write it in a shorter and prettier way, not to mention it would be easier to optimize it to O(n+m) as c++ has a builtin hashmap.
And this is why we have C++: #include #include template std::vector intersect( std::vector &a, std::vector &b) { std::unordered_set comp_set(a.begin(), a.end()); std::vector result; result.reserve(min(a.size(), b.size())); for( auto&val:b ) { if( comp_set.count(val) ) { result.push_back(val); } } return result; } Look how easy this is, and it's about as fast as you can get without implementing your own hashing function (afaik).
@@pepehimovic3135 Yeah, but that's Java. In C++, you use typename T is for actual types, whereas you would specify class T (so template T intersect(T&a, T&b); for a class (so not a primitive type). You do not have this distinction in Java because Java boxes everything (so everything is an object, even primitive types), adding unnecessary overhead, whereas C++ can work with primitive types without boxing. Which tends to be pretty good for performance.
I have a degree in computer science and I work with C/C++ almost daily for my job and python still makes me want to cry. I just cannot for the life of me get comfortable with that language. It’s not for everyone!
@@beanbag1415 There is a lot to learn, and no single human will ever learn everything there is to know about programming. Not sure if you’re looking for advice, but I understand how frustrating is to start out. Focus on one problem at a time, and even break it down into smaller problems if you can. And don’t think for a moment that if you’re doing something wrong, or make a mistake, that you’re failing. Every single programmer, from beginners to the highest level researchers, etc, all make mistakes way more often than you would think. It’s a weird mindset to adapt to, but making mistakes is necessary. There’s no way around the frustration of syntax errors though unfortunately. It gets easier the more you learn and practice.
i feel like the python code could just be return (set(a).intersection(set(b)) or whatever so its like faster than checking the second array every time we add a number hashsets and whatnot
Im not a programmer im just learning python but I can say that I appriciate C. Python feels like you're driving a nice car and C feels like you understand how the car was built and can adjust every bit of it for whatever the road calls for.
def inter(a,b): return set(a) & set(b) # Does not remove duplicates (you could create a remove duplicate function) def interNaive(a,b): result = [] for i in a: for j in b: if (i == j): result.append(i) return result a = [ 8, 3, 1, 5] b = [ 5, 3, 4, 6, 9] print(f"The intersection is: {inter(a,b)}") print(f"The intersection is: {interNaive(a,b)}")
To me Zig feels like someone got really fed up writing C in a team. And is more barebones but with very pure C integration. They put "Simple language, no macros" on their website as a feature. Nim feels like someone got really fed up with crappy abstractions and decided to make a language so expressive; That in team settings you better encourage everyone to use the least expressive features that suit the job so you do not end up in hell. Macros are wild, for good, and bad.
Maybe it could be a good idea to compare a running time also for larger arrays (with approx. 10000 numbers). In my experience, python is perfect for his ability to fast development; But the C language is perfect for fast execution (usually it is running time of C approx. 8 times shorter than python ) with solid development time. Lets imagine, that you need to covert video (some movie) and this conversion takes 10 hours by C code.. Are you really able to wait 80-100 hours to conversion of video with python code ? It is extremely wasting of time and electric energy.
coding this in rust takes me 30 seconds max, and the performance is 500nanoseconds, which is 1/2000 of a millisecond, so i think its pretty fast than python, and easy to code as well.
When you realize you could have just written "return list(set(a) & set(b))" instead of that pointless list comprehension in python to do the exact same thing.
it's still not that bad in assembly, since it's just comparing two arrays, you just need to store a null-terminator at the end of the two arrays stored in .data, then using pointe- oh wait, now i see the issue
If i understand correctly while it's dirty c version can be written like this, much shorter, well it's not clean and surely not the shortest but i perfer this version #include int main(void){ int a[4]={8,3,1,5}; int b[5]={5,3,4,6,9}; for(int i=0;i
My first class in coding was in C++ which I failed to learn. Then my second language that I’m still learning was R. The memories from C++ still hound me. I’m the only one in the class that uses for statements in R and python and the only that defines functions
Oh no this old static strongly typed and compiled programming language requiring more code to write than a dynamic and interpreted programming language! This is madness!
I like that fact you can type faster when writing in C. I think that's the best feature of that language.
He uses Vi that's why
The C sound track is pretty good, too.
@@doctorbobstone Doom OST - Meathook
Probably a gaming chair helped as well?
@@ennead322 not really
C is such a high level language... Glad i started with electric circuits applied to the radio in the 1890s
Bro electric circuits are so high level, I manipulate the atoms inside the chips
@@gtxg. bro manipulating atoms inside chips is so high level, I manipulate the quarks inside the electrons protons and neutrons inside atoms inside of chips
@@creepynutsrl you guys won't understand the basics.
I start by inventing the universe
I see smiling face in your pfp
Naw, he's got gas.
Try in JS, and it returns the string “true”, which equates to false.
In js think there are set datatype.... It's easyer ....
a_set = new Set(a);
return b.filter(elem => a_set.has(elem));
Much more readable than python and has lower runtime complexity than the C version
@@harrytsang1501 How is that more readable than it's equivalent in python?
a_set = set()
a_set.intesection(b)
Or even better using proper set notation:
a = {1,2,3,5}
b = {2,4,5,8}
print(a & b) # prints {2,5}
@@Israel220500 👍
@@Israel220500 Admit it, dude's solution is much more readable.
Set datatypes are way superior for this type of discrete problems.
As a C programmer, i can confirm doom music is playing when we start typing
Ahh. I see why I keep having issues whole learning it, I tend to listen to softer music.
I'll try a different Playlist
@@NotTheHeroStudios So, how's it been since you switched playlists? I might try it myself.
@@scartyz762 he has ascended.
its a scary language but i love it
I also write C in 4x speed compared to Python for some odd reason.
🤓
probably vim
everytime i go back to python i have to readapt myself to not using brackets
c has lot of useless stuff you need to type, python you have to think about everyline
@@abdelidrissi7241 I can't tell if this is a joke
Python dev would have done list(set(a) & set(b))
Stop letting the world know we don't know how to code
I’m just learning python and thought that would’ve been the solution, then when he started writing a far more robust code than I was prepared for I then thought to myself “ahh yes, I must have overlooked something - how foolish of me”
while true, technically a list is a collection, not a true array. You got to import array and do it the hard way.
@@nathanaeltrimm2720 the video has overlooked duplicate values. The set approach is more consistent. It's slow af but who cares we are using python anyway
@@TheMrCarnification set() is slow but isn't a list comprehension even slower in python?
CS major here:
Python is an interpreted language. That means the keyboard has to do extra work before the keystrokes can be send to the PC. That's why it is usually faster to type in C.
You could write print(*c) instead of print(" ".join(map(str,c)))
Who are you, & Why're you so smart in the ways of python?
What is this way called?
@@dingding4898 hes god
@@dingding4898 Hi!
It's called the unpack operator.
It's a very useful operator that can be useful in other contexts.
For example:
You can write [*...] instead of list(...)
Or {*...} instead of set(...)
But the unpack operator can be useful in a lot of other contexts.
For example, if you want the first and last element, you will usually do something like:
a,b=a_list[0], a_list[-1]
But actually, you can write it in a shorter way with the unpack operator:
a,*_,b=a_list
And yes it works
Python is a very rich and beautiful language.
If you want to learn some tricks like that, you can read the documentation. But there is also a branch of programming which is called code golf, which consists in doing the smallest code possible. And so, you often learn new tricks. I created my own website about code golf, its called WeekGolf (week.golf) but there are other good websites like code golf stack exchange.
Hope it helps :)
@@dingding4898 i think it is called unpacking
But why does *c does join and map at once?
In python you can simply do print(*c).
No need to use join and map.
Just wrote it myself to confirm. Same exact output so thanks
Woah
That's going to be really useful to me. Thank you!
that's right, but what I dont get is the "map" function there, why parse each array item to string before showing on screen? it would just show the same thing
@@stackercoding2054join can only accept Iterable[str] as parameter, it's not possible to directly join int.
I like the fact that he uses vi just for C
Using vi is enough
I like the fact he can close it
Not even vim, just vi...
@@Mmnc-bv3rk i think he used nvim by looking at all of those plugins and the line numbering.
@@edward8064 Nah, 'vi' command is mostly syslink to 'vim' in many linux distros
Your C code is O(n*m). You could sort the arrays first and then use two pointers to compare the sorted arrays and check for intersection. This would drop it down to O(n*logn + m*logm + m + n) which simplifies to O(n*log(n) + m*log(m)). This assumes an efficient sort algo like mergesort that offers O(xlogx)-. In case n>m, the final time complexity is O(n*log(n)).
Also, the C code being more explicit makes it clearer here what is happening in my opinion. The list comprehension + 'in' in Python hides how efficient this approach is. Is the interpreter smart enough to convert b into a hashset while checking to avoid slow linear search?
I don't think Python interpreter works this way - 1st code is O(n*m) too
@@bayzed in my eyes python solution was much less readable than C solution, but maybe that's because I'm not that much into python as I'm with C/C++
@@prodbytukoo wdym, the python solution was very easy to read and pretty logical too
Or just use hashmap....
Every time I see calloc I remember why I suffer from depression
And the memory hasn't been freed! That kills me.
@@radmir_khusnutdinov Just restart the computer... that frees everything.
@@RurikLoderr windows handles memory y'all, when the exe is done running windows will take its memory back
@@Bl0xxy He's not using Windows
@@IamPyu-v unix doesn't do that?
C is very user friendly. He's just choosy about his friends.
And the program in C, of course, has an error in working with memory.
The compile time of python is sprinkled into the runtime speed
@@linuxization4205 What does Python's combo runtime and compile time have to do with writing borked memory management into C code?
It is indeed very fast at segfaulting
You mean allocing without free? I'll admit that it's bad, but it's not an error because the program ends quickly.
When writing missile guidance software, their approach to freeing memory is "just wait until the ram is destroyed".
Personally, I would have used alloca() to allocate an "out" buffer on the stack before passing it to intersect().
Your C code and Python code work differently if there can be duplicates in the arrays.
If there are `n` of the same in a, and `m>0` of the same in b, then your Python code will give `n` duplicates, while your C code will give `n × m` duplicates, which might mean you go into unallocated memory
This flaw in the C code could be very easily fixed by adding a `break;` after finding the first bi such that `a.arr[ai] == b.arr[bi]`. (i.e. after line 18)
fuck of brother, let us be stupid
Your comment just reminded me about this video: ua-cam.com/video/oTEiQx88B2U/v-deo.html 😂
Impressive. Very nice. Let's see a Rust developer's speedrun.
They are still trying to satisfy the borrow checker
The tasteful indentation of it. Oh my god, it even has a docstring.
a.into_iter().flat_map(|x| b.contains(x).then_some(x)).collect::()
Honestly, it's just like the python but with weirder more verbose syntax.
*Rust, Dust and Guts starts playing*
why do you have emojis?
The soundtrack for each language gives very accurate feeling about the skill level of each programmer. C programmers are superior of course
as someone who knows both, i am not superior to anyone i am extremely stupid.
@@svlmain u ok, bro?
@@ignacio1085 he's having an existential crisis
Sir I respect you opinion but sir why would I just not do it the easy way. Why would I painful write 50 lines if I can just write 5. All and all that's my opinion :)
@@adil080_ the same reason if I would want my code to run 42x faster with the 5 lines than 50. C++ and C are meant for performance, python might be easier, but its to be noted that it's also slow as hell
This is weird,, since most Python interpreters actually written in C
And remember, just because it's written in C, doesn't mean you programmed it to run faster.
Yes it does. Python is interpreted, c is compiled. $20 says the c implementation is an order of magnitude or more faster.
@@keris3920 I said "doesn't mean you" programmed it to run faster. C often allows for some pretty dangerous code and can easily become very inefficient even when solving the same problem. Of course with the best implementation it's going to run faster.
@@DaJodad Sure, but you can write some really bad code in Python as well. A simple matrix multiplication in Python using for loops is more than 20,000x slower than the C implementation, and both of those implementations are O(n^3).
My point is, the argument you're trying to make is not an argument for using Python. Writing the code in C, contrary to what you just said, is almost guaranteed to make your program much faster. As you mentioned, C can be dangerous because it's close to the hardware. This is the real reason someone should weigh when choosing the language. Sometimes python is inappropriate, and sometimes C is too cumbersome for the scope of your problem. But good C code will always be faster than good python code. Heck, even poor c code is often faster than good python code.
Careful with the safety argument too, by the way. C has a compiler, which means it can catch a ton of bugs at compile-time. Python is interpreted and relies on runtime tests, which does make Python more dangerous to deploy to customers. Again, there are tradeoffs in all languages.
The compile time of python is sprinkled into the runtime speed
@@linuxization4205 you can't accidentally deploy code that doesn't compile.
Really nice and helpful... Thanks!
remember to mark your return types const just in case
most accurate part was that the C programmer coded in VI
but the C programmer should have just ended on getting a segfault lmao
i code in mcedit
LOL!
As someone that programmed in both, I felt that 🙂.
You know this is not the most efficient way right? you're doing in O(N * N) when you could do it in O(N). You would just put one of the arrays into a hashset, then loop through the array, every iteration for collision into the hashset, because the hashset is constant time lookup, assuming you are passing in values that have hashes, you are able to do this in O(N) in any language.
So intersect function body should be replaced with [i for i in a if i in set(b)] for maximum efficiency?
@@Virbox Honestly, I don't know python but that's the general idea.
@@mrmaniac9905 🤓
@@zm5856 Are you really trying to call someone a nerd while browsing programming humour? You're in OUR territory, you fool!
@@buffcode new phone who dis?
The music at the beginning with the really high sounds is from The Escapists OST. It is called Lockdown
Alright that’s write speed now let’s talk about compile and execution speed 😳
slightly more than 0 seconds for both
@@ninstagram For 5000 elements in each array?
@@Hardcore_Remixer still 0 seconds
@@Hardcore_Remixer 0.1 sec
Now, the knowledge of who use C is so much higher than who code in Python
i code in binary so i'm the smartest man alive then
@@EaZea i disagree with my comment, youre right. what i said dont make sense, i gonna delete it.
The video has only 2k views, but its actually hilarious
As someone who’s just started learning C, I’m gonna assume I unlock the inbuilt doom soundtrack in when I’ve learnt how to use 1 if statement without using stackoverflow
The doom soundtrack unlocks when you write your first 1000+LOC program without UB in it.
@@KohuGaly Does Implementation-defined UB (such as signed overflow with -fwrapv) still count as UB?
In python isn't converting to a set better, since that eliminates duplicates (and not to mention, comes with an intersection method)
Also has better runtime complexity
def intersect(a, b): return set(a).intersection(set(b))
There's no need to free up the memory the C code allocated in this case since it will just be freed by the OS when the program terminates. It'd only be a memory leak if the program continued running without the memory ever being cleaned up, e.g. if you ran the intersect function multiple times and none of the allocations were freed.
@Artify cleaning up memory is a very slow unnecessary process
Unfortunately there are geniuses like you who do not free memory even in production code because "the OS will free it anyway". Libfontconfig comes to mind, where they allocate memory and store the pointer in global static variable, never freeing it. Now everyone need to add this allocation to whitelist of their Valgrind/ASan/whatever.
@@GeorgeFosberry freeing up memory can be pointless, if you don't plan on running a program for too long/for a one time task, then it is pointless
@@blueghost3649 It seems that you didn't pay attention while reading my comment. Unless you wrote some one-off script and then threw in to garbage bin, chances are that your piece of code will be used as a part of a bigger system. A piece of code in isolation can't know if it is allowed to cut corners by leaking owned resources - and that means it should always properly release those resources.
@@blueghost3649 it might be pointless, but it's incredibly important for large corporate code. If the user who's using the abstracted code doesn't realize that there's a memory leak further up the chain and then runs multiple children using that memory leak or exists for the duration of the program (which could take a while), or isn't seen and closed by the system during the duration, then the problem is now no longer negligible
You know shits about to get real when he types v
After 7min already on reddit lol
Hahahah me too
In python you could just `c=set(a) & set(b)` which also removes duplicates
Not if multiple values are the same
@@protectedmethod9724 what are you talking about?
@@protectedmethod9724 you need to find intersected values, which means you need set of those values.
@@abyrvalg_ problem statement was intersection of two arrays not two sets. so the question is should repeated values be left in the result or removed? it's undefined
Then in C one should apply qsort on both arrays and just then check for intersection (duplicates are easy to ignore in sorted arrays).
That if you want the same time complexity.
use a hashmap. with the current impl it's O(n^2) since it contains nested loop. So unfortunately it would be slower than the python one when the input is large.
O(n*m)
also if "i in b" is O(n) then it will be the same
@@okkoheinio5139 By "n" I meant the total size of input. O(n^2) still stands here because big O denotes the worst case performance. Best case runtime happens when one of the inputs has size 1, which will take a linear (n-1) number of iterations to complete. The worst case happens when both of the input are of the same size, which would give a run time of 1/4(n^2) iterations. Thus the performance of this algorithm is O(n^2) after removing the coefficient from the worst case run time.
@@Yutaro-Yoshii isn't that what it means ot be O(n*m)?
@@okkoheinio5139 If you set n and m to be the size of each arrays the complexity would be O(n*m), and if you set n to be the sum of the two array sizes, the complexity would be O(n^2). The result depends on how you define the input size. I should have been clear that I meant the latter case.
@@Yutaro-Yoshii wouldn't your O(n^2) be in my n,m case O((n+m)^2)? since your n is the sum of my n and m
O((n+m)^2) = O(n*m)?
The C code is actually partially wrong...
For example, arr1[] = { 8,3,1,5 }; arr2[] = { 5,3,4,6,9,3 }; returns 3 3 5
The idea is, if the element 3 appears once in arr1[], and the element 3 appears twice in arr2[], once your FOR loop finds a correspondent for an element in arr1[], it can find many more printing it repeatedly.
So to fix this, once you find an element in intarr b, then leave the FOR loop searching for a correspondent, because you found one - so add a break; statement inside the IF statement.
That's just to show how easy it is to write bugs in C code
the python code has the exact same bug. also, they both use O(m*n) algo, which is not optimal
@@itellyouforfree7238 o que é esse algoritmo? Por favor eu preciso saber.
@@jhonatasbonfim4917 you can sort the lists in O(n log n), then compare them in order in O(n), leading to a total of O(n log n). For large n this is much better than O(n^2)
But if you have arr1[] = {5, 3, 4, 6, 9, 3} and arr2[] = {8, 3, 1, 5} the result is wrong too, also if we use a flag to break the for loop... We have to save also the indexes of the used values...
it's also easier to point out bugs unlike python
Cool. Let’s see the runtime performance on large arrays now
🤓
Isn't numpy written in C under the hood?
@@AR-yd2nd All of C-python (the most common python implementation) is written in C. The difference is that python is meant to be general purpose and easy to use, whereas numpy is optimized for processing large arrays of numbers. That comes with certain restrictions/responsibilities on behalf of the programmer.
For example, python integers have "arbitrary precision" to avoid overflowing. If a number becomes so large that it doesn't fit in the current datatype, python will automatically change the datatype to accomodate the new number. Numpy doesn't do that. Numpy arrays must contain one specific datatype (for example 64 bit integers). This means overflow is possible, but it also means numpy can implement certain optimizations by knowing the starting and ending datatype of all operations.
Python lists are also optimized for dynamic sizing. They are good at updating how much memory they require when you add or remove elements. I don't believe numpy does that. Since the point of numpy is parallel computing, you're expected to create large arrays with a known size and then perform computations with them. You're not supposed to add new data to existing arrays.
In fact, since numpy specializes in computations with large sets of data, it can actually be _slower_ than regular python if you're using very small arrays (or individual numpy numbers). It's important to know which tool is best for the job, and the best way to find that out is to test the execution time of different versions of the same code 🙂
@@AR-yd2ndis numpy also use multithreading?
@@farukyldrm8498 parts of NumPy are built on top of a standard API for linear algebra operations called BLAS and BLAS libraries make use of multiple threads to speed up some operations by default.
some are more equal than others
It worked! Tank you sir.
The moment I saw vi I knew shit was about to get down
Ok. But python do the same job (or even more time consuming) on the lowest software layer. Python just have dedicated library to do this job. In C you need to write code which do the same, but on the lowest layer. It depends on destination of your functionality. If you need low-cost time consuming (c) or just do this job regardless costs (Python).
aka use python for prototyping then (if you're running into performance issues anyway) remake it in C
Now make arrays large and include run time in comparison ;)
I'm a CS student and i still remembered that C is the second thing we learned after C++, don't understand shit and yet still wrote 100+ line of code for group project
What a worker
Generally curious what are the applications of finding the intersection of two unsorted arrays to begin with
In C++ it's super easy:
using namespace std;
vector a = { 8, 3, 1, 5 };
vector b = { 5, 3, 4, 6, 9 };
vector intersection;
ranges::sort( a );
ranges::sort( b );
ranges::set_intersection( a, b, back_inserter( intersection ) );
for( auto n : intersection )
cout
the hell is:
vector a = { 8, 3, 1, 5 };
vector b = { 5, 3, 4, 6, 9 };
i think you mrant
vector a = { 8, 3, 1, 5 };
vector b = { 5, 3, 4, 6, 9 };
Write python libraries in C, and then use Python for higher level tasks.
Or just use Lua with C
You don't gotta loop twice, you'd have O(n*m) roughly O(n^2) complexity. You could instead create a hashing function and make a hashtable and run through both arrays once, checking whether it appears in the hash table, showing that they are the same values (resolving collisions would also have to be incorporated). This would give you O(n + m) roughly O(n) complexity at the disadvantage of space complexity which would be O(n + m) roughly O(n) rather than O(1), but sacrificing space for speed is pretty worthwhile.
A hashtable, in C????😰😰😰
@@rutviksaptarshi745 easy way to make hashing functions in c using bit manipulation
@@freeshavaacadooo1095 I mean you're right - implementing a hash table is quite doable. But the amount of code you'd need to write to make it a) be efficient b) work with different types is cray cray
@@rutviksaptarshi745 unordered map
@@bunnycode3852 I'm pretty sure there's a bunch of header only representations for these things
Why don't just
def intersection(a, b):
return list(set(a) & set(b))
For those who dont understand why C was written faster than python:
Actually it was a bot (programmed by the language of the vid's moment) that coded those files, and how we all know that C is faster than python, the C code was done faster.
underrated vid
In fact in Python it's just return set(a) & set(b)
Have you tried writing both codes with the same speed?
You just nested two loops big man
openAI Python in 9 seconds:
def intersection(arr1, arr2):
# Find the intersection of the two lists
result = list(set(arr1) & set(arr2))
return result
arr1 = [3, 7, 1, 9, 2]
arr2 = [4, 1, 9, 5]
result = intersection(arr1, arr2)
print(result) # Output: [1, 9]
No wonder C is a popular language. Not only it makes you type faster, but it also plays doom music!
In ur first calloc call, u need to pass size of int right? Which is 2 or 4 bytes; I would have passed sizeof(int) instead ;why did u pass 1 then?
You are correct, it is a mistake - and a demonstration why no one should "speedrun" C :)
@@Virbox Just drill the correct way in by practicing. That should handle your unfreed calloc as well.
Me seeing the program in C: 👁️👄👁️
Now do it with the most GOAT’ed language of them all: C++
This c code is a c++ solution tho
@@DaMonster As somewhat of an expert in not knowing enough about coding to actually understand what you meant but is learning c++ and is having fun doing it, my answer is this: Ok🗿
@@DaMonster sure, but in c++ you can write it in a shorter and prettier way, not to mention it would be easier to optimize it to O(n+m) as c++ has a builtin hashmap.
u did not need to build a function, the collection set in python has a built in intersection method
C: type* pointer = NULL; python: WHAT?! POINTERS?!??!!
in python you can just use builtin method: set(a).intersection(set(b))
that’s what i was going to say. plus he never used arrays
I mostly just appreciated the music change when it went to C lol
No matter what you code in, you're cooler than the regular ole people. Every language has it's purpose.(Except Java! That doesn't even count! Sheesh!)
well technically java does have a purpose, and the purpose is JVM and boilerplate long enough to give neck pains to whoever's reading it
Minecraft
@@x-_-_-Drayz And FIRST FTC, which also is the only "embedded" use for Java.
“All languages are equal” some are just less equal than others! 😂
I miss myself that don't know anything about programming. I hate thinking syntaxes alot in mind everytime curiosity hits so exhausting.
I've only ever used python not sure why I clicked on this video
And this is why we have C++:
#include
#include
template std::vector intersect( std::vector &a,
std::vector &b) {
std::unordered_set comp_set(a.begin(), a.end());
std::vector result;
result.reserve(min(a.size(), b.size()));
for( auto&val:b ) {
if( comp_set.count(val) ) {
result.push_back(val);
}
}
return result;
}
Look how easy this is, and it's about as fast as you can get without implementing your own hashing function (afaik).
“typename T”? In Java it’s just class {}
@@pepehimovic3135 Yeah, but that's Java. In C++, you use typename T is for actual types, whereas you would specify class T (so template T intersect(T&a, T&b); for a class (so not a primitive type).
You do not have this distinction in Java because Java boxes everything (so everything is an object, even primitive types), adding unnecessary overhead, whereas C++ can work with primitive types without boxing.
Which tends to be pretty good for performance.
Im just learning python and this makes me want to cry.
I have a degree in computer science and I work with C/C++ almost daily for my job and python still makes me want to cry. I just cannot for the life of me get comfortable with that language. It’s not for everyone!
@@mjl3631 this is fair. It just feels like theres so much to learn about programming i dont know how i can possibly know enough.
@@beanbag1415 There is a lot to learn, and no single human will ever learn everything there is to know about programming.
Not sure if you’re looking for advice, but I understand how frustrating is to start out. Focus on one problem at a time, and even break it down into smaller problems if you can. And don’t think for a moment that if you’re doing something wrong, or make a mistake, that you’re failing. Every single programmer, from beginners to the highest level researchers, etc, all make mistakes way more often than you would think. It’s a weird mindset to adapt to, but making mistakes is necessary.
There’s no way around the frustration of syntax errors though unfortunately. It gets easier the more you learn and practice.
@@mjl3631 thank you for this!
pretty sure it could be faster in nim, convert each to a set, intersect them, and then echo that
intersect = lambda a, b: set(a).intersection(b)
If you don't need repeats
i feel like the python code could just be return (set(a).intersection(set(b)) or whatever so its like faster than checking the second array every time we add a number
hashsets and whatnot
Im not a programmer im just learning python but I can say that I appriciate C. Python feels like you're driving a nice car and C feels like you understand how the car was built and can adjust every bit of it for whatever the road calls for.
def inter(a,b):
return set(a) & set(b)
# Does not remove duplicates (you could create a remove duplicate function)
def interNaive(a,b):
result = []
for i in a:
for j in b:
if (i == j):
result.append(i)
return result
a = [ 8, 3, 1, 5]
b = [ 5, 3, 4, 6, 9]
print(f"The intersection is: {inter(a,b)}")
print(f"The intersection is: {interNaive(a,b)}")
Nim compiles to C(or llvm/c++/js/node) and honestly is as readable as anything.
*```*
*var*
*a = @[8, 3, 1, 5]*
*b = @[5 ,3, 4, 6, 9]*
*func intersects(a, b: seq): seq =*
*for i in a:*
*if i in b: result.add i*
*# Equivalent calls*
*echo a.intersects b*
*echo intersects(a, b)*
*```*
Thanks for the suggestion, Nim seems like a really nice language. But how it compares to Zig (besides syntax)?
To me Zig feels like someone got really fed up writing C in a team. And is more barebones but with very pure C integration. They put "Simple language, no macros" on their website as a feature.
Nim feels like someone got really fed up with crappy abstractions and decided to make a language so expressive; That in team settings you better encourage everyone to use the least expressive features that suit the job so you do not end up in hell. Macros are wild, for good, and bad.
that looks horrible wtf are u talking about lol
@@okie9025 Oh, what makes you think so?
I'm not sure anything simpler is possible without losing information tbf😅
But I get that some of it is taste.
@@HXTz0 simpler does not mean pretty.
Maybe it could be a good idea to compare a running time also for larger arrays (with approx. 10000 numbers). In my experience, python is perfect for his ability to fast development; But the C language is perfect for fast execution (usually it is running time of C approx. 8 times shorter than python ) with solid development time. Lets imagine, that you need to covert video (some movie) and this conversion takes 10 hours by C code.. Are you really able to wait 80-100 hours to conversion of video with python code ? It is extremely wasting of time and electric energy.
You should try it in assembly x86
No need for List Comprehensions, there is a more python oriented way:
def intersect(a,b):
return a.intersection(b)
Using pointer arithmetics is far more faster both in typing and running in c
coding this in rust takes me 30 seconds max, and the performance is 500nanoseconds, which is 1/2000 of a millisecond, so i think its pretty fast than python, and easy to code as well.
Wow I never heard that you type 2x faster in C
this video made my day :)
The length of the array containing the intersection can't be bigger than the smallest array.
For anyone wondering, the timer for the C clip was in days, not seconds
Actually it's not the speed of the C program but the fact that it makes time flow faster
All languages are equal. Equally bad just in different cases
kids: using python
doom guy: typical C enjoyer
When you realize you could have just written "return list(set(a) & set(b))" instead of that pointless list comprehension in python to do the exact same thing.
Assembly low-level developers:
pop myself
it's still not that bad in assembly, since it's just comparing two arrays, you just need to store a null-terminator at the end of the two arrays stored in .data, then using pointe- oh wait, now i see the issue
And me 《 print("3 5") 》 😎😎😎😎😎
def intersect(a, b):
return set(a) & set(b)
Just like Jython. Jython is a Python Programming Language Compiled in Java. It's Also a Python Programming Language Family
I just wanna know where is the soundtrack at the beginning from. The one that sounds like its from a horror movie or game
If i understand correctly while it's dirty c version can be written like this, much shorter, well it's not clean and surely not the shortest but i perfer this version
#include
int main(void){
int a[4]={8,3,1,5};
int b[5]={5,3,4,6,9};
for(int i=0;i
Yeah it does the job but it doesn't create a resulting array
@@Virbox welp task was finding it and didnt say it to store in any form so it counts IMO
Very nice... now lets see their time to result
The C programmer made python
My first class in coding was in C++ which I failed to learn. Then my second language that I’m still learning was R. The memories from C++ still hound me. I’m the only one in the class that uses for statements in R and python and the only that defines functions
This video deserves 2m views instead of 2k views
Instructions unclear, accidently created an operating system
He must have used keyboard with RGB lights for C, there's no other explanation
Oh no this old static strongly typed and compiled programming language requiring more code to write than a dynamic and interpreted programming language! This is madness!
Your comments Is so useless...
Python solution looks like O(N^2),
O(N) solution:
def intersection(a, b):
return list(set(a) & set(b))
Pulling up vim to write C code is the equivalent of showing your abs to the ladies