#algos-and-data-structs
1 messages · Page 47 of 1
you can for real just chill
unless you wanna be one of those people who works late every day. It's fine to walk in on day 1 and start learning. You'll be slightly behind but nothing crazy.
I say this as somebody who switched my degree to bioinformatics as a junior at an R1 research university and had no problem going through data structures, algorithms, comp bio, linear algebra, etc.
flipside of my advice: all the best coders I know started as actual children
why they are good at it has nothing to do with academic work
and everything to do with reading about compilers and interpreters
like not academic reading, just reading documentation. If you go look at postgres or python's docs, you'll find chill-style explanations of how stuff works. Python for example is actually an extremely flexible runtime, like lisp
but the way it's designed is not actually taught in schools cause you gotta learn what a linked list is, which isn't actually going to help u say interface with a bank's api or assemble a hodgepodge of code into a singular behemoth that operates via some coordinating principle u probably learnt from studying other behemoths
should say, it's taught, but it's glanced over
Does anyone knows where i can find oreilly books for free (learning algorithms book specifically)
You can get access to all their books for 10 days if you sign up for their free trial: https://www.oreilly.com/library/view/learning-algorithms/9781492091059/
But then I think the subscription is quite expensive after that.
If you're currently studying at a university, they may have it in their physical/digital library.
"these days going low level doesnt actually what gives you ideas, it is to express your ideas so cleanly that you can think about it and then optimizer can understand what youre up to"
hey guys i found this on my keep notes... to everyone here.. when or what was the catalyst that helped you able to express your ideas in code... like what helped you or what made it so that coding was like a second nature to you. thanks!
yo, I just added another class to my schedule, and he gave me the first homework assignment. I've figured out a few of the problems, but I'm not sure what he means by "provide a tree of recursive calls for a(2, 1)". Problem 4 btw
I would assume it's something like these, but I wouldn't know how to structure it the same way.
What is the idea making a simple photo editor?
- Load the photo
- Apply Blur
- Apply Brightness
- Reduce Blur
- Reduce Brightness
How can I store these data? So i can go back and forth?
In my current implementation i load the image and apply the blur then save the image in the loaded variable. So next time if i reduce the blur it just apply the new blur in the blurred image that leaded to blur the image but a small amount. It does not reverse.
So I am thinking like
Using a container and adding the new operation to it and removing the duplicate operation like ``adding new blur operation and removing the duplicate blur operation". Applying 1st added operation 1st and 2nd add operation 2nd on the original image instead of applying the modified image.
Will this work? What technique the image editor use? What container will appropriate for this?
hey smart people, im studying bfs and i found that sometimes you can use something like
graph = {}
deque = [(start, [start])]
while deque:
curr, path = deque.pop(0)
if curr == end:
print(path);break
if curr in graph:
for neighbor in graph[curr]:
deque.append((neighbor, path + [neighbor]))
that´s just an example btw
but sometimes I've seen codes where instead of that they use
from collections import deque
graph = {}
deque = deque([(start, [start])])
while deque:
curr, path = deque.popleft()
if curr == end:
print(path);break
if curr in graph:
for neighbor in graph[curr]:
deque.append((neighbor, path + [neighbor]))
i dont understand why would you import deque when you can avoid it without any problem, if you know more about the topic or you can give me more tips about bfs it would be great
For large datasets, the deque will be faster
How much knowledge of python should I have before moving on to algos and data structures?
int memoise(vector<int> &W, vector<int> &V, int n, int cap, int index, int v, int w, vector<vector<int>>& memo){
if (w>cap)
return 0;
if(index==n) return v;
if (memo[index][w] != -1) return memo[index][w];
int not_inclu = memoise(W, V, n, cap, index+1, v, w, memo);
int inclu = memoise(W, V, n, cap, index+1, v+V[index], w+W[index], memo);
int here = max(inclu, not_inclu);
return memo[index][w] = here;
}```
knap sack, where is the problem???
n= 7
W = 1 5 6 9 7 9 7
V = 1 7 1 5 1 7 7
cap = 37
case doesnt pass
I'd say you should be familiar with the basics such as functions, flow-control (if, while, for) and classes,
python's types including str, list, dict, set, int and some of their methods,
some common builtin functions such as max, min, sum
since these are pretty essential in DSA and it's good to be able to focus on the new DSA stuff when learning than to trip up on the python part.
I don’t have much experience with classes dict and sets
check this video https://youtu.be/0K_eZGS5NsU?si=cIjmX7sTZj0CzK65
🚀 https://neetcode.io/ - A better way to prepare for Coding Interviews
Checkout my second Channel: @NeetCodeIO
🥷 Discord: https://discord.gg/ddjKRXPqtk
🐦 Twitter: https://twitter.com/neetcode1
🐮 Support the channel: https://www.patreon.com/NEETcode
⭐ BLIND-75 PLAYLIST: https://www.youtube.com/watch?v=KLlXCFG5TnA&list=PLot-Xpze53ldVwtstag2...
ah I see. there are some good tutorials on realpython, this one for dictionaries:
https://realpython.com/python-dicts/
feel free to ask any questions as you learn about it
I expect it's just a tree where the connections show function calls, i.e. if f(n) calls f(a) and f(b) then add two children to f(n), f(n)→f(a), f(n)→f(b)
"without any problem", well that's not true
it will cause severe performance problems
popping from the beginning of a list is O(n), popping from the beginning of a deque is O(1)
hey guys, need some help rq
so heres my code:
``# Python program for binary search
import random
Create randomNumbers array
def randomNumbersArray():
randomnumbers = []
i = 0
while i < 24:
randomnumbers.append(random.randint(1, 100))
i += 1
print(randomnumbers)
print(len(randomnumbers))
print()
return randomnumbers
def sorting(numbers):
list.sort(numbers)
print(numbers)
return numbers
def numToGuess(numbers):
pos = random.randint(1, 24)
target = numbers[pos]
print("What number am I thinking of?")
return target
def binSearch(numbers, number):
array = numbers
toguess = number
minimum = 0
maximum = len(array-1)
guess = maximum+minimum/2
numToGuess(sorting(randomNumbersArray()))
binSearch(target, array)
``
problem is, apparently that last line throws an unresolved reference error
what did i do wrong?
import random
# Create randomNumbers array
def randomNumbersArray():
randomnumbers = []
i = 0
while i < 24:
randomnumbers.append(random.randint(1, 100))
i += 1
print(randomnumbers)
print(len(randomnumbers))
print()
return randomnumbers
def sorting(numbers):
list.sort(numbers)
print(numbers)
return numbers
def numToGuess(numbers):
pos = random.randint(1, 24)
target = numbers[pos]
print("What number am I thinking of?")
return target
def binSearch(numbers, number):
array = numbers
toguess = number
minimum = 0
maximum = len(array-1)
guess = maximum+minimum/2
numToGuess(sorting(randomNumbersArray()))
binSearch(target, array)
ask it in a help channel
Ok thanks!
neither target nor array are defined
buckets = []
outliers = [ [] , [], [] ] # <- data and stuff
minis = [mini,davg,uavg]
maxis = [davg,uavg,maxi]
for y in range(0,3):
if outliers[y]!=[]:
m = minis[y]
buckets.append([0 for x in range(0,maxis[y]+1-minis[y])])
for x in range(0,len(outliers[y])): # <- this loop
buckets[y][outliers[y][x]-m]+=1
else:
buckets.append([])
is there a way to make the inner for loop faster?
do you want to use numpy
how many items can a numpy array handle?
billions probably, if you have memory
buckets = []
outliers = [[], [], []] # <- data and stuff
minis = [mini, davg, uavg]
maxis = [davg, uavg, maxi]
for outlier, mini, maxi in zip(outliers, minis, maxis):
if outlier:
bucket = [0 for _ in range(maxi + 1 - mini)]
for out in outlier:
bucket[out - mini] += 1
else:
bucket = []
buckets.append(bucket)
as numpy:
import numpy as np
buckets = []
outliers = [[], [], []] # <- data and stuff
minis = np.array([mini, davg, uavg])
maxis = np.array([davg, uavg, maxi])
widths = maxis + 1 - minis
for outlier, mini, width in zip(outliers, minis, widths):
if outlier:
bucket = np.zeros(width)
bucket[outlier - minis] += 1
else:
bucket = np.zeros()
buckets.append(bucket)

though depending on what you're doing, numpy has histogram functions already
i had no idea baout the zip function before lol 
let's you iterate over multiple things at the same time!
!e
for a in zip("cat", "dog"):
print(a)
for a, b in zip("cat", "dog"):
print(a, b)
@stable pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | ('c', 'd')
002 | ('a', 'o')
003 | ('t', 'g')
004 | c d
005 | a o
006 | t g
numpy has https://numpy.org/doc/stable/reference/generated/numpy.digitize.html if you're binning data though, which might be useful
very interesting
another part of my code which is really slow is
list1 = [ 1,2,3 ]
llen = len(list1)
outliers= [ [],[],[] ]
for y in range(1,llen):
a = list1[llen-y]
if a < davg:
outliers[0].extend([a])
elif a >= uavg:
outliers[2].extend([a])
else:
outliers[1].extend([a])
list1.pop()
list1=[]
print(outliers)
i feel like adding and popping things repeatedly are slow, but i profiled it and it said it wasnt
while list1:
a = list1.pop()
if a < davg:
outlier = outliers[0]
elif a < uavg:
outlier = outliers[1]
else:
outlier = outliers[-1]
outlier.append(a)
you're creating lists just to have python delete them immediately with extend([a])
doesnt append do the same?
no, it doesn't create an intermediate list
the [a] in extend([a]) creates a list, append(a) does not
so you're creating a list, that then gets garbage collected (deleted), and extend also has to iterate over this list which is slower than just appending a single element
it performed at about the same speed as the original code
i think its just because im running 100 thousand if statements
this is another thing that can be vectorized by numpy though
list1 = np.array([1, 2, 3])
below = list1 < davg
upper = list1 >= uavg
middle = ~below & ~upper
!e
import numpy as np
list1 = np.array([1, 2, 3])
below = list1 < 2
upper = list1 >= 3
middle = ~below & ~upper
print(below, upper, middle)
@stable pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.
[ True False False] [False False True] [False True False]
is this faster than the if statements? im not that great with numpy lol 
yes, numpy does things in a compiled language
it's doing the if states in c++ instead of python
which can be 100x or 1000x faster depending
ohhh, so its faster but in the algorithm is the same
yep
for instance
list1 = np.array([1, 2, 3])
below = list1 < 2
to compute below, numpy iterates over list1 comparing each element with 2 just like your loop
!e you can get the indices of the truth values btw:
import numpy as np
list1 = np.array([1, 2, 3])
below = list1 < 2
print(below)
print(np.nonzero(below))
@stable pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | [ True False False]
002 | (array([0]),)
!e or probably better:
import numpy as np
list1 = np.array([1, 2, 3])
below = list1 < 2
print(below)
print(np.flatnonzero(below))
@stable pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | [ True False False]
002 | [0]
pretty cool
ill try and see if i can think of a way to algorithmically make it faster instead of relying on thousands of if statements lol
i have a different algorithm that does basically the same thing but with a much larger auxillary array
the auxillary verison takes 0.8 seconds to sort a list of 1000000
and the version without the array takes 2 seconds to sort the same sized list lol
there is a branchless way to get bin, but i don't know if its faster
for a in reversed(list1):
outliers[2 - a < davg - a < uavg].append(a)
oh thats rly interesting
ngl, ur probably better and cleaner at the syntax thats for sure lmao
or can flip the conditions i think
for a in reversed(list1):
outliers[a >= davg + a >= uavg].append(a)
apparently the branched version runs about 5-10% better lol
just from what i was testing
yeah, for the branchless version, python has to do some coercions and call some dunders --- i think branchless would be faster in c though
gc.disable()
for y in range(0,len(list1)):
a = list1[y]
if a < davg:
outliers[0].append(a)
elif a >= uavg:
outliers[2].append(a)
else:
outliers[1].append(a)
list1=[]
print(list1)
gc.enable()
out of all the versions this one seemed to run the fastest

weird lol
you don't need to disable gc for that
for some reason it just runs faster
its like slapping the top of the TV
i mean, you can, but there's nothing to gc, it just prevents python from checkiing
ahh, thats probs why its faster
it probably stops it checking each time the list updates (im guessing)
it won't be each time, it will just check after some number of operations
i don't really know much about the internals, just at some point python will check if anything needs to be collected
yeah, bulk garbage collection seems to be faster than lots of little collections and redundant checks
range(0, n) is the same as range(n) though
you can iterate directly over the list1:
for a in list1:
if a < davg:
outliers[0].append(a)
elif a >= uavg:
outliers[2].append(a)
else:
outliers[1].append(a)
oh, thats just because i like to leave the 0 open in case i need to change anything or fix an off by one error or something
it might be faster because i dont need to read from the list when assigning a, ill check and see 
you are right, i did notice that this morning
fixed most of my issues, rest of my issues are logic issues which i am dealing with one by one now, will update in 30 minutes hopefully
anyone down to help me rq?
import random
# Create randomNumbers array
def randomNumbersArray():
randomnumbers = []
i = 0
while i < 24:
randomnumbers.append(random.randint(1, 100))
i += 1
print(randomnumbers)
print(len(randomnumbers))
print()
return randomnumbers
def sorting(randNums):
list.sort(randNums)
print(randNums)
return randNums
def numToGuess(numbers):
pos = random.randint(0, (len(numbers)-1))
print(pos)
target = numbers[pos]
print("What number am I thinking of?")
print(target)
return target
def binSearch(target, numbers):
minimum = 0
maximum = len(numbers)-1
guess = round((maximum + minimum)/2,)
valGuess = numbers[guess]
while guess != target:
print(f"My guess is {valGuess} at position {guess}\n")
if guess < target:
print("Target is larger than your guess\n")
minimum = guess + 1
guess = round((maximum + minimum)/2, )
print(f"My new guess is {guess}\n")
elif guess > target:
print("Target is less than your guess\n")
maximum = guess - 1
guess = round((maximum + minimum)/2, )
print(f"My new guess is {guess}\n")
else:
print(guess)
print(f"My final guess is {guess}")
print(f"That's right my number is {target}!")
def main():
randNums = randomNumbersArray()
sortedNums = sorting(randNums)
target = numToGuess(sortedNums)
binSearch(target, sortedNums)
main()
my binary search never fully completes
might be a logic error, i am not entirely sure, thanks for the help in advance
this binary search does not make much sense. You are using guess as the index, but compare it with target, which is a value in the array. You never update valGuess.
also don't use round(smth/smth), use // operator if you want to round down (and here you want to round down)
round does not round how you want, most likely
This is not a Modmail thread.
oops
!eval
for x in range(1, 11):
for y in range(1, 11):
print(f"{round((x + y) / 2):>4}", end="")
print()
@outer rain :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 1 2 2 2 3 4 4 4 5 6
002 | 2 2 2 3 4 4 4 5 6 6
003 | 2 2 3 4 4 4 5 6 6 6
004 | 2 3 4 4 4 5 6 6 6 7
005 | 3 4 4 4 5 6 6 6 7 8
006 | 4 4 4 5 6 6 6 7 8 8
007 | 4 4 5 6 6 6 7 8 8 8
008 | 4 5 6 6 6 7 8 8 8 9
009 | 5 6 6 6 7 8 8 8 9 10
010 | 6 6 6 7 8 8 8 9 10 10
uhm not really, if it work for you then it's great
Is there any webpage that visualise how are recursive calls made and what is returned by given code and data?
have you tried just adding some prints?
!e untested code, go
def fib(n, depth=0):
print(' '*depth + f'call fib({n})')
if n <= 1:
res = n
else:
res = fib(n-1, depth+1) + fib(n-2, depth+1)
print(' '*depth + f'return {res}')
fib(4)
@haughty mountain :x: Your 3.12 eval job has completed with return code 1.
001 | call fib(4)
002 | call fib(3)
003 | call fib(2)
004 | call fib(1)
005 | return 1
006 | call fib(0)
007 | return 0
008 | Traceback (most recent call last):
009 | File "/home/main.py", line 9, in <module>
010 | fib(4)
011 | File "/home/main.py", line 6, in fib
... (truncated - too many lines)
Full output: https://paste.pythondiscord.com/BRJSL5ZDU2V5CFAFMT4YHF5QQQ
err
oh, return
!e
def fib(n, depth=0):
print(' '*depth + f'call fib({n})')
if n <= 1:
res = n
else:
res = fib(n-1, depth+1) + fib(n-2, depth+1)
print(' '*depth + f'return {res}')
return res
fib(4)
@haughty mountain :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | call fib(4)
002 | call fib(3)
003 | call fib(2)
004 | call fib(1)
005 | return 1
006 | call fib(0)
007 | return 0
008 | return 1
009 | call fib(1)
010 | return 1
011 | return 2
... (truncated - too many lines)
Full output: https://paste.pythondiscord.com/4MSR6REIDRNAPRIUOHWX5UR6GE
you know @halcyon plank, getting a few more lines would be nice
Another good example to practice imagining recursion 🙂
After writing in my notebook recursive calls, I understood one problem that I was trying to solve. I guess with time it is easy to visualise it in the head.
I mean, a lot of the time I don't need to visualize
often reasoning about the recursion can be done as "assuming the recursive calls I make returns the correct thing, do I do the right thing?"
Hi Guys, I am new to DSA. Although i am an intermediate level python developer i am finding it hard to understand backtracking and dynamic programming. Could anyone suggest some good material to learn those?
is yielding or returning a list/set faster? I’ve been assuming the former is faster
Like say i want to output a million numbers from some arbitrary function that i generate through some iterative method
would it be faster to yield each number or add each one to a list/set/dict, and then return that list/set/dict
Does anyone know of a library that supports sparse matrices of n > 2 dimensions on GPU in python? I'm currently using cupy for GPU matrix operations, but their implementation is just a copy of SciPy so their sparse matrices only support 2D matrices.
cool thing about iterators is that they are lazy
so if you dont need all values, but only few first, then iterator is the perfect solution
but if you only need all values, then there is no big difference
generators do have small overhead, but it is not bug enough to worry
kk
the convenience of iterators is usually well worth the tiny performance loss
As opposed to what?
creating a list, set, ... whatever directly
Wouldn’t the be even worse performance?
Im just confused by the part where you said there’s a tiny performance loss using iterators rather than making a list
Cause wouldn’t making a list be the performance loss part
generators have some overhead from being coroutines
Ah, so in cases where you need to iterate through all of it, making a list would actually be a little bit better performance?
right
Cool ty for clarifying 🙂
is it worth paying for LeetCode premium?
Also, is there anybody who would like to discuss about system design? We can share insights about what we learn and explain to each other how to create different systems. I am not some experienced dude, but I am passionate about it.
No, you can literally paste any leetcode question on gpt4 and you'll get an explanation tailor made for you
- there's always solutions on each problem
And every learning resource is available online
In videos, books, etc
blue subtree is incorrect
that's what I thought
so a subtree always starts with a root node, therefore the blue is incorrect because there are 2 root nodes, each with their own tree
basically every non-root node is the root of its own subtree
the blue circle isn't a tree, it doesn't have a root node. also it's disconnected
(it's a forest)
ye
When doing a depth first search on a tree, should I use pythons deque import instead of making the stack a list?
why? dfs needs only stack
Hmm, I think the underlying libraries like nvidia's cuSPARSE don't support n-dimensional tensors either, hence the implementation problems. Depending on what operation you're doing, maybe you can get away with reshaping your tensor to be 2d and then back afterwards?
This was my backup plan; thanks for providing a bit more info about it. I'll just work with that moving forward.
i want to learn data structure and algorithm in python can recommend any best course for it ???
Hello, here are all of the resources available from the channel pins and our resources page. There should be plenty of other options from searching the web as well:
#algos-and-data-structs message
#algos-and-data-structs message
https://www.oreilly.com/library/view/data-science-from/9781492041122/
https://inferentialthinking.com/chapters/intro
https://www.youtube.com/playlist?list=PLQVvvaa0QuDfSfqQuee6K8opKtZsh7sA9
hi guys i was wondering i am newbie could you help me with something i would like to make a graph bassed on some data and i dont really know how
When you say graph, do you mean one with nodes and edges, or a a plot/chart? If the latter, then #data-science-and-ml might be a better channel to ask in.
deque is a stack or a queue depending on which way to are using it, I've just seen it supposedly has a better time/space complexioty than a list. Idk though
Question,
[4, 2, 7, 11, 12, 8]
What is considered the "front" and what is considered the "end"? If this list was a queue?
is the front always the left side? that kind of thing
it doesn't really make sense to implement queue using list
but if it was stack, then top of stack is the end of list
In a stack, the most recent element is removed first, so you do one of these:
# Option 1 - use list[-1] as the top
list.append(e) # stack.push(); adding a new element to the stack
list.pop() # stack.pop(); removing an element from the stack
# Option 2 - use list[0] as the top
list.insert(0, e) # .push(); very slow
list.pop(0) # .pop(); very slow
```And as `Option 1` can do both fast, we can code it that way to use a `list` as a stack without being very slow
Where as in a queue, the oldest element is removed first, so then you have to do
```py
# Option 1 - use list[0] as the front and list[-1] as the end
list.append(e) # queue.push()
list.pop(0) # queue.pop(); very slow
# Option 2 - use list[-1] as the front and list[0] as the end
list.insert(0, e) # .push(); very slow
list.pop() # .pop()
```And both options have an operation that is very slow if the queue gets large enough
hello guys can anyone here give me some dsa courses for beginners because i'm Dispersed
Khan Academy have a nice intro course: https://www.khanacademy.org/computing/computer-science/algorithms
It's not comprehensive, but it's a good introduction to thinking about algorithms.
okay so if i do this ">" -> > in my grammar, will the json parser identify a ">" as a > comparator?
that about lark
Lark library
i don't know if this would work
this is what i'm trying to do in lark
comparator: (">" -> > | "<" -> < | ">=" -> >= | "<=" -> <= | "!=" -> != | "==" -> ==) (string | number)
i got how it works basically
like -> this arrow allows me to alias a thing to some other thing
then i'm planning to verify if it's true or false:
compare: ((roll | math | echo | select | if*) comparator) -> ("true" | "false")
Maybe just chat with ChatGPT about particular algorithm/data structure and if you do not understand it textually, then check YouTube video.
pure graph theory question: can a 4-regular graph have 2 vertices?
the only thing I see is that the degree of a 4 regular must be 4 on all vertices
so I would think you could have parallel edges
unless this could be solved in a different way
okay so i got a dillem:
macro_grammar = Lark(
r"""
use_var: "&" int
variable: "$" command
command: (if |roll | math | echo | select) ws? (if |roll | math | echo | select)*
if: (roll | math | echo | select) ws "!if" ws comparator ws else
else: "!else" ws (if+ | roll | math | echo | select)
roll.3: "!roll" ws string
math.3: "!math" ws string
echo.3: "!echo" ws (string|"[" input "]")
select.3: "!select" ws input
comparator.2: ((roll | math | echo | select | if+) ws comparator_indx) -> ("true" | "false")
comparator_indx: (">" | "<" | ">=" | "<=" | "!=" | "==") (string | number)
input: "(" string ")" ("," "(" string ")")*
string: ESCAPED_STRING
number: SIGNED_NUMBER
ws: WS
%import common.SIGNED_NUMBER
%import common.ESCAPED_STRING
%import common.WS
"""
)
class Compiler(Transformer):
def command(self, str_mc):
(cmd,) = str_mc
return cmd
def math(self, cmd):
(m,) = cmd
expression = m.replace("!math ", "")
result = exemath(args=expression, res_type="all")
return result
def select(self, cmd):
(sl,) = cmd
options = sl.replace("!select ", "")
result = exeselect(arg=options)
return result
def roll(self, cmd):
(rl,) = cmd
roll_prompt = rl.replace("!roll ", "")
result = exeroll(args=roll_prompt, res_type="all")
return result
def echo(self, cmd):
(ec,) = cmd
op = ec.replace("!echo ", "")
in echo i need to compare if the value inside is just a tring or a set of inputs
if it' a set of inputs it should divide the inputs into pages
so i will need to use it with my paginator
which is already created
if it's a string it just return
also i don't know if replace is a good idea of removing the identifier
like !roll, !select, etc
my plan here is just to work with the secondary part of each command
maybe i don't need replace !command's
and also have an impasse
if i use subcommand and makes the "##" as their identifier
it will coment
*comment
how do i use # inside strings without it commenting?
if you allow duplicate edges
in this case going a bit bigger is the solution
overall the construction is just this kind of pattern extended, and then connected back to itself to form a "ring"
I'm from VietNam.Could you help me how to learn DSA through book ,document or youtube, and give me some information about it
Any good crash course on utube for python dsa? Wanna learn the basics and start doing leetcodes
Prolly something like sub 10 hrs ish?
I’m a novice too but leetcode isn’t really how you learn you kinda just gotta make stuff with it
I made a crummy weather thing and refined it a lil after having it critiqued and that taught me a lot
Leetcode is for interviews and most people say that you’ll never really use leetcode stuff outside of that. Unless you specialize in something that requires the fundamentals idk
actually, I’m planning to learn it mainly for interviews, as I’m in data science rather than a SWE
so, about this
i need help with this two dillems
anyone know an efficient conversion from arabic to roman numerals? i modified some code i found on stackoverflow and have this but i dont think its the most efficient
def roman_numeral(num):
numerals = (
(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')
)
s = ""
for n,digit in numerals:
s+=(digit*(num//n))
num%=n
return s
im not really fond of how it has to have the subtracted bit as well as the regular numerals in it
Not sure what you mean by efficient, but there are some things you can change I guess
def roman_numeral(num):
numerals = (
(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')
)
s = [] # <-- repeated string concat is slow
for n, digit in numerals:
q, r = divmod(num, n) # <-- you can get the quotient and remainder in 1 go
s.append(digit * q)
num %= r
return ''.join(s)
guys, can someone make a regex for Lark that includes all characters, including other language characters and also numbers?
cus ESCAPED_STRING don't accept numbers in their set
"all characters"? that's just ..
I think escaped string matches things like "hello" (including the quotes)?
lark/grammars/common.lark lines 23 to 29
//
// Strings
//
_STRING_INNER: /.*?/
_STRING_ESC_INNER: _STRING_INNER /(?<!\\)(\\\\)*?/
ESCAPED_STRING : "\"" _STRING_ESC_INNER "\""```
oh, so all i need to do is using _STRING_ESC_INNER
I wouldn't use that as it's an implementation detail. Can you give some examples of tokens you want to match?
i want to math for example:
"!roll" string
then i want to execute and parse this string[
"!roll 1d20"
Are you sure you need a full parser for this, or could you use regular expressions?
what are regular expressions
?
As in the re module: https://docs.python.org/3/library/re.html
oh, re
You'd use a parser when you aren't able to represent the input using a regular expression.
the thing used in regex
For example, you can't represent a language with matched parentheses using a regular expression.
I.e. there's no regex that will match all possible matched parentheses strings: (), ()(), (()), (())(), etc.
But that doesn't look to be the case with the inputs you want to process.
i don't know how i would respond, but i could respond that i need one that match what i need
being this my grammar:
macro_grammar = Lark(
r"""
chain_command: command (ws command)*
command: (fif | command2)
command2: (variable |roll | math | echo | select)
variable: "$" command
fif.2: command2 ws "!if" ws comparator ws felse
felse.2: "!else" ws command
roll.3: "!roll" ws content
math.3: "!math" ws content
echo.3: "!echo" ws finput
select.3: "!select" ws finput
comparator.2: command ws comparator_indx
comparator_indx: (">" | "<" | ">=" | "<=" | "!=" | "==") (content | number | command2 | sub_command)
use_var: "&" int
sub_command: "+>" string
content: (string? (("{" (use_var | sub_command | command) "}"))+ string? | string)
finput: "[" ("(" content ")" ("," "(" content ")")*) | content "]"
int: INT
string: ESCAPED_STRING
number: SIGNED_NUMBER
ws: WS
%import common.INT
%import common.SIGNED_NUMBER
%import common.ESCAPED_STRING
%import common.WS
""",
start="chain_command",
)
i want to my string rule accepts all characters
i don't know if regular expression could do
and this including numbers
so the better answer would be, try both
to see which would fit better
Yeah maybe. I'm just looking at the grammar you posted to see whether it's something you could do with just regular expressions.
Ah right, yeah this isn't a language you can parse using regular expressions. Nevermind.
Can you give some examples of some more complex commands?
+>mresult -> "!echo [Sucess: {&1}] !if !roll 1d20 >10 !else !echo [Failure]"
+>mdamage -> "!echo [{&1}\nDamage: {!roll 1d10+5}] !if $!echo {+>mresult} == Sucess !else !echo [{&1}]"
+>multiplex -> "!echo [(Banana)(Acid)(Clockwork)] !if !roll 1d20 !if !echo [Monkey] == [Monkey] !else !roll 1d10 >10 !else !echo [Azura is a robber]"
+>azaki -> "!math 2+2 !if !echo [Apple] == [Pineapple] !else !math 4+4 !if !echo [Pineapple] == [Cupuaçu] !else !select [(Lapiz Lazuli)(Emerald)(Ruby)]"
@keen hearth here
this is complex
so @keen hearth
Hey sorry, so is each one of these lines a string you need to parse, or just the part on the right-hand-side of the ->?
It might be a good idea to make a post in #1035199133436354600 btw, as I'm only sporadically here.
no, they are indicator of their command and the first part before -> is their command name
the part on the right hand side -> is what i want to parse
the left ones are their names
Oh ok
fair
I meant more structural things but thats true
ideally it would work without the explicit declaration of values like ‘iv’, ‘ix’, etc
i think what makes it hard is the alternating power of roman numerals; it switches between 5 and 2
"assuming the recursive calls I make returns the correct thing, do I do the right thing?"
Yeah, after doing more recursive problems I feel same, like I just need to check whether my assumption is correct on subtree and then it can be generalized
guys, how do i clear tracemalloc
cus even with cleartraces it's not giving me memory
for my tracemallocs
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\Learning_Lark\fifth_lark.py", line 366, in <module>
snapshot = tracemalloc.take_snapshot()
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\tracemalloc.py", line 556, in take_snapshot
raise RuntimeError("the tracemalloc module must be tracing memory "
RuntimeError: the tracemalloc module must be tracing memory allocations to take a snapshot
in each second i gateway receives a request from domain request [i]. The gateway allows atmost 2 requests from a domain within 5 seconds and atmost 5 requests within 30 seconds hackerrank solve
has anyone came across this problem before?
Is there an easy way to evaluate a math expression where the terms can be non-numerical variables? In my case, they can have special characters in them, so something like:
(-ABC123 + CDE$01 - XYZ§02 + AAA-12 + BBBC=2)*-1 (Each term has 6 characters, so it would be no problem for me to extract the terms and maybe replace them, to make the evaluation easier). I would probably replace the terms and evaluate it and then insert the terms again. Any suggestions? I actually liked ast, but it doesn't work with special characters and is more for processing python code. Any suggestions?
Learn how to write recursive descent parsers. It will be hard, but not that hard, you will learn a lot along the way, and most importantly, it will give up an idea why solutions like "replace the terms and evaluate it" are very unclean.
Alternatively, go a completely different route, and learn how to use a parser combinator, like compynator. That way you won't have to learn anything, you will have a faster and less error prone solution, and maybe even learn a good tool in your skill list, but since you even asked that question in the first place, I might recommend against it (using parser combinators without knowing at all how they work at least on the basic level is very questionable in my opinion, but who knows, maybe first using one will give you a better ground)
thank you. Unfortunately time is of the essence right now, which is why I went with an unclean solution for now (replacing special characters, using sympy). I would like to be able to work more with parsers and ast though
https://en.wikipedia.org/wiki/Lexical_analysis https://en.wikipedia.org/wiki/Shunting_yard_algorithm
Lexical tokenization is conversion of a text into (semantically or syntactically) meaningful lexical tokens belonging to categories defined by a "lexer" program. In case of a natural language, those categories include nouns, verbs, adjectives, punctuations etc. In case of a programming language, the categories include identifiers, operators, gro...
In computer science, the shunting yard algorithm is a method for parsing arithmetical or logical expressions, or a combination of both, specified in infix notation. It can produce either a postfix notation string, also known as Reverse Polish notation (RPN), or an abstract syntax tree (AST). The algorithm was invented by Edsger Dijkstra and name...
is there a better way to assign IPA/ARPABET phonetics to letters in a word?
currently i have a mapping of latin letters to possible IPA phonetics and it's pretty messy
it's almost like mapping text to phonetics is ambiguous in most languages 😛
and ambiguous going both ways: homophones, homographs, ...
how about just distinguishing vowel and non-vowel areas in the word
I have a algorithm that takes dict of fields in format field_name: value, then in the csv file checks if all field_name contain the specified value in the same row.
For example if I get dict that looks like this {"car": "audi", "price": 3000} it needs to return only those rows where both fields match.
This is my code:
field = {"car": "audi", "price": 3000}
list_of_truth = []
for key, value in field.items():
if row[key] == value:
list_of_truth.append(True)
else:
list_of_truth.append(False)
if all(list_of_truth):
# append to a list
else:
continue
There is also outer loop that provides me with each row.
Is this a good way to solve this or there's something more efficient?
First make this change
field = {"car": "audi", "price": 3000}
list_of_truth = []
for key, value in field.items():
list_of_truth.append(row[key] == value)
if all(list_of_truth):
# append to a list
else:
continue
Thanks, that's very clever!
if you don't reuse list_of_truth elsewhere, you could just do the list comprehension inside all
field = {"car": "audi", "price": 3000}
if all(row[key] == value for key, value in field.items()):
# append to a list
else:
continue
it would avoid looping twice (+ it looks cleaner IMO but that's subjective)
nit: generator expression inside all
@keen hearth i discovered a thing about Lark, if you acess the input of a transformer, you get the token like a list
for example cmd[0] you acess the first token
since it don't work like a normal list you need to use a for
otherwise it won't work
problem solved
okay guys, how do i compare if a variable is a string?
!e
yourstr = "hello"
print(isinstance(yourstr, str))
@clear light :white_check_mark: Your 3.12 eval job has completed with return code 0.
True
@keen hearth for some reason i don't know why my grammar don't detect symbols that i included in CHAR as part of the char_set
like, i included the +
for math operations
i don't know why it's not detecting
how can i get started learning data structures and algorithms and start solving leetcode problems
by doing a dsa course
for free
Given a binary tree and a number sequence, find if the sequence is present as a root-to-leaf path in the given tree.
This is my solution
#class TreeNode:
# def __init__(self, val, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def findPath(self, root, sequence):
# TODO: Write your code here
return self.checkPath(root, sequence, [])
def checkPath(self, root, sequence, currentPath):
if root is None:
return False
currentPath.append(root.val)
if root.left is None and root.right is None:
if currentPath == sequence:
return True
else:
return False
else:
return self.checkPath(root.left, sequence, currentPath) or self.checkPath(root.right, sequence, currentPath)
del currentPath[-1]
I know other correct solution but I was wondering why this above is incorrect
the del after the return is an interesting choice
So the problem is there I guess. I will try to write in my notebook to see what is exactly wrong
you just keep appending to currentPath and never popping
when you do a dfs like this, the append should be paired with a pop, as written here you need to do it in two or three places
mybad for the inaccuracy, felt like it'd be more understandable this way but its true that we're giving the generator straight to the function, no list there
I'm just nitpicking about the proper usage so new people will hear the correct term and not mix stuff up 😅
if root.left is None and root.right is None:
return currentPath == sequence
import matplotlib.pyplot as plt
import math
x_values = []
y_values = []
for n in range(2,100): #avoided the edge cases for fun.
x_values.append(n)
flag = 0
count=0
for i in range(2, int(math.sqrt(n) + 1)):
count=count+1
if n % i == 0:
flag = 1
y_values.append(count)
break
if flag == 1:
print(n, "is not a prime")
else:
print(n, "is a prime")
y_values.append(count)
y_real=[math.sqrt(x)-1 for x in x_values]
plt.title("Time Complexity of Primality check: $\mathcal{O}(\sqrt{n})$")
plt.scatter(x_values, y_values, color='magenta')
plt.plot(x_values, y_real, color='black')
plt.show()
I tried to plot the prime number checking algorithm
i'm a beginner, can you check the code and give suggestions
as in better way to code this or missed edge cases etc.
I would have formatted the code (spaces around operators, like in count = count + 1), used f-strings instead of multiple parameters to print print(f"{n} is not a prime"), and used while loop (while i * i <= n instead of for i in range(2, ...)) with integers instead of for with sqrt. sqrt can be imprecise. Try running your code for a number 100_000_007 ** 2. It will take a while, but I should terminate eventually and say that it is prime even though it is a square. This is because floating point numbers don't have enough precision to represent numbers like these accurately.
oh, wait, sorry, no, it will work correctly, my bad
100_000_007 ** 2 is not big enough, I forgot float is 64 bits... I still would raise an eyebrow on such code though
i'd use math.isqrt in modern python
for any number n, the worst case is $\floor{\sqrt{n}}$ right?
ie, we take square root and round down to the nearest integer
yep
so, this will give a mess int(math.sqrt(n) + 1))?
i suppose this will do the same right?
!e import math; print(int(math.sqrt(10 ** 36) + 1))
@outer rain :white_check_mark: Your 3.12 eval job has completed with return code 0.
1000000000000000000
1-off
why isn't it adding the one ?
i think it may in fact be guaranteed that (int(math.sqrt(x))+1)**2 >= x, though not sure how to prove it.
because for floats, float(10**18) + 1 == float(10**18). not enough precision to distinguish between the two.
yeah, math.sqrt calcualates the square root in floating-point numbers, which have limited precision
10**18 is same as 10^18 right?
well, if you mean power by ^ -- yes
can you explain a bit more
10**18 is 10 followeded with 18 zeros
why is it considering to be same?
because of the range of float datatype ?
(obligatory)
a floating-point number can be thought of as a number written in scientific notation with a fixed-length mantissa. for example, if it was using decimal (instead of binary like real floats do), 10^18 wouldbe represented something like 1.00000000000000000 * 10^18, with, let's say, exactly 17 decimal digits in the mantissa.
so it can't represent 10^18 + 1, because that'd need another decimal digit in the mantissa. the next lowest number it can represent is instead 1.00000000000000001 * 10^18, which is 10^18 + 10.
Real floats work pretty much like this, just in binary. Their mantissa is 52 bits long. So they can exactly represent all integers up to 2**53 - but at that point the precision runs out, and 2**53 and 2**53 + 1 are the same float (the next exactly representable float is instead 2**53 + 2).
Hi im a very noob programmer and im starting python, is this the right place to ask for help?
ah, true.
@outer rain :white_check_mark: Your 3.12 eval job has completed with return code 0.
4503599627370497.0
so my code sort of coughs when seeing square numbers?
there's a nice function you can play with even, math.nextafter. math.nextafter(2**53, 2**100)-2**53 is 2.
only on very very very big numbers
theoretically it will only in square numbers right?
not anywhere else?
just wanted see if i got the math right
analogy to what, sorry? 😅
like something similar like this happening in C programing
10^18 wouldbe represented something like 1.00000000000000000 * 10^18, what does this mean?
like 10^18 is stored as 1.00000000000000000** 10^18
like a 18 digit long float and the number in its power?
is it some sort of datatype overflow?
i'm talking about how floats are internally stored. i'm using hypothetical "decimal floats" as an analogy - suppose you represent numbers in scientific notation, m * 10**e, where m is a decimal number, 1<=m<10, and, crucially, m has no more than 17 decimal digits (it can't be arbitrarily long). and e should be integer.
this approach can only represent some numbers. for example, you can represent 10**20 (it's just 1.0 * 10^20), but you can't represent 10**20 + 1, because you'd need more than 17 decimal digits in m.
for the same reason, floats can't represent some large enough integers.
floating point numbers are implemented the same way everywhere.
i get what you mean by limitation now
the calculation here goes like this:
# this is the initial number you are working with
10 ** 36
# this is an exact integer it is equal to, integers are stored precisely
1000000000000000000000000000000000000
# this is the first operation you are trying to do
math.sqrt(1000000000000000000000000000000000000)
# internally, `math.sqrt` converts argument to float:
# here is this number is converted to the nearest floating point number
math.sqrt(1000000000000000042420637374017961984.0)
# notice how it is already not precise: 53 bits of mantissa is not enough to represent it precisely
# this is the result of math.sqrt.
# again, remember that floating point numbers are not precise
# this is not an exact square root of that number above, but it is the closest
# floating point number to the exact square root of that number
1000000000000000000.0
# finally, you are adding 1 to that number...
1000000000000000000.0 + 1
# and again, since the exact result can't be represented precisely,
# we take the closest floating point number to the exact result, which is...
1000000000000000000.0
so only walkaround to avoid this is not to use square root?
stick with integer arithmetic?
i suppose thats why you used a while loop i*i <=n
I think you can do this... but yeah, I would recommend avoiding floating point arithmetic as much as possible
in python i'd just use math.isqrt. in general, I think just adding a 1 to the loop's upper limit will make it valid, though proving this might be annoying
the while-loop way would be obviously correct, though maybe a bit less performant
the next problem with floating point is infinity, of course, but you would not have to factorize numbers that big 😅
I am just a boomer, I don't know about your fancy isqrt, that makes more sense
i don't think adding 1 to the upper bound is nice
frankly this problem is a hypothetical one anyway, because this algorithm would take way way too long on the largest number that it can correctly factorize. so who cares that it fails above that :p
because we are visiting it even after we are done
yeah, good point, I was mostly nitpicking
math
cares :)
nvm lol
i can use the similar method to plot any algorithm right?
is appending into array and then plotting it the standard way?
i'd note that it's "number of iterations", not "time", but if it's an iterative algorithm, you could.
what else can I try
where?
it's sometimes difficult to figure out what to plot, and in relation to what, but you will figure it out lol
your y-axis.
i'm saying that the number of iterations isn't quite the same thing as time taken, so it's slightly misleading to label this "time complexity".
(but of course, actually measuring the time would be much more annoying and noisy.)
isn't time complexity the number of iterations ?
like the worst case scenario for number of iterations
it is, if you define iteraton correctly..
import math
def is_prime(n):
return math.factorial(n - 1) % n != 0
this algorithm technically does not iterate anywhere (visibly), but it's far from O(1)
only if every "iteration" takes the same time. that's a nontrivial assumption, though - consider something like
def f(n):
a = [1]
for _ in range(n):
a = a + a
return len(a)
i think when we said time, we really mean time in as in 2 times 2
not the clock time
can it also mean clock ticking time?
when talking about algorithmic complexity, we usually count amount of primitive operations a machine does
(e.g. instructions)
Hey anyone sorry for disturbing can u suggest me one thing.....
but you can't really count them from python anyway
Is it better to know many programming languages or only master one
the most useful metric (and related to algorithmic complexity kind of loosely) is the real running time
this is not the channel for this
sry for that
so when i say for a number n, the worst case of finding a divisor is \sqrt{n}
what complexity are they mentioning?
(but also real time is not just "one thing", there are a lot of different clocks you can measure, time is weird...)
this is surprisingly a difficult question, let me answer it as well as I can...
Imagine you having an abstract executor, which has infinite addressable memory, where each cell can store any whole number, and a processing unit capable of doing arithmetic instructions such as addition, multiplication, ... Then, if you write an algorithm equivalent to what you have written, for that abstract machine, given a number n in one of the memory cells, it is capable of checking whether it is prime or not in O(sqrt(n)) primitive operations.
O(sqrt(n)) means that there is a constant C such that an amount of iterations your machine does is less than C * sqrt(n). Notice that this is a very rough estimate: for even numbers your machine will terminate almost immediately. You can say that your algorithm actually works in O(d(n)) where d(n) is the smallest divisor of n, but it is not a pleasant function to work with, so you "round up" the time to sqrt(n).
Now, in real world, there is no infinite memory nor infinite numbers. The bigger the integers are in python, there slower operations on them become. However, you don't care about numbers that big. So, instead you say that your algorithm closely resembles one running on the abstract machine, and therefore, works in O(sqrt(n)). Even though technically, it would work slower with very very large numbers. Of course, you can just sat that everything is O(1) because the memory is limited anyway, but it's not a useful estimate either.
So, in other words, time complexity in practical applications is a lie. What it means is that the real time taken by the algorithm to finish for large inputs resembles the amount of instructions an abstract machine with infinite memory would execute to finish that program, and what O(f(n)) means, is that for large inputs, the algorithm on such abstract machine will take at most C * f(n) operations for some C.
Finally, it's worth mentioning that it isn't really a lie, and it can be formalized through a thing called "transdichotomous model", but I won't go into that...
give me some time
i have to read through this carefully
i'll comment after that
So for the above code, it had the assumption that the codeblock inside the for loop is single operation?
@outer rain
not a single operation, but a bounded number of operations
because think about it, from the point of complexity, it doesn't actually matters how much operations some part of code does as long as it is independed from the input
So is counting the iteration count really the way to explain algorithmic complexity?
if you assume the body of for does twice the operations, take C twice as large: the number of operations < C * sqrt(n) is still true
well, yeah, ultimately you only care about how much iterations your program does, but again: iteration is a tricky term
very formally, if there was a operator that checks a number is a divisor of n, then it would be done in exactly \sqrt{n} right?
ie choose c=1
ie if i assume modulo operator is standalone
are you counting the number of operations?
suppose % is the only operation
not really, because loop does quite a few more things..
increment i, jump to the next iteration
compare the result of % with 0
all of that are operations
but it doesn't matter how many operations it does, for the same reason as this
as long as it's bounded, you can find such C, and you don't care what that C would be
can be 19045685096834059683049586
as long as it doesn't depend on the input - it's fine
yeah, this is a definition of O
and when taking about time complexity you basically mean amount-of-operations(size-of-input) = O(g(size-of-input))
well, maybe not "size" of input, maybe something else
like in factorization the value of the number you are trying to factorize
isn’t g(x)= sqrt(n) here?
yep
yeah, that's pretty much what I do with the number of operations
so it’s sort of an abstract setting
it is
makes sense
but it's not that abstract actually, like the CPU does some operations: sure, they don't take the same time to finish, but that time is bounded by something (like 1000 CPU clock cycles), and you don't care about the exact time.
the more abstract stuff arises here, where you say "as x --> oo": unlimited memory is pretty abstract
makes sense
Thanks a lot for your help
I have no formal background in computer science
I got interested into this when taking a number theory class
oh, this is cool :)
We have proved Lame theorem which gives an upper bound for euclidean division algorithm
I have not been taught what time complexity is for a long time too, I just had some intuition lol
so thought to dig more into that and now i am here
I see
my next step was to try for primes
I know mathematically, there exist a prime divisior less than \sqrt{n}, easy to prove
just plotted in thinking it’s time complexity lol
I just googled it and wikipedia states the theorem in an interesting way, lol
The number of division steps in Euclidean algorithm with entries
uandvis less than 5 times the number of decimal digits ofmin(u, v)
never heard of that
I mean, I think you had a pretty good idea of what you were doing, it was just lacking some details here and there
yes, the notion is consecutive fibanocci numbers gives the worst case scenario
in gcd algorithm
like it will go all the way down to 1, as consecutive fibanocci are relatively prime or gcd=1
there is a pretty interesting corollary from that theorem, which is pretty useful
what’s that?
is that if you take gcd of not just 2, but a lot of numbers, say N, then it works in O(N + log min(a1, a2, ..., aN))
it is a surprisingly useful fact which doesn't necessarily useful by itself, but in the context of some data structures for calculating gcd is pretty interesting
glad you found it useful, but I’m really unaware
because normally if some operation <*> can be performed in O(log min(a, b)), then a1 <*> a2 <*> ... <*> aN will take O(N * log (a1 + a2 + ... + aN)), but with gcd it's special
comparing, it’s a huge difference
if you decide to pursue you number theory journey through programming and will experiment with gcd, just remember this fact: that gcd works faster than you expect when taken from multiple numbers at once, it may explain some interesting measurements you may get
just a small caution 😉
Noted
Have you tried complexity of RSA Algorithm?
I have never played with any cryptography, actually :(
that would be pretty fun
I was at a camp-thingy-or-something-idk with people from my class, and we had a few lessons dedicated to basic cryptography
and there was a practice day where we had to implement RSA and a few other algorithms
and there was also a challenge for breaking RSA with relatively small numbers
iirc it’s based of fermat theorem
... and I was sick that day and missed everything
I don't remember the details, actually
I’m just getting started tho
Is there any resources you can suggest me to learn more on complexity?
I tried searching a lot tbh
How do people do it in standard way?
well, my story is that we had a class dedicated to algorithms in school, and now in uni
from scratch?
I don't know about most people though
well... somewhat...
I think most people study data structures then complexity
I have somewhat zero exposure to data structures
I took a C++ course for a year in grade 11 and 12
well, if you study DSA you will have some understanding of time complexity anyway for sure
but then there is a whole other direction to that
theoretical informatics, where everything works slightly differently, I would say
like in DSA you mostly care about practical applications, and in theoretical informatics you mostly think about wide classes of algorithmic complexities
both are important, and I wouldn't say there is a strong correlation between them
The formalism of these notions are really great
it’s par with most of our adv analaysis course
I sometimes like learning about the formalism of these things a lot, and sometimes just find it very painful and boring
In my opinion, having intution is far superior than being able to define formally
Feel free to bounce off, if you are busy
Don’t mind to reply lol
not really, but I will eventually, don't worry 😂
I would say that having an intuition and being able to formalize is very close skills
yeah, sometimes happens... I was that person some time ago actually, but I got bored eventually
I will say though: writing your proof in LEAN (formal verifier) and having it finally not yell at you is so, so satifsying
It’s very analogous to algebra, you can do solve equations and really caught in algebra without knowing what you are doing
idk if it’s relatable lol
but most people can do algebra closing their eyes
I was never that good in algebra to do it that way, but I know what you are saying
no problem man, just trying to procrastinate ❤️
hey, what is the best platform to host my python files (on replit) for free?
do anyone knows how do i use regex to get all non-alphanumeric numbers?
i need it for my lark
what's a "non-alphanumeric number"??
floats I guess
if you mean non-alphanumeric character, can do e.g. [^a-zA-Z0-9], depending on what you mean by alphanumeric. not sure there's a good way to match what python considers an alpha char (the Letter unicode category)
maybe scientific notation
i see, thought the problem was solved, someone helped me
now i'm having problem with sympify
why my code is returning numbers as interger where there is no interget?
*interger
like
it's all string
there is no interger there
what my problem is smokin'?
okay guys, how do i compare the origin of a returned value?
i need that for Lark
so i can compare if the value comes from key_cont
so if yes i can join the strings
Which python DSA course is best for solving leetcode problems? is udemy good enough
Maybe search for DSA terms and use ChatGPT for explaining them and if you don't understand something, then use YouTube for it because if it's visually presented to you, then it could be eaiser for you to learn it
Btw, if you are into LeetCode, maybe it would be good to check NeetCode
Yes but sometimes when I know the concept I still can't do the question and it lowers my confidence
Yeah I heard about that guy
Do research about growth mindset, maybe ask chat gpt about growth mindset or watch some YouTube videos
One sec
So, the sum of the first n perfect squares is the nth "square pyramidal number". The formula for it is n * (n + 1) * (2*n + 1) / 6.
So they've just substituted in n*n into this formula.
Alternatively it's n**3/3 + n**2/2 + n/6. Even if you don't remember the exact formula, you can remember that the largest term is an n**3 term.
I don't remember the formula. I just looked it up https://en.wikipedia.org/wiki/Square_pyramidal_number
Right yeah. Do they provide you with a formula sheet?
Or alternatively is the exam open-book?
I remember this because summing is kind of like taking an integral.
its not
open book either
he will give a code and ask us to find the o notation
like that one
I think in general sum(i**k for i in range(n)) is O(n^(k+1)). I'm not sure if thats a fact you can just quote as part of your argument in the exam.
Are the questions the kind where you just have to find the right answer, or the kind where you have to prove the answer?
#microcontrollers message this may be better suited for this channel
prove the answer @keen hearth
It might be worthwhile memorising the triangle number and square pyramidal number formulas. I'd get clarification from your instructor about what's expected of you in the exam.
Btw, the sum of the first n cubes is just the nth triangle number squared. So you get that one for free.
i'd remember how to derive these formulas
thats magical
Is there a simple way to derive the sum of squares formula?
solve this
f(n)=f(n-1)+n^2
f(0)=0
hmm, i actually dont know how to solve this without:
- exhaustive search
- knowledge of differential equations
- knowledge of generating function
my bad, it is indeed easier to just remember formulas
Once you find the formula it's easy to prove by induction, and to get it you can either fit a degree-3 polynomial to the first 4 terms, or do that trick mentioned by nosqldb: #ot0-psvm’s-eternal-disapproval message
wow, i like this trick! 💯
can anyone provide tips for working with these formulas? I was able to get the temp one working but the humidity one is not returning sensible values
I think that is actualyl correct
I think im getting the wrong stats for H4+
h4 and the rest are offset by 4 bits i think
is there some way i can just add 4 bits into the bytes string i read?
im trying to do that but can't get it to work, the half byte offset is messing me up
im not sure if the library is right?
I've been working this out, the library says I should get 50 for H5
01111110 00000001 00000000 00010001 00100001 00000011 00011110
This is the data from each register
I'm getting 515 which I think could also be a reasonable value
okay
would this channel be appropriate to post about dynamic programming on the 0/1 knapsack problem ?
it would
ok
so basically here I'm solving the 0/1 knapsack problem and I implemented a naive dynamic programming approach with a table and the Bellman recursive equations
here is the code
#!/usr/bin/python
# -*- coding: utf-8 -*-
from collections import namedtuple
import numpy as np
from operator import itemgetter
Item = namedtuple("Item", ['index', 'value', 'weight'])
def solve_it(input_data):
# parse the input
lines = input_data.split('\n')
firstLine = lines[0].split()
item_count = int(firstLine[0])
capacity = int(firstLine[1])
items = []
for i in range(1, item_count+1):
line = lines[i]
parts = line.split()
items.append(Item(i-1, int(parts[0]), int(parts[1])))
# start of selection
value = 0
weight = 0
taken = [0]*len(items)
# preparation of the table
array = np.zeros((capacity+1,item_count))
# sort items by weight to start filling the table
items = sorted(items, key=itemgetter(Item._fields.index('weight')))
# Bellman recursion
for i in range(item_count):
item = items[i]
for k in range(capacity+1):
if i==0 and k==0:
array[k][i]=0
elif item.weight<=k:
array[k][i] = max(array[k][i-1],item.value+array[k-item.weight][i-1])
else:
array[k][i] = array[k][i-1]
value = int(array[capacity-1][item_count-1])
I'm able to get the optimal solution using this code
However, I'd like to implement a way of getting a decision variable vector
with each entry being either 0 or 1 depending on if we select the item or not
But for some reason I'm not capable of finding a way to do so. I know I should use the table to do that
my table has a column for each item and the rows are the capacities of the knapsack, going from 0 to K
if somebody can give me a hint on this one I'd be more than happy
thanks!
Hey guys, has anyone worked with the concorde algorithm which is implemented in C?
heelo
Fatimah has many digits. She wants to construct two numbers using these digits, so that the product of these two numbers is maximum.
For example, if she had digits 1 2 2, the best she could do is to construct the numbers 2 and 21, and get the product 42.
For the given list of digits, what is the remainder modulo 998244353 of the biggest product she can get.
Note: There are three different cases you need to solve.
5 9 3 7 4 8
any idea how can i do it ?
Create a new array with every possibly combination of digits, and then loop through the new array and multiply every combination of numbers in that array. Use an if statement to determine which of those numbers in the largest. I am not sure about the modulo portion.
If you had three digits, what’s the algorithm to maximize the result? What if you add a fourth digit? I’d think you’d be able to come up with a straightforward algorithm here, observing that most significant digit should be the largest digit, etc.
Know someone how I can force my A* algorithm to around a wall than doing this on the picture.
https://pastebin.com/Y8YPF3DR
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.
Anyone who can review my code on hash algorithmic part? https://discord.com/channels/267624335836053506/1201185236508217424
can someone help me understand why exactly is the space complexity O(bm)
I would say O(m), but depends on how you implement
if you use a stack I can see O(b m)
Hi guys
I’m new to BnB algos for knapsack problems
And I’m wrapping my head around the upper bound calculation
From what I’ve understood there is no unique way of computing an upper bound at each node and some people still do write papers on the subject. Also sorting by decreasing value of value to weight ratio will improve the pruning for certain upper bound formulas
That’s what I’ve understood
Is it correct?
but how is it O(bm)?
in the iterative version with a stack you push all children onto the stack at once, so you might push b children for all m levels
but on each level isn't there a different amount of b children like each level won't always have 4 nodes left to expand for example
in the example given it is
so they how is it b * m
if the "b" is different on each level
huh? I meant in the example b is assumed to be constant across every node, at least that's the one sane setup given the diagram
per layer: 1 node, b nodes, b² nodes, b³ nodes, ...
that occurs when every node has b children
iirc it would be called a "branching factor"
im wondering if there’s a more efficient method to doing this; so say i have a 3d grid of points, and i want to draw like, a line, or a curve, but with some thickness
what i would currently do is generate the line or curve, then for each position on that curve, fill in a sphere and add those positions to the final result, repeat until finished
the problem with this is that there's a ton of redundancy, though using something like a set to hold all the positions and then check if a position is already in the set helps remove a lot of that
but say if m in 3 and branching factor is 2 then how will there be 6 nodes in the memory
what happens in the iterative version is
push b children, pop 1
push its b children, pop 1
push its b children, ...
so (b-1) + (b-1) + ...
here's a function i wrote to give an idea of what im currently doing
def add_thickness(points,r):
output_points = set()
for p in points:
for z in range(-r,r+1):
for y in range(-r,r+1):
for x in range(-r,r+1):
if (x,y,z) == (0,0,0):
continue
pos = (p[0]+x,p[1]+y,p[2]+z)
if pos in points:
output_points.add(pos)
continue
if pos not in output_points:
if (x**2 + y**2 + z**2) < r**2:
output_points.add(pos)
return output_points
it takes in a collection of 3-tuples (or lists, or whatever) representing positions, and a value r (for radius) and generates a set of points which fill a spherical area around each of the input points
one thing i might try and add is a start and end thickness, and interpolate between them
but that might not work since im generally taking in a set
might change 'x2,y2,z2' to just 'x,y,z' since im not using anything called 'x,y,z' rn
there
i tested it out and it seems to work pretty well
That looks pretty cool actually 😄
i just realized that this would be slower when operating on lists due to the membership check
i can probably just remove that and it should be about the same
Maybe you could join a pair of points together, making a line segment. Then use the unit vector pointing along this line segment to determine how far a voxel is from the line? 🤔
take all positions and assign them distance zero, flood fill from all at the same time
at least that seems like a half sensible idea
acutally...maybe it's not that easy...
oh wait im dumb, i dont need that membership check anyway
you would need to check if the point is close to any input point
because it would always be the case 0,0,0 unless its some other point on the line
Find the tangent to a point on your curve and draw a circle such that the tangent line goes through its center.
@lilac cradle
I suspect that will look pretty weird given the discreteness of the points
If you do it for lots of points in-between it will fill
yeah, that will create a cylinder following the path
if you only have the points given to you it gets sketchier
you could probably do some interpolation
That'd be really cool.
the 2d version of this is already a bit of a challenge to get right
pick your favorite
that sounds kinda complicated to implement but i guess i could try that, though this is meant less for an actual curve and more for just any set of points
this was pretty hacky to implement but here's a somewhat smaller torus with a rainbow gradient
@lilac cradle what is this for?
i just wanted to make an umbilic torus in minecraft
thats all
looks cool on the map view
More algorithms please.
Woah that looks cool
That's awesome.
What's a good way to learn DSA? (python)
as someone whos is beginner-intermediate with python
By solving leetcode problem
leet code problems make me want to smash my head against a wall
Same!
i think u can follow neetcode roadmap
Check the pin
A better way to prepare for coding interviews.
Hi, can anyone help me, I'm working on a project using Keras but I have a problem with the result graph
You might want to go to #data-science-and-ml for this
Also didn't Keras merge with tensorflow or something?
Check out the learning resources in the pinned messages. I think, for practical purposes, you should try to get a basic understanding of the analysis of algorithms (that is, asymptotic complexity, a.k.a. big-oh) and also learn about the data-structures and algorithms that are available to you in Python.
Khan Academy also have a short introduction to algorithms course which, although it's not comprehensive, provides a nice introduction to the analysis of algorithms. It uses Javascript for the exercises, but you can skip them if you want to. https://www.khanacademy.org/computing/computer-science/algorithms
I understand your pain… I’m working on a course but it is going to take me a few months to complete.
@keen hearthWhat do you tink about these resources
A beginner-friendly introduction to common data structures (linked lists, stacks, queues, graphs) and algorithms (search, sorting, recursion, dynamic programming) in Python. This course will help you prepare for coding interviews and assessments.
🔗 Course website: https://jovian.ai/learn/data-structures-and-algorithms-in-python
✏️ Created by A...
Data Structures and Algorithms full course tutorial java
#data #structures #algorithms
⭐️Time Stamps⭐️
#1 (00:00:00) What are data structures and algorithms? 📈
#2 (00:02:20) Stacks 📚
#3 (00:11:45) Queues 🎟️
#4 (00:21:51) Priority Queues 🥇
#5 (00:26:51) Linked Lists 🔗
#6 (00:40:14) Dynamic Arrays 🌱
#7 (01:04:37) LinkedLists vs Array...
Which out of these would be a good ROI for my time?
I'm not a huge fan of these 12 hour long tutorial videos. First of all, they're very passive, and also I can't imagine anyone actually sitting all the way through one of these videos 😄
So what would be the way to go, out of all the options above, or any other option you suggest?
This guy seems ok. But I don't know if any of those course are put together by trained educators.
I recommend you start with the Khan Academy course.
The KA course was put together in collaboration with Thomas Cormen, who wrote the most widely used algorithms textbook, and has been teaching the subject for decades.
It's a pretty short course. It doesn't cover some popular data structures such as hash tables, but it looks like it would give you a good foundation to proceed from.
Hello is there a better algorithm i can apply to reduce a time complexity and do not complicate a code too much ?
def sub_palindromes(string):
max_len = 1
for i in range(len(string)):
left, right = i, i
while left >= 0 and right < len(string):
if string[left] != string[right]:
if right - left - 1 > max_len:
max_len = right - left - 1
break
left -= 1
right += 1
for i in range(len(string)):
left, right = i, i + 1
while left >= 0 and right < len(string):
if string[left] != string[right]:
if right - left - 1 > max_len:
max_len = right - left - 1
break
left -= 1
right += 1
if max_len == 1:
return None
else:
return max_len
sub_palindromes('CABCAB')
function has to return a len of longest palindrom in a string
Better algorithm, yes, not complicate the code too much, no
The best algo complexity wise is Manacher's algorithm at O(n), but it's really complicated
Slightly worse is to use a polynomial hash, giving you O(n log n), but it's complicated
I don’t have any computer science background, can you please check if the material is wrote is correct
ie if it fits the definition of complexity you are aware of
The reason why i feel sloppy is, the graph doesn’t necessarily capture what I’m trying to do, ie if I change the loop upper bound, different graph will be plotted.
your pink graph is incorrect
algorithm you wrote always does floor(sqrt(n)) iterations, which looks nothing like your pink graph but more like staircased black graph
The back graph is the real valued graph
It’s just drawn as a boundary to get some sense to function f(x)= \sqrt{x}, where x is any real no
im not talking about black graph
im saying that your pink graph is incorrect and should look like this:
do you mean for all input ?
@lean walrus
That's not true, you would only need to check till \sqrt{n} if the number is a prime
take n=10 for example, this will exit in first iteration. since 10%2==0
import matplotlib.pyplot as plt
import math
x_values = []
y_values = []
for n in range(2,100): #avoided the edge cases for fun.
x_values.append(n)
flag = 0
count=0
for i in range(2, int(math.isqrt(n) + 1)):
count= count + 1
if n % i == 0:
flag = 1
y_values.append(count)
break
if flag == 1:
print(n, "is not a prime")
else:
print(n, "is a prime")
y_values.append(count)
y_real=[math.sqrt(x)-1 for x in x_values]
plt.title("Time Complexity of Primality check: $\mathcal{O}(\sqrt{n})$")
plt.scatter(x_values, y_values, color='magenta')
plt.plot(x_values, y_real, color='black')
plt.show()```
it wont exit, according to the code you provided
i dont see any logic that would handle early exit
take n=10, floor(sqrt(10))=3
ie the loop iterates from 2 to 3
when i=2, 10 mod 2 =0 is true, so it will flag 1 and exit the loop
is there an algorithm that can do this? in other words, take an image, upscale it, then sort of smooth the rough pixels (without blurring)?
maybe hq2x would work but idk
i don't mean antialiasing btw, imagine the image being 1 bit color depth, so just on and off
well, kind of antialiasing, but not the kind that you'd normally see with different shades
so not like this
perhaps something like dilate + erode by the same amount?
might work
lemme test it
the problem i can imagine that having is with close by shapes but im not sure
i used GIMP to do it for that S shape and...
maybe a bit less :p
but yeah, this combination basically fills voids, so it's a bit problematic for some shapes
I am pretty sure all this thing is doing is filling the pixels with at least 2 neighbours, but I may be wrong...
hm
not so much for the upscaled version but yeah that might be true for the unscaled version
see what i was figuring it'd be was either some sort of convolution or seperating it into chunks and applying rules to each one
yeah, ofc
trivially upscale it, then do the thing for each pixel
I don't know about the convolution
well... I suppose you can think of that as convolution, and maybe even make kernel smart enough to figure out how to not fill the 1x1 holes, for instance
on the random sort, what is its complexity? n!?
about right, yeah. more like O(n n!)=O((n+1)!), I think, because each shuffle is O(n).
O(n) is the same as O(n+1)
O(n^2) is the same as O((n+1)^2)
O(n^k) is the same as O((n+1)^k)
but O(n!) is not the same as O((n+1)!)
annoying
this reminds me of marching squares
Looks exactly like the same algo if you just set everything to not blend color
For example, it's likely some kinda color = mix(col1, col2, fac)
Set it to:
color = fac > 0 ? col1:col2
Could you explain why O(n!) is not same as O((n+1)!)
use definition
because (n+1)! is n+1 times bigger than n!, and n+1 sure isn't a constant.
(1) f(n+1) / f(n) = C
(2) f(n+1) = f(n) + f'(n) * 1 // not really, but good enough for our purposes
subst (2)->(1): (f(n) + f'(n) * 1) / f(n) = C
1 + f'(n)/f(n) = C
f'(n)/f(n) = C-1
f'(n) + (1-C)*f(n) = 0
solve it:
f(n) = K * exp((C-1) * n) // for any K
f(n) = C1 * C2^n // different form
conclusion:
for any function that grows not faster than (anything)^n: O(f(n)) == O(f(n+1))
for other functions: O(f(n)) < O(f(n+1))
(i probably handled cases C<1 and C<0 incorrectly, but i don't care enough)
(and i probably shouldn't use big-O here, but i don't care about this too)
I like more the fact that
O(nk) is the same as O(n) if we say that k is always equal to some constant value, like 10^9
in python integers are limited by 2^2^64, so every integer in python is O(1)
Sorry for the more general coding question.
I was taught that in general "a function does 1 task". So in a situation where you have to take user input, validate that input, and then act on that input. Validation and Action (based on the input) would be 2 separate functions. Which makes sense.
But someone decided to leave some comments on my github over the poor practice I have of bundling, propting user input and validating (and re-prompting the user if the input is invalid) in the same function. Is this really that wrong. And is there a best practice for getting and validating input in a console app?
this a pretty general question (and certainly not #algos-and-data-structs), maybe try asking in #python-discussion
Anybody want to help with a trading Model I’m building ?🔥🔥
:incoming_envelope: :ok_hand: applied timeout to @pearl zinc until <t:1706814199:f> (10 minutes) (reason: duplicates spam - sent 4 duplicate messages).
The <@&831776746206265384> have been alerted for review.
>>> import math
>>> hash(math.inf)
314159
>>> hash(314159)
314159
>>> hash(math.inf) == hash(314159)
True
>>> # however...
>>> math.inf == 314159
False
>>> {math.inf: "foo", 314159: "bar"} # silent hash collision resolved by equality check
{inf: 'foo', 314159: 'bar'}
It is inefficient to use infinity and 314159 as keys in the same dictionary 😨 please be aware of this performance trap 🙏. Joking of course
How do I compute the complexity of this algorithm?
If I assume, reading variable, summing, declaring are three primitive operations
then inner loop statement sum+=1 runs 3n^2 times
what's the problem then
The resource that im reading says the total complexity is 5n^2+2n+3
I understand the 3 cames from lines 1, 2, 7
I'm not sure what resource it is, but if you're talking about time complexity then you can discard all but the largest term and ignore the constant, so just O(n^2)
It’s giving motivation to the big oh notation
It’s a step before that
This is their explanation, i think there are some typos
They assumed range(n) has a cost of n
they don't
they assume range(n) is one primitive operation
the second range(n) is in the for loop so it runs n times
they do seem to think range(n) includes n tho
which would be wrong

actually no they do, but not in that table
I guess maybe reading from the range takes one operation, and creating it doesn't
Hi, i am currently trying to make my own calcification neural network from scratch. Im done with the most part except the training part. my plan is to use gradient descent and i think understand the conzept of it but its really hard for me to apply it. i was able to "train" a one neuron per layer model but i have some questouion marks in my head when it comes to a multi neuron per layer mode. ive drawn this image to explain what i dont understand.
my goal is it to update the weight which is shown as a read line. now i dont know which of the blue lines i should take or if i should use both and add the derivatives together.
can someone help me?
I want to learn dsa but before that I wanna learn python
is this tutorial good? https://youtu.be/mDKM-JtUhhc?si=qZYIQ_P1z0zzA2Rr
The complete introduction to Python. This video will cover every part of it and also include lots of exercises so you can practice.
If you want to support me: https://www.patreon.com/clearcode
(You also get lots of perks)
Link to the full course:
https://www.udemy.com/course/learn-python-by-making-games/
Social stuff:
Twitter - https://twi...
i love the way he teaches
should i go for it?
looks nice, i guess
it definitely doesnt contain anything, but for only 11 hours it is awesome
doesnt contain anything means?
go for it or refer something else?
i would go for it, 11 hours is not that much actually
Yes
So this is O(3N) which equals O(N) right? My practice test says that it is N^3 but I guess the zero indenting confused me. I took these for statements as 3 empty for statements with nothing inside of them
for x in range(N):
for y in range(N):
for z in range(N):
tot = tot + z
print tot
A. O(3)
B. O(N^2)
C. O(N)
D. O(N^3)
I took these for statements as 3 empty for statements with nothing inside of them
that wouldn't be valid syntax
but it sure is evil of your test if it ruined the formatting.
I think the formatting of the code might have got a bit messed up.
also, yikes, python 2
haha unfortunately
this is how it is on the test 😦
The test is a bit messed up then. You have to have some statement inside a loop, even if it's pass. The only way this code makes sense is if it's actually:
for x in range(N):
for y in range(N):
for z in range(N):
tot = tot + z
print tot
technically the print can be indented to somewhere but yeah, that's the most likely indentation
I knew the "only way" would come back to bite me
yep... something I am frustrated about. Some of the questions are like this. All I can do is submit feedback
This is also from my test
it definitely doesn't contain anything
harsh
11 hours of nothing
The answer should be 0 0 1
but its not even on there
should it?
I agree the question is atrocious, but isn't the answer just 0 1 2?
Oh I didn't see the n = 2
yeah it's literally one single loop at that point
you might as well just have one for loop with i = 2 at the top
for n=2 it just reduces to
for (j=0; i <= n; j++) {
display j;
}
Oh you're right
oh, wrong word :)
i meant "doesn't contain everything" 😄
hey guys
hello
here, you could just use basic math
instead of o(n^3), it would be o(1)
is there a way to factorially distribute a number's prime factors
i don't know what i'm saying
sf(6) (super-factorial) is 1!2!3!4!5!6! = 24883200 = 1 * 1 * 2 * 1 * 2 * 3 * 1 * 2 * 3 * 4 * 1 * 2 * 3 * 4 * 5 * 1 * 2 * 3 * 4 * 5 * 6 or just 2 * 2 * 2 * 2 * 2 * 3 * 3 * 3 * 3 * 4 * 4 * 4 * 5 * 5 * 6 whose prime factors are 12 2's, 5 3's, and 2 5's
distribute it so that the number of consecutive numbers are consecutively descending
distribute what?
the factors
basically make consecutive factors from prime factors
whose counts are consecutively descending the higher the factor
like, rearrange {2: 12, 3: 5, 5: 2} into {2: 5, 3: 4, 4: 3, 5: 2, 6: 1}?
yea
hey imagine the user inputs the command : "go to desktop 5" now i will have match/ifelse to find go to desktop in string
then i need to find the number 5. any good way to get this values?
Can anyone suggest a good free DSA course in python
How can I make my code quicker/more efficient?
There are n people standing in a line: person 1, person 2, ..., person n
You are given the arrangement of the people as a sequence a[] of length n where i-th element (1-indexed) represents the following information:
-- if it equals -1 then person i is at the front of the line
-- if not, i.e. it equals x then person x is in front of person i
The task is to print the people's numbers in the line from front to back
See example below:
Example 1:
liningUp(6, [4, 1, -1, 5, 3, 2])
Output 1:
3 5 4 1 2 6
Example 2:
liningUp(10, [-1, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Output 2:
1 2 3 4 5 6 7 8 9 10
Example 3:
liningUp(30, [3, 25, 20, 6, 18, 12, 26, 1, 29, -1, 21, 17, 23, 9, 8, 30, 10, 15, 22, 27, 4, 13, 5, 11, 16, 24, 28, 2, 19, 7])
Output 3:
10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14
def liningUp(n, a):
new = []
last = -1
for item in a:
last = (a.index(last)+1)
new.append(last)
print(new)
in the pins
The first step is understanding why your code might be inefficient. Looking at it, I see a loop over N items, and each iteration does a.index(last) which is O(N) each. So O(N^2) in total. So the first thing I try to think of is whether I can get the order in a single pass over the list instead, or at least a constant number of single passes.
It takes some practice to recognize what might be helpful here, but my intuition looking at the problem is that I don't know immediately the absolute position of a person, only who is in front of them. If I could construct a "bridge" between a person and the one behind him, I could go over the list once to construct the bridges, and then just walk the bridges, starting with the person who had -1, to get the right order.
how can i link a def file to a python file?
for example i have a file with print_main_menus(): and i want to use that file with my current python file with all the elif statements
omgggg
Option 1: all arriving passengers are placed in a single queue, and service stations take passengers from the front of that queue.
Option 2: each service station has its own queue, and arriving passengers are dispatched to a queue according to one of many policies:
2.A: round robin (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, ...).
2.B: arriving passenger is placed in a shortest queue.
2.C: arriving passenger is placed in a random queue.
Inputs to the simulation:
The duration of the simulation measured in minutes (D: make it arbitrarily long, do not worry about it being or not being realistic).
The average arrival rate measured in minutes (A: arrivals are random, but on average there is one new passenger every A minutes),
The average service rate measured in minutes (S: service rates are random, but on average they need about S minutes of service).
For the sake of this study, make sure to crowd the system, by choosing S >> 5*A, without causing an overflow of your queues. Also choose D to be long enough to get rid of any transitory effects.
Outputs of the Simulation for each queuing policy:
The duration of the simulation (which may be longer than the input parameter, as when check-in closes, there may be passengers in the waiting queues and service stations).
The maximum length of the queue for each queue.
The average and maximum waiting time for each queue.
The rate of occupancy of each service station (percentage of time each station was busy).
If you want: show the real-time evolution of the queues during the run-time simulation.
so this is the question I am working on 😄
import random
class Customer:
def __init__(self, arrival_time):
self.arrival_time = arrival_time
self.service_start_time = None
self.service_end_time = None
self.service_time = None
class ServiceCounter:
def __init__(self):
self.queue = []
class QueueSimulation:
def __init__(self, duration, arrival_rate, service_rate):
self.duration = duration
self.arrival_rate = arrival_rate
self.service_rate = service_rate
self.service_counter = [ServiceCounter() for _ in range(0, 5)]
self.customers = []
def queue_policies(self, policy):
current_time = 0
while current_time < self.duration:
if random.random() < 1 / self.arrival_rate:
arrival_time = current_time
customer = Customer(arrival_time)
self.customers.append(customer)
if policy == "1":
self.service_counter[0].queue.append(customer)
elif policy == "2A":
round_robin = len(self.customers) % 5
self.service_counter[round_robin].queue.append(customer)
elif queuing_policy == "2B":
shortest_queue = min(self.service_counter, key=lambda counter: len(counter.queue))
shortest_queue.queue.append(passenger)
elif queuing_policy == "2C":
random_counter = random.choice(self.service_counter)
random_counter.queue.append(passenger)
am I going in the right direction?
I’m trying to learn insertion sort
is it swapping the elements ?
in line 6 i feel like a[j] and a[j+1] have the same value
it doesn't swap the elements
insertion sort needs to shift the elements up the list to insert the new element into the sorted subarray, so it does that by progressively rewriting elements until it gets to the place the element belongs
an example of one insertion might help
1 5 6 8 9 7
we're trying to insert 7 into the sorted subarray 1 5 6 8 9
the insertion would look like
1 5 6 8 9 7
1 5 6 8 9 9
1 5 6 8 8 9
at this point A[j] is 6, which isn't larger than 7, so we stop and assign 7 to A[j+1]
1 5 6 7 8 9
other than rewriting each time, can we just push the index of every element right of it and place wherever nessecary?
1 5 6 8 9 // insert 7 into array
1 5 6 8 9
15 6 7 8 9
And what steps do you need to take to "push the index of every element to the right" ?
No no, ignore this for a second
Just imagine you have this list
[1, 5, 6, 8, 9]
```And you need to insert `7`. So you "push the index of every element"
```py
[1, 5, 6, (?), 8, 9]
```How would you do this in code?
yea go ahead
I’ll first use a for loop to check what index to should be replaced
i know i know , can i tell can i tell
btw i tried it using heap sort but can't get it working , can write a code for the previous one bro?
you can just continue the discussion in the thread you already have
haha
arr[5]= arr[4]
arr[4]= arr[3]
arr[3]= 7
ie i’ll use such a for loop until the spot to be replaced
I suppose
So really what you're doing is
1 5 6 8 9 // ins. 7
1 5 6 8 9 9 // arr[5] = arr[4]
1 5 6 8 8 9 // arr[4] = arr[3]
1 5 6 7 8 9 // arr[3] = 7
I can make it more general if i get more time ig
Compare this to what Stickie said above; Notice anything?
I see
Essentially both are same
one use a for loop, while other does on the go
What is a more better way to sort?
So insertion sort is a "O(n^2)" algorithm, meaning that as the numbers to sort goes up, the time it takes scales quadratically
Faster algorithms like quick sort, merge sort, heap sort, are O(n log n) algorithms, and you can't really do much better than that without losing generality
@narrow mica
ye?
I suppose the worst case is descending for asc algorithm
so n+ (n-1)+ (n-2) + …+ 3 + 2+ 1
yeah so this, which is just O(n^2)
nope
Time complexity measures how well an algorithm scales, it doesn't care about actual run time
O(n^2 / 10000000000) is the same as O(1000000000000000 * n^2)
What "better time complexity" really means is that given a big enough input, one algorithm with better complexity will run better than the other
How big is "big enough" depends on the algo themselves
I recall now, the function inside should be a scalar multiple for all n>n0, for some n_0
$f(n)= O(g(n))$ , if $f(n)= c* g(n)$, whenever $n \geq n_0$
We don’t have TeXit here?
There's a bot command, I don't use it though idk what it is
found it, .latex
.latex $f(n)= O(g(n))$ , if $f(n)= c* g(n)$, whenever $n \geq n_0$
It's a more mathy way, but otherwise yes
You don't really need all the mathy stuff to under stand time complexity though
Just remember that it measures how well something scales, not how well it runs
Makes sense
In fact, there are a whole set of "galactic algorithms" that have great time complexity, but the constants are so big they're never used in practice
Thank you. However, I still don't quite understand how I would implement this.
Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order.
Example 1:
Input:
N = 5
arr[]= {0 2 1 2 0}
Output:
0 0 1 2 2
Explanation:
0s 1s and 2s are segregated
into ascending order.
Example 2:
Input:
N = 3
arr[] = {0 1 0}
Output:
0 0 1
Explanation:
0s 1s and 2s are segregated
into ascending order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function sort012() that takes an array arr and N as input parameters and sorts the array in-place.
i don't understand this.
wdym by not understand? Don't understand what the question is asking you? Don't understand where to start?
don't understand where to start.
this is my homework assignment, and i've never played with sort() before
Well it's pretty simple to use
my_list = [ ... ]
sorted_list = sort(my_list)
```Though I'm 90% sure that this isn't what they want you to do
oh, what are they askin gme to do?
Implement your own sorting algorithm
Not stuff like insertion sort or quick sort, but a very special one that works because you only get 0 1 or 2
so i should asort the numbers and assign them to the self,arr,n
You should write a function
def sort012(arr: list, N: int):
...
return # don't return any value
my_array = [0, 1, 2, 0, 1, 2]
sort012(my_array, len(my_array))
# my_array becomes [0, 0, 1, 1, 2, 2]
okay ill try to work this through
Try and think about how you can (ab)use the fact that you only get 0s 1s and 2s
hmm okay
this whole coding thing is confusing to me
all the syntax, rules, parameters, and my class room is moving so fast
class Solution:
def sort012(self, arr, n):
low = 0
mid = 0
high = n - 1
while mid <= high:
if arr[mid] == 0:
arr[low], arr[mid] = arr[mid], arr[low]
low += 1
mid += 1
elif arr[mid] == 1:
mid += 1
else:
arr[mid], arr[high] = arr[high], arr[mid]
high -= 1
i tried this, and it's saying
Runtime Error
Test Cases Passed:
0 /35
For Input:
65754
2 0 2 0 0 1 2 2 2 1 1 0 1 1 1 2 0 1 2 1 0 1 2 0 0 0 2 0 1 0 0 0 1 2 1 1 1 2 1 2 1 2 2 1 1 2 0 2 0 0 1 2 1 2 1 1 2 1 2 0 0 1 0 2 1 1 2 0 2 0 1 2 2 2 2 1 0 1 2 2 0 1 1 1 0 1 2 0 0 2 1 0 0 2 2 1 0 .................
Input is too large Download Full File
Your Output:
File "/home/014629319a406e51cb768a7745e673f1.py", line 15
elif arr[mid]] == 1
^
SyntaxError: invalid syntax
Its Correct output is:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .................
- loop through the array
2.count the instances of 0,1&2. - print them
figured it out. i had a couple typos.
shouldnt it be <= not just <
good resources to learn dsa in python?
if u want to call one class method from another, whats the proper way of doing it?
Classmethod as in a method decorated with @classmethod?
yea
i guess cls is a reference to the class, so what katy said does make sense in python discussion
Oh right. Just access the other class method as an attribute of cls, assuming you mean two class methods within the same class?
so cls.a() for example from classmethod b?
Yes, correct.
i see...
so cls is a reference to the class
that reference calls its a attribute
and it gets passed as the first parameter
class C:
@classmethod
def a(cls):
...
@classmethod
def b(cls):
cls.a()
Yep
i see, cool, thanks for the help
Check out some of the resources linked in the pinned messages of this channel.
channel pins links to some stuff
Alright 👍🏼