#algos-and-data-structs
1 messages ยท Page 19 of 1
for a list no
if you delete in the middle of a list you need to shift all the element right of it one step left
linked list -> O(n) indexing, O(1) removal if you're "at" the index
list -> O(1) indexing, O(n) removal
oh they're reversed
interesting
ditto for inserting, O(1) for linked, O(n) for normal list, because of the shifting 
alright but i really must go now, tysm!!!
as I mentioned, O(1) insertion is only true for linked list if you insert at a node you already have O(1) access to
and I suspect deque doesn't even have O(1) insertion in the middle anyway, even if it was a supported operation like in usual linked lists
deque is a linked list of blocks, and I expect it will "re-align" the blocks on insertion/deletion
I haven't checked the source though
but in short just use deque for deque operations, most other operations are terrible
Unrolled doubly linked list I think.
More memory, but if you want that O(1), worth.
(Can use the technique in general on linked lists / unrolled linked lists)
class Solution(object):
def plusOne(self, digits):
lis = []
for i in digits:
if i == 9:
return [1,0]
elif i != 9:
lis.append[i]
return lis[::-1] + 1
is it possible to solve this problem
this method?
Hey so I'm learning about shell sort and we have to find the worst case for it. We're using the original sequence and after running the algorithm with reversed and random input, we found the random input takes more time to run. Anyone have any ideas on this? Thanks
Hello
How can you calculate epochs of Adaline algorithm with a certain epsilons and learning rate for patterns like OR ?!
shell sort with gaps 1, 2, 4, 8, 16, ...?
iirc a power of two array size is a bad case there
yup.
Hi guys, i need help i don't know how to use str on my poo look
No. The .append method is not subscriptable. Your code returns an early, incorrect output for the input [9,9]. Also, you're trying to concatenate an int with a list
a power of 2 array?
you mean like 1 2 4 8 16 32...
no, power of two long
Shellsort, also known as Shell sort or Shell's method, is an in-place comparison sort. It can be seen as either a generalization of sorting by exchange (bubble sort) or sorting by insertion (insertion sort). The method starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compar...
oh, I mean what data order would result in its worst case
it mentions a worst case under the table in this section
like its best case is when the array is already sorted right?
probably
power of two is the worst case for the N, because consecutive zeroes
and then that even/odd config
so something like
n = 2**12
A = [None]*n
A[::2] = range(0, n//2)
A[1::2] = range(n//2, n)
shell_sort(A)
not a huge case, but you can raise that power
a few more powers up things should get pretty slow if it behaves quadratically
What is N in the example above? the number of elements in array a?
I still don't understand it very well... could you perhaps give me an example?
!e yes, n is the number of elements of A
n = 2**4
A = [None]*n
A[::2] = range(0, n//2)
A[1::2] = range(n//2, n)
print(A)
@haughty mountain :white_check_mark: Your 3.11 eval job has completed with return code 0.
[0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15]
so small/large elements on even/odd positions respectively
so like, 1000, 3, 9889, 34, 1485, 9, 438, 9...
like that?
you can see the example I gave
positions 0, 2, 4, ... have elements 0, ..., n-1
positions 1, 3, 5, ... have elements n, ..., 2n-1
oh... I see. thank you
Hey is this where I can ask help to optimise a program ?
probably
TLDR a group of friend and I are trying to get the number than appear more than 3 times in the inner triangle of a pascal triangle as fast as possible.
We've set up the limit row to 250
And so far I'm here :
def search_pascal_multiples_fast(row_limit):
triangle = [[1]]
for i in range(row_limit):
triangle.append([1] + [triangle[i][j-1] + triangle[i][j] for j in range(1, len(triangle[i]))] + [1])
return sorted([number for number, count in Counter([number for row in [row[1:-2] for row in triangle] for number in row]).items() if count > 3])
And I clueless on how to make it better. Any ideas ?
huh, interesting. what comes to my mind is what it means mathematically: e.g. 120 occurs in 5 places because binomial(10,3)=binomial(10,7)=binomial(16,2)=binomial(16,14)=binomial(120,1). That last one is always going to be there and the first four are two pairs. So what's special about 120 is just that it happens that
10!/(3! 7!) = 120 = 16!/(2! 14!)
which... doesn't give me any fancy ideas on how to predict it, hmm.
Of course, OEIS has an entry for it, https://oeis.org/A003015, and it's 20 years old and has tons of references. But it seems like it really is just a search task.
Yeah but most of maths concept they mention here fly right over my head.
The only one I have a grasp on is the nCr and it's slower than my approach
But I did try singmaster's conjecture
hey all, i need help implementing a randomized list for my algo. i don't want it to be truly random, so i'll need random seed i think. what i would like to do is, if the range i say (0-10), put the numbers in the same random order each time i run the program. i cannot pre-determine the random stack because it'll vary in size.
Random.seed(Random.randomint(1,10))?
Random.seed() takes an integer so what I wrote previously should work I think
Yes with the random.randint(1,10)
then you would be randomizing the seed to one of those
the seed confers a non-randomness to the random function, or at least, the same "randomness" each time
import random
random.seed(1)
list = [i for i in (random.randint(0,10))]
print(list)
this gives error:
Exception has occurred: TypeError 'int' object is not iterable
so i'll need a range somewhere as well
ah, randrange
Hi, I have a binary matrix of size o*p. I'm given K queries where each consists of given sub-matrix, with upper left coord (x1,y1) and lower right coord (x2,y2).
My task is to count one's in each submatrix. It should be done in O(o*p + k) time complexity. How do I do that?
i was trying to randomize a list in the same way each time, but i've abandoned that. now i am trying to fix up my logic for when i pick a new bucket to place a key into. its a hashtable algo
.
i'm struggling with my logic bc i essentially have like a while trying and failing thing is true: do x but i'm not able to see how to meet the original condition to break the while look
loop
i'm ending up just inserting the same key over and over again
Hint: consider a lower-dimensional version of this: you have an n-sized array, and you are asked to quickly (O(1) per query, after O(n) preparation) answer queries of the form "what's the sum of elements a[i]+a[i+1] + ... + a[j-1]?" (so, the sum of a[i:j]). Do you know how to solve this one?
[randint(...) for _ in range(n)]
its a bit more nuanced, i need them to be unique. but i'm more focused rn on trying to implement the logic you mentioned y'day, eg, while failing a try_insert, pick new bucket location, try again. i am going to read over the old messages now
lol i just end up inserting the same key for every location in the table
ok i think i got it
wait, nvm
i got it inserting and not overwriting anything but now i am unable to insert all of my keys and i'm not sure why
i have plenty of space in the table
oh, this gives uniqueness. thank you
this is exactly what i wanted thanks. this works:
self.stack = random.sample(range(0,primary_hash_int),primary_hash_int)
something about my chaining logic is still broken
can i use a random seed so that the sampling will be the same each time for a given primary hash variable?
yep
awesome
(For reference: https://en.wikipedia.org/wiki/FisherโYates_shuffle, non-inplace version uses a hashmap to keep track of swaps)
Knuth might as well be the field.
i swear everywhere i look i see his name. wild the influence he has had
Knuth also just wrote a lot of notes on various things that are useful and many use them as reference.
i like how in the fisher-yates wiki they have a pencil and paper method
nice way to learn an algo
Random shuffles were done on paper long before (modern) computers.
oh i'm sure
i wonder how these graphs are generated not that but what dataset they're basing this on
obviously its not search engine stuff
Book/paper references to the word.
right
Of all books we have.
Google scanned in a lot of books and papers that were physical.
But other organizations did too.
and sent them to Alphabet/GOOG?
only really post 1950, or the y axis doesn't allow one to see few mentions on the scope of millions
or whatever the upper bound is
Looks like it's per year, not cumulative, but there was a lot of "algorithm" prior 1950.
right im thinking we just cannot see the 1-1000 refs / year or whatever it was due to the scale of y
More than 1000.
Guys, what is the best place to learn and to train algorithms and data structurs?
see pins and also abdul bari on youtube
in TeX, which he wrote
Here ๐
and here
learn data structurs is know how things work like in the array for example?
I thought the reply was to something else.
also, depth first and breadth first searches
Yeah, Knuth is something else.
know the performance of components, what is better for use?
heaps
this is main types of data struturs?
those are some common ones, they you have the ways you want to represent your graphs, you have the graph traversal algorithms, there are heaps, etc
I'm just saying he made TeX to typeset his things ๐
kind of mixing in DS and different algos here, but each algorithm is underpinned by one or more datastructure
the stack and queue mentioned there can be implemented as a pythonic array or deque
fiery i hate to ask but are you able to assist with fixing my chaining logic
i have it running but i am failing to insert all of my keys
why is there an else there?
up top
ok, why is your else doing all that?
why are there try_inserts in the else?
the else is only there to deal with linking a new bucket
do you need make functions
๐ค
you're not seeing the entire prog
y'know, you might benefit from adding a comment to each block to remember what it's supposed to do, which might deter you from making it do something it shouldn't do
you aren't wrong, i need to do that anyway before submission
you still need the while loop if you're going with the "potential free list" route
how about this:
no i need my try except block
i keep getting pop from empty list errors
or maybe just a break
yep, I would move the keys[0] is not None into a method in the Bucket though
e.g. is_empty
while self.stack and ...:
...
if not self.stack:
raise ...exception about no free buckets...
return self.keys[0] is None
so i should not have any try_inserts in my else block at all?
why would you?
in the event that the insert is successful, i want to update the pointers
err
the is_empty method will guarantee successful insertion
if you want to add a comment to that block it would be like
# Add a new bucket to chain
as the comment suggests, no insertion is going on
you are in the else case, so you know you have to link a bucket
so find a free bucket, link it, and go to that bucket
yeah, you also need one of the pops before
well
you could also do a different style loop I guess
no matter the logic i continuously find i am unable to add all keys
for which return here
this is iffy but should work
while True:
if not self.stack:
raise ...
new_p = self.stack.pop()
if ...[new_p].is_empty():
break
it's the whole function body
what about the pointer updates
just do it afterwards?
the loop is to find a free bucket
when you have that you know what to do
ah ok
again, a reasonable place to add a comment
for the loop
that it finds a free bucket
hmm im struggling with the whole passing along new_p vs the original p thing
like i can't just name it p or it'll overwrite where i want to put the pointer
i guess this is where i need that logic i pasted above
the bottom two lines
yeah i got it
update the link, then follow the link
ugh it's just adding the same keys over and over
raising the string is wild
yeah, raise your custom exception
๐ข
let's think, how can multiple of the same key even be inserted?
why do i feel like the culprit is my second try_insert call in elif
if it inserts and follows a pointer and inserts the same key again for a given i
wait, why do you have an try_insert there?
it's an error
but not the error I saw
how else might i try to insert after following the pointer?
again, you wouldn't make these mistakes if you would have added comments about the steps you're supposed to do
the last step doesn't need insert anything
it just adds a bucket and moves to it
the second step (in the elif) should similarly not insert
it does a way simpler task
elif there is a link:
???
ok let me ask you this, how can we spend multiple iterations of i to insert a single element
we'd run out of loop iterations to insert them all, or could potentially
you could write it as a while loop, I think you opted for the for loop way back
if we spend several loops to find the proper p value, we've used multiple of our finite number of loops to insert one key
so?
the reason we have n iterations is that it's for sure enough iterations
it's effectively a while loop
i don't believe it is. it's only enough if we are guaranteed to insert the key during a single iteration. consider when keys = tablesize
correct
ok, what's the issue?
if we fail our first insert, and never attempt another one, we're using at least 2 of the tablesize number of iterations to insert a single key
so? it's not like we have tablesize iterations to insert a bunch of keys
hmm this might explain why i had it working before but i was failing to insert all the keys
this loop is to insert a single key
right, but its not guaranteed to. what if we use 5 of our tablesize iterations to insert 1 key, and each key takes > 1 iteration to insert on average? we would run out of iterations
if we are not inserting during any given iteration, only finding a new p, we could run out of iterations to insert every key, especially as the number of keys to insert gets closer to the tablesize
shit i really wish i could go back to what i had before and test with a larger bound on the loop..
fair enough. but i hope you can tell what i'm on about. i removed the try_insert from there
fill in the blank in the pseudocode
set p equal to the link pointer value
i.e. follow the link
ok, there is a very simple reason why your code inserts the same key over and over
where is apply data structurs in python, in what functions?
look at your if block
need a break?
I'll take a break
lol
yes, you successfully inserted
ok so that's much better, now im only failing to insert 3 keys
you're done, so break or even return
could you post the full loop?
just to check it looks alright now
yeah, that looks fine
btw i randomized the stack with a seed for better performance ๐
ok yeah it is somewhat surprising i am failing to insert every key
let's get back to this
so what's the issue you're seeing?
yeah no nvm. i was thinking this whole loop was to insert every key. nevermind
i was incorrect
I repeatedly said "the loop is to insert a single key" ๐
and why is n steps enough?
how many unique links can you even follow in a row?
in the unlikely condition that every try_insert fails
how many links are there?
oh idk
there are only n links
n-1
ok sweet. any idea why i'm failing to insert some? i should try a larger input too.
rn keys = 84
when tablesize is 120, and input keys = 115, i successfully add 106 keys and fail to insert 9
seems to be some constant fraction of the keys
interesting
I'm assuming you meant to call is_empty
where
uhh, you want it in elif? eg, elif table p has a pointer, follow it and check if that slot is empty?
you're not calling the is_empty function in your code
its in the else clause, do i need it more than that?
...call it
oooooooooo
thanks lancebot
ty
yep, that fixed it up
ty ty
ok, i think i am finally done tinkering and ready to complete the analysis. the rough part is, i need to redo every single test call and generate new numbers. i should really get busy on that bc the first time around it took me foreverrr
a statically typed language would have screamed at you, sadly this is python ๐
i have missed those () before, yeah
automate the boring stuff with python
you just read my mind, i had already typed out a message saying if i had more time i would write a method to run all the tests for me
i have 11 schemes at least, before i get into looking at the same scheme with changing input size
eh, i'm sure its pretty tough given i have so many varying command line calls and different input files
or at least save annoyance
even bash would do fine
just a bunch of nested loops
i should have made it do that automatically..
right now i am manually keying in each combination each time along with the arguments ๐คฆโโ๏ธ
๐คฆโโ๏ธ ๐คฆโโ๏ธ ๐คฆโโ๏ธ
even if you don't want the loops and that complexity
why not just write all the commands to run to a file?
and then run that
wdym
you know the commands you type out over and over? write them one per line in a file
and then you can just run that file as a script
how so
so thankfully i already have all the names, cause i have them as outputfile names
u0_a258: ~
> cat a.sh
echo do a
echo do b
echo do c
u0_a258: ~
> bash a.sh
do a
do b
do c
I'm saying write the commands to run to a file, then you can just run all in one batch
some manual work and copy pasting, then you can re-run stuff as needed easily
without typing stuff
(and you can avoid a ton of the initial typing using loops, but even without that this is a better way than writing everything every time)
im with you. i do need to learn bash soon
as said, you can literally just write out the commands one per line to a file
that's a valid script
then you can run it and have everything generated by executing one script
suddenly the annoying part isn't annoying anymore and you don't need to fear having to redo it
oh
since redoing it is just running the script
Guys in general, in big aplications do you develop a data structurs or use structurs of python, like lists, arrays?
not python script
You can treat the script file as if you typed it into the terminal yourself.
It just does what you write in the terminal.
@fiery cosmos what platform are you on again?
i mean like, everything in one line in a text editor?
one command per line
One line per program execution / command.
i'm on windows, i use powershell for unix/command line or w/e. my IDE is VScode
On windows you make a batch file.
yeah, for this trivial usage they should behave the same
just write one command per line to a .bat file
run that
so:
open notepad
write one command per line
save as .bat
run the .bat with.. command line?
python myfile.py --my --flags
python myfile.py --different --flags
...
Command line or double click it, but it will close the terminal window immediately after finishing if you double click it to run.
You can add a "pause" as the last line.
Will wait for a key press to exit then.
(Then just double click the bat file to run)
Squiggle do you can explain for me about use of data struturs in system?
Or do you need understand how work only
because already are created in functions?
Algorithms operate on data and that data needs to be stored somehow. A data structure is the specification of how that data is laid out.
embrace laziness
no this is so amazing that this works i've spent like 5 hrs before doing this manually
automatically naming output according to the inputs would have been cool too. next time
the other better thing about this is i can see every command all at once, and ensure i'm not rerunning things or overlooking things
Naming output?
right now i am manually passing an output name each time for a given input, eg, whatever text file name you write in
and actually, it'd probably error pretty bad if you named a non txt file
You can have it write to stdout (the terminal, same as printing normally), and batch / bash lets you redirect such output to a file.
But i'm guessing they want you to write to a file.
but in real how is apply?
in a system?
Systems operate on data and that data needs to be stored somehow. A data structure is the specification of how that data is laid out.
correct
But data structurs work in data base?
or in code?
alright let's try this bad boy
class MyData:
x = 10
y = 20
(and many other ways)
i think running that and having every input get run almost immediately is the most satisfying thing ive ever done in coding
watching out the outputs spring into existence with a single enter
i might be hooked y'all
understand, but data structurs can connection with data base?
:
A database is a system for managing data and the relationships within. It must have some layout for the data... (it must have some structure for the data)
how will this batch file treat blanklines
Like blank lines if you typed them into the terminal, so nothing.
itll skip and continue though
Imagine it auto types that file into the terminal for you.
Line by line.
It runs a "batch" of programs.
i got you
I have a file with records of how a user navigated through a website.
It is encoded as follows:
A;B;C;<;G;<;<;M
Where letters represent the current page and < represents the user clicking on the back button. Every action is separated by a semicolon. In the above example, the user started at A, navigated to B, then C, then back to B, then to G, then back to B, back to A, and from A to M
My objective is to track all the "movements" of the user in order to measure the popularity of each link. I decoded the path to a doubly-linked-list in order to navigate from the start point to the end point, counting each "jump" taken.
This requires a lot of resources since I have 100k paths to analyze and I was looking for an idea to optimize the process. Any suggestions?
why a doubly linked list? and how do you measure popularity?
Popularity is measured by frequency that a link is used.
Doubly linked because I decode the "path" and then traverse it to increase a variable storing the frequency each link was used.
I'm trying to find a way to decode and count at the same time, but can't put my finger on it
Squiggle i can send message for you in private?
so the < means the "parent" page is counted again?
< means that the user went back to the parent page, and that the next link will be from (parent) to (other) page
right, but how are you counting that in the frequency calculation
< are not counted, only the next action will be
Therefore, A;B;<;D should give: one click on link to B from A, one click on link to D from A
oh you need to track the links?
Yes, the end objective is listing all the links out of page A and identifying the frequency that each one is used by the user
But I have thousands of "paths" from a user jumping around the website
seems fairly trivial then. use a stack, when you see a letter, add to a Counter the link (the previous letter, the new letter). then pop if you see a <
you could either split the string on ; or just iterate using indexes
well a stack could be implemented with a linked list. though python's list will be just fine (and probably better)
python's list is basically a std::vector
Also for context, the project is based on this dataset https://snap.stanford.edu/data/wikispeedia.html
Where each page is an article with all outgoing links to other articles.
The goal is to analyze the popularity of outgoing links in articles based on their position, length of the word, etc.
A(?=B) and A(?!B) both of them in regex is equivalent positive and negation in logic?
How do I get good at solving Problem Solving questions?
You will be fine. Wish I knew c++ ๐
Is it true
?
I got a question
is there an equivalent to cpp's
set<pair<float, int>> my_set;
my_set.insert(pair<float, int>(1.2, 1));
my_set.insert(pair<float, int>(2.4, 2));
pair<float, int> min_number = *(my_set.begin());
my_set.erase(my_set.begin());
It gives the minimum in O(log(n))
So in other words, is there any way to get minimum in python O(log(n))?
If you already have a set?
Constructing the set is obviously O(n) already
There's a binary heap:
!d heapq
Source code: Lib/heapq.py
This module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm.
Heaps are binary trees for which every parent node has a value less than or equal to any of its children. This implementation uses arrays for which heap[k] <= heap[2*k+1] and heap[k] <= heap[2*k+2] for all k, counting elements from zero. For the sake of comparison, non-existing elements are considered to be infinite. The interesting property of a heap is that its smallest element is always the root, heap[0].
Thanks man
is a print statement considered a return
no 

this batch file is saving my life rn
hmm
feel like i might be counting my chaining comparisons wrong
either that or it really is just wildly more efficient
yeah i added a comparison counter to my while loop
if inserting n keys into a hashtable and each key takes time of O(n), it'd take n^2 time to insert them all?
yes
but insertion really shouldn't be O(n)
worst case yes, typical case no
thanks i just wanted to ensure i had the logic correct
one thing that could be interesting is a scatter plot of the cost per inserted key, as you add keys
i agree
i can say that it would look somewhere between exponential and linear for linear and quad probing, mostly linear in the case of chaining
chaining with a randomized stack is remarkably efficient, as measured by very low number of comparisons
what's the range for your random keys?
for their input or my supplemental input
I guess both are interesting
1s
what I'm curious about is really "do you expect any collisions?"
but knowing the range answers that
theirs are 0-99,999
that seems fine
mine are 0-90k
then you should get some random collisions
i would have thought its more dependent upon the number of keys you try to insert
oh i get quite a few
let me share a graph
sry, this is comparisons
but the primary and secondary collisions end up adding up to this value
or somewhere similar
as expected, chaining is much more efficient
the amortization analysis of cost to insert keys could be interesting as a function of load factor
it depends on the distribution of keys
say you insert numbers 1-120 in a table of size 120, with mod 120
no collisions
should be near to zero
right
yeah i should really test input with many of the same key, and measure the comparisons required to enter all
but alas i am burnt out
I just wanted to make sure that you don't sample numbers between [0, tablesize) or something
thanks for that
i'm a little bit brighter than that
slightly
๐
very pleased that you and squiggle got me going with the batch file
thats, a game changer
and as i have said before, i seriously hope i get to continue learning software dev because i really enjoy learning and troubleshooting
i see it as being more competent and automating things others simply don't know how to, but i appreciate the humbleness of describing it as lazy
even stuff like text editing can be automated with a powerful enough text editor ๐
oh, I thought you were going to say "github copilot" :p
hey, I put a lot of effort into being lazy!
so good at automation you automated yourself away
lmao
(the secret is automating your work and not telling anyone, so people think you're working hard)
my batch file open in a text editor was being really weird, instead of moving stuff when i tried to insert into the middle of a string line it started overwriting all the proceeding characters 
that brilliant lol. i always wondered what level of that there is
oooooh, that's what that key does????
wow
TIL
must be a legacy thing
also, oddly enough the only way to open a batch file on windows (that i could find), was dragging it into an open text editor. if you double-click it'll just run itself and disappear
i should get on linux, i have always hated windows and having to be logged into certain softwares
my friend discovered that the insert key enters insert mode in vim, which I found bizarre
you should be able to right click and edit
rightclick-Edit?
๐ฅด
lms
vim is a text editor
oh ok
quite old school text editor
but very powerful
(and very different from your usual text editors)
oh i see why i didn't find it, in windows 11 they hide all that useful stuff from you, including copy
you have to go right click -> show more options -> edit
that's terrible
something something boiling frogs
in windows12 you'll have to enable these options in the settings first ๐
I'm living in a cozy terminal on linux since like a decade
i've had every intention of going there, i've even partitioned my SSDs to make a linux enviro, never got around to it
how does it affect gaming
I don't use guis much unless it makes a lot of sense for them being guis, like web browser, image editing, and some other stuff 
depends on what you're playing
have you heard of puter
a lot of stuff works fine, mutiplayer games that use anti-cheat are likely not going to work
even if the game itself would work
oof that's a big ask. i guess one could always have one PC for gaming and one for everything else
its an OS within a web browser
pretty cool
but most games I want to play kinda just work
because of big efforts from valve
do you use steam
yeah
valve has proton which is a compatibility layer that allows running windows games on linux
(based on wine)
never heard of proton or wine. sidebar my email carrier is protonmail
just use WSL
why do you need to learn R?
basically a Linux vm
im going into computational bio which is stats heavy
yeah thats what i though thanks
what is linear regression all about
fitting a line to data
i don't think my explanation of insertion asymptotics is making sense
fitting a line to data
lol bruh
the number of people that buy ice cream if it costs 20 dollars is 50. the number of people that buy ice cream if it costs 10 dollars is 100. i wonder how many people will buy ice cream if it costs 5 dollars
seems simple enough
That is not a very accurate example. In reality, it should be more exponential than linear
let's say it's perfectly elastic :P
wait, why would it be exponential
that's not how it works
idk about that
if you give it away for free you'll have infinite customers
and if you sell it 200, half of a person will buy it
idk if this model makes total sense ๐
just floor it for the half a person case
and it does make sense for the free case if you change it from "how many people will buy it" to "how many people that are able and willing to buy it would buy it"
but anyways, it does work as an example
this feels related ๐
"sublinear"
but yeah, basically you can use linear regressions to try to fit functions to data
(it's fairly basic linear algebra to do it)
just for modelling? or like, what's the primary purpose. is it the same as linear regression analysis? what is multiple linear regression? why is there an entire branch of algebra dedicated to lines (actually, your previous post gives me some idea)
I have data, it seems to have a pattern, let me try to fit a function like the pattern, how good of a fit is it?
it's not only lines, you can fit other stuff with linear regression
i was referring to linear algebra
e.g. I could try fitting functions on the form y = c1*x + c2*exp(x) + c3*whateveryouwant(x) + c4 to some data
the linear algebra part is how the constants are picked
linear algebra is really about linear combinations of stuff, not just about lines
are those not the same things
no, see his example, he has 4 things that might vary, which gives a 4 dimensional thing, not a line
so a line must then be just a single equation
of multiple terms
which can each vary?
his example is a single equation. a line is an equation with 1 varying thing, essentially
in what way is that not the same
well a line is something like ax = b, right. a plane could be something like ax + by = c a 3 dimensional surface could be something like ax + by + cz = d
hello all! im learning DS and im kinda stuck.
so i made a random walk function that uses an adjacency list (from a dictionary) .. where would i start if i had to modify my code so that it can use a weighted adjacency matrix instead?
def random_walk(graph, nodeid, steps):
da_walk = [nodeid]
while len(da_walk) < steps:
nodeid = random.choice(list(graph[nodeid]))
da_walk.append(nodeid)
turns = steps - 1
random_walk(graph, nodeid, turns)
return da_walk
Basically the same thing but use a list of tuples (id, weight) in your adjacency list instead of just id
which books are good to learn ALGO and DS using python
none of them are super easy
which several are the least difficult
multithreaded algos is kind of a no-go in python
polynomials and fft is a fun topic
ok, that's super helpful to know bc i was leaning that way
fun for the mathematically competent
most of these are pretty mathy
it's fun because the naive way of multiplying polynomials is O(nยฒ) where n is the degree
with fft you can do it in O(n log n)
string matching might not be hard, but it very much depends on what's covered
there is a ton of stuff in that bucket
i mean this'll all be straight out of CLRS, so whatever they cover in there
yes, and idk what that is without having to look it up
lms
computational geometry is so very fiddly
linear programming is a pretty huge topic with a lot of theory around it
idk what they cover in number theoretic algorithms
i've x'd that out bc there is a ton of subject matter
like more than double the concepts of other chapters
if you're curious i can put it here
fft for polynomials sounds really cool
that's definitely on the list
need to pick several
sounds like polys and fft, string matching, and one more
the number theory topics looks fun though
no it just means hard
right
any section will have those, though
chpt 30 has only 3 subjects and none are starred
fwiw chapter 30 will involve some complex math
i don't think anyone in my group has a math background
(as in complex numbers, not necessarily so complicated)
we're a ragtag bunch of like virus people and fresh from undergrad ppl
and fft, which is somewhat complicated
i don't think the majority of math is necessarily that complex, i just think its a matter of not knowing it
is an issue
bc then you have to pause everything and try to go learn it
i mean obviously there is some very difficult math yes
cooley-tukey isn't too bad
it's just a bunch of even-odd splits
are B-trees still used? i think someone said for databases they are often used?
there are no spinning discs in my PC ๐คทโโ๏ธ
ram is still much faster than SSDs, so they're still useful
the book starts with showing a spinning HDD as soon as B-trees come up
aren't B-trees better BBSTs anyway?
in terms of memory usage and cache locality
that's what I thought
because it's old, but it's the same idea
so if you want something like an always sorted set, B trees are great
slightly better ๐
you lose some guarantees of BBSTs, but nothing overly useful
like, if I have a pointer to an element in the set and I delete some element, is that pointer still valid?
is there a formal definition of a pointer? for me its sort of like a murky concept
basically the address to somewhere in memory
like when we just made bucket.pointer = some other bucket
it's similar to an int with a value that's a memory address
those aren't really pointers in the formal sense
but indices
yes, it was just pointing at an index in the table
ill do some reading
rn i need to figure out this B-tree problem
how do memory addresses on the RAM work? is there like a mapping to individual bytes / bits ?
is that done by the RAM manufacturer or the OS
bytes, OS
i'm not understanding the third property of B-trees:
3. the keys x.key_i separate the ranges of keys stored in each subtree: if k_i is any key stored in the subtree with root x.c_i, then
k_1 <= x.key_1 <= k_2 <= x.key_2 <= ... <= x.key_x.n <= k_x.n+1
and a byte is 8 bits (the part of the semiconductor that can be either 0 or 1)
trivia (without looking): what is the name for a string of four bits
inserting into B-trees is rather involved
splitting nodes and adding the median key to its parent
Hey guy's a beginning programmer here with a question; I plugged the following formula into a variable and I get the following TypeErrors:
But if I split the problem up into seperate expressions it will run fine. can anyone tell me why?
do int()
num() actually
do int() on the whole mathematical expression? there is no built in function called num()
oh shoot I was thinking about a module that I'm used to using
pop = (1000*(1.02*0.001)**0.001)+50+0.0001 should work
that's what I thought but obviously I am wrong.
or are you not trying to multiply it by 1000?
you can see I changed them all to floats it made no difference
oh, ok duh. I feel stupid now. Thank you
nibble
technically a byte could be different from 8 bits, but not on modern and non-exotic platforma
if you want to be super pedantic 8 bits is an octet
8 bits = octet = 1 byte
isn't it common knowledge? ๐
bruh
The byte is a unit of digital information that most commonly consists of eight bits. Historically, the byte was the number of bits used to encode a single character of text in a computer
so there have been different byte sizes historically
I think 7 was kinda common for a bit
so there is an interesting implication of self vs. non-self in many things, one of them being the immune system, i wonder if it is the same in computers
and how the concept of sameness manifests
eg, how does a computer know an if check is same or non-same. i guess everything just has its own machine-code representation
but like, how does it know
if pi == pi```
or similar
not important
good old predecessor subgraphs, you gotta love em
It shows up commonly, specifically when doing bit manipulation and hexadecimal (1 digit).
yeah, 1 hex digit is a nibble
compare bits
it took me a bit to realize what they were trying to say
so it'd be like
is 10110110 = 10110110?
i can read it from this into english in my head but haven't yet made sense of it
!e
import math, struct
print(struct.pack('d', math.pi).hex())
@haughty mountain :white_check_mark: Your 3.11 eval job has completed with return code 0.
182d4454fb210940
looks like a lot more than 8, if each letter is 1 byte = 8 bits

i thought 1 bit was a single either 0 or 1
@haughty mountain :white_check_mark: Your 3.11 eval job has completed with return code 0.
18|2d|44|54|fb|21|09|40
easier to count now
TIL bytes.hex()
it's kinda neat
4 bits as an int can take values 0b0000 (0) to 0b1111 (15)
so it's a base 16 digit
2 of those gets us a byte
I could convert that hex into an int and get the parts of the double 
i'll need to look up some sort of graphic. i'm not seeing where you got from 0's and 1's to
this
oh its here
definition of predecessor subgraph is on pg. 600, pretty much the same as shown above
i'm noting that V_pi has a U {s} and E_pi has a - {s}
!e magic
import struct, math
x = int(struct.pack('>d', math.pi).hex(), 16)
sign = x >> 63 # 1 bit at 63
exponent = x >> (63 - 11) & ((1<<11) - 1) # 11 bits starting at 62
mantissa = x & ((1<<52) - 1) # lowest 52 bits
print('+-'[sign], (2**52 + mantissa) * 2**(exponent - 1024 - 51))
@haughty mountain :white_check_mark: Your 3.11 eval job has completed with return code 0.
+ 3.141592653589793
Gฯ = (Vฯ, Eฯ) where: Vฯ = {v โ V : v.ฯ =/= NIL} U {s} and Eฯ = {(v.ฯ, v) : v โ Vฯ - {s}}
Vฯ is all the vertices with a predecessor + the source (root)
Eฯ are the edges that goes from vertices v in Vฯ to their predecessor ฯ[v]
err, ignoring the source in the second one
"the predecessor subgraph G is a graph comprised of sets V predecessor and E predecessor where the set of predecessor nodes V is equal to the set of vertex v in the set of vertices V such that v's predecessor is not equal to None, union source node s, and. the set of predecessor edges are equal to (a vertex v's predecessor, v) edge such that vertex v is in the set of predecessor vertices V minus the set containing the source vertex s."
I read ฯ as "parent" in my head here ๐
no love for my magic trick ๐ฆ
they use ancestor and descendent in this bit
i was going to say, there is someone else more mathematically inclined which will have a proper appreciation for the mathematical gymnastics you are doing up there, as for myself, it was one big whoosh
so, the first line is me getting the 8 bytes representing the python float as an int
then I extract all the parts of the floating point number (sign, exponent, mantissa)
and rebuild the number
what are these symbols: >> <<, what is mantissa???
@brisk saddle do you like my magic trick?
aawhat the FUCK
bit shifts
i'm over here trying to understand graphs and dude is reconstructing pi using bits for fun
can i humbly request
that you stop coding forever
floating point numbers are basically scientific notation but in binary
i could say << should not be used in place of * but it gets optimized anyway and i don't like the look of 2**x (which gets optimized anyway as well)
your a floating point
lmao
I used the << and >> when I was thinking in bit operations
shifting and masking
thinking in bit operations
nope
you've never done this? ๐
do i look like someone who would dothis
is there a magic trick for making me understand all the crap in all these chapters all at once
like the matrix for CLRS
!e rounding
print(123.45 + 2**52 - 2**52)
print(123.54 + 2**52 - 2**52)
@haughty mountain :white_check_mark: Your 3.11 eval job has completed with return code 0.
001 | 123.0
002 | 124.0
@brisk saddle I probably showed this one before
๐ฅด
ugh, floating points are cursed
if you ignore nan inf and subnormals they are very well behaved
the + - trick is kinda dumb
!e
x = 5318008
x.reverse()
print(x)
@fiery cosmos :x: Your 3.11 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "<string>", line 2, in <module>
003 | AttributeError: 'int' object has no attribute 'reverse'
bruh
basically say I asked you to keep track of exactly 4 significant digits of this number, rounding if needed
start with say 123.4
I add 1000
1123.4
you round to keep 4 digits: 1123
I subtract 1000: 123
!e this?
x = str(5318008)
x = x[::-1]
print(x)
@haughty mountain :white_check_mark: Your 3.11 eval job has completed with return code 0.
8008135
and very mature ๐
lol i knew there was a reverse slicing method to go through it. and yeah, that's the only numbers related thing from my childhood i could dream up
#help-cookie i need some pointers to get me unstuck in my DS class. im kind of struggling atm. thanks
@haughty mountain took me some time, but it's working now! thanks for pushing me in the right direction!
G2 = np.array([
[0, 0.5, 0, 0.5],
[0.5, 0, 0, 0.5],
[0.25, 0.25, 0.25, 0.25],
[0.5, 0.5, 0, 0],
])
GG2 = nx.from_numpy_matrix(G2, create_using=nx.DiGraph)
def random_walk2(graph, nodeid, steps, da_walk=None):
if da_walk is None:
da_walk = []
da_walk.append(nodeid)
if steps == 1:
return da_walk
nodeid = random.choices(list(GG2[nodeid]), weights=[k["weight"] for k in list(GG2[nodeid].values())])[0]
return random_walk2(graph, nodeid, steps-1, da_walk)
print(random_walk2(GG2, 0, 4))
my friend made similar looking diagrams when making a regex parser
'hello'
`helo1
base=input('what is the base of the triangle:')
def basecalculator(base):
try:
newbase=int(base)
return newbase
except:
inp=input('enter an integer')
basecalculator(inp)
height=input('what is the height of the triangle:')
def heightcalculator(height):
try:
newheight=int(height)
return newheight
except:
inp=input('enter an integer')
heightcalculator(inp)
beforearea=basecalculator(base)*heightcalculator(height)
finalarea=beforearea/2
return finalarea
print('your area is',trianglecalculator())```
hey guys im new to programming and ive run into this problem where my python program cant handle an error
and i dont know what else to do
is anyone free to help
Hey I've been trying t do this leetcode problem for a while and am still having some issues
do you guys have any ideas on hwo to do it?
so we need to choose m-1 rows that maximizes the minimum of the maximum of each column
I guess the brute force is simple but I don't nkow how to optimize the problem
#Create a code generator. This can that take text as input, replaces
#each letter with another letter, and outputs the โencodedโ message.
string = input("What message would you like to be encoded? ")
key = input("Key: ")
#/.string = "abcdefg" # Used for testing
#/.key = 2
enc = ""
for i in range(len(string)):
enc += chr(ord(string[i])+key)
print("The encrypted message is: ", enc)
Anyone able to figure out why my code doesn't work? At the moment I know it has something to do with how I'm shifting the characters. This line:
enc += chr(ord(string[i])+key)
Dynamic programming algorithms improve time efficiency by efficient sorting algorithms.
is it true?
no
dynamic programming algos improve efficiency by maintaining a table and looking up answers rather than re-computing subproblems
Are you getting an error message?
Yes
The error tells me there's a problem with me using the utf-8
Nvm, I found something similar to the way I want to do it and I'll be looking at it before I ask more questions
another view: I get to remove one row, which one should I remove?
am I dumb or isn't that solution super greedy?
you want to maximize the minimum
to increase this you need to remove some existing minimum
so you can just remove some row that contains the minimum
why is picking any such row ok? because if there are many rows with a minimum you're screwed anyway
so remove one row containing a minimum, compute the result, done
yeah, I think that should work
i've been drawing pictures on trees for hrs, i feel like there is something wrong w a problem statement in the book
give the statement then
so i made a B-tree
and i've been drawing what each of the sets looks like
not seeing how a path can split the S' set given that by definition is it every key smaller than key k
they're saying the S' set is the set of trees with keys smaller than k
so how could the path split that further when it is sort of already upper bounding it
let me give an example B-tree
i drew another tree with more keys, since nodes with 3 keys will have 4 pointers
and give the range that is specified by each key of a tree
where is the problem in the book?
the only conclusion i could draw is that you follow different pointers leaving the node on the path to the children while each key k is less than the search k (terrible way of explaining it)
intro to algorithms 3rd edition
do b-trees require latches?
also stuck on another one if n e 1 is bored
r
Enough to pass an interview or final exam? Probably
well, in universities DSA is usually a semester or two (source: googled syllabus dsa course), so a month seems doable if you can go through it at several times the rate.
hi
i have a list removable
removable = [3,1,0]
I have to remove those indexes characters from a string to create a new string
upto certain range
how to do it
c=removable[i]
temp=temp[:c]+temp[c+1:]
Hii, You yeah You
Can u fix my code
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
def height(root):
if root==None:
return 0
return max((1+height(root.left)), (1+height(root.right)))
class Solution:
def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
h = height(root)
lis = []
def fun(root, level, q):
if root == None:
return 0
if level == 1:
lis.append(root.val)
elif level != 1 and q == True:
fun(root.left, level-1, True)
fun(root.right, level-1, True)
elif level != 1 and q == False:
fun(root.right, level-1, False)
fun(root.left, level-1, False)
for i in range(1, h+1):
if i%2==0:
fun(root, i, False)
else:
fun(root, i, True)
return lis
Output:= [[3],[20,9],[15,7]]
myoutput = TypeError: [3, 20, 9, 15, 7] is not valid value for the expected return type list<list<integer>>
Thanks!
question:=https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/```
well, it looks like you want each level of the tree to be in its own list
so you can't just add them all to the same list, you need to make a new list at each height
like height list = [] * height
[] * height is a fun way to spell []
It looks like you're correctly doing the zigzag level traversal at each iteration of your for loop. Why not create a new list, say L, append lis to that, then 'reset' lis with each iteration ?
if a method uses a method that is O(n) then is that method also of the same time complexity?
well, of at least that time complexity
if it calls it O(n) times, that's O(n^2) complexity already
oof i'm finally at NP-completeness, may god have mercy on my soul
so all this mess about encoding problem instances as languages / in binary certificates is just to formalize the P and NP classes yes?
does one ever actually have to pass binary certificates as arguments to their algo or this this just how to build a formal problem instance into a language?
from the book:
a verification algorithm a two-argument algorithm A where one input is an ordinary string x and the other is a binary string y called a certificate. a two argument algorithm A verifies an input string x if there exists a certificate y such that A(x, y) = 1
the language verified by a verification algorithm A is
L = {x โ {0, 1}* : there exists y โ {0, 1}* such that A(x, y) = 1}
this is really cool:
https://en.wikipedia.org/wiki/Deterministic_algorithm#Disadvantages_of_determinism
you use a hardware random number generator to get a randomized input seed to pre-program https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator
A cryptographically secure pseudorandom number generator (CSPRNG) or cryptographic pseudorandom number generator (CPRNG) is a pseudorandom number generator (PRNG) with properties that make it suitable for use in cryptography. It is also loosely known as a cryptographic random number generator (CRNG) (see Random number generation ยง "True" vs. pse...
Can anyone here help me how to start with data structures in python??
Like any books? To learn DSA in python
the main book of the field is language-agnostic. it contains pseudocode
Sorry could you please simplify it?? I didn't get you
the main book that is used to teach algorithms does not use a specific language, and there is good reason for that
Oh ok
if you want online courses that are python-specific, use EdX
maybe
although the python courses i took on there were about dictionaries and the like, nothing about real DS or algos
this is the primary book for DS/algo
Thanka a lot sir
these are more for the sake of formality
the study of NP hard problems boils down to being able to show things like problem A is easier than problem B
this is true if I can translate a problem of type A into type B
because if I can solve B I can by extension solve A
that's the basic idea, but there are loads of details to make it formal
viable as in will eventually get the right answer, sure
that eventually might be an eternity away though
Is my idea for a non deterministic algo accurate or is that not a typical paradigm one might use
I thought non deterministic was just not returning the exact same answer every time is runs. Iโm not talking about approximation algos, unless they are synonymous
Is non a non deterministic algorithm synonymous with an approximation algorithm?
Wikipedia didnโt seem to indicate that
it's not synonymous
they're surely related somehow, i would presume
but your scheme of keeping the minimum makes it some kind of approximation scheme
what might a typical non-deterministic paradigm look like? i'm happy if you just provide a link to a known algo
there is an interesting one, I just need to remember which one it is...
ok ok. my mind just went the approximation route apparently after reading the chapter. book didn't discuss deterministic vs non-deterministic, unless i missed it. i'll check the index
I think it's this, but the wiki is probably to hard to read
this is an algorithm that gets the correct answer with a not too low probability
so if you repeat it enough times you can be increasingly certain about the correctness of your answer
I tool this course years back, the only reason I found what I was thinking of http://www.cse.chalmers.se/edu/year/2018/course/TDA251/
say I have a class A whose objects each hold unique identifiers x y and z, what would be the right data structure to be able to get the objects from either one of these identifiers? a dict would work if they only had a single identifier but not with multiple, and a list would just be inefficient. is there something else?
I'm assuming you mean you should be able to get A from one of x, y, z?
you could use a dict and map all three to A
it won't be too wasteful since you're not storing copies, just references
With Reinforced Supervised Learning Using Machine Learning On A Closed System With Say Python, Is It Possible To Link My Library From Google Books And Choose Specific Books By Sequence For The AI To Read And Store It In Memory To Go Alongside Training Data For Such A System Later.
hey guys, is sorting an array then doing the rest of the algorithm is still a brute force or brute force doesn't include sorting ?
brute force is a very generic term
you could use it to describe a lot of things
when I hear brute force I think "try basically all relevant possibilities"
is this a brute force ?
the last part looks greedy, but it also looks weird
how can I make it a brute force then
what's the problem statement?
wouldn't budget += i.revenue - i.spend be the sensible thing?
spend could be negative ig
I don't think it can be negative
oh it's just greedy then right. establish as many as you can
How can I make it a brute force
yes, starting with the cheapest, if it would make profit overall, do it
sorting works because then you don't have to care about what project you go to next
why would you want to make something brute force?
ah yeah, it's not guaranteed to be profitable (smh being realistic?)
it is required
I guess brute force would just be trying each permutation of the investments to do in order
Yea
if so you just take all you can, starting from the smallest
oh so you can't lose money
no
yeah, it makes the problem pretty boring
that would just be an if statement though right? if it's negative profit, don't do it
yes
But I need to check back if I got a project revenue
nice, let's turn our sorting + O(n) solution to an O(n!) one ๐
isn't it n+1 cause you need to loop lol
ฮฉ(n!) then
btw is this O(n^2) ?
yeah, because the bubble sort
you're sorting in O(nยฒ) yes
the rest of the code is O(n)
idk why you would want to do bubble sort though
is it possible to do it with out sorting ?
rather than the built-in sort, or a more efficient sorting algo
Oh I see
that I can do
But my problem is
I have to write 2 algorithms
one is brute force
and the other one is any other algorithm with less O( )
and when I got this algorithm I dont know if it is a brute force or not ๐
this one
I don't think you can solve efficiently without sorting, I can construct cases where you need to do things in sorted order to be optimal
no, what you have (if we ignore the sorting being written in a slow way) is a very good solution
I can change the sorting algorithm
as said, I don't think you can do it without considering the sorted order somehow, and your second part is O(n) and cheaper than sorting
and it is greedy algorithm right ?
the very dumb solution that @agile sundial mentioned of considering all permutations of all subsets will eventually consider the sorted version of the thing you need
just insanely inefficiently
it is, after sorting you greedily pick things if you can
are you sure you're solving the right problem there?
because there's no obvious "brute force" version of this one, but if I tweak the parameters a bit it looks like an O(2^n) problem that might have an efficient dynamic programming solution
but it's brute force!
brute factorial
yea
sorting+greedy is the best you can do
so for the 2 algorithm the brute force one is the one @agile sundial mentioned, and mine is a greedy algorithm yea ?
well, asking for a brute force one here is just weird
that's why I'm wondering if the problem is specified wrong
there is no obvious brute force algo here
I guess our tutor hates us then
I think any brute force algo will use basically the second loop you have
because you need to check if the investment is valid
I was thinking of the case where you have n investments that you have to make simultaneously and you have to optimize your return in one investment cycle. The fact that you can get the return from investment A before you have to invest in B is what makes sorting+greedy work here
but that does seem to be what the poorly written explanation is saying so ๐คทโโ๏ธ
the second example shows that's the case
yeah I was just having a hard time reading it
still weird to ask for a brute force algorithm
it is
in a bunch of cases there is some obvious but inefficient brute force, this is not one of these cases