#algos-and-data-structs

1 messages · Page 73 of 1

opal oriole
#

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.

#

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)

haughty mountain
#

introsort is probably a disadvantage anyway

#

for purely random data

opal oriole
#

Maybe, yeah, depends on the data used to test. Also are the C++ sorts being used stable or not.

haughty mountain
#

afaict it's just random floats

#

which should be a perfectly fine case for quicksort

#

the difference is actually even larger on my desktop

opal oriole
#

For intro vs quick?

haughty mountain
#

numpy vs C++ stdlib

opal oriole
#

What flags did you compile with?

haughty mountain
#
clang++ -O3 -march=native -std=c++23 sort.cpp -o sort
#

I see 30x faster, which feels kinda suspicious

opal oriole
#

Hmm, stdlib can be implemented however.

#

It used to require quicksort.

haughty mountain
#

the stdlib is an introsort

opal oriole
#

Maybe, not required.

#

Only fixed in 2021...

opal oriole
#

So if it's giving nonsense.

haughty mountain
#

the C++ code also does a copy, but the numpy code also returns a copy, so that should be mostly fair

haughty mountain
opal oriole
#

And yes, I think LLVM was behind on it then.

haughty mountain
#

when did this actually come info effect?

#

11?

opal oriole
#

Not sure, 2016?

#

So later than that.

haughty mountain
#

idk if the last edited means it went in after that

opal oriole
#

Yeah, but at least 2016 earliest right?

haughty mountain
#

2007-2016 is a pretty big gap

haughty mountain
opal oriole
haughty mountain
#

accidental copy where?

opal oriole
#

std vector assignment or return from function.

#

Return is maybe a whole copy, if the compiler feels like it.

haughty mountain
#

the assignment is meant to be a copy

#

and the return should be guaranteed to optimize away I think

opal oriole
#

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.

opal oriole
#

Looking at some Github issues they added OpenMP support and maybe multithreading to qsort?

kindred portal
opal oriole
#

Not seeing how there is a 30x difference though, seems too much.

#

Even with scalar floats vs AVX512.

haughty mountain
#

so C++11 is probably a reasonable guess

#

too late for 03

kindred portal
#

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.

nimble canyon
#

Would this be the right area to talk about caching systems?

opal oriole
#

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.

kindred portal
#

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

opal oriole
#

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?

zenith raft
haughty mountain
#

the topical channels aren't really python specific

opal oriole
zenith raft
#

oh they might not be discussing python here?

nimble canyon
#

Would this be the appropriate place to discuss a cache system that doesnt use traditional LRU, etc?

haughty mountain
zenith raft
opal oriole
zenith raft
#

I think that's too deep for me. I stay at pythonist level.

opal oriole
#

Although usually more layers needed.

haughty mountain
#

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;
}
opal oriole
#

Yes.

haughty mountain
#

it's basically identical

#

+- a few %

opal oriole
#

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.

haughty mountain
#

I think you need to explicitly opt in to using the clang one

stiff quartz
#

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

opal oriole
#

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.

opal oriole
#

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.

opal oriole
#

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.

opal oriole
#

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.

haughty mountain
restive stone
#

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.

narrow mica
restive stone
#

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

narrow mica
restive stone
#

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

narrow mica
restive stone
#

Well, its all about linking one node with another but my thought gets unlinked, to be honest

narrow mica
narrow mica
restive stone
#

This is simply insertion on head. I understand but, it gets too complicated when I have insert, delete dynamically

narrow mica
narrow mica
restive stone
#

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

atomic summit
#

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

haughty mountain
#

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

hoary comet
#

3

cursive mantle
#

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 😅

austere sparrow
#

huh, why doesn't items match strings

austere sparrow
#

This? ```py
case [*_] as items:

#

seems like it doesn't

cursive mantle
#

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 😅

cursive mantle
#

So no actual iteration of the thing being matched 👍🏻

fiery cosmos
#

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.

agile sundial
#

clrs

fiery cosmos
#

imma check that out. thanks

#

is it true though ? do you have to be good at math ?

agile sundial
#

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

fiery cosmos
#

good. thanks

opal oriole
fiery cosmos
#

i see. did some research. thank you. that was a great recommendation

nova barn
#

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! 😊

whole warren
violet obsidian
#

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
whole warren
#

!e

item = 2
a,b = item[1]
halcyon plankBOT
# whole warren !e ```py 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 "/home/main.py", line 2, in <module>
003 |     a,b = item[1]
004 |           ~~~~^^^
005 | TypeError: 'int' object is not subscriptable
whole warren
#

@violet obsidian

mossy cloak
#

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.

atomic summit
#

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

haughty mountain
atomic summit
haughty mountain
#

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
royal basin
#

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

naive scroll
#

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
naive scroll
azure belfry
#

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 ?

naive scroll
#

You could run it on google cloud or aws.

tacit shale
#

how i could read numbers in files

ashen echo
#

Is there anyone looking for a dev

stiff quartz
#

!e

import sys
print(sys.float_info.max_exp)
print(1e+500)
halcyon plankBOT
stiff quartz
#

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

fallow viper
stiff quartz
#

Yeah I realized, I'm dumb

#

thanks

gloomy lichen
#

No don't msg me

#

Weird ahh

#

You're weird go away

#

No wtf

#

Blocked

severe stratus
#

how to deploy ml model with proper frontend and backend(using PyCharm & html,css) free of cost
anyone help?

atomic summit
#

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
stiff quartz
#

Sets are slightly slower than arrays

#

Without getting technical, that’s just inherent to how a hash set works under the hood

unborn crane
#

Which Algorithm is the best to use for large data sets and time complexity?

#

In sorting and searching algos?

austere sparrow
#

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

manic crow
narrow mica
limber dagger
#

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?

dreamy tulip
# limber dagger I'm tackling the cesar cypher, but I could choose between 2 algorithms One whe...

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 = }')
limber dagger
dreamy tulip
limber dagger
# dreamy tulip You should say "two implementation of the algorithm" rather than "two algorithms...

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

dreamy tulip
limber dagger
# dreamy tulip Objective: Put beans in water. Method 1: Pour whole lot in one go. Method 2: ...

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.

umbral mountain
glacial tapir
#

Anyone wanna takes a shot at this?

odd crater
#

does learning algorithms & data structures help with algorithimic thinking/problem solving skills?

dull temple
#

algorithms is literally problem solving so yes

stable jacinth
#

@wintry nymph your message was removed for advertising / soliciting

north nacelle
undone geode
#

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.

spare arrow
urban sapphire
normal hull
#

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?

modern gulch
# normal hull Hey guys, I'm new to grinding leet codes. Will solving easy problems help me imp...

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.

normal hull
grand aurora
lusty stone
#

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.

stiff quartz
#

Or android stuff

weak whale
#

I’m looking for a partner on a project. If any online likes math and science then hmu

empty yarrow
#

can the stream returned by socket.socket.makefile be used as the source buffer for struct.unpack_from

#

oh they're completely different things

wind depot
#

Hi

tiny wren
#

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

tulip turtle
#

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?

serene dome
#

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];

}

narrow mica
open dew
#

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
rigid swift
#

Has anyone here made any algorithm trading bot?

rare laurel
rigid swift
native patio
#

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

violet field
#

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

fiery cosmos
open dew
#

it took damn near a week to get that response. i've already long since fixed that.

fiery cosmos
#

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)
deft pewter
#

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

mint jewel
#

since each column has two ones I want to say someone screwed up and gave you the incidence matrix instead

deft pewter
#

I still don't understand the path matrix

haughty mountain
valid ledge
#

hii

#

can anyone tell me a good DSA course

rigid swift
#

does anyone use lightweight_chart in python ?

past relic
pseudo forge
halcyon topaz
halcyon topaz
past relic
brave jolt
#

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

narrow mica
past relic
rigid swift
#

how can I have somthing like this in VS code ?

junior onyx
#

it's called the "minimap"

rigid swift
junior onyx
#

if you are in VS Code, and press Ctrl-Shift-P, and type "minimap" you get a command to toggle it

rigid swift
junior onyx
past relic
rigid swift
#

Tradingview

#

There you can trade stock, Forex... All kinds of instruments

#

And development of your own algorithm trading bot

#

In pine script

blissful cypress
narrow mica
#

which algorithm do ppl use to match people names?

#

I'm looking at fuzzy matching but there's a lot of fuzzy matching algos

past relic
#

Brother what is objective of the book (black hat python)

calm bridge
narrow mica
#

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

wet radish
#

Guys i got a problem

calm bridge
exotic linden
#

whats everyones favorite algorithm

supple edge
#

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

halcyon plankBOT
supple edge
#

I was thinking of using BFS but I'm here to learn 👍🏿

earnest pumice
#

hi

harsh iron
#

HELP me please on pv

stiff quartz
#

pv?

harsh iron
#

yes

harsh iron
frozen surge
#

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.

azure inlet
glacial ice
#

By the time youre fluent with these you will figure where to move on to

frozen surge
agile sundial
onyx karma
#

Is there an easy printable Python Docs?

#

Like a PDF

versed flicker
#

hi

onyx karma
#

I made a graph to see the difference growth rate of BigO()

stiff quartz
#

LogN is terribly represented

#

It should be much lower

onyx karma
#

Thanks, will fix

onyx karma
#

Hello! Anyone also doing Leetcode or solving Programming Problems more generally?

past relic
#

Is it compulsory to learn time and space complexity with python?

regal path
#

No, but it comes in handy

#

But if you're learning dsa time/space complexity is a must, regardless of the language

onyx karma
#

True, knowing about Big O complexity will help you a lot!

long copper
#

whats the difference between algorthim compelxity and performance characteristic

mint jewel
long copper
sour sphinx
shut kernel
#

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

mint jewel
#

It's recursion, yeah.

#

Two parts per step:
xs[0], *comb(xs[1:], n-1) and
comb(xs[1:], n) IIRC

shut kernel
#

Thanks. I'll use that. Though recursion and duplicating all the entries seems ... inefficient.

mint jewel
#

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.

regal path
shut kernel
shut kernel
amber lion
#

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.

shut kernel
amber lion
amber lion
#

nvm i fixed it

sour sphinx
languid herald
sour sphinx
young moat
#

Hi (i apologize for my bad English)

sour sphinx
sour sphinx
languid herald
# sour sphinx 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

sour sphinx
naive oak
#

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
onyx karma
# sour sphinx Yup me

Nice, how many problems did you solve? Do you think LeetCode a good way to practice and learn about DSA?

onyx karma
sour sphinx
onyx karma
sour sphinx
onyx karma
#

Yeah

sour sphinx
# onyx karma Yeah

Yup I tried but I can't say consistent due to hackathons, classes, projects etc

onyx karma
sour sphinx
onyx karma
sour sphinx
onyx karma
#

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

shut kernel
naive oak
#

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)
halcyon plankBOT
naive oak
#

for the purposes of AoC day 10, you don't need to store each entire combination, just each of the intermediate results

shut kernel
sour sphinx
amber lion
#

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

shut kernel
#
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."
amber lion
#

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

shut kernel
#

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

amber lion
#

Traceback (most recent call last):
File "main.py", line 29, in <module>
File "main.py", line 26, in create_character
File "main.py", line 4, in createdot
NameError: name 'strength' is not defined

#

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

shut kernel
#

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' 
ocean cloak
#

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.

frigid cape
#

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?

cinder frost
#

I feel like it should be obvious but idk what I am having troubles with.

frigid cape
#

I m doing it

onyx karma
civic patio
#

Hi asmund101

#

I can do it

frigid cape
onyx karma
frigid cape
#

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?

onyx karma
onyx karma
fiery cosmos
#

This aint introductions maam

#

Also ur username says aisha and yr name is maryam pithink

keen owl
#

Aisha, are you a bot?

misty cedar
# amber lion I can't figure out how to do this last step

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.

  1. 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.

  2. 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 😊💪

patent junco
#

someone knows how to get few max values and not only one?

#

like 2 or 3 for example…

misty cedar
patent junco
#

i mean - i have 2d array

#

and from ALL values i want to get 2 or 3 values which are repeating most

misty cedar
#

Oh. From numpy or pandas?

patent junco
#

whatever, i just do a small script to parse some csv…

#

and i want like 2 or 3…

#

or maybe even 4 if there are same-common ones

misty cedar
#

Give an instance 🥲

Say:
[[1,2,3,4,7], [3,5,8,6,4]]

So you want 3 and 4 or 3 and 3?

patent junco
#

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

misty cedar
#

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.

  1. 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

  1. 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 😊😊

patent junco
#

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;
misty cedar
#

Try using len(lister)

haughty mountain
icy parrot
#

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

gilded cloud
#

1 leetcode question a day keeps the biches away

tawdry path
#

Hi, do you know any channels where they only study algorithms?

#

Not necessarily python

sharp meadow
#

Hey

void escarp
#

!kick 1166699033545416764 homophobia is not welcome here

halcyon plankBOT
#

:incoming_envelope: :ok_hand: applied kick to @copper crypt.

empty yarrow
#

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]

empty yarrow
#

solved

#

given I, D, n:

M[np.arange(n),I.transpose()] = D.transpose()
``` or something
lilac cradle
#

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

lilac cradle
#

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

  1. if a node's calculated result value would be greater than the max allowable value, it's invalid
  2. if a node's parent is invalid, that node is invalid
  3. 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

brazen turtle
#

The key here would be the deciding factor to check the value is, if it's less than MAX which is it + maxSubtractionValue

lilac cradle
#

mk i was thinking something like that but i wasnt sure

#

thanks

brazen turtle
#

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

mint jewel
haughty mountain
mint jewel
#

yeah, the question is more so whether traditional graph traversal will work, or if you actually need to do algebra

haughty mountain
#

!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))
halcyon plankBOT
haughty mountain
#

Maybe there would be benefit from a heuristic, idk

#

It would allow ignoring the really small stuff that is just wasting time subtracting

mint jewel
#

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

haughty mountain
mint jewel
#

well, that's what I was asking about in my reply

haughty mountain
#

for the limits given a badly writtem maybe correct A* takes like a few seconds for target 123456

mint jewel
#

yea, if it's 8,16 you can just graph traverse this

#

maybe trim some obviously futile nodes

haughty mountain
#

Dijkstra does not enjoy 123456

haughty mountain
#

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)
haughty mountain
#

(you might need to consider all the multiples where the sub cost to get there is equal)

haughty mountain
#

(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))
halcyon plankBOT
lilac cradle
mint jewel
#

I see, then see what fenix was cooking above

lilac cradle
#

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

haughty mountain
#

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

split garden
#

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)

split garden
#

333x faster than pycryptodome now

regal spoke
haughty mountain
#

Ah, it can be a bfs now yeah

regal spoke
#

Also, isn't it easier to start from the target

haughty mountain
#

I had cost +1 and +2 for a while

regal spoke
#

And use division and +

#

Going backwards seems nicer

stiff quartz
#

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

ember heath
halcyon plankBOT
nova berry
#

I dont use python often js wanna find scripters, anyway is this a good calculator?

stiff quartz
nova berry
terse hinge
nova berry
mystic marsh
#

Guys is there a chance for create a subtitle adder for yt videos?

leaden inlet
#

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 ?

haughty mountain
#

not the right channel

split garden
split garden
#

What gpu algos would be fun to implement?

mint jewel
#

maybe some of the noise algorithms?

agile sundial
#

some linalg algorithms would be fun

mint jewel
#

Marching cubes can look very neat

haughty mountain
split garden
#

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

haughty mountain
#

I'm more curious if you actually win performance over cpu for these loads

split garden
#

It's faster on gpu after about 16^4 values

#

Including cpu readback

haughty mountain
#

that would be the window size?

split garden
#

Yeah

#

Works for audio with some padding

haughty mountain
#

that's way higher than what's typically used, isn't it?

#

I'm used to see fft sizes in the 256-4k range

split garden
#

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

haughty mountain
#

I can see it being useful for bulk processing

#

I.e. compute for a bunch of windows at once

split garden
#

I want to use it to make SDR waterfalls next, need to figure out how to write a row to a texture each frame

haughty mountain
split garden
#

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

haughty mountain
#

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)

red axle
#

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

stiff quartz
deft spruce
red axle
#

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

uneven temple
#

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.

mint gate
split garden
#

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.

spare arrow
#

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.

haughty mountain
#

in O(len(nums)*target)

primal fossil
#

How do I start DSA? Whenever I start DSA, it stops at the array.

hexed gust
#

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. nekogocrazy

hexed gust
primal fossil
pallid hound
#

anyone has experience with AI Agents? I need an advice

modern gulch
devout escarp
#

Greatest command known to man: claude --dangerously-skip-permissions

spring gulch
#

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

chrome pewter
#

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

primal fossil
potent chasm
#

Hello

humble parrot
#

hey yeah

thick dawn
#

im new to algorithms, i cant figure out how to stop it from returning [0,0]

#

nvm figured it out

devout escarp
#

4.5 mogs codex

gritty python
alpine tide
# devout escarp

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.

cinder frost
#

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?

zenith solar
#

damn

#

i got a lot to learn

haughty mountain
#

If you have a general sorting function you would need to at least look at all the elements

cinder frost
haughty mountain
#

Constant time is not possible, yeah

zenith solar
#

where do i start off learning python?

haughty mountain
#

Unless you're talking about the useless case where you already know the list is sorted

haughty mountain
halcyon plankBOT
#
Resources

The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.

cinder frost
zenith solar
cinder frost
#

i need to spend less time with my friend, wasted 10 minutes thinking about it 😭

agile sundial
#

that's crazy

alpine tide
#

!ban 1226658402910998528 Wildly inappropriate response to being told to stay on topic.

halcyon plankBOT
#

:incoming_envelope: :ok_hand: applied ban to @devout escarp permanently.

next timber
#

does a* belong here? should I just use a library for it?

mint jewel
elfin wedge
#

can anybody explain to me this sorting algorithm in a understandable way.

mint jewel
#

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 ^

elfin wedge
junior swallow
#

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

radiant spear
lilac cradle
#

pixel sorting is cool

gaunt harbor
#

Hi

#

Can anyone help me In making a presentation

stray spruce
stray spruce
#

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 .

agile sundial
#

it is not bubble sort

stray spruce
#

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.

agile sundial
#

it is insertion sort

stray spruce
#

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.

agile sundial
#

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

deft spruce
#

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]
agile sundial
#

what do you think the time complexity is? why?

deft spruce
haughty mountain
#

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?

deft spruce
haughty mountain
#

If the third maximum does not exist, return the maximum number.
god that's an ugly thing to do

junior swallow
#

@modern gulch
how would I search for the picles that this is locaed

modern gulch
stray spruce
fiery cosmos
#

Hi

chrome pewter
#

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

haughty mountain
#

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

chrome pewter
#

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

haughty mountain
# chrome pewter Oh really?

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)

chrome pewter
#

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

chrome pewter
#

for the perfect sol (your method) we could create an array of size 3

#

and update maxes

haughty mountain
#

the awkward case is the case with 2 unique

naive oak
# haughty mountain so the point is that 3rd largest does not generalize smoothly to "pick the maxim...

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

chrome pewter
haughty mountain
#

one unnatural special case is worse than none

naive oak
#

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

haughty mountain
#

a sensible choice would also have been to return nothing

naive oak
#

yeah

#

like either fully separate the cases, or squish them together

#

don't squish them somewhat together but two elements is a separate case

haughty mountain
deft bluff
#

Hello

#

how to learn dsa

#

and how to apply dsa

#

I am learning python

opal oriole
# deft bluff and how to apply dsa

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.

deft bluff
opal oriole
# deft bluff 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.

#

See the pins for DSA resources.

deft bluff
#

Thank you

chrome pewter
#

If you like watching videos

#

But I'd always recommend using a book

chrome pewter
spare arrow
#

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.

swift coral
#

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 self to the dict with key hash, 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?
mint jewel
#

you would have to override __new__ for this no matter what, __init__ must return None

#

I'd probably prefer a factory method

swift coral
swift coral
#

cool, thx!

spare arrow
#

Just create a class variable “sequence_num” with a class method you call in new() that increments by 1and returns the current value?

lilac cradle
#

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

lilac cradle
#

the color represents the digit, transparent = 0, and then the darkest color = 1, and the lightest = 9 (or whatever N is, minus 1)

halcyon plankBOT
chrome pewter
#

Plus it looks like a prefix / suffix sum from both ends to the middle

lilac cradle
chrome pewter
#

Why 18:10 and not something else

#

Etc

lilac cradle
#

idk

#

mod 10 seems to take a lot of steps for some widths

#

10:10 is something like 40,000 i think

haughty mountain
haughty mountain
#

what you get if you start with an infinite array 1000...

wise bridge
#

I am just starting to learn python

#

How popular is list comprehension?

dreamy tulip
# wise bridge How popular is list comprehension?

What do you think is the most likely answer?

  1. It was included as part of the language because nobody would ever use such a thing.
  2. It was included as part of the language because some people thought it would be a handy thing to have.
wise bridge
#

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?

agile sundial
#

it would be hard to find a reasonably sized project without one

swift coral
haughty mountain
#

actually you probably could just slap a cache on things

#
@cache
def get_object(the_hash: str):
  return MyObject(the_hash)
spare arrow
swift coral
steep comet
#

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

eager swan
#

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?

spare arrow
# steep comet

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."

frozen tapir
lilac cradle
#

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
stray spruce
lilac cradle
#

hm, its for interior angles
i might be wrong on that tho ill have to take a look when i get home from class

stray spruce
stray spruce
stray spruce
#

Anyhoo, I fixed the code.

halcyon plankBOT
# stray spruce

Please react with ✅ to upload your file(s) to our paste bin, which is more accessible for some users.

stray spruce
#

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.

dull lantern
haughty mountain
#

there is a very dumb bug there

#

hint: your program will always return true

#

because ||an int and a set are never equal||

deft spruce
#

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

lucid zinc
#

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.

plain glade
stray spruce
#

Plain old PEBKAC.

still mulch
#

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

inland basin
#

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..

vague crater
empty yarrow
#

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

narrow mica
#

try something like line_profiler or scalene or whatever

empty yarrow
#

oh yeah i forgot line profiler exists

dull lantern
#

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

empty yarrow
#

thr constant term b was unexpectedly large

#

about 30 microseconds

steep comet
#

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

mint jewel
#

I have seen this ages ago advertised for smart watches

#

it does work tho

vocal gorge
stray spruce
#

Or whatever it is called that Python does when preparing to execute a code.

empty yarrow
#

and I'm averaging 3 trials

#

also my timing only takes into account the function call time as I do a control test

vocal gorge
#

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

wary pebble
#

hello i am new here and i want to learn coding what should i do -

dreamy sable
tidal mason
#

yoo can i get help

swift coral
fleet vapor
#

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..?

gilded fulcrum
lilac cradle
#

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

lilac cradle
#

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

desert chasm
#

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?

modern gulch
haughty mountain
real whale
gilded fulcrum
fierce warren
#

Hello how to learn python

gilded fulcrum
swift coral
#

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]
gilded fulcrum
real whale
thick granite
swift coral
#

Doesn't that make a list of 16 lists? (I don't have a REPL to test atm)

stiff quartz
#

It makes a list of 16 strings

stiff quartz
#

!e

board = [" "] * 16
print(board)
halcyon plankBOT
swift coral
#

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

agile sundial
swift coral
#

Aha! Thank you, that makes sense!

fiery cosmos
#

@whole marsh COME DM

mossy spire
#

What is dynamic programming

mint jewel
# mossy spire 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

mossy spire
#

Thanks, understood

opal oriole
#

They wanted to avoid mentioning "mathematical optimization" (which it falls under).

#

(Now also a name given to a specific programming technique based on it)

gritty tundra
#

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?

mint jewel
# gritty tundra Anyone know the name of the algorithm that takes lots of pixels and groups them ...

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...

gritty tundra
#

Mmmm not that one

#

Like a 2D run length encoding but also specifying the top left corner

#

specifically for bitmaps

#

Greedy meshing

clever sorrel
thick granite
#

behaviorwise, at least

#

implementation-wise, it's faster than actual repeat concatenation

undone geode
# gritty tundra Greedy meshing

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.

undone geode
undone geode
#

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.

haughty mountain
#

i.e. very much like pixels

marble delta
#

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.

marble delta
#

yeah this is his new gig. He knows his fate is sealed

stiff quartz
#

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

lilac cradle
#

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?

gritty tundra
#

greedy mesher

#

go up until change, go right until change, delete the old pixels, continue going up

lilac cradle
#

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?

haughty mountain
#

Something like greedy meshing will give you pretty good results. Fully optimal things would end up as solving some graph problem

supple obsidian
#

i made a data serialization structure and realized i have no usecases for it right now

worthy lodge
#

nice work

supple obsidian
#

send a friend requesti

burnt swift
#

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

agile sundial
#

define "primitive operation"

supple obsidian
#

naively, it contains two, assignment and addition

#

however it also would require at least 3 lookup calls to get/set the variables

sullen timber
#

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
supple obsidian
sullen timber
#

Im familiar with sorting, Im just trying to lay this all out in SOLID

#

seperating all the functionality and crap

supple obsidian
#

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

sullen timber
supple obsidian
opal oriole
#

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.

opal oriole
#

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)

opal oriole
sullen timber
#

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.

opal oriole
#

SOLID, when applied without context, is preparing for a problem you will never have.

#

And any such generalization / refactoring has a cost.

sullen timber
#

I feel it has a few advantages for my project, since I essencially am making market agent objects

opal oriole
#

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).

sullen timber
#

For example, if I had identical, very large finance data structures

opal oriole
sullen timber
swift coral
#

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

sullen timber
#

I appreciate the advice and lecture, I havnt used OOP much in the past

opal oriole
#

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.

sullen timber
sullen timber
opal oriole
# sullen timber You are right yes, thats exactly what I feel I'm doing right now lol

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).

sullen timber
opal oriole
#

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...

sullen timber
#

No, the coposition I mean.

opal oriole
#

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.

rare laurel
# opal oriole So combining this with Conway's Law we can predict that objects will form around...

( 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.

▶ Play video
sullen timber
#

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