But there should be many other SIMD impelementations out there for C++, they are probably just not even using introsort though, which makes a big difference. Even just using introsort to match and turning on compiler flags (including flags so it optimizes for their specific architecture (making the executable not portable)) would probably be decently close if not the same depending on how modern their hardware is / if the numpy library is applicable in its work or not.
#algos-and-data-structs
1 messages · Page 73 of 1
Since the Numpy one was about 2.8x faster for them. It could be lack of SIMD, which could be from just lack of compiler flags. Or just from not being introsort. Since it's only about a 2.8x difference Numpy's large amount of work may not be fully utilized and they may need to download a different version or compile from source to get its true max performance (it feels like it could be faster).
The big difference would be if the OP has AVX512 or not.
Since they put a lot of effort into that.
But their Numpy version may not really use that, it may be a more portable, conservative build.
But first step is finding out if they are even using introsort, otherwise it's not a good comparison at all.
(Numpy also has other sort options)
Maybe, yeah, depends on the data used to test. Also are the C++ sorts being used stable or not.
afaict it's just random floats
which should be a perfectly fine case for quicksort
the difference is actually even larger on my desktop
For intro vs quick?
numpy vs C++ stdlib
What flags did you compile with?
clang++ -O3 -march=native -std=c++23 sort.cpp -o sort
I see 30x faster, which feels kinda suspicious
the stdlib is an introsort
Maybe, not required.
LLVM seems to https://reviews.llvm.org/D113413
Only fixed in 2021...
Maybe we can throw it into Godbolt and look at the assembly.
So if it's giving nonsense.
the C++ code also does a copy, but the numpy code also returns a copy, so that should be mostly fair
so libc++ was not standard compliant before then? 
It was changed: https://cplusplus.github.io/LWG/issue713
And yes, I think LLVM was behind on it then.
idk if the last edited means it went in after that
Yeah, but at least 2016 earliest right?
2007-2016 is a pretty big gap
could be some edit not related to the standardization, idk
Maybe inline this sorting, so there is no accidental copying of the whole array happening with allocations.
accidental copy where?
std vector assignment or return from function.
Return is maybe a whole copy, if the compiler feels like it.
the assignment is meant to be a copy
and the return should be guaranteed to optimize away I think
Also looking at the assembly it calls: call void std::__introsort_loop<__gnu_cxx::__normal_iterator<float*, std::vector<float, std::allocator<float>>>, long, __gnu_cxx::__ops::_Iter_less_iter>(__gnu_cxx::__normal_iterator<float*, std::vector<float, std::allocator<float>>>, __gnu_cxx::__normal_iterator<float*, std::vector<float, std::allocator<float>>>, long, __gnu_cxx::__ops::_Iter_less_iter)
So it's intro on the godbolt's server at least.
It may be multithreaded.
Looking at some Github issues they added OpenMP support and maybe multithreading to qsort?
I looked at this for a while too, and I don't believe it's enabled by default.
Not seeing how there is a 30x difference though, seems too much.
Even with scalar floats vs AVX512.
is has at least been in there since the C++ draft repo was started, which was in 2011
so C++11 is probably a reasonable guess
too late for 03
Well I'm glad it's tripping up more than just me. It's not deeply surprising that numpy is able to come up with a faster sort since it can optimize for specific types where as std::sort is more generic. But I never expected it to be whole number multiples faster.
Would this be the right area to talk about caching systems?
When there is such a large gap as 30x though I assume it's because I have done something really wrong.
Your gap of 2.8x is much more understandable.
Doing something very clever is what gives those like 1.2x gains. Doing something really wrong is what gives these massive gaps.
My guess is that first of all, std sort is generic and needs to work with iterators and such. Numpy picks the float specific impl.
Tbf, I only get the 2.8x when using the best algos from the boost library. Std::sort is like 5-8x slower. I can't remember my benchmarks ATM and I'm away from my desktop
I also guess that std sort is not using median of three, and maybe using heapsort fallback too aggressively.
Branch misprediction?
@haughty mountain Can you try sort with plain C arrays instead of vector?
How is this related to python? Sorry, I'm catching the conversation half way
the topical channels aren't really python specific
We are trying to understand numpy's performance.
oh they might not be discussing python here?
Would this be the appropriate place to discuss a cache system that doesnt use traditional LRU, etc?
in what sense? passing pointers to std::sort rather than iterators?
that's true, if you're building the C in the python...
Yeah, it may be that vector adds just enough abstraction layers that the optimizer can't have enough room to do what it needs to.
I think that's too deep for me. I stay at pythonist level.
I really doubt it
IDK, it's a guess, I have run into this many times.
Although usually more layers needed.
so this?
std::vector<float> sort_floats(const std::vector<float>& float_array) {
std::vector<float> sorted_array = float_array;
float* data = sorted_array.data();
size_t len = sorted_array.size();
std::sort(data, data + len);
return sorted_array;
}
Yes.
So probably just details like maybe not using median of three, too aggressive on the heapsort, plus SIMD.
Not setting up branch prediction to be better maybe too.
It's hard to tell since it can be different on different platforms here, since it's stdlib.
And those have different authors and different levels of effort put into them.
the thing I'm running will be using the gcc stdlib
I think you need to explicitly opt in to using the clang one
Isn’t it PGO?
Python has PGO when it’s compiled, not sure if it numpy does too but that wouldn’t be surprising
And the c++ code you guys compile for the comparison doesn’t have PGO
PGO is usually for like the last 2%-15%, not like 2-5x and not 30x either.
This feels like something much more fundamental is wrong / the C++ std sort is not making use of the hardware or the algorithm is misconfigured (not tuned or other details wrong), or maybe it has safety assertions enabled or something.
I think the approach now would be to implement one ourselves (introsort) in C++ (almost plain C to keep it simple) and see how that works out in comparison to std sort and numpy's sort.
That way it's more simple code and we can see the full disassembly and if there is anything weird going on, and mess around with the setup.
If we can get closer to numpy then we know something is wrong with the std sort.
Maybe it's too generic or something.
We also need to test on the same input data just to make sure it's actually getting it right in our own implementations.
Made an implementation. Most simple C code version is the same as std sort. Some basic SIMD attempt improved it by 33%, still about 8x slower than Numpy's.
Not sure if Numpy is multithreading or not, could still just be off by a lot single threaded, not much effort put into it so far.
But it does tell me that std sort is just the most basic direct version.
Bad cache utilization and lack of SIMD could make up that performance gap.
Numpy is probably doing things in blocks for better cache usage and then SIMD on those blocks.
At least that would be the 8x difference for me. But 30x still feels like a lot, but it could be that on your hardware it's just so much worse to not make proper use of the cache and SIMD.
After some more effort I have a SIMD version that is about half as fast as the Numpy version (started at 10x Numpy's now 2x).
It's still pretty readable and straight forward at this point.
you could also test
https://github.com/numpy/x86-simd-sort directly
Anyone recently started learning DSA? I am also started to learn this, having problem to understand a lot of thing, if you are interested then we can discuss and teach each other.
it's gonna be a bit hard to find learning partners here
though if you have specific questions you can prob get answers
Yeah, i was trying to understand how in Linked List, dynamic inserting and deleting works? When I just focus on add Head dynamically that seems easy or deleting, but doing both at the same time confuses me
are you stuck on the implementation or the general idea?
Well i know the thoery but when I am doing practical, like practicing how it works, I got stuck, seems like my mind stops working
well, all I can really say is try thinking in small steps ig?
for example, deleting a node is when you start with something like this
A ---> B ---> C
```and want to end up with this
A -┐ B ┌-> C
└-------┘
so think about what you need to change to get from the first to the second
Well, its all about linking one node with another but my thought gets unlinked, to be honest
try writing your thoughts down on like a sheet of paper or something
or again try breaking down exactly what steps you need to take
for example, how do you get from 1 to 2? well the link from A to B and from B to C are gone, and there's also a new link from A to C
step 1: remove link B -> C
step 2: remove link A -> B
step 3: add link A -> C
```that may not exactly work out nicely but that's a start
This is simply insertion on head. I understand but, it gets too complicated when I have insert, delete dynamically
have you tried e.g. drawing out what happens on each line?
this could help you
(and also, when you simply have code, prefer pasting the text rather than a screenshot)
How does the drawing work?
grab a pen and a sheet of paper and draw out what happens at each step
there's no standard for drawing, just do it in a way you understand yourself
Well I have tried that like i draw blocks of node and how they connect each other then after 4-5 steps my mind stopped working, that is the main problem. It was the cause I gave up 1 month ago on DSA. I just can't concentrate on the chain, i got confused after a certain step
given a sorted array of non decreasing integers
and 2 values, lower and upper bounds
give me code that finds the starting and ending indexes of values in that sorted array that has values within range lower bound <= x <= upper bound
do it using modified binary search, in 1 pass
is this possible?
i have no idea how to do this in 1 pass
you could binary search to try to find some point in the range, and then fork the binary search in two to search the remaining parts on either side
i.e. first binary search with f(l) < lower and f(r) > upper, once a midpoint mid is found for which lower <= f(mid) <= upper split and search l to mid and mid to r for the start/end point
3
Proving tricky to google, and hoping this channel fits, but anyone know of a better way of matching 'any sequence' with a match/case statement than case [*items]:? case [] matches empty sequences only, sadly (as opposed to {} matching any mapping, curious)
case Sequence() matches str too, that's unfortunate 😅
huh, why doesn't items match strings
Oh... it's just explicitly banned https://docs.python.org/3/reference/compound_stmts.html#sequence-patterns
If the subject value is an instance of str, bytes or bytearray the sequence pattern fails.
str is a sequence though
This? ```py
case [*_] as items:
seems like it doesn't
Oh if it won't actually iterate over the items, that would be good, that's what I was wondering / afraid of
Was using list() | tuple() in the meantime
But that would bypass anything that would be a valid sequence but just not that
And not matching str or bytes was ideal already 😅
Looking at the byte code, @austere sparrow is totally right, both case [] and case [*_] result in MATCH_SEQUENCE, the former adding an additional check on the length of the sequence
So no actual iteration of the thing being matched 👍🏻
Hi, I am VERY confused about data structures and algorithms. and I learn best by reading so I would appreciate if you guys could recommend a book. also I have been told that you should be decent at math which I am not. but still, willing to learn it.
clrs
i don't know about "good", but there are topics that you should be familiar with. i believe the introduction of that book talks about some, and there are review chapters
good. thanks
Discrete mathematics.
i see. did some research. thank you. that was a great recommendation
I have my TCS CodeVita exam tomorrow, and I’m looking for someone experienced in Python and competitive programming to help me revise or discuss a few problems.
Your guidance would mean a lot — and if I qualify, I owe you a treat! 😊
I dont know what to say but can some of you be interested in checking out my sudoku solver.
Hey, probably silly question that youd usually ask chat GPT, but i just seek human connection 😭
What does error : TypeError: 'int' object is not subscriptable means
In context of this function that i wrote:
try: a = mat1[i]
except IndexError: a = 0
try: b = mat2[i]
except IndexError: b = 0
return a + b
!e
item = 2
a,b = item[1]
:x: Your 3.14 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File [35m"/home/main.py"[0m, line [35m2[0m, in [35m<module>[0m
003 | a,b = [31mitem[0m[1;31m[1][0m
004 | [31m~~~~[0m[1;31m^^^[0m
005 | [1;35mTypeError[0m: [35m'int' object is not subscriptable[0m
@violet obsidian
Ask this kind of stuff in https://discord.com/channels/267624335836053506/1035199133436354600
I dont know what to say but can some of you be interested in checking out my sudoku solver.
Im struggling learning data structures and algos so much, I really dont know what to do anymore. Maybe im entering this when i dont have the mind to yet, I just dont see how to solve specific problems efficiently.
I am doing this leetcode problem https://leetcode.com/problems/minimum-operations-to-make-array-equal-to-target/
and I can't understand why the first code passes in all the test cases, but the second one doesn't
class Solution:
def minimumOperations(self, nums: list[int], target: list[int]) -> int:
diffs = chain((a - b for a, b in zip(target, nums)), [0])
res = prev = 0
for diff in diffs:
if diff > prev:
res += abs(diff - prev)
prev = diff
return res
class Solution:
def minimumOperations(self, nums: list[int], target: list[int]) -> int:
diffs = (a - b for a, b in zip(target, nums))
res = prev = 0
for diff in diffs:
if diff > prev:
res += abs(diff - prev)
prev = diff
return res + abs(prev)
I thought they should behave in the same way
But they are not. Why?
I can't find the reason/problem here
simple case where the output differs
nums = [1]
target = [2]
So, the only way to make the second code work is to make it iterate once more just like in the first code?
no
the logic afterwards is just different from the extra loop, which is why they differ
class Solution:
def minimumOperations(self, nums: list[int], target: list[int]) -> int:
diffs = (a - b for a, b in zip(target, nums))
res = prev = 0
for diff in diffs:
if diff > prev:
res += abs(diff - prev)
prev = diff
if 0 > prev:
res += abs(0 - prev)
return res
```this would be equivalent logic
How did you guys become good at implementing data structures and algorithms because I’m useless at it if I don’t have google or any resources, I can confidently say if I was given a problem with no resources I wouldn’t even know where to start, I’m cooked
Just do hundreds of problems
Me, too.
I'm trying to solve a problem on Hackerrank for an upcoming assessment. Not sure why this code fails three of the hidden tests.
from heapq import heapify, heappop, heappush
from collections import Counter
from math import inf
def calculateMinimumTimeUnits(tasks, m, k):
counter = Counter(tasks)
maxHeap = [(-count, task_type) for task_type, count in counter.items()]
heapify(maxHeap)
idle_info = [{} for _ in range(m)]
time = 0
while maxHeap:
time += 1
scheduled = False
for machine_id in range(m):
if not maxHeap:
break
found = False
temp = []
while maxHeap and not found:
neg_count, task_type = heappop(maxHeap)
if task_type not in idle_info[machine_id] or \
idle_info[machine_id][task_type] <= time:
scheduled = True
found = True
if neg_count != -1:
heappush(maxHeap, (neg_count + 1, task_type))
idle_info[machine_id][task_type] = time + k + 1
else:
temp.append((neg_count, task_type))
for item in temp:
heappush(maxHeap, item)
if not scheduled and maxHeap:
min_time = inf
for _, task_type in maxHeap:
for machine_id in range(m):
if task_type in idle_info[machine_id] \
and idle_info[machine_id][task_type] >= time:
candidate_time = idle_info[machine_id][task_type]
min_time = min(min_time, candidate_time)
if min_time == inf:
min_time = time + 1
time = min_time - 1
return time
Hi, pls share problem link
Yeah, sorry forgot to include it. Here is the link: https://www.hackerrank.com/contests/software-engineer-prep-kit/challenges/task-scheduler-cooldown-multiple-machines/problem?isFullScreen=true
Hi guys , just created a algo to trade using python,so it connects using a library called meta trader 5. Now I don't want to keep my laptop running the whole day for 5 days a week. What are my options? Can I have something that can be used the whole day without getting damaged? Let me know ?
You could run it on google cloud or aws.
how i could read numbers in files
Is there anyone looking for a dev
!e
import sys
print(sys.float_info.max_exp)
print(1e+500)
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | 1024
002 | inf
Does anyone know why although the max exponent is said to be 1024, 1e+500 goes to infinity?
Ah nevermind, it's 2**1024, not 10**1024
It's in the power of 2 not 10
how to deploy ml model with proper frontend and backend(using PyCharm & html,css) free of cost
anyone help?
The first code is slower (51 ms) than the second code (19 ms) in leetcode
What makes the second code faster?
class Solution:
def matrixSumQueries(self, n: int, queries: List[List[int]]) -> int:
empty_rows = empty_columns = n
used_rows_and_cols = set()
res = 0
for axis, idx, val in reversed(queries):
if empty_rows == empty_columns == 0:
break
key = idx + 1
if axis == 1:
key = -key
if key in used_rows_and_cols:
continue
used_rows_and_cols.add(key)
if axis == 0:
res += val * empty_columns
empty_rows -= 1
elif axis == 1:
res += val * empty_rows
empty_columns -= 1
return res
class Solution:
def matrixSumQueries(self, n: int, queries: List[List[int]]) -> int:
total_sum = 0
remaining_row = remaining_col = n
row_is_free = [True] * n
col_is_free = [True] * n
for type, index, val in reversed(queries):
if remaining_row == 0 and remaining_col == 0:
break
if type == 0 and row_is_free[index]:
total_sum += val * remaining_col
remaining_row -= 1
row_is_free[index] = False
elif type == 1 and col_is_free[index]:
total_sum += val * remaining_row
remaining_col -= 1
col_is_free[index] = False
return total_sum
Sets are slightly slower than arrays
Without getting technical, that’s just inherent to how a hash set works under the hood
Which Algorithm is the best to use for large data sets and time complexity?
In sorting and searching algos?
Did you measure it yourself or did you just use leetcode's data?
Leetcode's performance measurement is really bad
do not rely on it to tell which implementation is faster, do a proper benchmark if you're interested
I've created a crypto monitor thats run for days on end. Just dedicate an old computer. Make sure it's set to not auto update/reboot/restart/go to sleep etc. Maybe put it on a UPS too. A little flicker is probable when running something dedicated for days.
practically? then the one implemented by your library of choice (numpy / pandas / polars) over basically anything you can write in python
I'm tackling the cesar cypher, but I could choose between 2 algorithms
One where each character of the cypher is checked against an alphabetic string and changed accordingly.
(So for cypher ZXCV it will check whether Z matches with any character of the alphabetic string, and then make the appropriate changes. Then X, etc)
And other where I would previously convert all of the cypher to an "array" of integers (so for ZXCV, it would be 26,24,3,22) and sum up the key, to then reconvert it to letters and output the result.
It is my understanding that the second is a faster and better algo for larger texts, as instead of making a maximum of 26 checks and one change, it would need just to make a single addition, per cypher character.
Is that intuition correct? Any other comments you guys might like to make about the better algo for this case?
The Caesar cypher has been known since ancient times. There is only one algorithm. However, there may be many implemetations. Possibly, the most efficient is to use a dictionary.
# Caesar cipher
from random import randrange
from string import ascii_letters, digits, punctuation
alpha = ascii_letters + digits + punctuation + ' '
def encode(plain, shift):
table = dict(zip(alpha, alpha[shift:] + alpha[:shift]))
return ''.join(table[ch] for ch in plain)
def decode(cipher, shift):
table = dict(zip(alpha[shift:] + alpha[:shift], alpha))
return ''.join(table[ch] for ch in cipher)
plain = ('This is my secret message.')
shift = randrange(1, len(alpha))
cipher = encode(plain, shift)
print(f'{shift = }')
print(f'{cipher = }')
decrypted = decode(cipher, shift)
print(f'{decrypted = }')
What do you mean by "there is only one algorithm and may have many implementations"?
More of a conceptual question - I mean, isn't what I listed 2 different algorithms for the solution?
You should say "two implementation of the algorithm" rather than "two algorithms". There is only ONE Ceasar cipher, therefore, only one algorithm.
So if I have two diff ways of solving a same problem, one more.efficient and one less efficient, I rather have two implementations than two algorithms? That seems counterintuitive to how algos are classically defined. But I'll accept it.
Say, cooking beans
We can pour a cup of them into the water for cooking. Or pick grain by grain until all grains in a cup are used. That's 2 implementations and not two algorithms right?
Not quite python, but I sometimes enjoy conceptual questions
Objective: Put beans in water.
Method 1: Pour whole lot in one go.
Method 2: Put them in one by one.
Two different techniques that achieve the same.
There is only one Caesar cipher and that predates any digital computer. The (note singular) algorithm was well defined in ancient times. There may be many implementations.
I think I got it... Though it is still hazy. They're the general, larger framework, and implementations are the details. I guess I'm just searching for an exact understanding.
So while the orange is the algorithm, the two pink imp1 and imp2 are the same opened box of "move 3 houses to the left", right?
Algorithms ate the conceptual framework, and implementations the how-to's. Is that correct?
I know my flow charts are wrong somewhere btw. I just want to exemplify.
Because what confuses initially is that all three could be described as finite set of instructions with input and output. But I guess since the output and input are the same, they're all a single algorithm.
I think doing the calculations is more efficient. Using a dict = unnecessary memory access. I could be wrong though.
does learning algorithms & data structures help with algorithimic thinking/problem solving skills?
algorithms is literally problem solving so yes
@wintry nymph your message was removed for advertising / soliciting
Learning and practicing, yes. Absolutely. You’re training your brain to get used to interpreting problems differently.
Incidentally, the same applies to data science. Not just algorithms / data structures. You can "ritualize" the portion of data science that isn't domain knowledge. For instance, get practice setting neural networks on PyTorch in a large variety of contexts, or the same for classification problems.
Those are two different algorithms. The first is O(1) and the second is O(N).
how do i fix this website https://h1.nu/1eCOK
Hey guys, I'm new to grinding leet codes. Will solving easy problems help me improve? What's a good number of problems to solve daily?
Variety, not grinding, is how you get good at programming. You want to be good at DSA? Then, take the time to read about individual topics and algorithms. Watch the MIT OCW courses, etc. Use different resources. The goal isn't solving problems, but building more experience problem solving & thinking. I think you learn more from one problem that takes you 2-3 days to figure out, than solving 3 problems in an hour.
While not DSA specific, exercism and codewars are also good practice sites. If you want DSA specific, look up USACO and https://cses.fi/problemset. The MIT OCW course: https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-spring-2020/
Tnx for the input I'll look into all of these ty 🙏
This has to be tuff question
Heya, I’m working on adding a new feature to a learning project i am working on about automated data extraction from mobile android games and game update tracking (tracking already implemented) and those all gets sent in discord by the bot and I could use some advice from more experienced programmers.
My bot already checks for game updates automatically through the App Store and Play Store APIs. What I want to do now is compare the previous config file with the new one that comes with each update. The file is located at:
Android/data/com.dirtybit.fire/files/Config/storeConfig.json.
The idea is that whenever a new version is detected, the bot grabs the latest storeConfig.json, compares it to the one I saved from the previous version, and then sends an embed to the Discord channel announcing the new update and highlighting any changes it finds (like i already have but instead of manually submitting the old and new config file, i want it automated). I’m trying to figure out the cleanest way to handle this comparison inside the bot’s structure, and how to make the workflow as reliable and efficient as possible. I already have the comparison/change detection implemented but as a command so you can submit old and new config file and it compares them and sends the changes it finds, but i want it to be automated but I am unsure how to get by this as I don’t even have an android device and my PC is too low end for any Emulator besides Android Studio.
If anyone has experience doing something similar or has suggestions on how to approach this, I’d really appreciate the help.
I’m looking for a partner on a project. If any online likes math and science then hmu
can the stream returned by socket.socket.makefile be used as the source buffer for struct.unpack_from
oh they're completely different things
Hi
So i have completed the OPPS for python and build some projects to clear the concept also I have completed 50+ easy question on LeetCode of Arrays and string but i want to learn DSA topics so that when a question of DFS, BFS, tree and more comes i can not skip them or try to slove them on my own instead of chatgpt so what is a good platform to learn them and what topic should i focus on
im writing my ma thesis on a comp linguistics subject (genetic change rates over time). would anyone be willing to look over a (short) script i recently wrote for it and give some feedback via dm?
Hi
Can anyone help me improve my code from O(n^2) to O(n) or O(nlogn):
public int selectCompounds2(int[] money) {
int n = money.length;
chosenCompounds = new ArrayList<Integer>();
compounds = new int[n][n];
dp = new int[n];
dp[0] = money[0];
compounds[0][0] = 1;
dp[1] = Math.max(money[0], money[1]);
if (dp[0] == dp[1] && money[0] >= money[1]) {
compounds[0][0] = 0; // copy col 0 into col 1
} else {
compounds[0][1] = 0;
compounds[1][1] = 1;
}
for (int i = 2; i < n; i++) {
dp[i] = Math.max(dp[i-1], dp[i-2] + money[i]);
if (dp[i] > dp[i-1]) {
for (int k = 0; k < dp.length; k++) // copy col i from col i-2 and add i
compounds[k][i] = compounds[k][i-2];
compounds[i][i] = 1;
}
else {
for (int k = 0; k < dp.length; k++) // copy col i-1 in col i
compounds[k][i] = compounds[k][i-1];
}
}
for (int i = 0; i < n; ++i) { // collect compounds with most houses
if (compounds[i][n-1] != 0)
chosenCompounds.add(i + 1);
}
return dp[n-1];
}
maybe ask in a java server instead?
https://paste.pythondiscord.com/YQ5A
i'm trying to use a mask on a video frame, but cv2 keeps throwing this error, and i cant seem to find a helpful answer.
maybe i'm not asking the right question? either way, i'm getting this:
Traceback (most recent call last):
File "/home/skye/Documents/video2noise/motionExtractor.py", line 39, in <module>
final_frame = cv2.add(zeros, Noise_Frame, zeros, grayscale)
cv2.error: OpenCV(4.12.0) /io/opencv/modules/core/src/arithm.cpp:687: error: (-5:Bad argument) When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified in function 'arithm_op'
around here:
success, frame2 = video_capture.read()
if success:
frame2 = cv2.resize(frame2, small_size)
over = cv2.subtract(frame2, frame1)
grayscale = cv2.cvtColor(over, cv2.COLOR_BGR2GRAY)
height, width = grayscale.shape
zeros = np.zeros([height, width])
Noise_Frame = np.random.normal(0,25,grayscale.shape).astype(np.uint8)
final_frame = cv2.add(zeros, Noise_Frame, zeros, grayscale) # <-- error here
Has anyone here made any algorithm trading bot?
all in, wait T time, sell, repeat
do you have any experience with lightweight chart of TradingView?
is someone here who could help me with something? i just try to run something but im no coder
trying to get this bot running but idk what to do
guys is it bad to solve leetcode problems using python? for ex i was doig neetcode 150, the yt freecodecamp one was solving it using java. idk java, so i went to og neetcode and he was solving the same problem with python. thing is ik python but my professors frown when they see someone doing dsa using python. so i started doing dsa in c/c++. now another problem arises
i was solving the leetcode 217 problem, contains duplicate and idk the code nor i could find solution for it in c/c++. i can understand the python one hence confused
🔹 Problems in your code:
zeros is float64 by default (because np.zeros without dtype).
Noise_Frame is uint8.
cv2.add expects both inputs to have the same type.
it took damn near a week to get that response. i've already long since fixed that.
simple fix
zeros = np.zeros([height, width], dtype=np.uint8)
Noise_Frame = np.random.normal(0,25,grayscale.shape).astype(np.uint8)
final_frame = cv2.add(zeros, Noise_Frame)
can someone help me with this question from my university's DSA exam, why does this adjacency matrix have 8 columns and 5 rows or is it black magic
since each column has two ones I want to say someone screwed up and gave you the incidence matrix instead
Yea unfortunately they didn't teach that in class and learnt it right now
I still don't understand the path matrix
seems it's just P_ij being 1 if there is a path from i to j
does anyone use lightweight_chart in python ?
Just study dsa books 📚
nice tip, what books exactly?
You can use this one for example: https://runestone.academy/ns/books/published/pythonds3/index.html
An interactive version of Problem Solving with Algorithms and Data Structures using Python.
is this sufficient?
Yes.
This one is good as it covers all topic
I have an exam tomorrow on algorithms and programming and I genuinely haven’t started. Anyone have any advice ? I fr haven’t even studied it I was slacking all semmm. Plsss help. Idk trees or maps
discord isn't the best for running you down an entire semester's worth of topics, esp. when we don't know the details of what your courses teach
if you have anything specific you don't understand, then you can ask and prob get an answer
Only use ai chatbots for this purpose
how can I have somthing like this in VS code ?
it's called the "minimap"
yes minimap
if you are in VS Code, and press Ctrl-Shift-P, and type "minimap" you get a command to toggle it
does it work on jupyter notebook ?
I haven't tried Jupyter in VS Code
What program is this
Tradingview
There you can trade stock, Forex... All kinds of instruments
And development of your own algorithm trading bot
In pine script
Serious advice— stay on top of things next time. There’s no way you can pass without cheating (don’t do that). Learn from the mistake and work harder next time
which algorithm do ppl use to match people names?
I'm looking at fuzzy matching but there's a lot of fuzzy matching algos
Brother what is objective of the book (black hat python)
Isn't levenshtein distance the standard?
it's one way yeah, but I'm not sure if calling it the standard is accurate
like imagine if someone is named Alice Maria Chen
and then someone else writes Chen, Alice Maria
python's difflib for example doesn't use edit distance but something called the Ratcliff-Obershelp algorithm
Guys i got a problem
I gotta look into it i am unaware about it
whats everyones favorite algorithm
Hello there, I've come here to ask about a project I have, it's a maze solver and It's supposed to take in a png and solve it, it's also supposed to be able to generate a maze itself but so far I just translated the png into a binary array and processed the image a bit so only the outline of the maze remains and it takes the ends of the two lines that remain and marks the middle point between them. I did this so I could mark the coordinates of where the maze end and begins. Now I want some help about how I should traverse and solve this maze and also what algorithm I should use for it
Click here to see this code in our pastebin.
I was thinking of using BFS but I'm here to learn 👍🏿
hi
HELP me please on pv
pv?
yes
private
I'm looking for a book that isn't quite a textbook in format that covers DSA - i haven't taken any DSA courses but have a decent math background from an engineering physics degree. Any recommendations?
So far i've found "Introduction to Algorithms" by Cormen et al.
What's a DSA book that "isn't quite a textbook in format"?
Save you trouble just learn BST, hash tables, stack, queues. You can solve a lot of hard problems with these
By the time youre fluent with these you will figure where to move on to
That’s what I’m trying to figure out.
"grokking algorithms" seemed good
hi
I made a graph to see the difference growth rate of BigO()
Thanks, will fix
Hello! Anyone also doing Leetcode or solving Programming Problems more generally?
we're doing #advent-of-code
Is it compulsory to learn time and space complexity with python?
No, but it comes in handy
But if you're learning dsa time/space complexity is a must, regardless of the language
True, knowing about Big O complexity will help you a lot!
whats the difference between algorthim compelxity and performance characteristic
performance characteristics I'd usually expect to include behaviour of the algo for all inputs on real machines, whereas complexity is only concerned with how performance/memory use changes with the inputs getting larger for sufficiently large inputs.
So lets take for example bubble sort we would say performance characteristic would be, an inner and outer loop etc etc that bubbles the index being sorted at the moment, with best case O(n), and worst case of O(n^2) with bestcase being an already sorted list and worst case a list that is reversely sorted. And complexity would be O(n^2) a stable and in place algorithm?
yup if you want to optimiize your ds and analyze what algo to apply on it
Is there an easy/reasonable/sensible way to essentially write my own itertools.combinations() function? I'm trying to figure out how to write a function that gives all combinations (of any length), no repeats without using itertools.
I'm thinking recursion may be my best option here
It's recursion, yeah.
Two parts per step:
xs[0], *comb(xs[1:], n-1) and
comb(xs[1:], n) IIRC
Thanks. I'll use that. Though recursion and duplicating all the entries seems ... inefficient.
Yea, track a start index ofc, but I don't think there's much merit to using explicit stacks, modern Python is reasonably fast in recursion.
the itertools docs have equivalent python implementations given as examples for all the functions in there + a few common "recipes"
I wasn't able to decipher those at a quick look
(I'm actually asking because I want to reimplement it in another language, but don't tell anyone. )
Hello. Not sure if I could ask this here but I'm trying to learn python on freeCodeCamp and I'm on the 'Build a RPG character' stage. I've gotten the first 5 checks right but I can't understand how to make a for loop.
- Is there an error message?
- What's line 11 checking?
theres no error message and its checking if stats (which is a list of 3 parameters strength intelligence and charsima) is an int
nvm i fixed it
Can you drop me the course link I need a dsa revision in python
I think that's freecodecamp
Yeah but i want the link so i can practice on that site
Yup me
Hi (i apologize for my bad English)
Bro i want the dsa course link of that site
Still thanks for this
The site isn't mainly for learning DSAs what you were shown earlier was one of the lab exercises in the python course and I thought you were interested
yes i know code camp is one for all they cover most programming concepts but i need to revise dsa
if you want all combinations of any length, you don't need the extra length param when recursing
since all combinations of any length is just every subset
in that case it's possibly even nicer to just do it iteratively
if this is for AoC day 10, you even get a bit of optimization doing it iteratively
@mint jewel ?...
With bubble sort Id also include that performs well for small lists. Time complexity would be O(n^2) worst case and average case, O(n) best case. In-place and stable is unrelated to performance.
Nice, how many problems did you solve? Do you think LeetCode a good way to practice and learn about DSA?
Around 220 and yeah I have now a hang of solving dsa, but if I take a gap from dsa solving, its feel like I am forgetting everything slowly dunno why
Don't worry you are doing great, ever tried Spaced Repetition?
Doing older questions every few days??
Yeah
Yup I tried but I can't say consistent due to hackathons, classes, projects etc
That's unfortunate... Anyway, wish you the best on your journey!
Thank you, wby what are you doing currently
Studying and learning about DSA, I'm practing LeetCode daily
Oo good, but what field are you in webdev, ai ml or any other
I'm into BackEnd and Infrastructure, that's what I want to work with.
Tho not gonna lie, ML and Data Science looks awesome too! I've tried it a bit, but I found it too hard to reason about
Just loop over a start index, I suppose? That makes sense. I'll give that a try. Thanks!
you'd loop over the elements and keep a dict/set of combinations as you go
so for every element you either don't add it, which is just every prior combination, or add it, which is every prior combination with that element
so you keep combining the dict/set with a copy of itself with that each element added on
!e
combos = {()}
seq = [1, 2, 3]
for elem in seq:
combos |= {(*c, elem) for c in combos}
print(combos)
:white_check_mark: Your 3.14 eval job has completed with return code 0.
{(1, 3), (1, 2), (2,), (1, 2, 3), (2, 3), (1,), (), (3,)}
for the purposes of AoC day 10, you don't need to store each entire combination, just each of the intermediate results
Yeah. I got that to work. It's a lot cleaner ... though it didn't impact the overall runtime much. Thanks!
Well yeah ml is a diverse field if you don't have a specilisation in it you better not touch it
I can't figure out how to do this last step
ive been stuck forever
lines 7 - 25 you can ignore for the last part I'm just trying to figure out how to build that dot graphic
The return line returns whatever follows it in the same statement. Values on the following line aren't part of the return.
def add(a, b):
return a + b
"This is a string that does nothing."
6
8 * 9 - 21
"More meaningless values that Python never reaches."
so how should i get it to return the dots after the def passes all checks
oh i see what u mean
now its saying this though how can i use the function so that i can define the stats
thats the part that im really confused about
You want a single string with newlines in it. Not multiple strings
What line is that error from? What's on that line?
Copy paste the terminal output as text in a codeblock. Images suck for text
BASICALLY I did all the code for the checks under
def create_character(name,strength,intelligence,charisma)
then after I completed all the checks the next step is to create the little graphic that is shown in the example on the left on the original pic I sent where for every point in a stat (strength,intelligence, or charisma) there is one full dot and the rest are empty (e.g. 2 points in intelligence = 2 full dots - 8 empty dots
to do this i came up with a different function
def createdot(stat):
stats=[strength,intelligence,charisma]
for stat in stats:
return stat * full_dot + empty_dot * (10 - stat)
however im not sure if it works
im now trying to call this function at the end of my create_character function with
return f'{name}\n' f'STR {createdot(strength)}\n'f'INT {createdot(intelligence)}\n'f'CHA {createdot(charisma)}\n'
but im not sure if this works either
so thats my issue in text i started learning python two days ago and hopefully this little freeCodeCamp project helps me understand some stuff but i keep getting stuck and confused
Codeblocks please 😄
File "main.py", line 4, in createdot
NameError: name 'strength' is not defined
Line 4 is trying to access a variable strength that is not defined in that function
return f'{name}\n' f'STR {createdot(strength)}\n'f'INT {createdot(intelligence)}\n'f'CHA {createdot(charisma)}\n'
You can drop all the end-quote-start-quotes in there.
return f'{name}\nSTR {createdot(strength)}\nINT {createdot(intelligence)}\nCHA {createdot(charisma)}\n'
So I wrote this snippet to sort a list of contours (OpenCV) of items placed in a grid-like fashion into a grid. The grid can be of varying dimensions. I noticed that this snippet fails when two lines are placed really close to each other. Are there any better ways to obtain the required result?
Code:
(Boxes is a list of the bounding rects of said contours)
RowCoords = [Boxes[0]]
YThresh = Boxes[0][3]*0.4
AvgY = (Boxes[0][1]+Boxes[0][3])/2
for box in Boxes[1:]:
if abs(((box[1]+box[3])/2)-AvgY) < YThresh:
RowCoords.append(box)
AvgY = sum(((box[1]+box[3])/2) for box in RowCoords)/len(RowCoords)
else:
RowCoords.sort(key=lambda box: box[0])
GridCoords.append(RowCoords)
print(f"Row {len(GridCoords)} has {len(RowCoords)} items!")
RowCoords = [box]
YThresh = box[3]*0.4
AvgY = (box[1]+box[3])/2
if (RowCoords):
RowCoords.sort(key=lambda box: box[0])
GridCoords.append(RowCoords)
print(f"Row {len(GridCoords)} has {len(RowCoords)} items!")
I had opened a thread on this but I did not get any reply for 4 hours so I decided to try my luck here.
Hi everyone. I’m looking for some advice on how to structure my DSA learning path.
I’m currently comfortable with Recursion, Backtracking, and Linked Lists. I’ve spent the last month deep-diving into DP. However, I'm feeling a bit stuck on what to pick up next.
Could anyone suggest a structured roadmap or the next logical topic to focus on after DP?
Can someone plz help me with my post in #1035199133436354600 ??
I feel like it should be obvious but idk what I am having troubles with.
Do Leetcode!
I m doing it
Basically learn the theory and then practice it, if you want a RoadMap, here it is:
https://www.freecodecamp.org/news/search?query=Data Structures and Algorithms
Which one is the best way to learn a concept ?Reading from text (I mean from books) or watching videos
Do you like to read books or watch videos?
I love learning things from books
What criteria should I use to determine if I’ve achieved sufficient mastery of a specific topic before progressing?
So read the concept :D
If you can constantly solve medium problems on that topic
How was your exam?
Aisha, are you a bot?
Have you done this?
Hi @amber lion . I'm also new so let's learn together 😊🙏
For this problem,
Your create dot function makes sense but two things. One you are returning early inside the for loop so it only ever creates dots for a single stat . Secondly, your stat variable name in create_dot(stat), is the same as the one you are using here ( for "stat" in stats:...) So it's overiding the input from when you later call the function 🥲
Since we are learning, I'd just give you the hints.
-
You already have the stats list inside of your create_character function, you don't really need it again in the createdot function since you will be passing each stat to it anyway.
-
ALSO, since you are calling each function one by one inside the create character function, i.e "f" {create_dot(strength)....."
you don't need the loop in the create_dot function anymore. You could just pass each as an argument just like you did inside the create_dot and return that calculation of the dots 😊
Finally, when returning from the create_character function, your function will stop at " return" without returning anything. Try to make the first f be on the same line as the return and use /n for new lines. Though multiple "f" isn't advisable, one is enough but would still work if all were on the same line as return
I hope it helps 😊💪
Do you mean for instance (20,30,50,60)
You want, 60,50......?
i mean - i have 2d array
and from ALL values i want to get 2 or 3 values which are repeating most
Oh. From numpy or pandas?
whatever, i just do a small script to parse some csv…
like found this but this returns only 1 number: https://www.tutorialspoint.com/find-most-common-element-in-a-2d-list-in-python
<p>A 2D list has list as its element. In other words it is a list of lists. In this article we are required to find the element which is most common among all the lists inside a list.</p><h2>With max and count</h2><p>We design a follow with a in cond
and i want like 2 or 3…
or maybe even 4 if there are same-common ones
Give an instance 🥲
Say:
[[1,2,3,4,7], [3,5,8,6,4]]
So you want 3 and 4 or 3 and 3?
3 and 4
like. if there are 3 most commonest numbers then it gives 3 commonest, if there are 4 commonest then gives 4 commonest, if there are more commonest it gives 2 commonest. or something
Oh, I get you now
So you could try to joint the inner lists together to one and count them using the list.count() method
What have you tried so far though?
As it is, if you want to check for any commonest, you'll have to loop through the lists, and check each values appearance in the list. Something like this.
- List = the list of list
Since it's a 2D array,
2. Loop through the lists and join them together to get a single list
- Do a new for loop,
For every number in the new list, check the count of each number in the new list (new list.count(num).
If it is greater than 1, then print it out
This is the most basic way. After this, you can optimize your code from here 😊😊
okay, now having other problem. why this doesn't work? now i want to see if sum of array i put to function is above or below medium of a combination of same length and provided max number.
def isItLower(lister,nums):
listSum = 0;
smList = [];
lgList = [];
for i in lister:
listSum += lister[i]
for i in range(lister.length()):
smList.append(i)
for i in range(lister.length()):
lgList.append(nums-i)
numOfCombs = math.comb(lister.length(),nums)
smallest = 0;
biggest = 0;
for i in smList:
smallest += i;
for i in lgList:
biggest += i;
mid = (smallest+biggest)/2
cur = 0;
for i in lister:
cur += i;
return cur < mid;
Try using len(lister)
Have you considered just stuffing everything in a Counter?
Hi so i have been tryna download the opencv library from my cmd but it keeps failing who know why also i tried it on py 14.3 and 11.9 versions
1 leetcode question a day keeps the biches away
Hi, do you know any channels where they only study algorithms?
Not necessarily python
Hey
!kick 1166699033545416764 homophobia is not welcome here
:incoming_envelope: :ok_hand: applied kick to @copper crypt.
little numpy question
i have an n × k array M of zeros
and an n × l array I with each row containing distinct integers between 0 (inclusive) and k (exclusive)
i also have an n × l array D with arbitrary values
for each row M[i] and for each j between 0 and k, i want to replace the item at index I[j] of M[i] with D[i][j]
solved
given I, D, n:
M[np.arange(n),I.transpose()] = D.transpose()
``` or something
i'm trying to write an algorithm rn, but i can't seem to get it to work
anyone have an idea on what i'd do for this?
i want to calculate a path of steps of multiplying and subtracting from a starting value (1) to get to a target, with limits on how what value can be multiplied or subtracted (i'm doing up to 8 for multiplication, and up to 16 for subtraction, so you can't multiply or subtract it with a greater value)
so for instance, to get to 118, you could multiply by 8, (n=8) multiply by 8 again (n=64), then multiply by 2 (n=128) then subtract 10 (n=118)
the problem is i can't figure out how to actually get it to solve it since it gets stuck
i've looked into backtracking but that seems to be more for getting ALL solutions of a problem, i just want a solution
im trying to use a list of steps rn but it doesn't have any way to know that a step isn't viable
i could just have a list of every combination that leads to an undesirable value but that sounds way too slow and like bad design
a general solution to this sort of problem would be ideal since ive run into this before
i guess constructing a sort of tree would work where i mark certain nodes as good or not
problem is that you can get to the same value by both subtracting and multiplying, so it's not a true tree
oh wait i think i got something working
i made a Node class that has a mode (string), integer for the value to change by, a flag for if it's valid or not, a list of children, and a reference to the parent
i came up with the simple rules being
- if a node's calculated result value would be greater than the max allowable value, it's invalid
- if a node's parent is invalid, that node is invalid
- if a chain of nodes reaches the target value, return it as the solution
doesnt seem to work with big values
or some values ig
like doing 250 works instantly
251 doesn't find a solution after 60k steps
It sounds like it can be solved reasonably well using BFS search, you have a queue that you keep pulling out of
For each value if its equal to desired then great, else calculate a max that is it + maxSub
multiply the value from 2 -> maxMult 8 any value less than the MAX and not already visited gets added to the queue
Similarly 1 -> maxSub subtract and do the same
The key here would be the deciding factor to check the value is, if it's less than MAX which is it + maxSubtractionValue
Yeah I haven't verified FYI, just thinking out loud, it can definitely be optimised further
Oh something using A* might be even faster if needed
What sort of magnitudes are there for the limits? Can you get away with going one by one for the values, or will it be like 1-1000 a step
in any case the length of the answer should be something like O(log n)
yeah, the question is more so whether traditional graph traversal will work, or if you actually need to do algebra
!e here is a basic Dijkstra solution
from heapq import heappop, heappush
MUL_LIM = 8
SUB_LIM = 16
Op = tuple[int, str]
def reconstruct_path(best_cost: dict[int, int], start: int, target: int) -> list[Op]:
def step(cur: int):
for inc in range(1, SUB_LIM + 1):
prev = cur + inc
if best_cost.get(prev, -123) + 1 == best_cost[cur]:
return (prev, f'-{inc}')
for div in range(2, MUL_LIM + 1):
if cur % div == 0:
prev = cur // div
if best_cost.get(prev, -123) + 1 == best_cost[cur]:
return (prev, f'*{div}')
return None
cur = target
path = [(cur, '')]
while cur != start:
prev = step(cur)
if prev is None:
raise ValueError('No path found')
path.append(prev)
cur = prev[0]
return path[::-1]
def dijkstra(start: int, target: int) -> list[Op]:
heap = [(0, start)]
best_cost = {}
while heap:
cost, node = heappop(heap)
if node in best_cost:
continue
best_cost[node] = cost
if node == target:
break
for mul in range(2, MUL_LIM + 1):
heappush(heap, (cost + 1, node * mul))
for sub in range(1, SUB_LIM + 1):
heappush(heap, (cost + 1, node - sub))
return reconstruct_path(best_cost, start, target)
print(dijkstra(1, 118))
:white_check_mark: Your 3.14 eval job has completed with return code 0.
[(1, '*8'), (8, '*5'), (40, '*3'), (120, '-2'), (118, '')]
Maybe there would be benefit from a heuristic, idk
It would allow ignoring the really small stuff that is just wasting time subtracting
I mean, this will fall apart if the subtraction limit is something like 10000
or well, it will be slower than it could be
I may try to binary search this with z3
but idk how well it'd deal with the arbitrary polynomials here
I assumed these limits were fixed
well, that's what I was asking about in my reply
for the limits given a badly writtem maybe correct A* takes like a few seconds for target 123456
yea, if it's 8,16 you can just graph traverse this
maybe trim some obviously futile nodes
Dijkstra does not enjoy 123456
that's what the heuristic does, yeah
the heuristic I used is pretty naive
something like
if cur < target then
h = log(target - cur)/log(MAX_MUL)
else
h = floor((cur - target)/MAX_SUB)
a smart thing to do could be to search from the end, try to go to the closest multiple of M using subtractions, and then divide by M
(you might need to consider all the multiples where the sub cost to get there is equal)
(actually there is always a multiple within range, so no real searching for the given limits)
(it would be more interesting if larger muls were allowed)
yeah, going backwards is so much faster
69x faster with backwards Dijkstra vs forwards A*
!e
from heapq import heappop, heappush
MUL_LIM = 8
SUB_LIM = 16
Op = tuple[int, str]
def reconstruct_path(best_cost: dict[int, int], start: int, target: int) -> list[Op]:
def step(cur: int):
for dec in range(1, SUB_LIM + 1):
prev = cur - dec
if best_cost.get(prev, -123) + 1 == best_cost[cur]:
return (prev, f'-{dec}')
for mul in range(2, MUL_LIM + 1):
prev = cur * mul
if best_cost.get(prev, -123) + 1 == best_cost[cur]:
return (prev, f'*{mul}')
return None
cur = start
path = [(cur, '')]
while cur != target:
prev = step(cur)
if prev is None:
raise ValueError('No path found')
path.append(prev)
cur = prev[0]
return path
def dijkstra(start: int, target: int) -> list[Op]:
heap = [(0, target)]
best_cost = {}
while heap:
cost, node = heappop(heap)
if node in best_cost:
continue
best_cost[node] = cost
if node == start:
break
for mul in range(2, MUL_LIM + 1):
if node % mul == 0:
heappush(heap, (cost + 1, node // mul))
for mul in range(2, MUL_LIM + 1):
for sub in range(1, SUB_LIM + 1):
if (node + sub) % mul == 0:
# Align for next step.
heappush(heap, (cost + 1, node + sub))
if 0 < start - node <= SUB_LIM:
heappush(heap, (cost + 1, start))
return reconstruct_path(best_cost, start, target)
print(dijkstra(1, 12345678))
:white_check_mark: Your 3.14 eval job has completed with return code 0.
[(1, ''), (5, '*5'), (25, '*5'), (125, '*5'), (875, '*7'), (6125, '*7'), (42875, '*7'), (42867, '-8'), (257202, '*6'), (2057616, '*8'), (2057613, '-3'), (12345678, '*6')]
any value from like say, 1-16
arbitrary but anything too high would be annoying
I see, then see what fenix was cooking above
mk
ok yeah i swapped it out for a list of values it can use and it still seems to work
ngl, i have no clue how a heap works tbh
like i know its kind of like a tree but
note that this last thing currently depends on the ranges of values for correctness in finding a smallest solution
if the muls can be large then some logic would need to be reworked/generalized
and I think things can also break if the intervals aren't continuous
I've been playing around with using python + glsl, I managed to get DES encryption working at ~ 100x pycryptodome speed for individual 8 byte chunks
Almost 10Mbps (~72.5mbps)
333x faster than pycryptodome now
Why would you use Dijkstra instead of just a bfs?
Ah, it can be a bfs now yeah
Also, isn't it easier to start from the target
I had cost +1 and +2 for a while

To find a path that goes through all edges once and only once?
I don’t have any videos in mind but if I remember correctly the necessary and sufficient conditions are pretty intuitive and so is the algo to find a valid path (just dfs with a bit of care)
Find an implementation of the algorithm and draw every step of the algorithm it’ll be clear
a couple matrix multiplier scripts im working on, looking for feedback
There was an error uploading your paste.
I dont use python often js wanna find scripters, anyway is this a good calculator?
You probably wanna join a Swift (?) discord
Yes
But they’re none
What language is that? It's similar to python.
It’s swift very similar and simple,it’s very easy to understand for beginners.
Guys is there a chance for create a subtitle adder for yt videos?
Is this the right way to get from parent to child using selenium pip in python?
in this, i removed ".//" from child~still not working ?
not the right channel
GPU accelerated FFTs
maybe some of the noise algorithms?
some linalg algorithms would be fun
Marching cubes can look very neat
Is running fft on a gpu even worth it for stuff like audio? I imagine the sizes involved aren't that large
Spectrograms perhaps
It's done as a compute shader that feeds a vertex shader through a shared buffer, it's also done async of the cpu so it doesn't block
you can also extract the results back to cpu if needed (which does block), I'm working on a system where you can chain actions on glsl shader buffers to layer effects in a way that feels like numpy
I'm more curious if you actually win performance over cpu for these loads
that would be the window size?
that's way higher than what's typically used, isn't it?
I'm used to see fft sizes in the 256-4k range
True, however this is faster than prepping on the cpu side with numpy then submitting to the glsl
I'm using 16^3 values for this
I can do 30 in parallel just fine, trying to do that with numpy would be too slow
I can see it being useful for bulk processing
I.e. compute for a bunch of windows at once
I want to use it to make SDR waterfalls next, need to figure out how to write a row to a texture each frame
You mean having the effect of adding a row and shifting all other rows?
Yeah, I was thinking just use an index I increment +modulo for the texelfetch
So the compute shader writes the row and the fragment shader draws the texture treating the current row like the top
Maybe a double long buffer could be sensible. You write to rows i and i+N and then read the contiguous block of N lines starting at i
(no clue if sensible, but seems like a thing that would work)
Hey guys , I come from a tear 4 clg 🥲 and I'm entering into my 6 th semester. And I got to know that dsa is important and I have to do that. But I don't understand how much time should I dedicate to learning it, it's therotical stuffs 1 month, 2 month or what , although I will be solving 1 LC problem everyday but how long should I dedicate for learning the therotical stuff ???
I would really appreciate if you would help
Im sorry but I don’t think there’s any way we can answer that question, some people learn faster, and we don’t even know how much you want to cover
Maybe don't think about time that you will need but just try to understand topic and then solve some problems, then go to the next topic and solve problems. Also do spaced repetition, after some time solve same problems again
You know we guys don't have on campus placement so I have to give the interview off campus itself , hence it makes me wonder how much dsa do I to do to crack off campus placement
Hello, I’m mike
I'm looking for a serious and reliable partner in Starting a new business idea.
I already have a solid niche in place and now I just need someone ready to grow and build with.
If you’re genuinely interested, let’s connect.
on one side you look like a scammer on the other side you look like well just a random dude in com
When through his socials etc yesterday, just young and enthusiastic.
Mike I would recommend building a personal portfolio, github, personal toolchain, etc and learning the fundamentals rather than jumping straight into the deep end of trying to start a business
If you do want to pitch an idea, there are much better places for it than a programming discord though.
Managed to get frequency waterfalls working fairly performantly
Wow. I am experiecning the power of "pruning." I converted my k_sum algorithm to a decision problem and also learned how to prune impossible branches and I can now find one solution to k==99 for 1000 numbers that sum to 500. This is like black magic.
len(nums)=1000, len(display_list)=1, k=99, target_sum=5000, display_list=[(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 149)]
And it returns instantly.
isn't this a problem where you can do the typical dp approach to construct every constructible number?
in O(len(nums)*target)
How do I start DSA? Whenever I start DSA, it stops at the array.
Which year of MIT 6006 should I be looking at? I'm like in 3rd lecture of 2011, but I just found out there's a 2020 version that covers data structures. (2011 version seems to weakly cover it?)
nvm, pins mention the 2020 version. 
MIT 6006 open course ware. Or neetcode. Or just start by reading Introduction to Algorithms, CLRS (I do not recommend this one)
MIT 6000 open course ware?
Thank you!. I’ll check them out
anyone has experience with AI Agents? I need an advice
hi
#data-science-and-ml is probably the channel you want. Just ask the questionl
Greatest command known to man: claude --dangerously-skip-permissions
anyone ever pass function and its arguments entirely through a function that also returns any errors from the child?
I did yesterday, I guess you see something new everyday
Also just pick topics up by yourself random and learn it, most topics are interrelated but you don't "really" need pre-requisites so you could start graphs tomorrow per se
The stopping at arrays happened to me multiple times
Tbh I only know bs pretty well i guess
I still have sm to do
Leetcode pmo
Got it! I’ll pick topics randomly and start with graphs tomorrow..
Hello
hey yeah
im new to algorithms, i cant figure out how to stop it from returning [0,0]
nvm figured it out
did you use opus to find that tweet?
Please do not post topic-irrelevant things. You've been posting about this a lot the past week and this is not an appropriate place to post it.
Quick question, i know this is an easy question, for a sorted list if the goal is to sort lists in increasing order would that not be constant time complexity but linear time at best because you would still need to check for each element in the list and make comparisions, right?
I mean, if you know that the list is sorted there is nothing to do
def sort_sorted_list(lst):
return lst
```but it's a totally useless thing
If you have a general sorting function you would need to at least look at all the elements
That is what i was saying, for any sorting algorithm (to sort numbers in ascending order) it is impossible to get constant time complexity because of the reason you said, right? I just got confused because of listening to a friend justifying constant time is possible
Constant time is not possible, yeah
where do i start off learning python?
Unless you're talking about the useless case where you already know the list is sorted
!res is a collection of resources
Resources
The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.
print("Hello world")
ty
i need to spend less time with my friend, wasted 10 minutes thinking about it 😭
that's crazy
!ban 1226658402910998528 Wildly inappropriate response to being told to stay on topic.
:incoming_envelope: :ok_hand: applied ban to @devout escarp permanently.
does a* belong here? should I just use a library for it?
A* does belong here. I usually hand roll A*, since the complexity of implementing it is usually not that much higher than getting my format into something a library implementation can work off of
can anybody explain to me this sorting algorithm in a understandable way.
Take the first element of the array. The array is now sorted up to its first element.
Take the second element. Place it before or after the first such that the first two elements are sorted.
Take the third, do the above.
In every step, you extend the sorted portion of the array by one (i in the outer loop is the length of the sorted section), until the whole array is sorted.
It's a very efficient algorithm for sorting small arrays, but it grows quadratically, so for larger arrays it's not great.
@elfin wedge ^
I mean like can u explain what every lien of code is for
im not sure if this is the good channel for this but
My school does something called senior assasin with this app and everyone opts into thier location like this
Is it appropiate (legal) to observe the UI and calculate the locations to use probability models to figure out who is my target
and if so is this a ogod metyhod
they pretty much did...
pixel sorting is cool
What should it be about? What's it for?
This looks to me more like bubble sort than insertion sort.
I'll try to explain every line of code.
(I'm starting to sound like ChatGPT... 😆 )
Line 1: definition of the function with an input argument of arr.
Line 2: initiation of a for loop with the length of the array minus 1 as its loop length.
Line 3: setting the secondary index of the algorithm to be the current primary index, which determines the iteration/pass number. The secondary index is like a moving cursor which tells you which element of arr you're currently looking at, and the primary one is just for keeping track of how many passes have you done so far.
Line 4: initiation of a while loop with the condition that j-1th element of arr (for the current value of j) is greater than the jth element AND the secondary index j is greater than 0. This makes it so that you keep switching the neighboring elements while they are in the wrong order, and you'll stop when you reach the 0th element, i.e. the start of the list arr.
Line 5: you assign the first item in the pair after the = sign to the first one before it, and the same for the second items. This means the value of arr[j] becomes the new value of arr[j-1], and vice versa. In simple words, you switch them within the list arr.
Line 6: you decrease the value of the secondary index by 1. In simple words, you move your cursor towards the start of the list.
Lines 7 and 8: two blank lines after a function's definition, as per the proper Python formatting.
Line 9: initiation of the list arr which is getting sorted.
Line 10: calling the function which sorts the list in its argument.
Line 11: printing the list arr.
I'm always a bit hazy on the details of arguments of functions, and what changes and what doesn't, but I suspect there is a bug, causing the printed version of arr to be the same as the original one, since (I think) the function operates on a different instance of arr than the original, because arr was given as an argument, which creates an internal version of it within the function's run.
I'll test and confirm/deny my suspicion.
Hm, it does sort the list successfully. Ignore that part then.
And I nearly forgot the explain line 12: a single empty line at the end of a Python program, as per the proper Python formatting.
In short, you move each element of the list towards a chosen end of the list, progressively creating a sorted ordering from that end. Since the elements "bubble up" towards that end, this is called Bubble sort.
I hope this helped you, @elfin wedge .
it is not bubble sort
You sure?
It sure looks like it to me.
Bit inversely implemented, in that the smallest elements bubble down, instead of the biggest ones bubbling up, but it's still a bubble sort.
it is insertion sort
Why? In what is it different from reverse bubble sort?
Hm. Looking at the geeksforgeeks's implementations of the two sorting algorithms, it does look like insertion sort, but a badly coded one. It swaps neighboring elements, instead of storing one element in a key variable and shifting the elements to the right until the key can be inserted into the correct position.
bubble sort finds the min or max value on each iteration and puts it in the correct spot. insertion sort builds up a sorted slice of the array
I am solving this problem
https://leetcode.com/problems/third-maximum-number
Can time complexity of this solution be considered O(N)? How do you commend time complexity for this solution?
class Solution:
def thirdMax(self, nums: List[int]) -> int:
min_heap = []
taken = set()
for index in range(len(nums)):
# If current number was already taken, skip it.
if nums[index] in taken:
continue
# If min heap already has three numbers in it.
# Pop the smallest if current number is bigger than it.
if len(min_heap) == 3:
if min_heap[0] < nums[index]:
taken.remove(min_heap[0])
heappop(min_heap)
heappush(min_heap, nums[index])
taken.add(nums[index])
# If min heap does not have three numbers we can push it.
else:
heappush(min_heap, nums[index])
taken.add(nums[index])
# 'nums' has only one distinct element it will be the maximum.
if len(min_heap) == 1:
return min_heap[0]
# 'nums' has two distinct elements.
elif len(min_heap) == 2:
first_num = min_heap[0]
heappop(min_heap)
return max(first_num, min_heap[0])
return min_heap[0]
what do you think the time complexity is? why?
I guess N because it is not that there are N elements in heap. And add and remove are O(1)
As written the heap can become O(n) size, no?
ah nvm, I misread a part
the len 2 case at the end just looks silly
the second number is guaranteed to be larger, no?
Right, the second number is larger if it is inside min heap
(although this is not my solution)
If the third maximum does not exist, return the maximum number.
god that's an ugly thing to do
@modern gulch
how would I search for the picles that this is locaed
Ah, ok... that's not my specialty, but could you open a help thread, more people will look at it; #❓|how-to-get-help . I imagine that if the outline of the profile is always the same shape, that'd be fairly straight forward.
I'm looking at the problem, and I'm a bit confused. Does it simply require the third biggest number to be returned?
Hi
Basically
But if there's only 2 distinct elements in an array you'd return the max
It's pretty much just frequency counting
Or just convert to set and if size <= 2 return max
Else return 3rd max
which is a very silly choice
it's not quite the right term, but it makes the problem kinda "discontinuous"
behaves in an incompatible way for some outputs
picking the minimum in those cases would make much more sense
Oh really?
I mean frequency counting is obviously the most optimal sol
But using sets makes the impl 10x easier
Btw i should've mentioned set back to list
The complexity is pretty high but yeah very easy implementation
I mean as in, I don't see a way where the 1 and 2 unique element cases is not a special case that needs handling
If what was asked for was "if there are <3 unique elements, return the minimum" you can have solutions like:
- get the (up to) 3 largest elements
- return the smallest
no special cases
(or equivalently, if there are 2 elements produce the second largest, if there is 1 element produce the largest)
Did I read the question wrong?
if it's minimum wouldnt you just sort and return min
or are you rewording the given problem as min of the three largest
which makes sense
no special cases
oh yeah
youre right
for the perfect sol (your method) we could create an array of size 3
and update maxes
so the point is that 3rd largest does not generalize smoothly to "pick the maximum otherwise"
not sure what a natural phrasing is, but the natural generalization is either to return None (there is no 3rd largest) or return the minimum of the up to three largest (which is 2nd largest if there are 2, and largest if there is 1)
the awkward case is the case with 2 unique
I would phrase it as
a reasonable postcondition of the function is that if you sort the set of unique elements in the input list, the result should be an element in the list that is less than or equal to the last 3 elements, if they exist, and greater than to all other elements
(other combinations of <, <=, >, >=, or considering the last 2 elements should still have the same issue)
by returning the max when there are only two elements, this violates that postcondition specifically when there are two elements, which is weird
Yeah true
it's just one specific case to handle so it's ok, the question is made to make sure we handle cases separately i think
one unnatural special case is worse than none
the special case of "there isn't enough elements" is unavoidable
the special case of "there is exactly two elements" is avoidable
well
I say unavoidable, but you can kinda squish it into the postcondition
a sensible choice would also have been to return nothing
yeah
like either fully separate the cases, or squish them together
don't squish them somewhat together but two elements is a separate case
one element is also special cased weirdly, it just happens to have the same answer as the more sensible generalization
extensional equality moment
DSA shows up naturally in the problems you try to solve with software. They show up in the need to model the problem (structure) and then compute something based on that model (algorithm). This can range from the most simple of data structures such as lists, to complicated trees and graphs. And algorithms depends on which questions you need to answer from that data and how efficiently.
Where can I learn it? Or how can I practice it?
I recommend using a resource like https://automatetheboringstuff.com/ for getting ideas of what kinds of real world problems you can solve with Python. And another resource for more about just DSA itself (more theory than applied), which you can then hopefully find in the actual real world problem you are trying to solve: "oh, this needs a queue, and this needs a sorting algorithm," etc.
A Page in : Automate the Boring Stuff with Python
See the pins for DSA resources.
I will also throw in the classic CLRS book, but note this is the "learning DSA the hard way" route. But for some this works. https://www.amazon.com/Introduction-Algorithms-3rd-MIT-Press/dp/0262033844
Thank you
Neetcode
If you like watching videos
But I'd always recommend using a book
Good book
I just wrote 18 implementations for two_sum() 🙂
There are 4 basic kinds of solutions : decision, count, search, enumerate. For search and enumerate I wanted to provide an API for getting either the values or the indices. Then for each of these 6 methods, I consider whether the input array is sorted or not, via a bool flag the user can provide. So there’s 12. Also, each of these methods takes an ignore_dupes (default is true) so they have two branches where if dupes are present in the list I can either treat them as unique elements or ignore them. ex two_sum_count([2,2], target=4, ignore_dupes=True) returns 0. Then I have the original 6 cases where the user tells me there are no dupes in the list, so that’s 6 more cases. In each of those I branch on sorted vs. unsorted via a flag.
For the API I’ll just have three methods, decision, count, and find, with argument flags controlling which implementation I dispatch to.
Each specific implementation is fine tuned for the particular parameters in each case. I wanted to minimize time and space complexity in each case.
I'm looking for a mechanism to make instances of class Foo() unique based on a unique hash which is known at instantiation time. My first instinct would be to give Foo a class variable containing a dict with these hashes as keys, and the instances as values. Then __init__(self, hash, bar, baz) would either:
- find an existing instance by hash in the dict, update its bar and baz, and return it
/or/ - add
selfto the dict with keyhash, fill bar and baz and return self.
However in the former case, the instance that was made by__new__is immediately discarded. Would I be better off overriding__new__? Or am I totally off base and are there better ways to do all this in general?
you would have to override __new__ for this no matter what, __init__ must return None
I'd probably prefer a factory method
oh right, d'oh! I wrote a little PoC where I used __new__ and that seems to work. Can you point me to a resource/example for this factory method pattern you mentioned? Is it just a class method that does all the aforementioned work, and external code shouldn't touch the actual constructor? Or something more advanced?
just that
cool, thx!
Just create a class variable “sequence_num” with a class method you call in new() that increments by 1and returns the current value?
i think i accidentally made a sequence that takes like hundreds of millions of steps to repeat
it's very simple mathematically
you take a row of values and each cell in the next row is the sum of the above and adjacent values in the previous row, and the sum is taken mod N
for example:
00100
01110
12321
36763
96969
54145
90909
98989
for most values it seems to repeat pretty quickly, but specifically with rows 18 cells wide, and the modulus being 10, it takes a ridiculous amount of steps to repeat
here's the first 100 steps of the long one (i'll notate it '18:10' for being 18 wide, and modulus being 10)
i tried running the code to find a repeat and came back a few hours later to find it was taking up over 20 gigabytes, it had calculated over a hundred million steps without any repeat
the color represents the digit, transparent = 0, and then the darkest color = 1, and the lightest = 9 (or whatever N is, minus 1)
in text too
Click here to see this code in our pastebin.
How would you prove it though
Plus it looks like a prefix / suffix sum from both ends to the middle
you just check if any row has appeared twice
No the fact that it takes so many steps without simulation
Why 18:10 and not something else
Etc
idk
mod 10 seems to take a lot of steps for some widths
10:10 is something like 40,000 i think
the number of symmetric integers is O(sqrt n), so that checks out
also, related sequence https://oeis.org/A064189
what you get if you start with an infinite array 1000...
What do you think is the most likely answer?
- It was included as part of the language because nobody would ever use such a thing.
- It was included as part of the language because some people thought it would be a handy thing to have.
2 is the obvious answer
But I wonder how popular it is among python developers
do you see it in one out of 10 projects or out of 100 projects?
it would be hard to find a reasonably sized project without one
The point is that if external code wants an instance for hash 'xyz', it would call the constructor or factory method with the hash. If there was a previous call with the same hash, the instance already exists and the caller should receive that one, instead of a new instance.
Does the instance need to know the hash? If not it sounds like
defaultdict(MyObject)
```will do what you need
actually you probably could just slap a cache on things
@cache
def get_object(the_hash: str):
return MyObject(the_hash)
I use it every day.
It does
There are more fields needed in the object, but only hash determines the instance. I don't think cache supports this behavior. But I'm set anyway, thanks for the help,
uhh are there any good use for iterators in lists?
and are these enought to learn in lists
if it is just dm me yes
i am a newbie in py
my school's py sylabus is so shitty that they still make us do patterns with for loop
hi guys ı need help , now we adding dropbox cloudstorage to our pyhton code but we dont now how we do dropbox code can anyone help us?
Yes you should absolutely learn all these by writing little example methods that use them. If you don't know these methods then you really don't "know lists."
The proof there are is the fact that a for loop over a list is syntactic sugar over a list iterator behind the scenes.
is this a reasonable way to write getting all the interior angles of a polygon?
```py
@dataclass
class Point:
x: float
y: float
def getidx(i:int,l:list) -> Any:
return l[i % len(l)]
def get_angles(points:list[Point]) -> list[float]:
angles = []
for i in range(len(points)):
p_prev = getidx(i - 1, points)
p_curr = points[i]
p_next = getidx(i + 1, points)
ang = abs(math.atan2(
p_curr.y - p_prev.y,
p_curr.x - p_prev.x
)) + abs(math.atan2(
p_next.y - p_curr.y,
p_next.x - p_curr.x
))
angles.append(ang % math.pi)
return angles
% math.pi doesn't seem right. Did you want to write / (2 * math.pi) * 360, i.e. conversion from radians to degrees?
hm, its for interior angles
i might be wrong on that tho ill have to take a look when i get home from class
It might be interesting to make the sides wrap around.
I tried running it on an equilateral triangle, but it doesn't output correct values...
Please react with ✅ to upload your file(s) to our paste bin, which is more accessible for some users.
atan2 is not a good function for this. It's much better to treat the edges of the polygon as vectors, and use the formula for angle between two vectors, arccos of dot product divided by the product of the vectors' lengths.
wot
lol
there is a very dumb bug there
hint: your program will always return true
because ||an int and a set are never equal||
guys, what do you think that is important to do in the interview when it comes to my communication with interviewer? I think that "thinking in examples" (if I can say that in English) is important and that could help interviewer understand my thinking process - by that I mean verbally explains how my program behaves for different inputs. Also to explain space and time complexity and explain about what edge cases I am thinking about
Hi, I am looking for people who have experience in signal processing. I am developing my own small package for python. If you are interested in joining me pls let me know.
Thats interesting, lets report a bug to compiler
No, it's not interesting. They just forgot to add another "len()" around the set conversion.
Plain old PEBKAC.
Oh I see
Why is leet code soo hard 😭 😭 I m just 3 months in and relunctantly trying to level up and today I tried solving 4 sum and it just didn't work can anyone explain any easier way like I asked ai but yea
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
if len(nums)<= 4:
return nums
nums.sort()
right = len(nums)-1
left = 0
left_1 = left + 1
right_1 = right - 1
combo = []
add = 0
while left+1<right-1:
while left_1 < right_1:
add = nums[left] + nums[right] + nums[left_1] + nums[right_1]
if add == target:
combo.append([nums[left],nums[right], nums[left_1], nums[right_1]])
elif add > target:
right_1 -= 1
else:
left_1 += 1
if add < target:
left += 1
else:
right -=1
return combo
This was my iniial try
But couldn't see what is wrong coz memory limi hit
yo can yall help me? idk how to install python
im stuck
and how to learn basics of python, im trying to learn like an entry where someone logs in and says hello (name) then etc..
EZ
Download Python here: https://www.python.org/downloads/
i'm curious about how much overhead function calls actually require
speecifically numpy function calls
code in question
class OrbitalStrikePayloadAccelerator:
...
def calculate_acceleration_strategy_matrix(self, delta_v: np.ndarray) -> np.ndarray:
# Convert delta v into column form for solving matrix equation
delta_v = np.vstack(delta_v)
# Solve the matrix equation, obtaining an array containing the number of times
# a compressor is used.
acceleration_strategies = np.linalg.solve(self.basis_vectors, delta_v)[:,:,0]
acceleration_strategies.round(out=acceleration_strategies)
# Discard all acceleration strategies that involve negative counts
acceleration_strategy_mask = np.all((acceleration_strategies >= 0), axis=1)
viable_strategies = acceleration_strategies[acceleration_strategy_mask]
viable_strategy_acceleration_vector_mask = self.basis_indices_mask[acceleration_strategy_mask]
# Choose the strategy that has the fewest total counts
total_counts = viable_strategies.sum(axis=1)
strategy_index = np.where(total_counts == np.min(total_counts))[0][0]
optimal_strategy = viable_strategies[strategy_index]
optimal_strategy_acceleration_vector_mask = viable_strategy_acceleration_vector_mask[strategy_index]
# Apply the mask to obtain a row matrix of compressor uses.
optimal_strategy_matrix = np.zeros(self.vector_count)
optimal_strategy_matrix[optimal_strategy_acceleration_vector_mask] = optimal_strategy
return optimal_strategy_matrix```
delta_v is a 3-array of floats
self.basis_vectors is a stack of n 3 * 3 matrices
i was expecting that the execution time scales linearly with how many matrices there are in self.basis_vectors, and this is indeed true
however, i was surprised to see this:
i feel that this function is wasting time doing something instead of processing numbers, and i want to know what constant-time tasks could be accounting for this
maybe this is what i get for using python :c
the surefire way to find out what is eating up processing time is to profile the code
try something like line_profiler or scalene or whatever
oh yeah i forgot line profiler exists
I mean i doubt u see anything diff if u plot that in go?
or C
i would be extremely surprised if everything was exactly on the line
i mean its hella complicated what cpu is actually doing in the background while ur program is running
the line is of the form y=ax+b
thr constant term b was unexpectedly large
about 30 microseconds
oh
hey bro i have a suggestion for u
go over to microsoft store
instal py 3.12
then
download VS code studio
open a new file
and here
u will see "plain text" instead of "python"
click on it
it will pop the search bar
search py
select the module
and ur good to go
👍
then
tun this: python -m ensurepip --upgrade
in the terminal
ts terminal
it will install pip (trust me u will need it)
@inland basin
https://github.com/dronnix/bwarr
New data structure seems to have recently dropped? 👀
It's an ordered multiset. Very similar in complexities to BTreeSet, but with multiset behaviour and array-based, so does few allocations.
Is that number based on an average of 100+ runs, or is it a single run time? It might just be the initial compilation of the code.
Or whatever it is called that Python does when preparing to execute a code.
I'm running the function 100000 times per trial
and I'm averaging 3 trials
also my timing only takes into account the function call time as I do a control test
I've seen numpy be surprisingly slow when used for tiny arrays, but I agree 50μs is a bit surprising. maybe profile the code at a low n?
for example, set a python process to run this in an infinite loop, and in another terminal run py-spy top --pid somenumber
hello i am new here and i want to learn coding what should i do -
Start here: https://youtube.com/playlist?list=PL-osiE80TeTt2d9bfVyTiXJA-UTHn6WwU&si=gSRf4dNwXoYANJbe
thank you
yoo can i get help
Read #❓|how-to-get-help
Hello everyone, recently I started learning c++ in order to get a good grasp on DSA concepts and manual memory allocation. But one of our college faculty suggested that I learn DSA concepts in JAVA.
what should I do...?
Should I continue learning C++ or switch to JAVA..?
Depends on what you want to do. Look at what the industry uses C++ for. See what it uses Java for. What do you want to do?
But really, data structures, once you know one set, apply across all languages pretty easily. I learned DSA in assembly and then reused them all in C. Now I mainly work in python/C++/groovy. DSAs are all the same - just different syntax.
once you know one language the others are a lot easier to learn
unrelated, i worked on an ascii art algorithm a bit yesterday
亹亹亹亹亹亹俨丅二二二丶丅亸亹亹亹亹亹亹
亹亹亹亹俨二乄乸亹亹亹亹乸乨丶亸亹亹亹亹
亹亹亹严乄亹亹亹亹亹亹亹亹亹乸乚予亹亹亹
亹亹严乄亹亹亹亹亹亹亹亹亹亹亹亹乨予亹亹
亹俨乁亸俨亹亹亹亹亹亹亹俨亸亹亹亹乚俴亹
亹丶亃乸俷丶亹亹亹亹亹乁乸俷丶亹亹乸丶亹
亂乄乂亹亂丶乛亹亹亹仁亹亹亂丶乛亹亹乢亀
仁乽乗亹亹乸亻亹亹亹乁亹亹亹乸亂亹亹乸乛
一乸亊亹亹亹亻亹亹亹乄亹亹亹亹亻亹亹亹一
一亹乸乸乸乸乸亹亹亹乸乸乸乸乸乸亹亹亹一
一亹亹亹亹亹亹亹亹亹亹亹亹亹亹亹亹亹亹一
一亹亃丶丶丶丶丶丶丶丶丶丶丶丶乛亹亹亹一
亻亹亹一一一一一一一一一一一一一亹亹亹丂
亂乁亹亃一一一一一一一一一一一一亹亹亻乽
亹一亹乸乛一一一一亝亂亂亝一一丂亹亹一亹
亹亂乛亹亃丶一一俪亹亹亹亹亂乀乸亹亻乽亹
亹亹乚乃亹乨乛亝亹亹亹亹亹乀亷亹严乄亹亹
亹亹亹乨乛亹乸之俨亹俨丐乄亹亹亻乄亹亹亹
亹亹亹亹乸丶严亹亹乸亹亹亹严乀乸亹亹亹亹
亹亹亹亹亹亹乸乚二丶丶二乄乸亹亹亹亹亹亹
it takes in an image, segments it into blocks of some size, and then using a generated bitmap of a character set, compares the pixel data for each character and scores them based off of the difference between that character's pixel and the luminance of the image pixel
the character with the lowest score is then chosen to fill that space
it's a lot simpler than some things i've seen for ascii art like vectorization+edge detection, but that method doesn't allow for shading
thx
the code is kind of spaghetti but the concept is p simple
most of the work was getting it to properly generate the character bitmap
hey everyone I'm new here
I know OOP and the basics and I've figured that I missed something incredibly important, DSA
Now, I've learned Binary Search, Linear Search, Selection Sort, Bubble Sort
What is the next step?
Sickk
Fun read, thx for sharing
this page in general has so much neat stuff
https://ttwong12.github.io/
Do you know a good way to learn DSA?
I learned in college and from years of experience. For you, if you don't have a college class to take, I'd look at books, youtube, or just jump into leetcode. Depends on how you learn.
Hello how to learn python
By learning it
it's late and i'm having trouble deciding between these:
board = []
for _ in range(16): board.append(' ')
# /or/
board = []
board.extend(' '*16)
# /or/
board = [*' '*16]
Python is about readability. I'd use either A or B. I haven't actually seen the notation of C before.
board = [" "] * 16
board = [" " for _ in range(16)]
U should try out list comprehension
most common pattern would be ```py
board = [" "] * 16
Doesn't that make a list of 16 lists? (I don't have a REPL to test atm)
No it doesn’t
It makes a list of 16 strings
You can use the discord server to execute things
!e
board = [" "] * 16
print(board)
:white_check_mark: Your 3.14 eval job has completed with return code 0.
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
Cool, thx! But I don't really understand why that works. In my head, the square brackets are what contains an entire list, not a list item. The syntax above reads to me as list + list + list + etc instead of list item + list item + list item.
Like, board = ['' '] is a list with one item, right? Board = [' ', ' '] is a list with two items. The repetitions happen inside the brackets, and there is just one set of brackets, hence just one list . Board = [' '] * 16 feels to me like it would repeat the brackets, hence making 16 lists
it is essentially as you said, list + list + list + .... but adding lists concatenates them
Aha! Thank you, that makes sense!
@whole marsh COME DM
What is dynamic programming
The core idea that you can solve a given problem by solving simpler problems, and combining their solutions in some way, and repeating that until the problems end up trivial. You also make sure you check if you already solved a subproblem before solving it, since you can end up with the same problem on multiple branches
The two variants are top-down (usually implemented as a memoized recursive function. ) - start from the problem you want to solve, and work your down, and bottom-up, which instead starts from the trivial problems and grows from there
The name doesn't make much sense
Thanks, understood
The name is intentionally bad.
They wanted to avoid mentioning "mathematical optimization" (which it falls under).
(Now also a name given to a specific programming technique based on it)
Anyone know the name of the algorithm that takes lots of pixels and groups them as a point with a width and height to make it simpler?
A variant of this?
https://en.wikipedia.org/wiki/Polygon_covering
In geometry, a covering of a polygon is a set of primitive units (e.g. squares) whose union equals the polygon. A polygon covering problem is a problem of finding a covering with a smallest number of units for a given polygon. This is an important class of problems in computational geometry.
There are many different polygon covering problems, d...
Mmmm not that one
Like a 2D run length encoding but also specifying the top left corner
specifically for bitmaps
Greedy meshing
repeated concatenation of an immutable: TRUE:
behaviorwise, at least
implementation-wise, it's faster than actual repeat concatenation
I am sure such an algorithm exists, but I am at a loss as to what to call it. Quadrilateralization is a thing, but there is no "rectangularity" enforcement in that space.
Greedy meshing :)
can you link something that focuses on pixels instead of on voxels?
everything I am finding online points at it being bespoke
you have to implement it yourself
say, you have an odd shape that must be greedy-mashed in this manner. First step is knowing whether you are inside the shape. That isn't trivial, actually.
Particularly troublesome if you don't have an a-priori understanding of the internal convexity of that shape.
it's not too different, iirc part of the algo is applying it to an projected view
i.e. very much like pixels
This greedy mesher is blazingly fast. Written with Rust and Bevy, using clever bitwise operations we can generate chunk meshes, an average of 0.000195 per 32x32x32 mesh!!!
This mesher blows most culled meshers out of the water, and I want to teach you the "secrets" of how to implement this for own voxel engine.
There are 2 algorithms we'll explo...
random site about the binary image problem in particular
https://utia.cas.cz/en/research/research-areas/signal-and-image-processing/decomposition-of-binary-shapes-into-rectangles/
UTIA of the CAS is a public research institute focused on AI, signal processing, systems theory, and applied mathematics. It conducts top-level research, cooperates with universities, and contributes to science and practice.
Totally new to the whole data structure thing - I have come up with this technique to abstract literature data for better accurate searching - It seems to work well with small data sets 10K publiactions or so. But as the data sets become large, scaling becomes an issue. The abstracted layer carries lot of information and multiple this with the volume of publications say tens of millions we are talking about large humongous data sets running into petabytes. I am thinking of storing the large dataset on s3 and have local inverted index and HNSW index (Hierarchical Navigable Small World) which are smaller files and only retrieve the full information for a smaller subset 100-500 items. Is this approach the right approach. These are things I am learning on the go, as I am mechanical engineer by training and beyond some basic python coding I have not handled anything remotely like this.
Gukesh left chess 🥀
yeah this is his new gig. He knows his fate is sealed
Anyone knows if there such a thing as suggesting a test case on AtCoder like there is on cses (https://cses.fi/hacking)?
I just submitted something, realized it should've been WA but it passed
ive done this before but my code is fucked up
it didnt generate the optimal configuration but at least mostly managed it
lemme format it so that i can send it here, i can't say i'd recommend using my code but maybe you can reference it in some way?
greedy mesher
go up until change, go right until change, delete the old pixels, continue going up
input and output (scaled up 4x for easier viewing)
dark areas were plotted earlier, to allow distinguishing different areas of the image
sadly it doesn't do it optimally as you can see with the area on the right
I'm not sure if there's an optimal solution that doesn't involve just trying random things, maybe by some sort of search algorithm that tries to maximize the area taken up by each quad?
Something like greedy meshing will give you pretty good results. Fully optimal things would end up as solving some graph problem
i made a data serialization structure and realized i have no usecases for it right now
nice work
can i dm you?
hello guys
i would like to know the number of primitive operation that are in this python statement " y= u+v" any one please help me
define "primitive operation"
depends on your definition of primitive operation
naively, it contains two, assignment and addition
however it also would require at least 3 lookup calls to get/set the variables
Would somebody be able to give me some advice class composition?
Currently I have a class, call it "AgentRegistry", agent registry is essentially a list manager class, that stores agents.
My market wants to be able to GET the list of agents, SORT them into descending or ascending, and get the AVERAGE of a value each holds
Im attempting to write this is solid, so im confused as if I should:
- add a sorter class to my AgentRegistry, sorts my agents, can return average
- create a custom list class, for AgentRegistry to store agents in, the class has(.average, .sort etc)
- create an entirely external class for list/dict sorting
you can usually sort by using sorted(list, key=) and the key has to be a lambda expression that takes in an object from the list and evalutes to something that can already be sorted and is (but not always) unique to that object, such as an ID, a text representation (if you're sorting a custom Name class for example)
text representation? like a var name?
Im familiar with sorting, Im just trying to lay this all out in SOLID
seperating all the functionality and crap
i guess? it's up to you really. usually i use sorted(key=) and try to always output numeric values for the key lambda
an example usage of sorted:
def songsFind():
songlist = []
for root, dirs, files in os.walk(songDataPath):
for fi in files:
if fi.split('.')[-1] == "pbdata":
fullPath = os.path.join(root, fi)
songFileObject = open(fullPath, 'r')
songlist.append((songFileObject, fullPath, fi))
songlist = sorted(songlist, key=lambda x: x[1]) #<-- here
songNameList = []
for i in songlist:
_RAM[135] = gas.rawgets(i[0].read())
i[0].seek(0)
songNameList.append(_RAM[135]["info"]["songname"])
print(_RAM[135]["info"]["songname"])
return (songlist, songNameList)
this however does sort using a string instead of a numeric
python string sorts are asciibetical i believe
ah i see
what is asciibetical??
ASCII ( ASS-kee), an acronym for American Standard Code for Information Interchange, is a character encoding standard for representing a particular set of 95 (English language focused) printable and 33 control characters – a total of 128 code points. The set of available punctuation had significant impact on the syntax of computer languages ...
SOLID requires context. Specifically what the changing requirements from the job may be. Without that SOLID's S means nothing.
The single responsbility is relatve to the task. It determins where those lines are drawn. Without that there is no sense of what "do just one thing" means.
In others it's not contextless rules that can be blindly applied to some code.
All those options you gave may be correct or not.
You may have heard of "premature optimiztion." This would be a case of "premature generalization." Without any further context showing that you need (with high probability) that generality to be able to swap out these parts in the places you mentioned it's just increasing the code complexity.
SOLID's S is based on this, it assumes you know that you will probably need to be able to swap out those parts. A classic example is having a class that has a dependency injection of a specific database class so that different databases can be swapped out as needed (including a dummy test database). This assumes that the job requires that to happen at some point. So that class that can swap out which DB is used now "does one thing." Instead of handling the specific DB plus the other stuff it does on top of that.
But note that this example involves a decently big object (a DB). If you try to use SOLID on very simple things it breaks down as it starts to become micro objects that start resembling lambda calculus at some point (breaking down into the most tiny of things and building up from that again, nice for math, but for practical use this makes your code size explode (also functional programming is better suited for this because the bloat is less with it)).
(It being relative to the project (changing) requirements is what causes these lines to be drawn in a way that is not too tiny (or too big, which would be no SOLID at all, just one big class), but exactly the right size)
(This kind of code is jokingly now referred to as "enterprise software" (caused a blind application/misinterpretation of SOLID, etc))
Maybe move this to #software-architecture btw.
I see so I am definitely just doing premature generalization
I have realised that when I use solid, boilerplate code is everywhere, and my projects are much larger.
So perhaps Ill have a look into functional programming.
The problem is solving for a problem you don't have, and maybe never will. In other words, predictive programming can get you in trouble, because predictions can be wrong, and if then applied to the whole codebase, results in many mispredictions.
SOLID, when applied without context, is preparing for a problem you will never have.
And any such generalization / refactoring has a cost.
Honestly Ive just been attempting to refactor a microeconomics project for SOLID, just because I heard its somewhat industry standard in software development?
I feel it has a few advantages for my project, since I essencially am making market agent objects
SOLID is common, but the original intent behind it is within context.
In the DB example I gave, if that is within the context, then it's very useful to have that ability to swap it out.
It also does make the code more clear in that case (according to many, this is partially opinion/taste).
The added complexity is worth it there. So there was some cost-benefit analysis, and it was based on a prediction that is likely correct (in an immediate sense).
Ok, I think I kind of understand, that the context for using it is just for modular DB management?
For example, if I had identical, very large finance data structures
The use in general is modularity. As is the intent of OOP in general, but these principles can help you further in your thinking of how to tackle those changing requirements.
Alright, Ill have a research into both OOP and functional
i think one should refactor for readability/grokability first. that covers at least the SOL part of SOLID. Anything extra but unneeded will generally worsen your readability
I appreciate the advice and lecture, I havnt used OOP much in the past
They are not mutually exclusive, functional just has a bunch of other useful ideas too.
Basically all these principles, are principles, a guideline, not hard rules to follow, ideas for how you can make code adaptable, reusable, etc. And all of that requires some context to which your design is relative to, otherwise it's kind like flailing around and following design patterns for the sake of having patterns, not to actually improve the project.
You are right yes, thats exactly what I feel I'm doing right now lol
I know, I feel most of my abstractions exist for functionality I'm NEVER going to implement
If you are really unsure of what you need, and the context is vague. I recommend prioritizing simplicity over everything else, since complexity is what usually brings a project to a grinding halt. The simple code may be specific (not general) and you may need to just delete it and replace it entirely, but because it's simple, it's small and easy to replace.
You have add a bit of flex in there, and Python gives you some of that by default.
But don't start adding patterns before it's clear what the problem actually is / what the requirements are (and how they will likely change).
I know what functionality I need because Im refactoring, its just where I'm supposed to put it.
Where depends on your communication structure.
Conway's law describes the link between communication structure of organizations and the systems they design. It is named after the computer scientist and programmer Melvin Conway, who introduced the idea in 1967. His original wording was:
[O]rganizations which design systems (in the broad sense used here) are constrained to produce designs whi...
Is the communication structure not just the OOP?
No, the coposition I mean.
Encapsulation is the drawing of lines for the boundary of communication. It's the most fundamental aspect of OOP.
Everything within works directly with each other. Everything from the outside must go through the public API.
So combining this with Conway's Law we can predict that objects will form around the groups of people that have each been given some responsibility.
So if at Microsoft they have an audio team they will have a public audio API through which other teams interact with their code. That is an object (there may be more than one).
So for your case, who is using it and who is working on it directly?
Imagine you are making a tiny library for others to use. What do they care about in terms of its API? And what do you care about internally that makes it as easy as possible for you?
And how might that change if at all?
There is also another method that works for sure. And that is try something and then submit to your team and measure "wtf's per minute" when they use it. Then iterate until it's low.
( i just want to mention https://youtu.be/5IUj1EZwpJY )
There are promising candidates for "laws" governing computer software. But are there any specifically for software architecture? In this lecture, I describe the only viable candidate I've so far seen.
Originally given to the School of Informatics of the Technical University of Madrid.
Ok
When I think about it in the way of APIs and wanting easy access
When your just ensuring easy public accessibility
I think it makes this all alot more simpler
