#algos-and-data-structs
1 messages · Page 26 of 1
But I do not need to use that to solve for the number of comparisons right?
I found this online, and was wondering why they stopped at T(1)=0
how many comparisons do you need to merge sort a one element array
Oh, I have not answered this as well
1? Since it compares itself?
why would it compare itself with itself
My bad, I meant 0. There's only 1 element, so it has nothing else to compare to
exactly
What about this though? Is it just n(n-1)/2?
yeah
Thank you!
Hello guys, can anyone give direction on how i can work with this xml type of files. (there are files i send, and then files i receive).
same service run on API but i didn't yet access that,
Is there any method i can use ? or library ?
Hey all, I have a question, given a grid like this; grid = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 4, 0],
[0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0],
[0, 1, 1, 5, 1, 5, 1, 0, 1, 0, 1, 0],
[0, 0, 1, 0, 0, 5, 1, 0, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0],
[0, 3, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
How would i go about finding the shortest path from the start point 3 to end point 4, while passing by all the 5? (1 indicates open path, 0 indicates closed path, 5 indicates must pass points)
Have you figured out how to solve a problem you wanted to solve before? This one is harder.
Let points 3, 4 and 5s be called checkpoints. Run bfs from every checkpoint to compute the shortest path between every pair of checkpoints. Build a complete graph which vertices are checkpoints and edges have a weight equal to shortest distance between a pair of checkpoints. Solve the traveling salesman problem on that graph. There is a fairly simple dynamic programming algorithm in O(n^2 * 2^n). In total it would work in O(n^2 * k + k^2 * 2^k) where k is the amount of checkpoints.
yes, i solved the previous one
hi all may I what is the best stop condition for left hand wall follower algorithm, assuming there is no path to the destination?
When you return to the original location.
thank you very much i solved my problem
I wrote an algo that produces a perfect maze as such but may I know how do I convert the perfect maze to a non perfect one so that there are more than one possible path from one point to another. This is to increase the difficultly of the maze solving.
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0],
[0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Thank you
Can you punch a few holes in random places? Which generation algorithm you used specifically?
back tracking
Also wdym by "the difficulty of maze solving"? Are you trying to heuristically increase humans' confusion?
In that case you can randomly destroy the wall that goes to the dead end you have already traversed if the distance to the start of the maze between the dead end tile and your current tile is relatively small.
The constant parameters will require a little tweaking, but I think this can be done. Tbh it's purely experimentation at that point. Just try out stuff and see what works for you.
Hello, may I ask if bitmask works?
Do you mean "how"?
Yea bitmask for the dp right?
Bitmask (or bitset) is basically a set of numbered objects represented by bits in the number. For example if you have 3 objects A, B, C, you can represent a set, say {A, C} as 0b101 (binary number 1*4 + 2*0 + 1*1 = 5), set {A, B} as 0b011, or {A, B, C} is 0b111. You may notice how if object k is in set, the k-th lowest bit is set to 1 and vice versa. This is a very compact representation which usually only takes a few bits, which fit in a single integer, allowing to do some sick stuff with it. Want to intersect 2 sets? Do binary and (pipe operator: |). Want to add element numbered k? bitset |= 1 << k. Empty set? Easy, just 0. You should probably read about bit manipulation yourself, there is lots of cool stuff out there. In the dp you need to use bitmasks as indexes in your dp. You can think of it as map which uses sets of vertices as keys, but more efficient.
By the way, I said that DP algorithm of solving TSP is "fairly simple" but if you struggle with it I suggest you using the trivial algo which works in O(n*n!). If your amount of checkpoints is about 10 it would take about one second. The DP approach usually works in under a second if n < 20. Is implementing it worth it? Sorry for possibly sending you in the wrong direction. I should have asked constraints first.
The trivial algorithm is super easy: iterate over every permutation of 5s in the graph you build and trivially calculate the length of the path from start to end visiting every 5 in the order of permutation.
Thanks for this, if i wanted an O(n^2 * 2^n), is bitmask the way to go?
Yes, I don't know a simpler way
Thank you
By the way for my previous problem, I tried solving in another way because I wasnt so familiar with your proposed solution: Use BFS to label each node with distance from start node, then use DFS to find all paths from the start node to the end node such that the depth increases for each step of the path
I see. I believe that's slightly slower because you do extra work of going into dead ends, but it definitely works. Good job!
Yea, do you happen to know what is the O
In best case O(n) where n is the area of a grid if there is a single path to the end. In the worst case it's O(ans), same as what I proposed. There isn't a way to solve it faster though. By the way I said that I believe ans is always less than n^2 on grid graphs, but I was super wrong. It's more like O(2^sqrt(n)) (this is achieved if there is no walls at all, and start and end are on the opposite corners of the map) but I'm still unsure if this is the real upper bound.
Edit: no sqrt in big-O, I messed up my math
Edit 2: yes sqrt in big-O, but in a different place. I messed up my math again!!1
Thank you very much
I'm super bored anyway so glad to help
Hey, for the problem of TSP, would you consider that problem to be a generalized version of TSP?
Not really. In this problem all the weights are integers defined by distance on the grid. If you allow tiles to have a weight-like property then maybe it is a generalization of some sort.
This is an opposite - a specialized version of TSP
So in my case it is a specialized version?
Seems like it
Hey, sorry another question, is there a more efficient method to solve the 2 problem scenarios i mentioned in terms of time complexity?
No one knows, of course, if P=NP there is a polynomial algorithm for sure.
As far as I'm aware, (big-O wise) there is no known algorithms for solving TCP in o(2^n). There are a lot heuristically more optimal algorithms though.
I don't know if your specific problem can be solved faster though. It probably can be reduced to some faster ILP, but I don't know how. But it's definitely NP-complete. You would be able to find Hamiltonian paths in planar graphs otherwise, which is NP-complete problem.
So if P != NP the there definitely would be an exponent in the big-O, the base might be smaller than 2 but I don't know how to achieve this. I also don't know if the power itself can be smaller than k.
Thanks, I have around 8 checkpoints as of now, and it takes like around 5 seconds for me to run, is this normal?
If your algorithm is O(n^2 * 2^n) and grid size is less than 1000x1000 - No. It should run much faster.
Can you help me see my program please
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] in [3,4,5]:
checkpoints.append((i,j))
m, n = len(grid), len(grid[0])
graph = {}
for i in checkpoints:
graph[i] = {}
def bfs(start, end):
queue = Deque([(start, [])])
visited = set()
while queue:
curr, path = queue.popleft()
if curr in visited:
continue
visited.add(curr)
x, y = curr
for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
neighbourX, neighbourY = x + dx, y + dy
if 0 <= neighbourX < m and 0 <= neighbourY < n and grid[neighbourX][neighbourY] != 0:
if (neighbourX, neighbourY) == end:
return path + [(neighbourX, neighbourY)]
queue.append(((neighbourX, neighbourY), path + [(neighbourX, neighbourY)]))
return []
for i in checkpoints:
for j in checkpoints:
if i != j:
graph[i][j] = bfs(i, j)
# employs a recursive backtracking approach (the tsp function), which generates all possible permutations of visiting the checkpoints and finds the shortest path using Breadth First Search
def tsp(mask, pos, path):
# If all the checkpoints have been visited, return the path
if mask == ( 1 << len(checkpoints) ) - 1:
return path
res = []
for i in range(len(checkpoints)):
if not (mask & (1 << i)):
new_path = path + graph[checkpoints[pos]][checkpoints[i]]
tsp_result = tsp(mask | (1 << i), i, new_path)
if len(tsp_result) > 0 and (len(res) == 0 or len(tsp_result) < len(res)):
res = tsp_result
return res
def shortest_path(grid, start, end, checkpoints):
result = tsp(1 << checkpoints.index(start), checkpoints.index(start), [start])
return result
print(shortest_path(grid, (7,1),(2,12),checkpoints) )```
What's the grid size?
grid = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 5, 1, 5, 1, 0, 0, 0, 0, 0, 1, 4],
[0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 5, 1, 0, 1, 0, 1, 0, 0, 0],
[0, 1, 1, 1, 5, 1, 0, 1, 0, 5, 0, 1, 0],
[0, 1, 0, 1, 1, 0, 0, 1, 0, 5, 0, 1, 0],
[0, 3, 1, 1, 1, 1, 5, 1, 5, 5, 1, 1, 0],
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Your TSP works in O(n*n!). It stateless and does not store the intermediate results. It basically checks every single possible path and returns the shortest path among every path... The easiest way to fix it is to get rid of path from function parameters (to make tsp only return the path from pos to the end) and add @functools.cache decorator to tsp. It will do memoization for you and every shortest path is going to be only evaluated once and cached.
Was tcp generated by Chat-GPT?
yea i tried but no idea so needed some help
Sorry, how would I do that? not too sure about memoization
Rewrite tsp to make it not take the prefix of the path as a parameter (find a way to not prepend path at new_path and tsp itself), add @functools.cache to tsp.
Thanks, lemme try
So in my problem bitmask is used to keep track of the checkpoints visited. This is to improve efficiency and compactness right?
Yes
Hey, may I ask for the BFS for calculating shortest path between every pair of checkpoint, would it be faster if A* is used?(for tsp)
Depends on the maze, but BFS should be faster. In you implementation you do bfs between every pair of checkpoints, but you don't have to. Start a bfs from every checkpoint, use distances it calculated to find shortest path to every other checkpoint. A* can't do that - it works if there is only one destination.
Okay so heres the thing, i have working A* code right now, should I just swap the BFS into an A*?
Why not just try both and see what's faster?
Failing is too cheap to not try
ur right thanks
Hello there, we found a puzzle or some kind of code regarding people that are probably working (or being in recruitment process) for Polish or Russian special services - military intelligence, support and counterintelligence. No one seems to be able to solve it… maybe you could?
80-g. d-4n 8r. 0d- 5ky1 - Uptime Development, Przasnyska,
49n-1e. 57-k. 4 K. r-ó 1, Oerlikon, 83773R 3n3rgy,
M-1k. 01-4. j 5. 0n-d. 3j, Traveller, 5F,
Hu-b. 3-rt J. 4-m. r0-z. 1k - exInter Polska, Włodarzewska,
K-4r0. l1n-4 L1-w1ń. 5k4 BAT, Wyścigi Służewiec,
94. try-cj. 4 Ur. b4-n. 3-k, in USA,
L-y. d1-4 6. 4-vr. y-l1. v, 83773R 3n3rgy,
1-9. 0r 5-k4. w1-n. 5-k1, Fly Focus.
M. 4-6d. 41-3n. 4. J4-r. 0-57, 83773R 3n3rgy,
K. 47- 4r7. yn4 6 4-w3 nd. 4 - Leasys, Garwolin, Accounting,
J04nn4 5k4tu15k4, Zoofocus, Makolągwy,
94w. 3ł C. hr-u 57. c7 3-w 5. ki - PIT RADWAR, 6PS-2OO-SO2,
D0m1n1k4 Kn1tt3r, D0D0, Traveler, in Costa Rica,
K4r. 1n-4 R3. 7 n-3r, Actress, WUM,
J. 04-n n4 (N. 4-6ł. 0-n3. k) C-up. r1-4. k, Legal,
M1ch4ł 4d4m14k, BAT, Turek,
Jul14 Ł0w1ń5k4, Warsaw Film School, Bruna.
You found it where?
Not sure this belongs in this channel
l33t sp34k anyone?
3 → E
8 → B
...
best sources to learn DSA?
1337 H4XØR?
Hi everyone! Can somebody recommend books/courses about algos and data structs while I learn python syntax(googled suggestions differ vastly)? Sorry if the info about it is presented somewhere on this server.
@pliant oyster @modern coral don't overthink. If you read a book try to actually read it as little as you can and practice more. Usually when studying with a good resource people begin to procrastinate and say to themselves "I will first finish a book, and then practice once I learn the basics", but even after finishing the book they realize how they learnt nothing and become frustrated while practicing. Aside from that,
!resources
The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.
Thanks @outer rain !
I wonder whether it is possible to write a decorator to convert any recursive function into an iterative one

so as to preserve readability
You'd have to have some way of distinguishing the recursive calls from the non-recursive ones.
Maybe by inspecting the call stack?
I wonder how much black magic / #esoteric-python would be needed to achieve this
that would be super cool
well, the first step would be to implement our own call stack that listens to when the wrapped function gets called
actually it would need to be a call graph
You can build a call graph, find calls which cause recursion (mutual recursion exists so you will have to deal with that). Then we must somehow analyze the function to build, say, a state machine. For example
def phi(x):
if x <= 1: return x
return phi(x - 1) + phi(x - 2)
has three states. Before first recursive call, after but before the second, and after the second. So you can unwrap it into something like this:
# we will need a return stack with values which functions return,
# a state stack, which is similar to return addresses in normal recursion
# and a local variables stack which is not really used here
def phi(x):
state = pull_state_from_a_special_stack()
match state:
case 0:
if x <= 1: return push_value_onto_return_stack(x)
put_state_on_a_special_stack(1) # we will continue from state 1
put_locals_onto_stack(locals())
return now_call_this(phi(x - 1))
case 1:
pop_locals_from_stack(locals())
put_state_on_a_special_stack(2)
put_locals_onto_stack(locals())
return now_call_this(phi(x - 2))
case 2:
pop_locals_from_stack(locals())
v1 = pop_from_return_stack()
v2 = pop_from_return_stack()
return push_value_onto_return_stack(v1 + v2)
This idea might work... the problem is that there is a lot of annoying stuff in python, with's for example. Or generators. I have to idea how to deal with those.
learning about sorting algorithms, what's more expensive? swapping elements in array, or creating new array?
i think creating new one is def more expensive in space wise, but what of time complexity?
actually just realized this is useless discussion because I remembered I'll rarely get in situation to implement my own sorting algorithms...
creating a new array uses more memory
it's not useless. mergesort in practice uses O(n) memory. quick sort and heapsort do not
it's kinda possible, my friend has a non decorator thing that allows you to write code with some yields rather than returns and it will run mostly iterative
!e from an earlier discussion
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
@bootstrap
def fib(n):
if n <= 1:
yield n
else:
yield (yield fib(n-1)) + (yield fib(n-2))
for i in range(1, 10):
print(fib(i))
@haughty mountain :white_check_mark: Your 3.11 eval job has completed with return code 0.
001 | 1
002 | 1
003 | 2
004 | 3
005 | 5
006 | 8
007 | 13
008 | 21
009 | 34
you might be able to hack on the ast to make this happen automatically
I remember playing around with it, but I didn't spend much time on it
basically replace return with yield, replace recursive calls to yourself with a version with yield
this restricts what you can do a bit though
e.g. no recursive calls in a comprehension
!e
import ast
import inspect
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
def get_ast(f) -> ast.AST:
return ast.parse(inspect.getsource(f))
def auto_bootstrap(fun):
f = get_ast(fun)
fname = f.body[0].name
f.body[0].decorator_list = []
class Yieldify(ast.NodeTransformer):
def visit_Return(self, node: ast.Return):
self.generic_visit(node)
new_node = ast.Expr(value=ast.Yield(value=node.value))
return new_node
def visit_Call(self, node: ast.Call):
self.generic_visit(node)
if node.func.id == fname:
new_node = value = ast.Yield(value=node)
return new_node
else:
return node
fixed_ast = ast.fix_missing_locations(Yieldify().visit(f))
d = {}
code = compile(fixed_ast, filename='', mode='exec')
exec(code, d)
return bootstrap(d[fname])
@auto_bootstrap
def fib2(n):
if n <= 1:
return n
else:
return fib2(n - 1) + fib2(n - 2)
for i in range(1, 10):
print(fib2(i))
@haughty mountain :x: Your 3.11 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "<string>", line 55, in <module>
003 | File "<string>", line 30, in auto_bootstrap
004 | File "<string>", line 27, in get_ast
005 | File "/usr/local/lib/python3.11/inspect.py", line 1270, in getsource
006 | lines, lnum = getsourcelines(object)
007 | ^^^^^^^^^^^^^^^^^^^^^^
008 | File "/usr/local/lib/python3.11/inspect.py", line 1252, in getsourcelines
009 | lines, lnum = findsource(object)
010 | ^^^^^^^^^^^^^^^^^^
011 | File "/usr/local/lib/python3.11/inspect.py", line 1081, in findsource
... (truncated - too many lines)
Full output: https://paste.pythondiscord.com/anomeciyuv.txt?noredirect
nope
in any case, code that converts returns/calls to yields which work with the recursion bootstrapper
i don't think there's any
welp
however you might be able to do some bytecode manip
case in point
@auto_bootstrap
def fib2(n):
if n <= 1:
return n
else:
return sum([fib2(n - i) for i in (1, 2)])
```this fails with
SyntaxError: 'yield' inside list comprehension
that'll be pretty easy to ignore when you manipulate bytecode
🥴
it'll just fall through the if when you convert ```py
def f(n):
if n < 2:
return n
...
oh right
I should still keep a return
that doesn't work well with NodeTransformer huh?
ye maybe
I guess return (yield ...) would work
to exit the function
even though the return value is just ignored
That is actually quite elegant, nice one! What if the original recursive function is already a generator though?
e.g. for graph traversal
def preorder_traversal(node):
yield node
for child in node.children:
yield from preorder_traversal(child)
Has anybody read the book Data Structure and Algorithms in Python by Michael T. Goodrich ?
is anyone able to point me in the right direction? Im having trouble figuring out my issue with a forward chaining program that uses rules to determine a type of animal. The problem is I am a beginner and only allowed to use 'if' statements.
Like a linked list?
Okay i misunderstood the question, but you could solve this with nested if statements. ##first rule if (something == 1337): ##now for the second rule if (something_entirely_different == 0): ## the patient has a fever.. else: ## patient doesnt have a fever else: ...
in the case of animals, i would find out what larger groups and sub groups exist, like mammals and Felidae
How would I calculate the complexity (O notation) of the following algorithm?
def f(x, y):
if x == 0:
return y+1
elif y == 0:
return f(x-1, 1)
else:
return f(x-1, f(x, y-1))
I don't seem to understand everything, could anyone tell me step by step how to calculate it for that algorithm?
Hi, need your help. Having 2 arbituary boolean functions, is it possible to tell that the first one implies another without brute-forsing them on all domain values?
issue with a forward chaining program
I am a beginner and only allowed to use 'if' statements
huh? Are trying to implement a reasoning engine using only ifs?
You can try to build CNF from both and check if one implies the other (for each clause of one formula check if there is a clause in another formula which implies it), but those are sometimes very hard to obtain and may have an insane size. If I was you I would probably just dump both into high level SAT solver like z3 and ask if one implies another. That's an overkill, but it probably won't even be that slow.
I thought about it for like half an hour and I have no idea either. This function clearly computes the Ackermann function, the but complexity of this function itself is though to calculate. The relation between call count and values is as follows:
1:1 2:2 5:3 15:5 107:13
1:2 4:3 14:5 106:13
1:3 6:4 27:7 541:29
1:4 8:5 44:9 2432:61
1:5 10:6 65:11 10307:125
1:6 12:7 90:13 42438:253
1:7 14:8 119:15 172233:509
1:8 16:9 152:17 693964:1021
Perhaps an upper bound of calls(x, y) < A(x + 1, y + 1) can be proven?
In computability theory, the Ackermann function, named after Wilhelm Ackermann, is one of the simplest and earliest-discovered examples of a total computable function that is not primitive recursive. All primitive recursive functions are total and computable, but the Ackermann function illustrates that not all total computable functions are prim...
just looking at the values of A(m, n) there seems to suggest the big O might involve knuth uparrow/conway's chained arrow
since the only operation is +1 and you so you need at least that many increments
relatively comfortable with python (know the following : while/if/functions/for/data structures).
I'm looking for co-programming, or for a more senior python developer to work with on projects
can anyone help?
It grows much faster than either of those.
Hey I am making a tic tac toe game AI. In that I have to find if a cell in a row/column is empty, how to do that?
The board is a matrix of 3x3 size and if it is empty, its value is 2
this is my code
Hey @fiery cosmos!
You either uploaded a .txt file or entered a message that was too long. Please use our paste bin instead.
Alternatively I did this change
# Scan column 1
for i in [(0,0),(1,0),(2,0)]:
if states[i]==2:
counter=counter+1
emptycell=i
if counter == 1:
product = product * i
if product == 18 or product == 50:
return emptycell
is it better?
this is my final code : https://paste.pythondiscord.com/uzihidoxid
But the algorithm I have says to take count of the players too in Posswin(p) function? is it important? Like will it cause some ridiculous moves
Guys I dont think I will get help in help channel as it will be offline soon. So if anyone of you can help me please read this and answer me here :
#1073859018843488296
hey guys, i'm new here and would like some help with a merge dataframe thingy that's been keeping me frustrated for 2 days now
or a workaround, idc really
df is 1min chart for a full day and df1 is all trades, about 2 million rows
only 28 show up upon merge
i figure because only 28 have the same datetime
'Date' in my columns as index on both tho
Hey @open hearth!
You either uploaded a .txt file or entered a message that was too long. Please use our paste bin instead.
this is the linked list cycle II problem. I'm confused about the while statement here. If I reverse the 2 conditions in the while statement to, "while rabbit and rabbit.next" I get a NoneType object error, but if I keep it as is I don't. What's goin on here?
I don't think so? That's what I thought too but I remember that when turtle does hit rabbit, all you have to do is return that node
yeahhh
yeahhhhh
I watched neetcode's explanation on floyd's algo and implemented that
and it somehow worked
i wrote out you algorithm on paper and it always seems to give the head as the node where it starts
tf
it does tell if there is a cycle correctly just not where it starts
ah
when the turtle and rabbit first meet
I put the turtle alll the wayyyy back to head
i think i kept stepping by 2 instead of switching to one steps for rabit
and then I move the rabbit(initially it was at that node where it met turtle) and the turtle would move forward one node at a time, until they do meet each other again, and at that piont is the node where the cycle starts
its very very unintuitive
yeaa
also btw for my previous question
some dude explained it to me nicely
while turtle!=rabbit:
turtle = turtle.next
rabbit = rabbit.next
yea after u set the turtle to head, u do that
this seems like it would loop indefinetly if turtle and rabbit do not start at the same place
the thing is turtle will go back to head right
and rabbit will always stay some distance away from the node that started the cycle
and the funny thing is, the distance from the head to the node taht started the cycle is identical to the distance that the rabbit is from the node that started the cycle
right and each will step by one in a never edning cycle
not really, coz rmr the rabbit is always behind the node that started the cycle
floyd's algo is better explained with a vid:
as he said, this question is shit lol
ooo you basically implemented floyd's algo here too
yeah likew tf
I thought I was a dumass coz it seemed simple
its like one of hte niche things in leetcode u need to memorize/understand
well kinda, its like, the rabbit and hare meet up some distance away from the node that started the cycle, then u put the rabbit in the head, where the distance between the head is the exact same distance from the node that started the cycle. Then you increment both the rabbit and turtle one move at a time til lthey meet up again, and taht's the node that started the cycle
it's fine, I get to reiterate my thoughts
sameeeeee
its so gud for retainign info
and someone can catch ur mistake if u have any
I always get the head if it cycles
i'll draw out an example in gimp
they always end up at the starting point
@bright geyser
I have tried with 3,4,5 nodes
maybe its different for higher nodes
one sec lemme try drawin it out tooo
wai is this looping back to the starting?
yeah
then yea it would make sense that it loops back to head, coz the head node is the start of the cycle
try making it loop back to the middle node
i did it with 8 nodes and it ended at B
wai did u watch the vid I sent?
well i am saying it ended at b when it has fouind a match
not when it trys to find the starting node i mean
sure ill watch the video
o
kk yea watch the viddd and lemme know wht u think afterwards
@fiery cosmos I also tried with 8 nodes, and I still ended bacc at the head
i messed up my table you are right
still watching the video
better to use numbers and just count by two and one instead of letters
yea it gets kinda confusing when for a moment the 2 pointers are in the same node, but u also have to move the other pointer at the same time
yeaa
niceeee
yép !
Hey as anybody got experience using scrapy?
What is the most efficient way to for example share data between a python and a java program during runtime?
shared memory, I would think
Hey guys, I am currently into trying to understand recursion, unfortunately after 1 month hustling 6h a day I can tell that I've made no progress at all, I know all the theory but when it comes to code I'm getting lost, so my questions are : Is recursion actually very impotent when it comes to write code, applying for job...? Is there any chance that I still learn this after failing to learn it for a month if yes please tell me how if not should I just quit recursion and continue with something different?
Hi Vandet.
Recursion is very easy.
May I help you?
Hello folks, I need help understanding a particular section of my zero one knapsack algorithm
def knapsack(capacity, weights, values, numOfItems):
if numOfItems == 0 or capacity == 0:
return 0
if weights[numOfItems - 1] > capacity:
return knapsack(capacity,weights,values,numOfItems-1)
else:
val_included = values[numOfItems-1] + knapsack(capacity-weights[numOfItems-1],weights,values,numOfItems-1)
val_not_included = knapsack(capacity,weights,values,numOfItems-1)
return max(val_included,val_not_included)```
I really don't understand why we have to `[numOfItems - 1]`, am completely lost there
which one are you talking about?
I have this code for calculating what value is the nth int in fibonacci sequence. It is my understanding that the recursive call to return fib(n-1) has to run its course, before the first (and all subsequent) calls of fib(n-2) can be pushed to the stack and eventually run. My question is: it seems there is redundant calculations going on, since anything fib(n-2) calculated will have already been calculated by fib(n-1). Is there a way to save all the results of fib(n-1) instead of running fib(n-2), and is this any more efficient?
def fib(n):
assert n >=0 and int(n) == n, 'The number must be a positive integer only'
if n in [0,1]:
return n
else:
return fib(n-1) + fib(n-2)
[numOfItems - 1]
just create a list
save anything thats already calculated
and if a value is already calculated, stop the recursion
Is that any more efficient than doing the recursion?
functools.cache is a pretty easy way to implement memoization
Hey 👋 What learning resources are you using? What aspect of recursion are you having trouble with?
I strongly suggest installing the thonny editor just for that; it has a debugger that can visually show the recursion stack, really helps
I didn’t really know which one to go on for this, but I’m wondering if there’s anyway that you can change the color of words inside of an embed
Or even if it’s not inside an embed
ah, yeah it's a bad interview question
good interview questions are about combining and applying a few points of DS&A knowledge solve a problem
what the level of expected knowledge you can expect can differ, but if the task is literally just "apply thing" that's pretty questionable
yeah?
in your problem we are working with a graph, we have parts of the graphs that are already connected, those are components
the question then boils down to just connecting the components
so step 1: find all components
step 2: connect the components with the fewest possible edges
to rephrase the idea, take each component and replace it by a single node
then you just need to connect these nodes
and it shouldn't be hard to figure out how many edges you need to connect a set of points
there shouldn't be any edge cases in the thing I wrote
Hey, so I'm finding that on my machine, this function is taking about 1.5 seconds...
There has got to be a faster way!
s = requests.Session()
content=""
with s.get(url=searchUrl, headers=header3) as response:
content = response.content.decode('utf-8')
begin=content.find("https://yahoo.com/")
if(begin>-1):
content = content[begin:content.find('"',begin)]
The code is trying to search through a response for a specific hostname. Any ideas on what I can do to speed this up?
you should measure which parts are taking time. if it's the request, not much you can do without getting better internet
the 1.5 seconds is only processing this code here. The request is already complete.
s.get is the request, isn't it?
Oh well... yes I suppose you're right 😄
I made an improvement to cut the time in half
s = requests.Session()
content=""
with s.get(url=searchUrl, headers=header3) as response:
content = response.content
begin=content.find(b"https://yahoo.com")
if(begin>-1):
content = content[begin:content.find(b'"',begin)]
yeah I'm wondering if there are maybe other tricks. Like if I know approximately where the URL is expected to appear, do you think I can reasonably save time by starting the find somewhere in the middle ,etc
or if this is maybe doing unnecessary copies
do you need to slice? also have you done benchmarks?
The URL appears in the HTML response like
<img url="https://yahoo.com/whatever1235346346" > for example. So I'm searching the page for that image to get the full URL path. The slice is just going to end of the string.
I'm doing very rudimentary time.time() benchmarking right now
i wonder if something like bs4 would be faster, but probably not. i don't think you'll get any faster from just .find
Well after benchmarking the internals here, I think you're right. The .find time is almost 0, the remaining time is network time
maybe 1 MB
yeah, the find should be very fast then
yeah the UTF-8 decode was slow, but the rest seems OK
alright I won't hyper optimize this 🙂 benchmarking is always the right answer
always the first thing you should do when trying to speed things up
Yeah I was benchmarking the full function, and forgot to benchmark the network separate from the parsing. Thanks for reminding me
now the only thing left to do is get better internet
💀
def tryparsenum(w):
w = int(w)
return type(w) == int
w = '56'
print(tryparsenum(w))
print(type(w))
how do I make tha argument passed also change
in print(type(w)) I don't want it to be a string, I want it to be an int
as modified in the tryparsenum method
how do I do that?
w = int(w)```
your function will always return true or just error
it's kinda pointless
idc about it being pointless
and yes, wrong channel - see #❓|how-to-get-help
I can help you in #1035199133436354600
Hey guys, I want to use the python wrapper talib for TA-lib but I cannot install it, what could be wrong? (I'm using manjaro-linux)
Hi is it true that we aren't allowed to functions while solving a problem in company interviews
Hi so this is the leetcode question is 2529 and Its saying to use Binary Search ```nums = [-2,-1,-1,1,2,3]
target = 0
def binarySearch(arr,target,start, end,counter):
print(start,end,counter)
mid = int(start + end / 2)
print(mid)
if end < start:
print("Pritning the final",counter,arr)
return [len(arr),counter]
else:
if target < arr[mid]:
counter += 1
arr.pop(mid)
print(arr,target,start,end,mid,counter)
binarySearch(arr,target,0,mid - 1,counter)
elif target > arr[start]:
binarySearch(arr,target,mid , end,counter)
elif target == arr[mid]:
arr.pop(arr[mid])
end = len(arr) - 1
binarySearch(arr,target,start,end,counter)
end = len(nums) - 1
start = 0
counter = 0
value = binarySearch(nums,target,start,end,counter)
print(value)
I am trying to do this
it is able to separate it out however it goes out of index can some one please help me
can anyone recommend me a good algos course with exercises?
Kunal Khushwa
this isn't python?
Ya understand the logic , go try to code if your unable to do it , go to his discord and ask
hi im learning DS&A through a udemy course. I basically wait for the instructor to describe how the algorithm works (demonstrating on real data) and then I write my own code accordingly, then compare with their code at the end (after thoroughly testing mine). Is this fine or are you supposed to know how algorithms and data structures work just based on mentioning them?
^im asking what the correct approach is to learning this stuff
which course are you doing?
Hello guys, I have some questions:
-
Look at the instruction set for the IAS computer which we discussed in class. Pick one of those instructions that you think is the fastest, and another that you think is the slowest, and use what you know about instruction execution to describe why they take such a different amount of time.
-
Use an example to illustrate the tradeoffs (both the advantages and disadvantages of different design decisions) of different specific CPUs.
uhh the one by Abdul Bari
Beside Cython, is there any way to increase the speed of looping through a JSON file? I tried doing it async using aiofiles but its slower than normal synchronous loop ... my JSON loop is in a pyx file written in cython
you don't need to crosspost immediately 🙂 i replied here #async-and-concurrency message
i was trying to use vosk i got the model and everything
but this error keeps coming
File "/Users/dineo/PycharmProjects/polan_deepspeech/venv/lib/python3.11/site-packages/vosk/__init__.py", line 12, in <module>
from .vosk_cffi import ffi as _ffi
ImportError: attempted relative import with no known parent package
Process finished with exit code 1```
or this
Exception ignored in: <function Model.__del__ at 0x1065900e0>
Traceback (most recent call last):
File "/Users/dineo/PycharmProjects/polan_deepspeech/venv/lib/python3.11/site-packages/vosk/__init__.py", line 60, in __del__
_c.vosk_model_free(self._handle)
^^^^^^^^^^^^
AttributeError: 'Model' object has no attribute '_handle'
URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:992)>
How are you running your code?
Can you show your code?
do you need to see its structure ?
like file structure
also i dont really want to use the small model
As a math noob what is the name of this type of number 3.510000e+03
this is written in engineering notation, if that's what you mean. it's just 3510.0.
Oh ok
huh, i've never heard "engineering", always "scientific"
though my calculation has an "ENG" mode which always uses a multiple of 3 for the exponent
I have a cached thought that engineering notation is when you use E instead of * 10^
Hey guys, what is the best way to enforce that a subclass of a Django ModelViewSet has a specific class attribute implemented? I've created a function that does so, but when should I call it? __new__ and then call super().__new__?
whats the fastest time complexity of fibonacci numbers?
O(log n)
here's an article describing a couple of methods https://www.nayuki.io/page/fast-fibonacci-algorithms
so like this?
def fast_fib(n):
if n == 0:
return (0, 1)
a, b = fast_fib(n >> 1)
c = a * ((b << 1) - a)
d = a * a + b * b
return (c, d) if n & 1 == 0 else (d, c + d)
that looks like the fast doubling method, sure
although you'd probably also want to define fib = lambda n: fast_fib(n)[0]
so that it doesn't return two numbers
also be careful that this is not the most efficient way to generate a sequence of fibonacci numbers
i.e. if you wanted to generate the fibonacci numbers between 100 and 200, it would be a lot faster to just generate the 100th and the 101th, then just add those together linearly up to the 200th term
Hello all, users of NumPy might like this: https://www.phoronix.com/news/Intel-AVX-512-Quicksort-Numpy
Engineering notation is something else. The "E" (for exponent) thing is a calculator thing to make it shorter, inline, and single font size. Some used "D" instead for "decapower." There are some other inline notations too. @agile sundial
how do we deal with doing for loops within for loops within for loops within...?
i feel like explicitly typing each 'iteration' isnt the best way
unless it is
you could use itertools.product
for x in xs: for y in ys: for z in zs: becomes for x, y, z in product(xs, ys, zs):
i want to do n number of nested loops
you can do like
for items in itertools.product(range(10), repeat=n):
If you have a list l consisting of iterators it1, it2, up to itn, then you can use for items in itertools.product(*l):.
do note that I believe it's slower than nested for looping
and obviously, this explodes really quickly as n grows larger
it also allocates all results at the start, so it uses a lot of memory
https://paste.pythondiscord.com/nuxuyojufe
guys, is there a better way to improve my code for calculation?
what does?
itertools.product is lazy
@staticmethod
def triplet_result_zero(l: list[int]) -> list[list[int]]:
result: list[int] = []
for i in range(len(l) - 2):
for j in range(i + 1, len(l) - 1):
for k in range(j + 1, len(l)):
if l[i] + l[j] + l[k] == 0:
result.append([l[i], l[j], l[k]])
return result
thoghts?
slow
you can easily do this in O(n²)
for all pairs a, b; check if -(a + b) exists
mine is O(n^3) because 3 nested loops innit?
why -(a+b)?
typo
whoops
it's not that simple generally
mhm
e.g.
for i in range(1, n):
for j in range(0, n, i):
...do some O(1) work
would a while loop be faster?
what is a + b + -(a+b)?
any help?
i'm planning to make a operation thing
no?
just curious
like, operate division first then multiplication then sum and subtraction
fwiw this is O(n log n)
i also discovered a bug that the thing is instead of subtracting it's summming and turning into negative
i would thnk what you are on about is precendence in math?
yep
i am learning time and space complexities, thought this was a good problem
precedence is kinda annoying to deal with
but the math part is failing
you wanna make it faster?
i ignore it sometimes tbh(wrong habit dont follow me)
welll, i'm searching to fix the problems and yeah, possibly make it faster
so i can learn better with python
lemme see the code
complete code or....?
nah the paste discord link you sent
.
reverse polish notation might be interesting to look at to see a system without precedence rules
avoid using del for deleting elements from the list. Instead, consider using slice notation to remove elements, which is faster and more efficient
i see, i was planning to remove those elements just to show up as solved problem
i saw another flaw
slice? uhmm..., lemme search rq
use built-in Python functions like 'sum', 'reduce', and 'operator' instead of manually iterating through the list and performing calculations, this isnt a flaw rather an improvment i would recommend
and use list comp
thats all i could see from your code
list comp?
the problem of using this is that i'm using indice as a pivot for what to do, so i'm struggling on how i would use it using the indice as pivot
like, indice is the pivot list that store the operators
like +-*/
My bad.
It lazily creates resulting tuples, but all internal iterables are iterated over and cached at beginning (on product() call or on the first next() call, idk).
I remembered that some part of .product is not lazy, but forgot what, so i was wrong.
product needs to store the entire iterable first because it needs to support repeat
like for product((i for i in range(10), repeat=2), there's no way to ungenerate the values, so it needs to store them
how do i add a and condition here?
re.compile(r"^(")
i'm plannning to add a ")$" condition
i'm planning to the pattern gather all values from ( to )
like, gather value that starts with ( and ends with )
that's the condition i'm plannintg to add
you probably want something like r"^\(.+\)$"
!e
pattern = re.compile(r"[+-*\/()]")
x = "5+8+(5*3)-7
print(pattern.split(x))
@fiery cosmos :x: Your 3.11 eval job has completed with return code 1.
001 | File "<string>", line 2
002 | x = "5+8+(5*3)-7
003 | ^
004 | SyntaxError: unterminated string literal (detected at line 2)
!e
pattern = re.compile(r"[+-/()]")
x = "5+8+(53)-7"
print(pattern.split(x))
@fiery cosmos :x: Your 3.11 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "<string>", line 1, in <module>
003 | NameError: name 're' is not defined
!e
import re
pattern = re.compile(r"[+-/()]")
x = "5+8+(53)-7
print(pattern.split(x))
@fiery cosmos :x: Your 3.11 eval job has completed with return code 1.
001 | File "<string>", line 4
002 | x = "5+8+(5*3)-7
003 | ^
004 | SyntaxError: unterminated string literal (detected at line 4)
!e
import re
pattern = re.compile(r"[+-/()]")
x = "5+8+(53)-7"
print(pattern.split(x))
@fiery cosmos :white_check_mark: Your 3.11 eval job has completed with return code 0.
['5', '8', '', '53', '', '7']
#bot-commands
!e
x,y = 1,2
print(x)
print(y)
@fiery cosmos :white_check_mark: Your 3.11 eval job has completed with return code 0.
001 | 1
002 | 2
!paste
Pasting large amounts of code
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the floppy disk icon in the top right, or by typing ctrl + S. After doing that, the URL should change. Copy the URL and post it here so others can see it.
Hey @neon sierra!
It looks like you tried to attach a Python file - please use a code-pasting service such as https://paste.pythondiscord.com
hi im having hard time rounding these numbers for my intro class
any suggestions
ive tried a lot of things
is there a way for me to create a function assuciated with a function?
like
function a(x, y, z)
function a(w)
i'm planning to make a function of a function
like, one fuction to compile an input for another function
i'm planning to it accept one input and it have a function to compile the input to make 2 input for another function
is there anything in python maybe itertools where i can give it an array of cahracters like
["a", "b", "c"]
and it'll return all combinations of those so
"a", "b", "c"
"ab", "c"
"a", "bc"
"abc"
or an algorithn
!e import itertools
print(dir(itertools))
@crude pond :white_check_mark: Your 3.11 eval job has completed with return code 0.
['__doc__', '__loader__', '__name__', '__package__', '__spec__', '_grouper', '_tee', '_tee_dataobject', 'accumulate', 'chain', 'combinations', 'combinations_with_replacement', 'compress', 'count', 'cycle', 'dropwhile', 'filterfalse', 'groupby', 'islice', 'pairwise', 'permutations', 'product', 'repeat', 'starmap', 'takewhile', 'tee', 'zip_longest']
There's nothing exactly like this in itertools, and I'm not sure what the combinatoric term is for this, but you can achieve this with the following recursive function: ```py
def foo(s):
if len(s) == 0:
yield ()
return
for i in range(len(s)):
prefix, suffix = s[:i+1], s[i+1:]
for rest in foo(suffix):
yield (prefix,) + rest
Probably some term with "partition" in it.
If s is long, the maximum recursion depth may be an issue, but then the number of possible ways to split up the string would be impractically large anyway.
Probably yeah.
But "ac" isn't a possible output, so I'm not actually sure.
!eval ```py
def foo(s):
if len(s) == 0:
yield ()
return
for i in range(len(s)):
prefix, suffix = s[:i+1], s[i+1:]
for rest in foo(suffix):
yield (prefix,) + rest
print(list(foo('abc')))
@keen hearth :white_check_mark: Your 3.11 eval job has completed with return code 0.
[('a', 'b', 'c'), ('a', 'bc'), ('ab', 'c'), ('abc',)]
@umbral gulch
nice
!e from itertools import combinations as c
print([list(c("abc",i)) for i in range(1,4)])
@crude pond :white_check_mark: Your 3.11 eval job has completed with return code 0.
[[('a',), ('b',), ('c',)], [('a', 'b'), ('a', 'c'), ('b', 'c')], [('a', 'b', 'c')]]
tfw you read 2x10^5 instead of 2x10^6 and you realize it's not brute forceable 😔
if i have n!, would n be considered a constant when referring to Big O Notation?
because I’m trying to see if n!+2n is = or ≠ to O(3^n)
If n changes, it's not constant.
i was wondering if i could send over my code for anyone to take a look at and see if i can reduce clutter and increase readability while keeping optimisability the same in my code?
Happy to take a look
many thanks, heres the pastebin 🙂
https://pastebin.com/n9HhYpQi
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Sure. Give me a few and I’ll check it out
I have two pandas data frame a and b, both have the same shape.
I want to element-wise divide a by b
Tried things like
a / b
a.divide(b)
The resulting data frame has only NaN and somehow duplicates the columns
I am very new to pandas... what is the "normal" way of dividing one data frame by another element-wise?
is it faster to go through a large list a small number of times, or a small list a large number of times?
try using pandas drop.na() method to remove the nan values
`# create a sample dataframe with NaN values
df = pd.DataFrame({'A': [1, 2, np.nan, 4, np.nan], 'B': [5, 6, 7, np.nan, 9]})
remove rows with NaN values
df = df.dropna()`
this will drop the nan values in the sample df
I don't think there would be much of a difference in python. In lower level languages I would think the smaller list would be faster, due to caching.
do you mind sending over the code and i can take a look
Pasting the code right here ok?
yeah maybe put backticks round it so its a bit easier to read
df = pd.read_csv("sheet3.txt", sep='\t') at0w = df2.iloc[:, [i for i in range(len(df2.columns)) if i%2 == 0]] at4w = df2.iloc[:, [i for i in range(len(df2.columns)) if i%2 == 1]] at0w.div(at4w)
(Sorry but I cannot share the actual sheet3.txt)
So I am trying to divide each even column but its next, uneven column.
Actually:
df2 = pd.read_csv("sheet3.txt", sep='\t')
(There was a typo in the first line)
``df2 = pd.read_csv("sheet3.txt", sep='\t')
at0w = df2.iloc[:, [i for i in range(len(df2.columns)) if i%2 == 0]]
at4w = df2.iloc[:, [i for i in range(len(df2.columns)) if i%2 == 1]]
result = at0w.div(at4w)
print(result)``
is this identical to your code?
just got myself a bit confused with your typos haha
Yes, identical
I just ran it with my data
The result is just NaN in each element of the frame
And the column are duplicated with _at0w and _at4w appended.
``# Drop NaN
at0w = at0w.dropna()
at4w = at4w.dropna()
Same dataframe lengths
min_rows = min(len(at0w), len(at4w))
at0w = at0w.iloc[:min_rows, :]
at4w = at4w.iloc[:min_rows, :]
Divide
result = at0w.div(at4w)
print(result)``
Try this
I just tried that. at0w and at4w originally both have a shape of (58, 59).
After doing dropna on both frame, at0w has a shape of (57, 59) and at4w is unchanged.
I then bring both into a shape of (57, 59). and div
However, the result is still the same
I.e. all NaN and columns duplicated with _at0w and _at4w appended.
It sounds like the issue might be that there are NaN values in the same rows of both dataframes, so dropping NaN values from only one dataframe will result in a mismatch in the number of rows between the two dataframes, you could use how='any' when dropping NaN values so update that line to...
at0w, at4w = at0w.dropna(how='any'), at4w.dropna(how='any')
let me know how this works
I will try that in a minute but I noticed something interesting
So after I do the min_rows thing
both at0w and at4w have 57 rows × 59 columns
But the indices of the rows must be different because at0w has 57 as the last index
and at4 has 56 as the last index!
It did not fix it
But I noticed, after removing NaN from the input data frames and bringing them to the same number of rows
Both at0w and at4w now have the same shape, 57 rows × 59 columns
However they do not have the same row indices!
I.e. in at0w row 27 is missing and in at4w row 57 is missing
could that be the issue? I.e. maybe div someow expects both data frames to not only have the same nu,ber of rows, but also the same indices?
By "at0w row 27 is missing and in at4w row 57 is missing" I mean: after I remove rows containing NaN from the input data frames, these lines are removed
I think I figured it out
The indices apparently must align
This works:
at0w / at4w.values
what is your dp problem?
what implementation are you referring to?
@cobalt mirage Your goal is to find the minimum cost to reach stop i, for each i in {1, ..., n}. That's going to be the minimum cost ending up either on the regular or express side. Ie py min( cost(stop=i, express=False), cost(stop=i, express=True), ) How would you recursively define the function ```py
def cost(stop: int, express: bool) -> int:
...
Hopefully that's enough of a hint without giving too much away.
Hello team in pandas I have two dataframes df1 & df2 I want to compare column ['A'] on both. If the values are the same concat df1. Is that possible?
#data-science-and-ml is a better fit for this question.
If I have a bunch of enums all with a ".CREATE" and a ".UPDATE" member and I pass any one of these enums to a function is there a pythonic way to see if I passed a "CREATE" or "UPDATE".. At runtime I won't know the actual enum type
class type1 (str, Enum)
CREATE = "type1.create"
UPDATE = "type1.update"
class type2 (str, Enum)
CREATE = "type1.create"
UPDATE = "type1.update"
def somefn(some_enum):
if some_enum == <variable>.CREATE: #Here I won't know what type of enum until runtime, what is the correct way to do this conditional?
do something
Any help appreciated
It seems like you should narrow the type of some_enum before you get to the point of comparing it against an enum value.
what are the advantages of using redis sorted sets compared to an ordinary hash table that is manually sorted
they said it has O(log n) compared to O(1) of hash tables which is a disadvantage
This would probably mean a huge if else statement testing what type some_enum is
Could you tell me more about the structure of the code and why you have multiple enums with overlapping values?
how are you going to manually sort the hash table
I have lots of different types of data models (SQLAlchemy) and then an API which can update, create,delete etc. If I get an update event this would be marked with a corresponding enum as Data_model.UPDATE etc which is then used in lots of downstream processing for aggregation. The value of the Enum is also stored in the DB as<data_model>.update
Those can be made persistent and are perfectly amortized. Insertion into hash table can take up to O(n) if you decide to rehash, which is usually unacceptable for servers (imagine if your endpoint can randomly stop for like half an hour to rehash). Search trees are always O(log n), no matter the context.
like how you would sort an array
when a new element is added you use an online sort like insert sort
oh wait "sort a hash table" does not make any sense
it does if its ordered
like, what part are you going to sort? if you sort the hash table, then you can't find your values anymore
it's... not... I mean it's kinda is but insertion is O(n) now
sort by value
ok so it doesn’t work
if you sort by value, then you won't be able to find where it is, since it's no longer where you expect it to be according to the hash function
Hmm, it seems like the kind of event is orthogonal to the thing the event acts on. Wouldn't it make more sense to structure your data like this? ```py
@dataclass
class Event:
kind: EventKind
target: Thing
...
class EventKind(Enum):
CREATE = 1
UPDATE = 2
...
I'm not very familiar with SQLAlchemy though.
This question is probably a better fit for #software-architecture or #databases
Hmm I see that might work but when saving the value of the enum to the db I would be losing the context of what was created or updated as I currently use the enum value to write directly
sorry yeah you're right this is definitely in the wrong section. my bad
don’t you know about ordered hash tables?
no? i've never heard of that
If you had those you would be able to sort in O(n)
ah. these use a linked list to maintain the order. insertion is still linear
also, they're not "ordered" in the sense like a binary search tree is, rather they're "ordered" in the sense that you can change the order
so sorting is infeasible?
well you don't need to sort to insert
can change order => can sort
sure, but you're extremely limited in how you can do that with OrderedDict. you're probably better of making your own linked list if you want to do this
actually, this entire point is moot if you're using redis. redis doesn't expose any ordering functions for its hash sets
ok im talking about sorted sets in general
the takeaway here is that hash table is not an end all be all data structure 😩
well yeah. if you need to order a hash table you're literally doubling your memory consumption
no surprise there
BBSTs are what you want if you want to keep an ordered mapping
Hey @spice prism!
It looks like you tried to attach a Python file - please use a code-pasting service such as https://paste.pythondiscord.com
See #web-development
can anyone tell me what the time complexity of this code is
it's O(n log n) because sort() uses Timsort which is O(n log n)
and the rest of the code are O(1)?
actually len check means O(n)
but it doesn’t matter anyways
len call is O(1), but adding lists together is O(n + m)
oh i forgot length is precalculated
because its a dynamic array
python lists are not linked lists
This code is very weird by the way... round(length//2) is just length // 2, int(length / 2) and round(length / 2) is the same thing. (Sorry I was wrong about what is been there before)
that depends on the value of length
I know, but in the case of code above it's the same thing
for example, int(5.5/2) != round(5.5/2)
because in else branch length % 2 == 0
but yes, you are right, that's worth clarifying anyway
oh that, sorry, I had a brain fart
Sorry im fairly new to dsa in python and these inbuilt functions are quite overwhelming 🥲
actually, in this case it should be linear. timsort optimizes this case
even in a linked list, it'd be very strange to not store the length
timsort can detect that list is made of two sorted parts? TIL...
it does even better. it optimizes for "runs", where there might be multiple presorted bits
and then your interviewer will go, "that's great, but i really meant do it without sorting, not just do it in O(n)" 😩
yeah, joining two sorted lists is O(n) for timsort
I think there's an intended solution to this question, which is O(log(max(m, n))) or something.
big if true
Well I know there is because I solved it yesterday
Someone was talking about this same question in voice-chat.
oh we can binary search
It's a bit edge-casey though.
hmm. a lot of off by one errors
bruh that's an insane overkill
Just check for each point signs of cross product with sides of the triangle like in the picture. Or sum up areas of triangles PAB + PBC + PCA and compare it with area of ABC.
Not sure if my signs in correct, but the idea is that if all three signs are equal, then point is inside of the triangle, otherwise outside
Or check if the ray going in horizontal direction intersects triangle once
intersecting segments with horizontal ray is not difficult
be careful with edgecase where ray goes though triangle vertex
are you asking for 2d triangles or 3d triangles?
just geometry with a bit of a vector algebra basics
does graham work in 3d?
there is dot product and cross product, you probably need cross, but you should read about both. Those are related.
no clue
if they're asking for a 2d triangle tho then cross products are a bit overkill
but to compute the area you need cross product, huh?
sure, that will work... I guess just don't use heron's formula to compute the area and you are fine
I see
well that works
I usually just use cross product
To calculate the area of a triangle
it's the just the length of the 3d cross product
You can do that. This requires you to check if point is above/below a segment, which may be painful because of the edge cases (stupid vertical lines with no equations), but will work.
well... yes... plus it requires cross product so I'm not sure if... you know... you are able to implement it... but if you can just copy and paste it I guess you can use it...
I might be dumb but when I'm given a convex hull and I don't know how it's oriented I might sometimes just copypaste my implementation of convex hull and apply it to... already a convex hull just to orient it properly. So if something works for you - go ahead.
Explain
gimme a second
In 3d space the cross product of two vectors on XOY plane is a vector which is perpendicular to XOY plane (so both X- and Y-components zero out) and length of which is equal to the area of parallelogram between those two vectors. And 2d cross product is just a signed area of that parallelogram. Convenient.
By the way I said the wrong thing. Not the "length" of 3d cross product, the projection of 3d product on Z axis.
ok that's fair
i don't like messing around with geometry either
That's not a cross product.
if it works it works
In mathematics, the seven-dimensional cross product is a bilinear operation on vectors in seven-dimensional Euclidean space. It assigns to any two vectors a, b in
R
7
{\displaystyle \mathbb {R} ^{7}}
a vector a × b also in
...
You're using the exterior product, which is a different construction.
It happens in three dimensions that you can identify the two. The cross product of two vectors is the result of taking their exterior product, then their interior product against a fixed volume form.
But the two are distinct operations in general.
See https://en.wikipedia.org/wiki/Exterior_algebra for more on exterior products.
?????? I'm confused. Exterior product is a generalized cross product from clifford algebra, no? I'm just referring to a simple cross product in 3d space.
No.
i mean this notation is kinda correct since there's the || signs
that's geometry 🙂
The cross product is defined by interpreting a Euclidean space as the imaginary part of an algebra and using the multiplication in that algebra. In order to define a cross product, this multiplication has to satisfy several properties (like being well-defined, a vector cross itself being zero, and so on). This can only be done in dimensions 0, 1, 3, and 7.
I used to
It's identically zero in dimensions 0 and 1, which just leaves 3 and 7.
The exterior product, however, is a general operation you can perform on any vector space.
It happens to be related to the cross product, basically because there aren't that many things you can do with a three-dimensional object.
But it has a completely different definition in terms of the exterior algebra.
And the "two-dimensional cross product" you defined above is exactly the two-dimensional exterior product.
Ok fair I got it. Let me defend my self:
So you are right, in algebra we call that exterior product, but in most programming literature it's called just 2d cross product, because it's similar to 3d cross product and used way more often than that. So usually when referring to cross product in algorithms definitions, either "z-component of cross product" or just "cross product" term is used. I agree that it refers to a formally different algebra operation, but I believe it's ok to use domain-specific jargon. Thanks for clarifying that though, this may be helpful for others.
If the programming literature calls it anything, it calls it a determinant.
Anyone calling it a 2d cross product is just wrong.
let me refer to a "relevant" source https://codeforces.com/blog/entry/48122
I hate to sound so aggressive, but this terminology has been stable for at least 80 or so years.
Again: that's jargon
It's simply wrong.
You can look this up in innumerable algebra books.
Artin, Hungerford, Dummit and Foote, etc., will all agree that this is the exterior product.
People on the Internet are allowed to be wrong. They're often wrong. (I'm wrong too sometimes.) So I'm not too surprised that you can find websites that use wrong terminology. But it's still wrong.
My objection to calling this a two-dimensional cross product is that the result is not a vector (unless you embed the whole situation in three dimensions. To be fair that's what you did, but then it's not really two-dimensional). It's a scalar.
Alright, I'm not going to argue about it and I won't change my opinion either. I understand what you mean and where are you going from, but I disagree and I will continue to call it "cross product" like in "Introduction to algorithms" or something.
The exterior product isn't supposed to give you another vector (it just gives you back another element of the exterior algebra, or the Clifford algebra if you've embedded everything there). But that's what a cross product is.
I disagree with your decision, but I think it's better that we stop here.
deal
ow sorry
I did a bit of codeforces, but mostly a local contests at my school under teacher supervision, so I can't really give good sources.
I dont remember one sec
To be honest, I don't know... just try out different sources
this really depends on who you are and how you learn things so I can't recommend something specific
I remember a one good article which helped me a while ago but it's in russian so... you know... language barrier
you know a lot of methods now, just use any
I don't think you are ready for convex hull yet
a somewhat good understanding of cross product, stack, basic sorting
and the first one is the most relevant
well again I can't really recommend the source so...
the length is correct if you're working with vectors in the xy plane
since the cross product points along z
but it can be negative if vectors for a clockwise oriented angle
oh right, you want the signed version of the thing
right
projection is right then, my bad
but yeah, the two ways I think of when doing point in (convex) polygon is to either check a bunch of cross products or a bunch of dot products
so, on the orientated edges of the polygon, is the point always on the same side of the edges
which can be computed using a cross product like thing, or doing a dot product against the normal vector of the edge
there are other fun approaches that work even for the non-convex case
take a ray from the point, if it intersects an odd number of segments it's inside, if even outside
but that has fun edge cases, as with a lot of computational geometry
what if math makes your head hurt?
story of my life
but I need to learn this stuff for a project I'm working on with some friends😭 but it will definitely worth while!
tell me where is the best place to start if one wants to learn this topic
hands Fring some ibuprofin
What you need is that the cross product takes two vectors as input and returns a third vector which makes the three vectors form an oriented basis. Informally, we say that the cross product satisfies the right-hand rule: If you point your right hand in the direction of the first vector and curl your fingers towards the second vector (which may require turning your hand over!), then your thumb will point in the direction of the cross product.
The most elementary way to check whether a point is inside a triangle is: Each segment of the triangle determines a line, and the line cuts the plane into half-spaces. The remaining vertex of the triangle is in one of these half-spaces; the point is inside the triangle if and only if it is in all three of these half-spaces.
That method uses the third vertex to determine which half-space is the correct one. We can also encode this information as an orientation of the sides of the triangle (making them vectors and not just segments). If the orientations are consistent, so that the triangles make a loop, then the interior of the triangle is either the intersection of the left half-spaces or the intersection of the right half-spaces, while the other intersection of half-spaces is empty.
This is where the cross product comes in. We embed the plane into space (as {(x, y, z) : z = 0}). If v and w are two vectors in the plane, and we translate them so that they begin at the same vertex, then v x w points up (z positive) if w is in the left half-space of the line determined by v, and it points down (z negative) if w is in the right half-space. So if the triangle has vertices A, B, C, and the point is P, then we can test whether P is in ABC by checking whether AB x AP, BC x BP, and CA x CP all have the same sign. We get effectively the same test if we look at AP x AB, BP x BC, and CP x CA (because this just reverses the orientations, i.e., all the signs)) or PA x BA, PB x CB, and PC x AC (again, it just reverses all the signs).
Hello,
I have a understanding problem with pandas / dask.
My Data looks at the moment like this:
id;neuronal_data;
9928-1231-1231; [.23, .21 ....]
9928-5218-1101; [.1, .51 ....]
The shape of neuronal_data is [1024] now I want do a simple matmul over all that values with a 1D list of data with the same shape.
If someone want know what I do, I use open_clip.
Now I have problems to implement that in a fast and good way. My CSV files are big (over 250.000 rows) and I have to get the ID later back, after the matmul + top_k function. To get rid of the memory problems, I wanted use dask, but even in pandas i dont know if I have the best practice. Dask is a another thing, I cant get it running like I want to. My goal is to process in chunks over the data and that so native with the library like it is possible.
In pandas its works then I use:
df.neuronal_data.apply(lambda x: x @ search_list) but that seems to be impossible in dask?
And is that even a good practice?! Should I save the data just pure matrix like? Without the ID? So that I can use transpose and the matmul function?
I am a newby in data science and big data. Hope someone can give me a bit advice. GhatGPT isnt a help atm 😦 And sorry for my super bad english.
Hey, quick question. From what I can find online, heapq.nsmallest runs in O(n * log k), where n is the size of the list and k is the number of smallest elements to return. I understand that it does this by heapifying the first k elements, then doing heappushpop for the rest (log k operation n times). However, wouldn't it be better to heapify the whole thing (O(n)), then do heappop k times to find the smallest elements (log n operation k times)? This would be O(n + k log n), which seems better than O(n * log k) since n > k. Why does Python choose the former implementation?
https://github.com/python/cpython/blob/main/Lib/heapq.py has extensive comments about this. In particular it says:
Other algorithms were not used because they:
- Took much more auxiliary memory,
- Made multiple passes over the data.
- Made more comparisons in common cases (small k, large n, semi-random input).
See the more detailed comparison of approach at:
http://code.activestate.com/recipes/577573-compare-algorithms-for-heapqsmallest
My guess is that the solution you suggest was rejected because of (1). The chosen solution makes it possible to find theksmallest elements without ever having all the values in memory (for example, if they're generated by an iterator).
Actually, the link in the source code confirms my suspicion: It explicitly considers your suggested algorithm but says it "is not space efficient".
right, O(n + k log n) and O(n log k) differs in memory usage
O(n) vs O(k) extra usage
but ultimately it's a trade-off
both solutions are good and make different sacrifices
I didn't know this, but that makes sense. Imagine the case where the iterator has trillions of elements.
In typical usage, k is probably pretty small compared to n.
an interview question I asked actually touched upon that
the problem boils down to some preprocessing followed by getting the top K values
and I expect people to arrive at one of the two solutions here (maybe with some hints)
in C++ there is also std::partial_sort which does the right thing, though I never saw anyone jump to that in an interview
(granted, not a lot of people interviewing in C++)
I think I've only had one candidate completely nail it without any hints, and that candidate was just impressive to the extent that I had to pad with extra twists on the problem 😄
This looks like a problem that can be solved with a bottom up approach but I'm not sure how to go about it
To do this bottom up, you would figure out, for each day n, the minimum cost strategy for days up to n. The cost of a strategy is the cost of whatever contract you most recently bought, plus the cost of whatever contracts you bought previously. That is, if you're on day n, then you want to consider: What's the cost if I bought a one-day plan yesterday? A one-year plan one year ago? A two-year plan two years ago? A five-year plan five years ago?
what is two sum algo used for? it was the first problem i tried solving but failed miserably lol
anyway i wonder how they use it in real applications?
It's an instance of the knapsack problem. Generalizations of this problem turn up frequently in combinatorial optimization.
thank you alot
Okay so I start at the last day and not day 1?
You can do it either way.
Can someone help with my pandas dataframe project? I am trying to find all duplicates
I am trying to find duplicates by group
group with an aggregate function of count then select those with count > 1
Hey @jagged void!
You either uploaded a .txt file or entered a message that was too long. Please use our paste bin instead.
Hey @jagged void!
You either uploaded a .txt file or entered a message that was too long. Please use our paste bin instead.
what are some good resources to learn algorithms and data structures?
clrs
@agile sundial https://en.wikipedia.org/wiki/Introduction_to_Algorithms ?
Introduction to Algorithms is a book on computer programming by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The book has been widely used as the textbook for algorithms courses at many universities and is commonly cited as a reference for algorithms in published papers, with over 10,000 citations documented on C...
indeed
will check out
hey guys, I solved the problem Given a roman numeral, convert it to an integer. ```python
def romanConversion(s):
complete_string = 0
roman_dictionary = \
{"I": 1, "V": 5,
"X": 10, "L": 50,
"C": 100, "D": 500,
"M": 1000}
for letter in s.upper():
if letter in roman_dictionary.keys():
complete_string += roman_dictionary[letter]
print(complete_string)
romanConversion("XXVII")``` What's a better way to solve it?
romanConversion("IV") returns 6 instead of 4.
That’s true
How would you solve it
You need to keep track of the most recent value you saw.
If the next value is larger than the previous one, then you should have subtracted.
fwiw I prefer parsing roman numerals backwards
add if the next number is ≥ than the biggest seen so far, otherwise subtract
!e let's try
values = {"I": 1, "V": 5,
"X": 10, "L": 50,
"C": 100, "D": 500,
"M": 1000}
s ="XIV"
biggest = 0
res = 0
for d in reversed(s):
val = values[d]
if val >= biggest:
res += val
else:
res -= val
biggest = max(biggest, val)
print(res)
@haughty mountain :white_check_mark: Your 3.11 eval job has completed with return code 0.
14
I wrote a program to calculate the slope of a line.
` rise = 0
run = 0
y1 = 3
y2 = 7
x1 = 2
x2 = 5
rise = y2 - y1
run = x2 - x1
slope = rise / run
print(slope)`
That's not what this channel is for, if you want a basic introduction to python you can search for many tutorials online (preferably written documentation).
You can try the official tutorial https://docs.python.org/3/tutorial/
thank you reffie
I can't get this code to work
roman_numerals = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
}
class Solution:
def int_to_Roman(self, num):
roman_numerals_tuple = [(value, key) for value, key in roman_numerals.items()]
roman_numerals_tuple.reverse() #reversing the order of the list, for divmod function
roman_numeral = ""
for (n, roman) in roman_numerals_tuple:
(d, num) = divmod(num, n) # error on this line
roman_numeral += roman * d
return roman_numeral
S = Solution()
print(S.int_to_Roman(131))``` does anyone know how to fix it? /
there isn't really any choice to be made, so no
@round valley :white_check_mark: Your 3.11 eval job has completed with return code 0.
MMCCXLII
!e
map = {1000: 'M', 900: 'CM', 500: 'D', 400: 'CD', 100: 'C', 90: 'XC', 50: 'L', 40: 'XL', 10: 'X', 9: 'IX', 5: 'V', 4: 'IV', 1: 'I'}
map2 = {v: k for k, v in map.items()}
def dec_to_roman(n):
roman = []
for r in map:
x, y = divmod(n, r)
roman.append(map[r] * x)
n -= (r * x)
if n <= 0:
break
return ''.join(roman)
def roman_to_dec(r):
dec = 0
while r:
for k in map2:
if r.startswith(k):
dec += map2[k]
r = r.removeprefix(k)
break
else:
raise ValueError('Not valid Roman numeral.')
return dec
print(dec_to_roman(2242)) # 'MMCCXLII'
print(roman_to_dec('MMCCXLII')) # 2242
@round valley :white_check_mark: Your 3.11 eval job has completed with return code 0.
001 | MMCCXLII
002 | 2242
You can get a free sandwich on Thursday only if you buy a sandwich or a cup of soup
how can i translate this propositional logic?
like what could the propositions be here
p: You buy a sandwich
q: You buy a cup of coffee
r: You get a free sandwich on Thursday
or
p: You buy soup or sandwich
q: It is Thursday
r: You get a free sandwich
i would have
you buy a sandwich
you buy a cup of soup
it is thursday
you get a free sandwich
Has anyone here tried solving the cses.fi problem set in python. Is it doable with the 1 second time limit?

I lost my account where I did most of the stuff in c++. It's been years and I have forgotten everything 🥲
Are there other platforms with the same cses questions?
:(
i am really struggling with time complexity
can someone help me
I understand the basic concept that x^n is worse than n^2 and that is worse than n etc
but i do not understand how this relates to actual numbers. of time relative to n
like here's an example:
n time
40 000 2 024 ms
80 000 6 085 ms
120 000 13 781 ms
160 000 23 079 ms
200 000 35 934 ms
I look at this and cant figure out if this is n, n^2 or something else. n is suppose to be linearly increasing, so something like 2-4-6-8 etc. In this case first one is 2k and second is 6k, then it's 13k. It does not appear to be a consistent linear increase. for n^2 it is suppose to be 2k, 4k,8k etc right? but this is 2k, 6k, 13k which is not quite right
but perhaps my understanding of linear is wrong
x^n is really really slow for large n's. so slow that, for larger n's the program would literally not finish before the heat death of the universe
n^2 is not linear
it's polynomial
2^n is exponential
n is suppose to be linearly increasing, so something like 2-4-6-8 etc.
i mean, in reality you'll never see perfect values like that. A regression plot of your values looks like this, so it might be quadratic or linear, it's hard to say.
that's a pretty graph but it implies negative execution time for n < 40000
that seems... unlikely
but i do not understand how this relates to actual numbers
In theory, not at all, since the O-notation is about asymptotic behaviour, and at finite n other factors may dominate.
(though in practice, algorithms with better asymptotic complexity are in fact usually better.)
but if that is the case how do I figure out which time complexity a table like above has ?
like another table is
300 000 000 545 ms
350 000 000 610 ms
400 000 000 740 ms
450 000 000 831 ms
500 000 000 900 ms
this seems like logn tho
because its so small increase
900/545 is 1.65, 500/300 is 1.66, so this looks nearly perfectly linear to me.
yeah so basically I dont know where the problem lies but I cant figure this out
what problem? what's to figure out?
problem that I look at second table and think its logn when its linear
well, it could be log n. unlikely, but possible
you don't have a wide enough range of n's to be sure
both polynomial and exponential and logarithmic time can appear linear time if you zoom in enough
I don't know how to ask this without sounding like an a**hole, but didn't you cover this shit in HS math class?
This is not going to be a simple answer, so I won't explain what time complexity is. I will only explain why it's a bad idea to guess time complexity from runtime. This answer is not for a beginner and probably is not suitable for learning exercises.
According to my experience, time is not in a tight relationship with time complexity. If you know a time complexity of an algorithm and a general time it takes to handle a small query, you can use that to roughly approximate the time it will take the algorithm to run on a larger query. By "roughly" I mean you will be at most 10 times off (with good enough p-value). This does not work the other way around at all. By having just 5 points you will not be able to guess the time complexity. At least a few dozens (preferably hundreds) are required to make some assumptions.
Why? Because big-O only shows the largest term in value-time mapping. In reality it always comes with a constants you will need to guess. Even if you assume your time is a polynomial an^2 + bn + c this will require you to have at least 4 points to determine a, b, and c, but since they can vary significantly, those are absolutely useless, and you need to do actual analysis to find a, b, and c. Otherwise you are just eyeballing the big-O with no scientific reasoning.
And time will vary. The address space in your PC is randomized, which means sometimes the physical page layout will be better and your code will perform better. Sometimes your environment variables will be larger, the stack will be aligned better and will use cache more efficiently. Sometimes you will randomly shuffle a few pieces of code around, compiler will optimize it slightly differently, and branch predictor in the processor will be correct more often. This may lead to performance differences of up to 50%. This is much less noticeable in python of course, you effect still applies to some degree, especially the garbage collection/allocation overhead may vary significantly.
There is a wonderful talk at strange loop about it: "Performance Matters", and I can confirm that everything what is said there is true. Trust me, I've done a lot of optimization in my life and I know that modern processors don't make any sense.
so when all thats given to me is just n and time and asked to figure out the time complexity based on only those two things is, my best bet is just gut feeling
and some rough context based on how close numbers are to each other
Pretty much. To be honest, I have no idea why would you even ask that question. If you have an algorithm, you can easily approximate it's time complexity. If you have it's actuall run time, you have a way, way more powerful tool than time complexity.
well above tables were mostly training questions which I was not confident in my answers so wanted to ask here to get some help
and there was no other context
just a table with time and n
no, your best bet is to look at the ratios
don't go with gut feel
you can make a guess, you will never be sure
Look at the ratios of times and the ratios of ns. For linear, the ratios should be roughly the same. For quadratic, the time ratio should be roughly the square of the n-ratio. For logarithmic, a multiplicative increase of n corresponds to an additive increase in time. For exponential, the opposite.
because "gut feel" is your lizard brain at work and your lizard brain is not good at math
i see
on another question about time complexity. When there is two or more recursions the time complexity becomes someting like 2^n, so if there is only 1 recursion is it 1^n ?
or what is it exactly in that case
because 1^n is constant
as in its always 1, so it does not feel right to assume that
That's a cool video btw, watching it rn
Don't try to invent rules like "when there are two loops, it's O(n^2)" or "when there are two calls in recursion it's O(2^n)". Those are all wrong. Actually learn what time complexity is and use it for what it is.
yeah i am guilty of going based off of memorization rather than context
A cool example that my prof gave for time complexity was the following
i = N
while i > 0:
i = i // 2
j = i
while j > 0:
j = j - 1
Which isn't O(N log N) like you would expect at first glance 🙃
yeah looking at that code makes me feel like i know nothing about time complexity 😦
like that feels like nested loop to me
so a n^2
Or there are a lot of examples based on harmonic series, where it's absolutely not obvious why it's O(n log n) unless you know what too look for
for i in range(2, n)
for j in range(0, n, i): ...
But my personal favourites come from number theory
for example a simple gcd fold with euclidean algorithm, like this:
a = [...]
g = 0
for x in a:
g = gcd(g, x)
Is not just O(n log C), but O(n + log C). And I love this because there is so much complexity in this simple example. It's so, so deep and beautiful.
This is a "guess what comes next problem." Answering it involves inductive reasoning (statistics), which does not guarantee things. The best option here is to apply Occam's razor and pick the most simple curve that explains it (there are infinite valid answers otherwise), which is one of the simple curves like linear, quadratic, log, exp. Start with the most simple one. But without really having more points, it could easily be anything, and even then, we are dealing with probabilities, not guarantees. Given only those few points, you could give many different answers and argue that they are valid (this is why the "guess the next number," problems you may have seen are silly (they are more of a bias check than anything else)). @crude galleon
it's such a cool example I've on separate occasions encountered nested loops with this complexity in a help channel and in the staff lounge 🥴
also, pedant inside me wants to say it is in fact O(N log N), it's just not Θ(N log N)
It's O(N), because it takes n/2 + n/4 + n/8 ... iterations
But this was more of a trick question when we had it on the exam
I know it's Θ(N), the pedantry relates to the fact that e.g. any function that's O(n) is also O(n^2) and O(e^n) and infinity others, because O is an upper bound.
Most time people talk about O, they actually mean Θ - not just an upper bound, but a tight one, one that can't be improved.
when it talks about growth rate in this table what does it mean exactly
like are those numbers suppose to represent t or t(n)
I'm not sure what you're asking. For example, n^2 for n=8 is 64. It's just examples of some functions.
ah I see, I was reading the table wrong
its columns in relation to the first column
that's O(n) isn't it?
lol
t(n), of course
t is dependent on n
it's ||Θ(n), because the complexity is the sum N//2 + N//4 + ... + 1, which sums up to at least N/2 and no more than N, which is Θ(n)||
well, at least my conclusion was correct 🙂
that doesn't count if you can't support it 😛
there, now I appear smarter
one can naively say it's O(n log n) in the same way one can naively say that || because the sum N//2 + N//4 + ... + 1 has ~log(N) terms with the highest being N//2, it's at most N log N|| - it's a correct upper bound, but it turns out it's not a tight one in this case.
*With the added bias of Occam's razor this becomes abductive reasoning.
so I am looking at this code
public void compute(int a[], int l) {
if (l <= 1) {
return;
}
compute(a, l - 1);
int last = a[l - 1];
int i = l - 2;
while (i >= 0 && a[i] > last) {
a[i + 1] = a[i];
i--;
}
a[i + 1] = last;
}
and trying to figure out the time complexity for it . There is a single recursion so that is an O(n) and there is a simple while loop that check two conditions which is another O(n). Does it mean that this code has time complexity of O(n^2) ?
or am I totally wrong
you can't think "loop -> O(n)". stop thinking this, it's not correct
should I look at whats inside the parenthesis instead of the loop ?
well you need to look at both
your answer ||seems to be correct, but your reasoning is a bit shaky||
ultimately, you need to count the number of operations, pretty much.
i actually like this code it's kinda clever
i know that the if statement is one and done thing so thats O(1), so is int last and int i
same applies to the things inside the while loop
last line of code seems to be O(1) too
even that isn't true. consider, if all(a_list): (unless you're talking about this situation, in which case, it is true)
i suck at this
The thing inside the while loop is O(1) * however many times it is triggered in while loop
yeah I was thinking this specific code
Just to be clear, you understand what this code does, right?
because I would start from here. It's hard to figure out how long something takes if you don't even know what it does
More importantly how it does what it does
hmm, I am actually not sure if I am being honest.
perhaps my issue is fundamental
like I know there is a recursion and a break point
Not as fundamental as my brain damage, you will figure this out eventually 🙂
lol, sorry for annoying you guys
Look at the code, follow it step by step, figure out what it does, estimate how many operations it takes
no this is fun, don't worry about that. well, not fun in the sense that we're happy you're struggling, but like
it's fine
my one issue is that inside the while loop
int last = a[l - 1];
int i = l - 2;
while (i >= 0 && a[i] > last) {
let's say l is 5. a[4] then i = 3. Which means inside the while loop a[3] > 4 will not be true
and this is an and statement
so this will be false
then nothing inside the while loop matters
then you would do a[3 + 1] = last which means last is now 4.
a[4] is a value in array a at index 4, this is not 4, so last is not necessarily 4, so a[3] > 4 can be true
so int last = a[l - 1]; is not the length of the array ?
it is the value in the array at l - 1
which language is that btw 💀
Nope, why would it be the length? Consider a = [6, 2, 3, 5, 1, 4, 5, 7] and l = 4 or something, idk. The a[l - 1] is 5. You know how arrays work, don't you?
c# or java
java
same thing
ah 2 I don't care to learn 💀
hmm perhaps I have confused new Array [4] being the length of array with the above case
so I take it you are taking something like a discrete math course or algos and datastructures?
I'd recommend you familliarize yourself with the language first
it'll be hard to understand any of this if you don't know what the code actually does at any given point
write some code
maybe even do leetcode
I suggest you start sooner rather than later
just do some easy leetcode problems
your code will be slow
ask people why
i never heard leetcode so will check that out
https://leetcode.com/problems/isomorphic-strings/
maybe try this one not a hard problem
if your solution is n^2 it won't submit I think
so i have been trying to understand this code. i don't know how close I am to truth but here goes nothing. it seems like this program compares a value in a specific index of an array with the value at one lower index. if the number with smaller index is larger than the other one then it replaces the value. Then you basically repeat this to compare the value at one lower index until the while loop becomes false. After that the value at index 0 becomes the value at index of "last" variable
I am not confident in my logic ngl
idk how the compute recursion comes into this playing field
not index 0, last is placed in the last location of i before the loop stopped
so this code basically turns an array into ascending right
from smallest number to highest
in other words it is a sorting algorithm if you are right
indeed. can you guess which one?
👏👏 👏 👏 👏
(bonus points)
you mean which type of algorithm, like bubble sort etc
?
(different sorting algorithms will obviously perform differently)
hmm, Its very hands on method so i believe it's definitely not one of the more complex algorithms like the one that uses binary tree
but i cant quite name which exactly
looking up in internet it seems to be insertion
it ends with "sort"
well if you just ran it for one item it would not work
are you familiar with insertion sort?
you'd need at least another loop
not really
in py sorting with recursion wouldn't work so well 😭
quick read up on the internet says, well basically what this code does. Just compare everything to one specific thing until everything is in right place
ah that would explain it. essentially the idea in insertion sort is you incrementally build up the sorted array on the left side of the array by shuffling elements to the right if they're greater
which sounds like it would be really annoying algorithm to use if you have millions of number to go through
indeed, it's relatively slow compared to more advanced algorithms. but insertion sort is actually still pretty useful
it is easy to write but for that I prefer selections sort
so i know that due to the nature of arrays it's a O(1) to pick a random number in it
but for things like comparing the index value of an array with another index value
is that still O(1)
yuh for time complicity you always want to look at how many times the loops run
the elements themselves you mean? that depends on the type of the elements
yeah element
recursion in this case being a sort of loop
hi everyone. does anyone have experience with linear programming using PuLP? :D
for ints you can assume it's O(1), but strings is O(len(the_string))
i'm trying to implement a problem where the values in one time period are dependent on values in another but don't know how to model that using PuLP
are u sure would the pc not just swap the memory adresses of the items or however the fuck that works?
my first solution was O(n^2) which is how I know it fails ;D
in relation to how soon it achieves it's goal right? or in relation to something else?
well how long it takes in the worst case is what they will ask most of the time
i am now thinking(and I might well be wrong) that above while loop is O(n) because the loop basically goes through every element and compares it to the key element
well yes it is but you are forgetting something very important
is it the recursion?
it only changes the element of the last index right
nothing? the function returns
well if it's insertion sort something should happen
wdym "an iteration". of the while loop?
not sure can't read java
I know how the algorithm is supposed to work but I can't say anything about this implementation
since again I don't understand the syntax
@crude galleon have you looked at binary search yet?
and you understand its time complexity?
binary search is logn right, cause when n increases you need only one more time to run it
it is logn that is correct I don't really understand what u mean by that explanation but I think it's nice to compare it to linear search to see the difference between logn and n
hi everyone. does anyone have experience with linear programming using PuLP?
i'm trying to implement a problem where the values in one time period are dependent on values in another but don't know how to model that using PuLP
binary search halves the searchspace on each iteration, so doubling the input size only requires one more iteration
also if u know some basic linear algebra try to find a algorithm that does not need to multiply the matrix n times for matrix exponentiation
talking about binary trees a[i] = Arrays.binarySearch(n, i); this has no effect on time complexity right? i dont quite understand what this method does
if there is a part of the function with higher time complexity than it it can be ignorted but it depends on the program
then that will probably n*log n
public int[] compute(int[] n) {
int[] a = new int[n.length];
for(int i = 0; i < n.length; i ++) {
a[i] = Arrays.binarySearch(n, i);
}
return a;
}
so the for loop is simple iterative function
so that's n
idk what binarysearch does but if it is using binary tree algorithm it would be log n right ?
and then you multiply these
ye
cuz the loop runs n times if you are telling the truth
and it will run this method n times
also it could be n*logm don't know what the method does specifically
hmm so I can not just say a loop is O(n) by looking at the for front of (). Thats just wrong way of thinking. But algorithms have specific always the case time complexity right ?
like a binary tree is always log n
how do you even do it in O(n^2)? i can't come up with a solution that would genuinely work in quadratic time
then you don't have the type of galaxybrain I had when I started coding
💀
actually. binary search will be log n always, but binary trees do not necessarily have O(log n) search
that's not quadratic
don't worry abt it

