#algos-and-data-structs

1 messages · Page 2 of 1

fiery cosmos
#

oh so for this example, the left subtree has at most 2n/3 nodes

#

floor(22/3) = 7

haughty mountain
#

ignoring the -1s

2^d/(2^d + 2^(d-1)) = 2/3
#

but yeah

fiery cosmos
#

where did your expressions come from

#

2^d is the number of nodes at a specific level of height d?

#

oh no that can't be right looking at your picture

#

oh it could be the max nodes at said height

#

2*2*2 = 8 nodes max at height 3

#

ok so i've sort of just derived where your top expression comes from, what about the 2 on the bottom. i notice one is also equal to the max nodes of a given height

haughty mountain
#

from here this

#

my fraction is roughly how much is in the left subtree

#

I guess the exact thing is

(left subtree size)/(full tree size)
(2^d - 1)/(2^d - 1 + 2^(d-1) - 1 + 1)
(2^d - 1)/(2^d + 2^(d-1) - 1)

for simplicity let x = 2^(d-1)

(2x - 1)/(3x - 1)
```which is < 2/3
#

(when x >= 1)

fiery cosmos
#

they claim the total cost to build a max heap is:

#

i'm wondering how to read this, is it that expression in the ceiling * O(h)?

soft hawk
#

Guys I got coding assessment from Amazon, how can I become better at data structure and algorithms

haughty mountain
#

I'm assuming it's sum(number of elements at level h * O(h))

#

i.e. the cost of inserting elements one at a time, each time the cost is O(current tree height)

#

actually, maybe my interpretation is wrong, idk pithink

fiery cosmos
#

if i send the entire expression, you'll be able to tell what it means

#

maybe i'll do that tomorrow i don't want to type it all out and i haven't gotten much work done today

#

do you have a copy of CLRS?

#

3rd ed

haughty mountain
#

I have a pdf

fiery cosmos
#

ok that was from pg 159, the time required by max heapify

#

section 6.4

#

if you care to look. if not no worries

haughty mountain
#

it is prefaced by

#

The time required by MAX-HEAPIFY whencalled on anode ofheight h is O(h) and so wecan express the total cost of BUILD-MAX-HEAP as being bounded from above by

#

so yes, it is close to what I said

fiery cosmos
#

hmm.. can you help me conceptually with what it means to sum terms * big O of something

haughty mountain
#

for all levels (heights) in the tree, all nodes at that height costs O(h)

haughty mountain
#

you know the definition of O(f(n)), basically the thing is bounded by a constant times f(n)

#

you can see that they don't care to much in their derivation though, all we care about is O if the whole expression

#

so that constant doesn't matter, the outer O will just absorb that

#

the n is independent of h, so it can be factored out of the sum

#

ok, before that, we can replace ceil(n/2^(h+1)) with n/2^h

#

it will not be more than a factor 2 larger, and constant factors will be absorbed by the O

fiery cosmos
#

one sec

haughty mountain
#

then as I said you can factor the n out, since it's no longer in the ceil

#

and you basically have their thing on the right

fiery cosmos
#

what is O(0)

#

like i'm looking at how to evaluate it when h=0

haughty mountain
#

I wouldn't look at individual terms like that, it doesn't make much sense

#

evaluating their thing on the right makes more sense

fiery cosmos
#

but like if O was some other function, to evaluate it you'd go through each h=0,h=1,...floor(lgn), where for each h you evaluate the ceiling and multiply that term times O(h), to get each term

haughty mountain
#

what theynare saying is just that the work is proportiinal to the height

#

O is not a function

fiery cosmos
#

hmm

#

i just think while i'm considering this i may as well know what precisely its describing so in the future i can describe said thing with this precision

haughty mountain
#

but we want the O of the whole expression anyway

#

which will absorb that constant

fiery cosmos
#

i can't see what is meant mathematically without considering O(h) as a function or like, some term

#

otherwise what would you multiply the expression in the ceiling with

#

this means the expression in the ceiling * O(h) correct?

#

expression in ceiling * O(h) + new expression in ceiling * O(h+1) ... +

#

is this incorrect

haughty mountain
#

this is the kind of stuff that can feel sloppy when using O notation, but if you go through the details it checks out

fiery cosmos
#

or maybe expression in ceiling * O(f(h)) + h+1 in ceiling*O(f(h+1))???

#

am i reading it correctly? i'll reread what you wrote above

#

i don't know if its sloppy i just don't know what its saying

haughty mountain
#

formally you would write things in terms of the definition of O

#

O(h) basically is a function c*h

#

for some constant c

#

(and the usual details of the definition)

#

does the sum make more sense if you replace O(h) with c*h?

fiery cosmos
#

something like f(x) being upper bounded (O of) means that there is some other anonymous function, g(x), for which g(x) is always greater for all n > n0

haughty mountain
fiery cosmos
#

idk i still haven't gotten the answer if the expression is interpreted as multiply by the term on the right (the ch term)

haughty mountain
#

yes, it's just a multiplication

fiery cosmos
#

ok yeah then i think it would be fairly easy to evaluate. to look at an algorithm and then spit this out, not so much

haughty mountain
#

fwiw we are kinda living in the big O world here, where these constant factors like c don't really matter

fiery cosmos
#

i understand i just want to know what is meant by the notation when looking at something like that

haughty mountain
fiery cosmos
#

like i want to understand what the person who wrote it meant exactly, as that is the intention of the notation, to convey a precise meaning

haughty mountain
#

summed over all the valid heights

haughty mountain
fiery cosmos
#

ok

haughty mountain
#

the work required for nodes at height h is O(h)

#

and then the simplifications from there is in this big O world where constants (and lower order terms don't matter)

#

they simplify to the sum on the right, which is something they know how to evaluate

#

they mention some result they have calculated before, but the sum of h/2^h from 0 to ∞ has a pretty cute proof

1/2 + 2/4 + 3/8 + 4/16 + ... =

1/2 + 1/4 + 1/8 + 1/16 + ...
    + 1/4 + 1/8 + 1/16 + ...
          + 1/8 + 1/16 + ...
                + 1/16 + ...
```sum each row and you have

1 + 1/2 + 1/4 + 1/8 + ... = 2

#

it's the geometric sum over 1/2^n all the way down 🐢

fiery cosmos
#

it's turtles all the way down huh

#

believe it or not i've read some of the brief history of time 😜

#

sorry *a brief history of time

wise oar
#

is there a significant difference in a Queue object and just...a list where i append and pop(0)

agile sundial
#

yes, the Queue will be substantially faster

fiery cosmos
#

appending adds to the end, pop(0) would pop from the head of the list no?

agile sundial
#

right

fiery cosmos
#

got it

#

range runs from 0 by default?

agile sundial
#

right

fiery cosmos
#

ty

#

seems like in this quicksort implementation they actually want one of their array indices equal to negative 1, it doesn't look like it'd throw an error as they never call array[i] anywhere until its equal to at least 0, does that sound right

#

there even is a picture where i is before the first element of the array

#

sorry they want a variable that'll be used to look at indices into the array the begin at -1*

agile sundial
#

that sounds reasonable

fiery cosmos
#

i'm debugging on paper so i can't just run and see

#

thanks

#

debugging is wrong word. i'm running it on paper / in my mind

fiery cosmos
#

well it worked in theory. it sorted my array into everything lower than the pivot element and higher and then moved the pivot into the middle

#

i think i lucked out on picking an array where that could work cleanly, in practice i'll need the recursion

#

this is a great exercise if you want to really get a feel for how the thing works

dawn cobalt
#

hello

rich meteor
#

Hey I'm having a little bit of trouble, I'm writing an algorithm to return the intersections of two lists of intervals as such

def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
        if not firstList or not secondList:
            return None
        res = []
        for i in range(len(firstList)):
            if secondList[i][1] < firstList[i][0]:
                res.append(secondList[i])
                continue
            elif secondList[i][0] > firstList[i][1]:
                res.append(firstList[i])
                continue
            else:
                try:
                    if firstList[i][1] == secondList[i + 1][0]:
                        res.append([firstList[i][1],firstList[i][1]])
                    elif secondList[i][1] == firstList[i + 1][0]:
                        res.append([secondList[i][1], secondList[i][1]])
                except IndexError:
                    pass
                res.append([max(firstList[i][0], secondList[i][0]), min(firstList[i][1], secondList[i][1])])
        return res
#

Expected:

Input:
[[0,2],[5,10],[13,23],[24,25]]
[[1,5],[8,12],[15,24],[25,26]]
Output:
[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
#

However from the previous input I am getting this output:

[[5,5],[1,2],[8,10],[24,24],[15,23],[25,25]]
#

For reference, this is leetcode 986 interval list intersections. Any suggestions?

haughty mountain
woeful hinge
#

guys having difficulty in understanding leet code questions ,i do know the basics of python but not able to solve easy questions as well ,any suggestions guy

agile sundial
#

are you familiar with any data structures or algorithms?

woeful hinge
#

i am getting introduced to it so what i thought was ,if a watch a topic like arrays i feel if we implement it right away will get more knowledge towards it ,this is what i thought correctly me if i am wrong

#

please guide me whats the correct approach towards dsa and leetcode

#

as well

woeful hinge
#

if you could give me any advice on how do i improve dsa and leetcode problems it would be very helpful for me

agile sundial
#

don't just bash your head against a problem for hours. after some time, maybe 30mins to an hour, try looking at hints/the solution and try to understand it

woeful hinge
#

yes sir i have done that as well ,but the problem i am getting over there is that i do understand correctly as soon as i see the solution but again if i see a new problem its the same story again i dont understand what exactly they want from me from the question

agile sundial
#

do you have a specific problem in mind?

woeful hinge
#

no sir i dont know if i could articulate properly like i will give you an example sir i was solving an boot camp question related to converting Celsius to Fahrenheit it is pretty straight forward answer i am able to get the answer is different intelij platforms and all but when there is a test cases

#

i fail sir

#

i fail to recognize what excatly do they want

#

from me which method do they want

agile sundial
#

the method you use doesn't really matter, all that matters is the right answer

#

it might just be that you don't have enough experience interpreting problem statements

woeful hinge
dry portal
#

Hi, is there a way to create an array of matrices efficiently in numpy?
Suppose I have a 1D array a.
From each element, I want to create a matrix, say M_i=[[a[i], a[i]+1], [2, 3]].
Can I vectorise this operation? That is, can I create all matrices M_i with a single command?

safe spade
#

Hey guys i need some help

array 1 : ['itachi', 'naruto', 'kakashi',]

array 2: [['requestrr-itachi', 'false'], ['mylar3-itachi', 'false'], ['pyload-itachi', 'false'], ['pyload-naruto', 'true'], ['pydio-kakashi', 'true'], ['mariadb-kakashi', 'true']]

result : [[itachi,['requestrr-itachi', 'false'], ['mylar3-itachi', 'false'], ['pyload-itachi', 'false']], [naruto,['pyload-naruto', 'true']], [kakashi,['pydio-kakashi', 'true'], ['mariadb-kakashi', 'true']]]

#

using array 1 and array 2 i want result array can someone help me with it ? thank you 🙂

fiery cosmos
#

What's the point of adding a C here. If it's gonna surpass for a certain n. Then it will anyways. Why we include a constant unnecessarily?

#

Okay looks like, it's to include the same degree of polynomials too

agile sundial
#

right

fiery cosmos
#

hi all,
can you help me to understand this proof. i tried by plugging in cn^2 for all n in the expression but that doesn't work out

#

i sort of see what's going on

haughty mountain
#

the lower bound calculation seems wrong

T(n) = T(n-1) + Θ(n)
    >= c1 (n-1)² + c1 n    (use hypothesis)
     = c1 (n² - 2n + 1) + c1 n
     = c1 (n² - n + 1)
```which is not `>= c1 n²`
fiery cosmos
#

may i ask what you did here:

     = c2 (n² - n + 1)```
haughty mountain
#

factor out the common c2, then -2n + n = -n

#

so, middle step c2(n² - 2n + 1 + n) if that helps

fiery cosmos
#

thank you. so i can just pull out the c2 from the c2(n) term and add it to the c2(n^2...) term without changing the coefficient

haughty mountain
fiery cosmos
#

it is already "factored out" from the first term so to speak

#

ok ty

haughty mountain
#

the lower bound just looks wrong

fiery cosmos
#

when i get done algos i'm going to have to repay you somehow

#

i didn't understand their statement that the pivot element of quicksort is never again selected in any subsequent call to quicksort

#

seems like it easily could be if it happens to fall at the end of one of the subarrays

#

oh nvm, maybe the q partitioning changes that

silver crystal
fiery cosmos
#

🤷‍♂️ these are probably student solutions. was found online

#

here:

#

what is meant by this line:
"counting sort assumes that each of n input elements is an integer in the range 0 to k, for some integer k. When k = O(n), the sort runs in theta(n) time."

silver crystal
# fiery cosmos

T(n) = T(n-1) + th(n)...[keep expanding] = th( 1 + ... + n) = th( n(n+1)/2 ) = th( n^2 + n ) [coefficients can be ignored] = th( n^2) [polynomial growth is higher than linear growth]

agile sundial
fiery cosmos
silver crystal
fiery cosmos
#

still don't understand what they mean by an integer being O(n)

agile sundial
#

think about what O() means

fiery cosmos
#

an integer n in the set O means that there is some anonymous function f(n) such that n can be upper bounded by c(f(n)) for all n greater than n naught

haughty mountain
#

idk what you mean by anonymous function here

fiery cosmos
#

O( ) is the set of all functions that upper bound n for all n greater than n naught

haughty mountain
#

yes

#

also "an integer n" is weird

fiery cosmos
#

let it be an integer k, as in their statement

agile sundial
#

it just means it's bounded by n

haughty mountain
#

oh, they are just saying that if the biggest number isn't asymptotically greater than n, then the overall complexity of counting sort is O(n)

#

the complexity is really O(n + k)

#

but if k is not greater than n, then we can just say O(n)

fiery cosmos
#

if the biggest number in the input array isn't asymptotically greater than the length of the array?

#

idk what it means, maybe its not important

#

but thinking of an integer k as having O(n) is making my brain hurt

agile sundial
#

it's the same as any other function being O(n)

fiery cosmos
#

but its not a function

#

i mean i guess the set of all integers can be upper bounded by a linear function

agile sundial
fiery cosmos
#

an integer k is a function?

agile sundial
#

it's a 0 degree polynomial

fiery cosmos
#

like as in k^0?

agile sundial
#

kx^0

fiery cosmos
#

oh right bc k^0 = 1

#

not k

#

hmm.. interesting to think of integers in that way

#

i guess it just comes naturally to some

#

so in that case when might some integer k not be O(n)?

#

are all loops mathematically equivalent to summations?

#

like all loops of the form for i in range (1,x)

agile sundial
#

no, only if you're summing inside

fiery cosmos
#

but typically we are interested in getting the runtime by summing constant operations that happen inside loops, no?

agile sundial
#

oh, for the runtime you mean. then yes, you're summing the operations inside over the number of iterations

silver crystal
fiery cosmos
#

right right

agile sundial
fiery cosmos
#

but in their statement they say that each of the n input elements is an integer in the range 0...k, for some integer k

#

so we know its going to be linear / k is just some integer at the end of a range beginning at 0

#

well maybe not that its going to be linear

#

well actually yeah bc regardless of the input elements, an array grows linearly with each additional element

#

so it doesnt matter even if its in the form of like a=[1,10,100,1000]

agile sundial
#

well, that's not linear

fiery cosmos
#

the length of an array grows linearly with each additional element

agile sundial
#

sure, but the elements in it might not, which is what we actually care about

fiery cosmos
#

so counting sort runs in O(n+k)? which is equivalent to the size of the input array plus the largest integer in the array?

#

at first i struggled to see why that may be, but then, it must be dependent on the greatest integer bc if you have k of say, 100, then you need an array of length 100 to get the equivalent index.

#

makes sense i guess

agile sundial
#

exactly

fiery cosmos
#

hmmm ok so then radix sort becomes theta(d(n + k)), where d is the number of digits, and you use a stable sort such as counting sort with runtime of (n+k)

#

what is meant by a 64 bit version of windows?

#

i ask bc they are now discussing n b-bit integers

fiery cosmos
#

as in, the cpu?

#

nvm i see it

#

64-bit operating system, x64-based processor

fiery cosmos
#

how can bucketsort be faster than insertionsort if it requires multiple calls to insertionsort?

#

i could see it being possible if each call to insertionsort was runtime complexity of omega(n)

haughty mountain
#

tl;dr a sum of small squares is less than a big square

#

there is also the option to continue sorting by doing bucket sort again on the bucket contents, which ends up being something like radix sort

fiery cosmos
#

It looks like probability data if I’m not mistaken

#

Like the reason why an input array might be all of that decimal type and size?

tacit nova
#

tim sort is best overallpithink

fiery cosmos
#

Hmm haven’t covered that one yet

fiery cosmos
#

woah. quicksort is stupid fast

#

sorting 10k random numbers in 0.053s

#

which random command do i use for a random element of an array

#

random.choice(array)

#

having trouble understand the pseudocode, they call for i = RANDOM(p,r), where an array has indices array[p,r]. i don't know if they want to pick randomly between p and r or a random index of the array

#

pg 179 CLRS

agile sundial
#

that means get a number in that range

fiery cosmos
#

its just one of the elements of the array, i believe. i tried
i = random.choice(array)

#

code wasn't having it

agile sundial
#

no, it's looking for a random index

fiery cosmos
#

then which argument would i use for it

agile sundial
#

wdym?

fiery cosmos
#

i can't pass random.choice the indices, it takes zero arguments

agile sundial
#

their pseudocode is using a random index, but your code chooses a random element. i'm sure you can figure out how to make that work

#

they're only using the index to access the list anyways

agile sundial
fiery cosmos
#

alright i have office hrs now, i'll be back

bright ginkgo
#

anyone here familiar with DFS?

#

if anyone is familiar with DFS/BFS please send me a PM. thanks guys

half lotus
#

You should post here what your actual question is regarding DFS/BFS.

spare olive
#

Hey I need sanity check from someone else. For work not learning/fun - program has tons of graph theory going on, its basically calculating routes for estimating. Anyway using a Point class with x and y instead of just a tuple with floats for a vertex. But I have thought about making a edge class 5 times and can't decide if its stupid or a good idea.
edit: nm, its going to be useful for sure.

fiery cosmos
#

there appears to be a mistake here with an extra c in the second line

#

between the two expressions

haughty mountain
#

no, it's intentional

#

to get rid of the ceil

#

ceil(n/5) < n/5 + 1

fiery cosmos
#

hmm ok

#

so they make the expression slightly larger in keeping with the <= inequality

haughty mountain
#

yeah, they make it larger but gets rid of the annoying ceil

fiery cosmos
#

so, so far in this book they've been simply ignoring a lot of the floors and ceilings, when must you deal with them instead?

#

"under reasonable assumptions, the average time to search for an element in a hash table is O(1)."

#

🤯 🤯 🤯

agile sundial
#

that's why they're so useful

lament totem
#

There is a trade-off, which is that it often requires more memory than for example an array with the numbers tightly packed in consequent memory

agile sundial
#

not asymptotically more, though. and the search speedup is huge

fiery cosmos
#

when i run python and tell it to generate an array of 10k numbers, they are stored in one large continuous spot in memory. is this on/in the ram or the cache of the cpu

agile sundial
#

RAM

#

maybe tiny parts will be in cache at once

fiery cosmos
#

so hard drives must then be only for dedicated written storage

#

and retrieval

agile sundial
#

pretty much, yeah. a lot of things you can deal with in memory, but stuff like dbs are on disk

fiery cosmos
#

whats the upper limit on what my RAM could handle in terms of an array of like 1m, 10m, 100m numbers

agile sundial
#

depends how much you have

fiery cosmos
#

right but then there is the linearity aspect as well

#

in other words, a linked list has the advantage that its stored in discontinuous space in memory(ram)

#

if i'm understanding correctly. but i don't understand what that means exactly

onyx meteor
#

Good afternoon

agile sundial
haughty mountain
fiery cosmos
#

ok. sounds like there are subtleties not caught in my reading. thanks

haughty mountain
#

it's more a python thing

#

in many other languages the objects would be stored contiguously

agile sundial
#

in C, an array is literally just a block of memory

fiery cosmos
#

so inferring from what y'all are saying, i'm envisaging that RAM is addressed or indexed in some way

haughty mountain
agile sundial
haughty mountain
#

stuff is in ram, if you read from some location in ram the data at that location (and stuff nearby) is pulled into the cpu cache

fiery cosmos
#

so one giant array in C would be like pages in range(1,n)

agile sundial
#

you don't really have control over what the OS gives you

haughty mountain
#

ram nowadays is kinda complex

#

with virtual address space and whatnot

fiery cosmos
#

ok ok. i guess i'm getting off topic. the cool thing about my ram is that is has RGB. also off topic. back to hash tables

#

haha!

#

CPU: AMD Ryzen 5 5600X 6-Core Processor 3.70 GHz

#

i also have a pretty sweet GPU if i ever learn to do ML stuff

fiery cosmos
#

does python have an inbuilt linked list data structure?

agile sundial
#

kinda, collections.deque

fiery cosmos
#

would that be equivalent to single or doubly linked lists

agile sundial
#

doubly

fiery cosmos
#

so might one create a hash function/table and resolve collisions via deque?

agile sundial
#

one might. although, others might say that's cheating

fiery cosmos
#

ok. setting aside the question of cheating wrt programming, i've seen linked lists implemented in python as classes. why might one do this rather than, say, linking pythonic lists themselves? or is that what they are doing in essence

agile sundial
#

wdym linking pythonic lists

fiery cosmos
#

arrays. maybe in the class approach, that is what they do

#

lms

agile sundial
#

python lists aren't linked lists, though

fiery cosmos
#

right i'm saying why not build a linked list by using lists themselves? is it bc its inefficient? or it would break when you go and delete one?

#

like you could have each node be a list of length 3 with list[0] = previous node, list[1] = data, list[2] = next node

agile sundial
#

that could probably work

fiery cosmos
#

there must be some reason that's not the conventional approach

agile sundial
#

having names for them is easier, self.val, self.prev, self.next is better than data[0], data[1], data[2]

fiery cosmos
#

hmm right. i wonder how the time complexity operations would change

agile sundial
#

it's the same, list indexing is O(1), same as looking up a variable name

fiery cosmos
#

i'm not following. i know constant time operations are O(1), like eg. declaring a variable or something. O(n) operations are like, when have to search every item in an array, as an example. wdym list indexing

agile sundial
#

doing a_list[an_index]

fiery cosmos
#

oh ok

agile sundial
#

iiuc, all you've changed is instead of using 3 separate variables, you store them together in a list, right?

fiery cosmos
#

idk what i mean exactly, just when thinking of building a linked list, the first logical step is to take an existing data structure and find the way to link it. all theoretical at this point

#

i was just wondering why people don't take that approach, and as you've pointed out it could be as simple as having better naming conventions

zenith kernel
#

anyone know the time complexity of my code in pseudocode ? ```python
function g(n)
i = 1
x = 0
while i < n do
i *= 4
j = 1
while j < n do
x += j
j *= 2
k = 1
while k < n do
x += j
k += 8
return x

#

i think its O(n^2logn) but not sure

jolly mortar
#

isnt it n(log n)^2 pithink

zenith kernel
#

how?

jolly mortar
#

i loop is log n, j loop is log n, k loop is n

zenith kernel
#

or is that just the logn?

jolly mortar
#

n * ((logn)^2)

zenith kernel
jolly mortar
#

sure same thing

zenith kernel
#

oh ok thanks

haughty mountain
#

there is a fun structure where you have a prev array, a next array and a data array to implement a doubly linked list

#

so prev[i] is the node before i

#

next[i] is the node after

#

and data[i] is the value

opal oriole
#

You can def. do a struct with data, and then a list of 2 pointers / references (next, prev).

#

In Python you could do a list, because lists in Python can have different types for each element, but those are really annoying to deal with.

#

(You could also just not use classes and use lists for everything, it won't be fun)

opal oriole
#

(You want the node to be its own type)

#

(Python is more loose with typing, but I don't think most devs want to be that loose with typing (in a large project) (also entering LISP territory (everything a list)))

#

(Unless you really like LISP)

native oar
#

what would be the time complexity of this algorithm?

#

ArrayStack() basically being a stack data structure means push and pop operations are both O(1)

agile sundial
#

what do you think?

native oar
#

doesn't it depend on how many time the while loop runs? also most of cost will be due to find operations i assume

agile sundial
#

it doesn't really matter how many times the while loop runs

native oar
#

find runs in O(n) in the worst case since it needs to search through the entire raw text ?

agile sundial
#

yes

native oar
#

i am not sure, wouldn't you keep doing find("<") and find(">") until you exhaust all possibilities in terms of tags? So it is kinda dependent on the number of tags. Ngl i am a bit lost, could you help

agile sundial
#

you call find multiple times, but you adjust the start location for each call. this means subsequent calls don't search previously searched parts of the string, right?

native oar
#

yh

#

the search area decreases

agile sundial
#

so no matter how many times you call find, you only look at each character once

native oar
#

ahhh i just realised so we will not revisit characters due the adjustment of the position to start from for the find() method, that means overall it runs in O(n) (as we look at each character once)?

agile sundial
#

technically you do revisit chars with tag = raw[j + 1: k], but this is also linear. so you visit chars at most twice

native oar
#

ah ok thank you for the help

mint arrow
#

Can someone please explain the colors in red black tree I don’t get it, it says black is null but I see it at the root?

quasi basalt
#

I'm doing

Y=train['target']
X=train.drop(columns=['target'])```
when len(train) is 200.000, but after this len(Y) is somehow 200.001. What could be the reason?
primal pivot
#

Hey anyone knows some good resources or YouTube channel to learn data structures and algorithms? I can't find any which must be good

plain fiber
#

how does this function have O(sqrt(n)) Time complexity?

jolly mortar
plain fiber
#

or t(t+1)/2 = n

#

i dont understand this method

dark aurora
#

If anyone has a solution to this task in python that passes please send it

#

my code passes all tests except one but I really don't know what to improve

haughty mountain
# plain fiber i dont understand this method

in the loop s goes

s = 1
s = 1 + 2 = 3
s = 1 + 2 + 3 = 6
...
```in general the kth iteration is

s = 1 + 2 + ... + (k-1) + k = k*(k+1)/2
```so how many iterations are there until s > n and the loop terminates?

#

i.e. what's the smallest k such that

k*(k+1)/2 > n
#

you could solve the quadratic explicitly, but the solution is basically a square root of n

plain fiber
#

thanks

fiery cosmos
#

Hey all, page 393 I’m reading about the longest common subsequence of two DNA sequences. Under step 2: a recursive solution, I’m not understanding when they say if x_m = y_n if they are meaning the bases are the same or the sequences are of the same length.

#

Pg 393 CLRS 3rd edition section 15.4

#

I also do not understand their notation for the longest common subsequence of two sequences Xi and Yj to be c[i,j], because as they pointed out, the LCS may be anywhere along the sequence and omit any bases from either, thus you cannot capture it in an array type fashion

haughty mountain
#

it's not saying anything about what the subsequence is, just what its length is

fiery cosmos
#

Ok thank you for that any idea about the if x_m = y_n statement?

#

Looks like in theorem 15.1 they must be meaning if the elements are identical

#

Not the length

haughty mountain
fiery cosmos
#

Correct

haughty mountain
#

and capital X, Y are the prefixes I think

fiery cosmos
#

But, when they speak of Z_k-1, then they’re talking about a subsequence

#

I don’t understand the logic following the and in statement 1

haughty mountain
#

Z, where?

fiery cosmos
#

Statement 1 theorem 15.1

#

You don’t have page numbers in the pdf correct

haughty mountain
#

I have page numbers

fiery cosmos
#

392

#

Z = LCS

#

of X and Y

haughty mountain
#

ah yeah, Z is an LCS there

fiery cosmos
#

I don’t understand the logic following the ‘and’ in statement 1

#

I understand statement 2

#

If the final elements are not equivalent, then the LCS must be of the subsequence X_m-1 and Y

haughty mountain
#

first statement:
if x_m == y_n; then the last element is part of the LCS, and the LCS without the last element is the LCS of the sequences without the last element

#

second:
if x_m != y_n then the last char of X not matching the last char of Z means the LCS must be that of X without the last element and Y

#

third is the same but with X and Y swapped

#

basically, if the chars mismatch and the last char of X doesn't match the last char of the LCS, then you can ignore the last char of X and the LCS is the same

fiery cosmos
#

Yeah I think I’m getting it now, thank you

fiery cosmos
#

What’s the purpose of dummy keys in optimal binary search trees

haughty mountain
#

if the value you're looking for isn't in the tree you end up in a dummy node

fiery cosmos
#

Is that just to make a returning NIL feature

haughty mountain
#

if dummy node then element doesn't exist

#

I think in practice that dummy might be None in python or null/null pointer in other langs

ashen niche
#

can you expand on this? I'm a bit confused why I would need ternary
I've been thinking of something like "divide by 2" every time it continues improving, otherwise multiply by 2
I just didn't know how to prevent them from bouncing back and forth if I'm going towards the minimum

for example f(10) = 10, check f(5) = 15, check f(2.5) = 20, check f(1.25) = 19
that means the answer is between 1.25 and 2.5 so these are my new ranges

ashen niche
ashen niche
#

so I am probably going to do that
if score better than prev run => continue with operation
if score worse than prev run => do opposite operation but not as strong

lean junco
#

how fast did you get how to solve?

runic tinsel
#

2hard4me

vital parcel
#

You are given two non-negative integers A and B. Let the function Sum(A) be the sum of digits of the number A. You can perform any number of moves (possibly zeros).

In one move you can increase the value of A by 1.
Find the minimum number of moves you have to perform to make sum(A) <= B.

Constraints:

1 <= A <= 10^12

1 <= B <= 108
Sample Test Cases:

  1. A = 1, B = 1 Output = 0
  2. A = 9, B = 2 Output = 1
  3. A = 555, B = 10 Output = 45
    *inputs are given in string.

How to solve this problem in python?

Here is the code I have tried:

def countmoves(A,B): int_A, int_B = int(A), int(B) count = 0 while True: digits = [int(str(int_A)[i]) for i in range(len(str(int_A)))] s_m = sum(digits) if s_m <= int_B: return count else: int_A += 1 count += 1
But my code is getting Timed Out. How can I optimize it?

runic tinsel
#

do bigger steps

#

suppose the number is 783910
look at the very end 783910
you can't decrease this, so look at 2 digits 783910
you can drop this to 0, make a 90 step and you get 784000, now start over

vital parcel
runic tinsel
#

it doesn't feel optimally optimal, but that should be fast enough anyway, and it's easy to understand

vital parcel
runic tinsel
#

well it should be easier to do it in one step then

#

assume you zero out the suffix and it increases the rest by one

#

suppose you need to decrease the sum by 12
783910 we assume this will decrease it by −1
783910 that's 0, −1+1
783910 10 − 1 = 9
783910 13 − 1 = 12, that should do it

#

you just add this up, subtract 1 and see if it will be enough

#

at that point you checked 4 digits, so you subtract 10**4 − 3910

balmy oak
#

Hello there, if I need to uniquely identify a dictionary (for example by computing a hash) would using something like

hash(tuple(sorted(dict_.items())))

be a good solution?

runic tinsel
#

why not str

lean junco
# runic tinsel 2hard4me
class Solution {
public:
    int maximumGroups(vector<int>& grades) {
        return -1+ ((1+ pow(1+4*(2*grades.size()),0.5))/2);
    }
};```
one liner medium
runic tinsel
#

that ignores the actual grades

#

does that really hold?

#

oh

#

kinda does, but not enough i think

#

hm

balmy oak
runic tinsel
#

yes, hash(str(dict_)) is just simpler

balmy oak
#

It wouldn't be sorted then

runic tinsel
#

oh true

balmy oak
#

In my case order shouldn't matter

#

Dicts preserve insertion order, i think that holds for the str repr but i should check

runic tinsel
#
hash(tuple(str(x) for x in sorted(dict_.items())))
balmy oak
#
>>> x = {1: 1, 2: 2}
>>> y = {2: 2, 1: 1}
>>> str(x)
'{1: 1, 2: 2}'
>>> str(y)
'{2: 2, 1: 1}'
balmy oak
#

This is just longer

runic tinsel
#

yes, but your values can be unhashable this way

balmy oak
#

That's not required, I assume all values are hashable here

runic tinsel
#

ok then yeah the original should work

dark aurora
haughty mountain
dark aurora
#

yeah, now it passed, thanks

haughty mountain
dark aurora
mortal hare
#

Can anyone help in suggesting some good course to learn data structures and algorithms..it will be really helpful..

haughty mountain
#

check channel pins

grizzled kindle
#

and use neetcode on youtube to explain solutions

lament totem
#

Yeah, hackerrank, leetcode, codewars are all great options to practice with that imo

fiery cosmos
#

hi all,
in reading CLRS, page 288, I'm not seeing where the expression (c + d)n + c comes from

#

its the proof that inorder tree walk will take O(n) time

#

d is some constant that represents the time taken for the body of the script independent of the recursive calls

#

c is i suppose the usual constant we'd use when trying to upper bound something in the form cn when providing an O(n) proof

solid moon
#

if I have an html code with a list ( <ol><li>... ) Whats the best way to convert it into a json?

vocal gorge
#

beautifulsoup, perhaps

solid moon
#

thanks

mortal hare
fiery cosmos
#

check pins, there are free MIT lectures on youtube

#

Introduction to algorithms 3rd edition is pretty much the gold standard, its sold over 1m copies

#

by cormen, leiserson, rivest, stine. colloquially CLRS

#

very technical for beginner i'm finding

grizzled kindle
#

i didn't begin learning algos by learning how they work, i just did questions and solved coding problems

#

once you know the basics of any programming languages (functions, conditionals, loops, data types, lists ... etc), your ready to learn algos and datastructures

#

why cant for the first one return [50, 50,51,51]?

#

so try and make as little chucks as possible whilst making it between min and maxx?

vocal gorge
#

Hmm, what comes to my mind is... Take n=math.ceil(number/max). Then you need to divide number into n equal-ish chunks, which can be done like

mod = number % n
results = []
for i in range(n):
    results.append(number//n + (1 if i<mod else 0))

This produces splits like in your example.
Now, does that always ensure that MIN is also guaranteed... well, only if number//ceil(number/max) >= min, which, hmm, I'm not sure about.

#

for your MIN and MAX, it seems to work fine

#
def split_to_chunks(number, min, max):
    n = (number+max-2)//(max-1)  # that's like ceil(number/(max-1))
    mod = number % n
    results = []
    for i in range(n):
        x = number//n + (1 if i < mod else 0)
        assert min <= x < max, x
        results.append(x)
    return results


MIN = 10
MAX = 100
[split_to_chunks(i, MIN, MAX) for i in range(MIN,1000)] # no errors
fiery cosmos
#

should i practice implementing binary search trees, red-black trees before learning about B-trees?

#

the implementation of trees in python is straightforward using classes for tree and for node, although i haven't done it myself yet

#

i'm also not seeing the similarity between the pseudocode and implementations of trees in python, as CLRS has algorithms for every operation, and in the node class you just have attributes to do things like delete, insert, etc

haughty mountain
fiery cosmos
#

would y'all recommend creating my own binary tree type from classes from scratch so that I can implement their algorithms for searching, inorder tree walk, insertion, etc? i feel like this is a waste of time when I could just use binarytree module however, the book does build up to more advanced structures such as red black and 2-3-4 trees which idk if i can implement with python's binarytree mod

haughty mountain
#

a binary tree type is pretty trivial though

#

a tree node is just

class Node:
  left_child: Node
  right_child: Node
  value: ValueType
#

and for a general tree, replace left/right child with a list of children

fiery cosmos
#

this is what I have so far:

#
class node:
    def __init__(self, key):
        self.left = None
        self.right = None
        self.key = key
#

i need to learn how to build it into a tree and use CLRS algorithms on it. i mean i don't have any assignment or anything like that but the chapters seems to be building like first we learned trees, then bsts, red-black, now i'm on b trees

#

@haughty mountain could you walk me through it sometime if you have the time

#

brb

agile sundial
#

sure

fiery cosmos
#
class node:
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None

class tree:
    def __init__(self):
        self.root = None

    def node_addition(self, node, key):
        if node == None:
            self.root = node(key)
        if key < node.key:
            if node.left == None:
                node.left = node(key)
            else:
                self.node_addition(node.left, key)

        else:
            if node.right == None:
                node.right = node(key)
            else:
                self.node_addition(node.right, key)

    def inorderPrint(self, node):
        while node is not None:
            self.inorderPrint(node.left)
            print(node.key)
            self.inorderPrint(node.right)

test_tree = tree()
test_tree.node_addition(test_tree.root, 10)


print(test_tree)
#

hey all i'm getting an error:

#
'NoneType' object is not callable```
#

lines 13 and 33

agile sundial
#

this is another reason why you name classes in PascalCase, fwiw

#

your node class is overwritten by the node variable

fiery cosmos
#

like pascalCase

agile sundial
#

PascalCase

fiery cosmos
#

ok ty

fiery cosmos
#

<__main__.Tree object at 0x00000274255AEAD0>

plain fiber
#

T(n) = 8T(n/2) + n^3/logn why is this not solvable by master theorem??

shut breach
#

what is a master theorem?

agile sundial
haughty mountain
#

there are variants that can handle it, but the typical one is only for when it's polynomial

fiery cosmos
fiery cosmos
haughty mountain
#

I guess you could ignore the division by log n and you would get some upper bound, but a worse upper bound than necessary

#

the wiki article includes this case, the expansion of case 2: 2a, 2b and 2c

#

in the example above you're in case 2d
T(n) = 8T(n/2) + n^3/logn
which ends up being Θ(n^3 log log n)

fiery cosmos
#

when building a binary tree would you give the root attribute to your Node class or Tree class

agile sundial
#

Tree

fiery cosmos
#

i'm really struggling with how to write a tree such that the CLRS algorithms will be compatible

#

there are plenty of tree implementations online but i don't think they're compatible with the CLRS algorithms which have their own algo for every function

sage plank
#

I'm learning about data structures and memory location , is there any meaning behind 200 towards 244 for memory locations or are they just random numbers used to showcase memory location ? (I thought I could attach an image) here is a link to the image -> https://ibb.co/Qjgy06n

Image array hosted in ImgBB

agile sundial
#

the start seems to be arbitrary, but it does show they are all next to each other

fiery cosmos
#

what's wrong with this code:


class Node(self, key):
    def __init__(self, key):
        self.left = None
        self.right = None
        self.key = key
agile sundial
agile sundial
sage plank
# agile sundial yeah

so 200 means nothing when it comes to memory location ? they just uses it as an example . You could start at 0 or 400 ?

fiery cosmos
#
class Node:
    def __init__(self, key):
        self.left = None
        self.right = None
        self.val = key
 
 
if __name__ == '__main__':
    # Create root
    root = Node(1)
#

whats going on w this syntax

#

specifically the __name__ line

agile sundial
agile sundial
halcyon plankBOT
#

if __name__ == '__main__'

This is a statement that is only true if the module (your source code) it appears in is being run directly, as opposed to being imported into another module. When you run your module, the __name__ special variable is automatically set to the string '__main__'. Conversely, when you import that same module into a different one, and run that, __name__ is instead set to the filename of your module minus the .py extension.

Example

# foo.py

print('spam')

if __name__ == '__main__':
    print('eggs')

If you run the above module foo.py directly, both 'spam'and 'eggs' will be printed. Now consider this next example:

# bar.py

import foo

If you run this module named bar.py, it will execute the code in foo.py. First it will print 'spam', and then the if statement will fail, because __name__ will now be the string 'foo'.

Why would I do this?

• Your module is a library, but also has a special case where it can be run directly
• Your module is a library and you want to safeguard it against people running it directly (like what pip does)
• Your module is the main program, but has unit tests and the testing framework works by importing your module, and you want to avoid having your main code run during the test

fiery cosmos
#

has anyone had success implementing binary trees that work with the CLRS algos?

#

they span many chapters in there

#

when you say if something: it just evaluates to True if the thing exists correct

#

why is root not defined in the following code:

class Node:
    def __init__(self, key):
        self.ley = key
        self.left = None
        self.right = None

class BinaryTree:
    def __init__(self, root):
        self.root = Node(root)

    def inorderTreeWalk(self, x):
        if x == None:
            inorderTreeWalk(x.left)
            print(x.key)
            inorderTreeWalk(x.right)


tree = BinaryTree(1)
tree.root.left = Node(2)
tree.root.right = Node(3)
tree.root.left.left = Node(4)
tree.root.left.right = Node(5)

print(tree.inorderTreeWalk(root))
agile sundial
#

you never made a variable named root in that scope

fiery cosmos
#

can i give inordertreewalk the scope of root or how might one correct this

agile sundial
#

it's supposed to take a node, right?

#

you don't have a variable named root

fiery cosmos
#

idk i'm sort of fudging it to make it work with the algos in the book but not entirely sure of what im doing

#

hmm i changed my call to print(tree.inorderTreeWalk(tree.root)) which runs but returns None

agile sundial
#

yeah, because your function doesn't return anything

fiery cosmos
#

why do they write the pseudocode that way

agile sundial
#

what way

fiery cosmos
#

this is the pseudocode:

INORDER-TREE-WALK(x)
if x =/= NIL
  INORDER-TREE-WALK(x.left)
  print x.key
  INORDER-TREE-WALK(x.right)
agile sundial
#

why wouldn't they write it that way, all it's supposed to do is print

fiery cosmos
#

whats the name for a function within a class, again?

agile sundial
#

method

fiery cosmos
#

i think its not working bc this wasn't written to be a method, but rather an entire separate algo/function to call on a binary tree. i'm trying to write it within the binary tree class as a method

agile sundial
#

yes, the functions are procedural, not oop

fiery cosmos
#

ok ok. got it going

#

now to figure out how to write a BST instead of a binary tree

#

like i'm not seeing how to confer the proper properties to it. i guess i have to write it into the tree class.. like eg if node_to_add < node: node_to_add = node.leftchild

#

or something similar

haughty mountain
#

this could easily be written standalone or in a node class

def add_node(root, value):
  if value <= root.value:
    if root.left is not None:
      # There is a left subtree, add value there
      add_node(root.left, value)
    else:
      # No left subtree, create one
      root.left = Node(value)
  else:
    # Same logic but for right
fiery cosmos
#

so far to add nodes i

#

i'm just doing like tree.root.right = Node(7)

fiery cosmos
#

and if i'm asked to implement such algos then having a node addition method in the tree will break things

#

i'm working my way to that but first i need to get the tree search running which currently is not

#

this is my code that currently does not work:

#
class Node:
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None

class BinaryTree:
    def __init__(self, root):
        self.root = Node(root)
        

tree = BinaryTree(1)
tree.root.left = Node(2)
tree.root.right = Node(3)
tree.root.left.left = Node(4)
tree.root.left.right = Node(5)

def TreeSearch(x,k):
    if x is None or k == x.key:
        return x
    if k < x.key:
        return TreeSearch(x.left,k) 
    else:
        return TreeSearch(x.right,k)

print(TreeSearch(tree.root,2))
#

my inorder tree walk is working, so that's good

haughty mountain
fiery cosmos
#

oh you said it could be written standalone i see

haughty mountain
#

it's pretty much the same code for standalone or if part of the node class

fiery cosmos
#

hmm

haughty mountain
#

only the function call changes

fiery cosmos
#

that's good to know, i was getting flustered not knowing how to implement a tree that could utilize all their pseudocode

haughty mountain
#

e.g.

class Node:
  ...

  def add_node(root, value):
    if value <= root.value:
      if root.left is not None:
        # There is a left subtree, add value there
        root.left.add_node(value)
      else:
        # No left subtree, create one
        root.left = Node(value)
    else:
      # Same logic but for right
fiery cosmos
#

ayyy its working

#

well not the tree search, but the node addition as a standalone class:

#
class Node:
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None

class BinaryTree:
    def __init__(self, root):
        self.root = Node(root)
        

tree = BinaryTree(7)


def tree_insert(root, key):
    if key <= root.key:
        if root.left is not None:
            tree_inser(root.left, key)
        else:
            root.left = Node(key)
    else:
        if root.right is not None:
            tree_insert(root.right, key)
        else:
            root.right = Node(key)

tree_insert(tree.root, 9)
tree_insert(tree.root, 4)
tree_insert(tree.root, 17)


def InorderTreeWalk(x):
    if x == None:
        return 'empty tree'
    else:
        InorderTreeWalk(x.left)
        print(x.key)
        InorderTreeWalk(x.right)

InorderTreeWalk(tree.root)
#

standalone function*

#

this tree search isn't working:

def TreeSearch(x,k):
    if x == None or k == x.key:
        return x
    if k < x.key:
        return TreeSearch(x.left,k) 
    else:
        return TreeSearch(x.right,k)

print(TreeSearch(tree.root,5))
#

returns (or prints?) this:
<__main__.Node object at 0x0000025289624880>

jolly mortar
#

you could implement a __str__ or __repr__ in TreeNode for a custom string representation

fiery cosmos
#

looks like their treesearch is only identifying if a given int occurs in the tree, not where it is or anything else

opal yoke
#

Is this a normal behavior ?

fiery cosmos
#

they have:
if x == NIL, i have if x == None which i'm thinking are not equivalent statements

agile sundial
#

they're pretty much the same

fiery cosmos
#

can y'all help w my randomized quicksort? it's running but no longer sorting although its based on my quicksort which is working

#

yes

#

a new function randomized partition swaps the element occurring at the randomly chosen location with the element at the end of the array

fiery cosmos
#

no but out of curiosity what does that one do? graph traversal?

fiery cosmos
#
import time
import random

arrayA = []

for x in range(10000):
    arrayA.append(random.randint(1,100000))

def randomizedQuicksort(array, p, r):
    if p < r:
        q = randomizedPartition(array, p, r)
        randomizedQuicksort(array, p, q-1)
        randomizedQuicksort(array, q+1, r)

def randomizedPartition(array,p,r):
    i = random.randint(0,len(array)-1)
    array[r], array[i] = array[i], array[r]
    return partition(array,p,r)

def partition(array, p, r):
    x = array[r]
    i = p-1
    for j in range(p,r):
        if array[j] <= x:
            i += 1
            array[i], array[j] = array[j], array[i]
    array[i+1], array[r] = array[r], array[i+1]
    
    return i+1


start=time.time()

randomizedQuicksort(arrayA, 0, len(arrayA)-1)

print(arrayA)

print(time.time()-start)
#

if i were a better coder i know it'd be obvious by looking at it what's going wrong, but alas i am not

haughty mountain
#

it's kinda weird to do do the randomized partition by swapping an element to the end and then re-using the other partition

#

though it shouldn't be wrong

#

it would be more sensible to pass the index of the pivot to partition

fiery cosmos
#

its just no longer sorting

#

running without errors

haughty mountain
#

just saying it's a weird code design

fiery cosmos
#

thats what it says in the book unless i've interpreted the pseudocode incorrectly

#

uh sort of. more broadly im trying to understand how adding an element of random behavior to a known algorithm changes its runtime

#

it should be faster, otherwise people wouldn't bother

#

depends on the input

haughty mountain
#

wait this is wrong

i = random.randint(0,len(array)-1)
#

it can pick stuff outside the current range

fiery cosmos
#

its gotta use p and r huh

#

yep, that fixed it right up, thanks @haughty mountain

haughty mountain
#

fwiw, picking the last/first element is a very bad choice for quicksort

#

it's Θ(n²) on already sorted data

#

what's the specific question about Bellman-Ford?

fiery cosmos
#

@haughty mountain really don't know what i would do without your help

#

hmm its only returning the first so many elements of an array after my print/return statements, is this a function of python itself or my IDE

#

if i bump it down to n=1000 it'll print all of them

#

but i need to be working in the 50k - 100k range

haughty mountain
#

python has a 1000 deep recursion limit by default

fiery cosmos
#

is that what's known as the "stack size"?

haughty mountain
#

kinda, though this is more of an arbitrary limit python sets

fiery cosmos
#

hmm i wonder what i'll have to do to get my scripts working in the higher range

haughty mountain
#

if you pick the last element as pivot and run on sorted data you have O(n) recursion depth

#

you're probably encountering something like that

fiery cosmos
#

run on sorted data?

haughty mountain
fiery cosmos
#

like when i test my scripts i'll print out the randomly generated array and then the array after i call randomizedquicksort

#

and the terminal only shows the first so many elements of the arrays

#

i don't get a recursion error or anything, it just doesn't print the entire arrays when they are like, n=10k

haughty mountain
#

it should be a bit similar to Dijkstra, Dijkstra is essentially Bellman-Ford but it only does the relaxation greedily

#

while Bellman-Ford is just "run relaxation over all vertices n_vertices - 1 times"

#

if it's code you can just share the code here

#

you can also put images here

#

that does not look how I expect Bellman-Ford to look pithink

#

what is the unvisited thing even doing?

fiery cosmos
#

@haughty mountain i'll wait until you're done helping hinder and then i'd love for you to take a look at why my tree search algorithm isn't working, upon running i'm getting

<__main__.Node object at 0x0000023F312B4880>
haughty mountain
#

stealing part of the wiki pseudocode, this is the main thing happening in bellman-ford

repeat |V|−1 times:
  for each edge (u, v) with weight w in edges do
    if distance[u] + w < distance[v] then
      distance[v] := distance[u] + w
#

do the relaxation over all edges |V|-1 times

haughty mountain
#

floyd-warshall is way overkill here though?

#

and floyd-warshall does not like negative cycles

#

in bellman-ford you can detect negative cycles

#

the last code you posted?

haughty mountain
#

yeah, that's what I'm guessing as well

#

getting a Node or None is a sensible api as well though

fiery cosmos
#

ok i fixed it up with py return x.key

fiery cosmos
#
def TreeSearch(x,k):
    if x == None or k == x.key:
        return x.key
    if k < x.key:
        return TreeSearch(x.left,k) 
    else:
        return TreeSearch(x.right,k)
haughty mountain
fiery cosmos
#

here's the code

#

ok i think it works like this:

#
def TreeSearch(x,k):
    if x == None:
        return "does not exist" 
    if k == x.key:
        return x.key
    if k < x.key:
        return TreeSearch(x.left,k) 
    else:
        return TreeSearch(x.right,k)
haughty mountain
#

right

fiery cosmos
#

sry its sloppy CLRS pseudocode

#

theirs literally says if x == NIL or k == x.key:

#

return x

haughty mountain
#

oh, that's fine

#

it returns the node if it exists, or it returns NIL

fiery cosmos
#

it doesn't work that way

#

either it returns that weird statement above or you can tell it to return x.key, and if x = None it gets fussy

#

this way works better, the if x==None statement i added

haughty mountain
#

@west vigil why are you deleting your stuff? pithink

fiery cosmos
#

but its only returning a search term itself if it exists, not like a node location or anything

haughty mountain
#

it's one of your nodes

fiery cosmos
#

ok should i go back to that version and try to modify it to return the nodes value?

haughty mountain
#

it's the default repr of a class, if you want it to print something more useful you can implement __repr__

haughty mountain
haughty mountain
fiery cosmos
#

ok, i'll add a __repr__ method to my Node class

#
def __repr__(self):
        return key
#

like this?

fluid gate
#

why is a priority queue such a pain in python? I just want to put objects in it and change the sort order

#

am I missing something?

stable pecan
#
import heapq
fluid gate
#

ugh aren't there any classes?

stable pecan
#

it uses a normal list and you can use heapq.heappush and heapq.heappop

fluid gate
#

k how do I change the sort order?

haughty mountain
#

the basic idea of BF isn't that hard, you start only by knowing that the distance to your start vertex is 0, and assume everything else is ∞ (or just some large value)

then you perform this relaxation step on all edges, everytime you run the relaxation more vertices end up with the right distance, after |V|-1 steps everything has the right distance (unless you have negative cycles, which can be discovered with some extra work if those are interesting)

fiery cosmos
#

ok i got it working with None, but not with search term

haughty mountain
fiery cosmos
#

like if the search term isn't in the tree i return None

haughty mountain
#

what part of it?

fiery cosmos
#

if it is, i get:
__repr__ returned non-string (type int)

haughty mountain
#

can you re-post that code?

haughty mountain
fiery cosmos
#

idk if its working or just returning my search term to me

#

here's my repr function:

    def __repr__(self):
        return str(self.key)
haughty mountain
fiery cosmos
#
def TreeSearch(x,k):
    if x == None or k == x.key:
        return repr(x)
    if k == x.key:
        return x.key
    if k < x.key:
        return TreeSearch(x.left,k) 
    else:
        return TreeSearch(x.right,k)

print(TreeSearch(tree.root,9))
#

this is succesfully returning the int if its in the tree, and None if it isn't

haughty mountain
#

so let's ignore the parent part for now at least, what's the issue with using direct access arrays? the first loop is the |V|-1 relaxations, you can see that?

fiery cosmos
#

i guess it couldn't possibly work that way if it were simply printing the search term

haughty mountain
#

ah, yeah they assume you have vertices numbered 0 to |V|-1

#

so your adjacency list could be written

myGraph = [
  [1, 2, 3],
  [2, 4, 5],
  [3],
  [2],
  [3, 2],
  [4],
]
#

more like the weight is some function of the two vertex indices

#

you could provide such a function that does lookups in a dict or similar

#

or you could rewrite their code to work with your type of graph representation

#

when I formulate graphs I tend to write down the edges, e.g. a list of (u, v, w) where u and v are indices and w is a weight, and then I can easily write a few lines of code to generate the graph representation

#

@west vigil e.g.

edges = [... list of (u, v, w)]
n = number of vertices

adj = [[] for _ in range(n)]
weight = {}
for u, v, w in edges:
  adj[u].append(v)
  adj[v].append(u)  # if undirected graph
  weight[u, v] = w
  weight[v, u] = w  # if undirected graph

def w(u, v):
  return weight[u, v]
#

that would be a valid function w to pass to their function

#

you can totally rewrite their code to work with your graph representation as well, it wouldn't be that huge changes

#

I didn't really study computer science, I took a few courses, but most stuff I learned outside school doing programming contests and similar

#

I didn't really learn Bellman-Ford in particular either, I had to read into it a while back when someone was asking about it in another server when they were working on a problem :P

#

the idea is similar to stuff like Floyd-Warshall though, in that you do a bunch of relaxations

#

so not that hard to pick up

#

all down to practice 😄

#

programming contest stuff is nice in that you actually get some hands on experience implementing the stuff

#

just memorizing specific solutions is not a good path to learn things

#

it's much more important to understand the ideas that goes into the solution

#

ideas and problem solving techniques is what tends to generalize to new unseen problems

#

if you know what you need to do but have troubles expressing it in code, then solving some problems would probably be useful, yeah

#

I think in a lot of cs courses there is too little focus on actually implementing stuff. I think I could grab a bunch of students that took the same algo classes as me and they would struggle a ton to implement stuff

#

I mean, the theory is also needed

lament totem
#

I've tutored someone that is doing a master in AI, and they had to look up how to print a variable in python :/

#

People get taught theory a lot, but practice a bit too little

haughty mountain
#

but if you don't do things in practice you will struggle to have a deep understanding of things

#

solving programming problems is all about breaking a task apart in sensible pieces, finding the right tools (algos, data structures, ...) to solve the pieces, and then putting all the pieces together into an implementation

#

(tl;dr: theory is important, but should be complemented with something more practical)

haughty mountain
#

not now, it's like 1am here :P

fiery cosmos
#

so binary search on a sorted input array is lg(n)?

vocal gorge
#

lg is usually used to mean base-10 logarithm. This isn't wrong, since O(log(n)) = O(lg(n)) = O(log2(n)) anyway, but it's a bit weird to see.

#

but yes

fiery cosmos
#

i think in CLRS they use lg to mean log_2

#

if i'm not mistaken. but i realize not everyone is thinking in CLRS terms

vocal gorge
#

ah, alright

fiery cosmos
#

so whats the precise mathematical notations for say, a binary search algorithm with a single while loop

agile sundial
vocal gorge
#

I checked wolfram, and it thinks lg is natural 😔

#

fucking mathematicians, smh

fiery cosmos
#

natural log is ln

vocal gorge
vocal gorge
fiery cosmos
#

yeah in chem we use log_10

agile sundial
vocal gorge
#

real mathematicians only use ln(n)/ln(10) 🥴

fiery cosmos
#

hey all i am practicing a mathematical induction proof, i have the recurrence:

#

is T(n) = nlgn

#

now i've just shown myself its true for when k=2

#

but i'm used to proving its first true for n=1 and then the inductive step is showing it must also be true for n+1

#

so i guess that the k is throwing me, will my inductive step be showing its true for k+1 instead?

fiery cosmos
#

here is the whole thing, i don't see where the 2(2^k lg(2^k)) + 2^k+1 expression comes from

gentle crater
#

Hello can someone help me with a travelling salesman problem?

#

Its a long piece of code but its under introduction to Artificial Intelligence

#
def survivorSelection(population, eliteSize):
    
    # TODO 5 (10 marks) - Replace the dummy survival selection function below with  
    # either Fitness Based Selection or Merge, Sort & Truncate.
      
    # Marking scheme: 
    # 7 to 10 marks:  Correct implementation. 
    # 5 to <7 marks:  Minor errors.
    # >0 to <5 marks: Major errors. 
    # 0 marks:        No answer is given. 
    
    elites = []
    
    # Replacement starts here  #this is dummy/ Use the merge, sort, truncate and take the elite size list from the top
    def merge_sort (survivorSelection):
    if len(survivorSelection)>1:
        left_arr = 

    
    for i in range(eliteSize):
        elites.append(population[i])
    # Replacement ends here
    
    return elites
#

I have to replace the piece of code BETWEEN"# Replacement starts here" AND "# Replacement ends here"

haughty mountain
gentle crater
#

Hey guys, can anyone help with this Genetic Algorithm problem?

#

I sent it in picture form to be able to see the notes and full question

#

Im just not sure what values I need to put in and how to write the python code for Merge,sort and truncate

#

thank you

fiery cosmos
#

hey all,
i don't understand when they say "we add k + 1 to both sides of the equation P(k)". i already see a k+1 in the LHS

fiery cosmos
#

i guess the why is that, that entire T(something) expression evaluates to 2^k when you plug into it your k+1 term

haughty mountain
# fiery cosmos

1st eq is what they start with, 2nd is what they want to show, 3rd part is working from 1st eq

fiery cosmos
#

so i can see that they have derived an expression for the P(k+1) summation, namely, (k+1)(k+2)/2

haughty mountain
fiery cosmos
#

but then that seems to disappear and they go back to working with the original term for the summation k(k+1)/2

#

could you please just try to walk me through it, everyone in the math server just is condescending

haughty mountain
#

the derivation is the third part

fiery cosmos
#

oh i see something i hadn't before. they show the two equations are both equal to the same expression

haughty mountain
#

the assumption is that 1 + ... + k = k (k+1)/2

this also predicts that 1 + ... + k + (k+1) = (k+1) (k+w)/2

let's see if this is true, i.e. take
1 + ... + k = k (k+1)/2
and add k+1 to both sides and see if the right hand side ends up correct, if so the formula seems to work

haughty mountain
fiery cosmos
#

ah i got it! and i even used the old multiply by 2/2 to get there

haughty mountain
#

🎊

fiery cosmos
#

boy what a relief. ok. next problem

#

"conjecture a formula for the sum of the first n positive odd* integers. then prove your conjecture using mathematical induction."

#

hmm

haughty mountain
#

try to write the first few sums and see if you can guess a pattern

fiery cosmos
#

i'm on it

#

looks like n^2 to me

#

P(1) = 1, P(2) = 4, P(3) = 9, P(4) = 16, P(5) = 25

#

i'm going to let P(n) be the proposition that the sum of the first n positive odd integers is equal to n^2 and try to prove

haughty mountain
#

cool

#

as a quick check the base case checks out, e.g. P(0) = 0

fiery cosmos
#

so in this case my inductive proof will involve n+2 to keep the next integer odd?

haughty mountain
#

you could do that, or assume you have 1 + 3 + (2*k-1)

#

then you have the k first odd integers

#

with the way you phrased it the number of terms is kinda awkward

fiery cosmos
#

i think im using n where i should be using k

haughty mountain
#

e.g. 1 + 3 + ... + n has (n+1)/2 terms

haughty mountain
fiery cosmos
#

right

haughty mountain
#

phrasing odd numbers as 2*k - 1 is quite useful in general

fiery cosmos
#

so my k+1 term would be 2*k+1

haughty mountain
#

yep

#

2*(k+1) - 1 = 2*k + 1

fiery cosmos
#

i must have gone wrong somewhere

#

so i set up that 1+3+...+2k-1 = k^2

#

so i get that my k+1 expression should be equal to k^2 + 2k+1

#

ok i got it

#

i showed that the (k+1) term gets the form k^2 + 2k + 1, which is equal to my expression for the first 1+3+...+k + k+1 term, which was in the form (k+1)(k+1) .. (from the k^2 assumption)

#

k^2 + 2k + 1 = (k+1)(k+1) .. QED

#

huh, that's exactly what the book did

#

thanks for the 2k-1 tip

#

so you like to show things first for k-1 term and then for k is it?

haughty mountain
#

I often prefer that, yeah

#

the kth term tends to be a neater target to try to reach

#

e.g.
(k-1)² + 2k - 1 =
(k² - 2k + 1) + 2k - 1 =

fiery cosmos
#

ok i'm working on:
use mathematical induction to show 1 + 2 + 2^2 + ... + 2^n = 2^(n+1) - 1

agile sundial
#

parentheses are important lol

#

that should probably be 2^(n+1) - 1

fiery cosmos
#

so the first thing i did was set the expression to end instead at 2^n-1, making the entire summation equal to 1 + 2 + 2^2 + ... + 2^(n-1) = (2^n)-1

#

which i think is a legitimate manipulation

#

just shifted the entire thing by one exponentiation

haughty mountain
#

you can pick your starting point to be one earlier, it's not really a manipulation of anything though

fiery cosmos
#

ok so so far i have that 1+2+2^2+...2^(n-1)+2^n should be equal to (2^(2n))-1

haughty mountain
#

that doesn't look right pithink

#

where did the 2 in the exponent come from?

fiery cosmos
#

ok let me back up then

haughty mountain
#

I'm guessing that's what you rewrote incorrectly?

fiery cosmos
#

no what happened was i incorrectly tried to plug in a 2^n term into the 2^n-1 expression

fiery cosmos
#

misunderstanding

#

i think i am at the entire expression on the left should be equal to 2^n - 1 + 2^n

haughty mountain
#

yes

#

you can simplify that further

fiery cosmos
#

2^n + 2^n ..?

haughty mountain
#

you have 2 of the 2^n terms (or equivalently you can factor 2^n out), in any case you have 2*2^n - 1

#

which is just 2^(n+1) - 1

#

fwiw the statement itself is pretty obvious when you think about it, you have a binary numbers with all one bits, if you add 1 to it you're left with a single one bit, one higher than the previous highest bit

#

0b111 + 1 = 0b1000

fiery cosmos
#

right bc 2*(2^n) = 2^1 + 2^n = 2^(n+1)

haughty mountain
#

that middle step is wrong

#

and the last one

#

you're missing () in the last one?

#

() are important for the expression to be correct

#

the middle step is wrong though

#

you have + where it should be *

fiery cosmos
#

no i know. i meant to write that 2^1 * 2*m == 2^(1+m)

haughty mountain
#

but yes, x^a * x^b = x^(a+b)

fiery cosmos
#

but at any rate i'm not getting the right answer, now i have 2^(n+1) - 1 for one expression and (2^n) - 1 for the other

haughty mountain
#

how so?

fiery cosmos
#

idk i was using the previous problem as a model and trying to follow it but i must have gone awry

haughty mountain
#

err, wrong reply, but whatever

fiery cosmos
#

thats how it was given to me, i changed it to
1+2+2^n+...+2^(n-1) = (2^n) - 1

haughty mountain
#

you chose to prove this by starting at a term one before and adding a term

1 + 2 + ... + 2^(n-1) = 2^n - 1
```add the next term
```py
1 + 2 + ... + 2^(n-1) + 2^n = 2^n - 1 + 2^n
= 2^(n+1) - 1
fiery cosmos
#

then i add the next term to both sides:
1 + 2 + 2^n + ... + 2^(n-1) + 2^n = (2^n) - 1 + (2^n)

#

so RHS equals 2^(n+1) - 1

haughty mountain
fiery cosmos
#

ahh ok. i didn't go all the way back to the beginning, only went back to my eq RHS

#

gotcha

#

ok this is beginning to make sense. i think i need to practice a few every day so that i'm not relearning every time i come back to it

#

the recursive ones get really tricky for me

haughty mountain
#

so the process of the induction is this: let's call G(n) your guess for the sum, let g(n) be the nth term

pick any k (in this case I picked k = n-1) and show that this equality holds

G(k) + g(k+1) = G(k+1)
#

if you can show that you have showed that your guess is consistent with just adding the next term (they give the same result)

#

so if you can just prove that some starting point is true e.g. that G(0) has the right value, then by the property from earlier G(1) is also true, and in turn G(2) is true, and so on

fiery cosmos
#

yeah i don't get the logic of the starting point zero when the expression says 1 + 2 + ... + n, bc they aren't claiming in their proposition that 0 is included anywhere

#

or in their equation or however you would like to name it

#

not in their sequence nor could n be some higher number zero

#

but hey if you're allowed to do that, i like it bc its easier

#

my next one is:
use mathematical induction to prove the inequality n < 2^n for all positive integers n

#

base case of 1 checks out

#

ahh this one is very curious

#

they make use of the fact that 1 <= 2^k, and create a new expression which we know to be greater than (2^k) + 1

fiery cosmos
#

hmm i seem to have gotten snagged on proving the summation for the for n even positive integers. my proposition is that 2+4+6+...+n = n(n+1). so the base case checks for n=1, that is, the first even positive integer 2 is equal to 1*(1+1)

#

so next i set it to the form 2 + 4 + 6 + ... + 2k-2 = k(k+1)

#

right so far?

#

oooh

#

something just clicked with k(k+1)+2(k+1)

#

unless i am mistaken, this can be rewritten as (k+2)(k+1) by asking the question, how any (k+1) terms are there, and the answer is k+2 of them

#

is this correct

#

i don't really see how getting the form (k+2)(k+1) proves the summation of the first n even positive integers being equal to k*(k+1)

#

i guess its bc the (k+2) is the next term in the series, and thus it has the form of the original hypothesis where k+1 is the same and the next term is in the form k+2 bc its the next even integer

haughty mountain
#

so you want to pick the earliest sensible thing

#

0 is sensible as the sum of none of the integers

haughty mountain
haughty mountain
haughty mountain
#

G(n+1) is indeed (k+1)(k+2)

#

though do note that deriving the sum of even numbers can be done a lot easier, you're already familiar with the sum of 1 + 2 + 3 + ... + n = n(n + 1)/2, right?

#

the sum of even numbers is just double that :P

fiery cosmos
#

why would it be double if you are only adding half of the numbers in the sequence

#

you're omitting the odd ones

haughty mountain
#

btw, I think this thing is wrong

2+4+6+...+n = n(n+1)
```I think it's supposed to be
```py
2+4+6+...+2n = n(n+1)
#

sum of the first n even numbers

fiery cosmos
#

lms what they have in the back of the book

#

you're correct it should be 2n

#

i've moved on to reading about greedy algorithms but perhaps i should practice another proof while you're around

#

yeah i feel like i should go back and practice the one from CLRS with this fresh practice

#

when evaluating this, i first look and say to myself what is lg(2^k), which must equal k, correct

#

the way to eval that is ask what must 2 be raised to to equal 2^k

#

not bc of the two inside but bc of the base, eg lg_2

#

yes that's correct according to wolfram

#

ok so then i have 2((2^k)(k)) + otherterm

#

so that must be (3^k)*2

#

but in the sltn they get k(2^(k+1))..

#

what gives

opal oriole
#

2*2*2=2^3

#

2^1 * 2^1 * 2^1 = 2^(1+1+1)

#

2 * 2^k = ?

fiery cosmos
#

ah ok

fiery cosmos
opal oriole
fiery cosmos
#

wait back up

haughty mountain
#

the last thing is grouped as:
2^k + 1 = (2^k) + 1

opal oriole
#

Without the ability to actually superscript and subscript we need those extra parens.

haughty mountain
fiery cosmos
#

wait so what is (2^k) * k?

opal oriole
#

Just as is.

haughty mountain
#
2 * 2^k * k = 2^(k+1) * k
opal oriole
#

2((2^k)(k)) You can change the parens here (associativity rule).

haughty mountain
#

maybe you're thinking of k^2 * k = k^3?

fiery cosmos
#

yes

fiery cosmos
haughty mountain
fiery cosmos
#

oh so you're saying its all one term so i can multiply the (2^k) by 2 (which is 2^1)

haughty mountain
#

yes

fiery cosmos
#

ok ok

haughty mountain
#

within a term you can play around with products pretty freely

fiery cosmos
#

now i see where they get k2^(k+1)

haughty mountain
#

a*b = b*a and whatnot

fiery cosmos
#

hmm so how do they go from

opal oriole
#

And 5 * 4 * 3 = (5 * 4) * 3 = 5 * (4 * 3), etc

fiery cosmos
haughty mountain
#

common factor 2^(k+1)

#

factor it out

opal oriole
#

2^(k+1) = 1 * 2^(k+1)

fiery cosmos
#

yeah i'm seeing what clicked before, where we have:
k two to the k plus ones
+
1 two to the k plus one

(k+1) two to the k plus ones

opal oriole
#

You can imagine the more simplified version of it by treating the 2^(k+1) as one thing.

#

Then it's just like kx + x = (k + 1)x

haughty mountain
#

k apples + 1 apple :P

fiery cosmos
#

right thats essentially what i'm doing i think

#

right

opal oriole
#

(You can even do the substitution in your work, temporary variable usage (let x = 2^(k+1)))

fiery cosmos
#

hmm ok. i'll get there

opal oriole
#

(But you don't have to)

haughty mountain
#

yeah, substituting stuff can be useful to keep expressions simpler

#

especially when expressions grow larger

fiery cosmos
#

looks like it finished in this manner:

opal oriole
#

You can make up variables with "let ...". To help make it more simple for you.

fiery cosmos
#

so indeed you have your x lg x where x = 2^(k+1)

opal oriole
#

Yeah it's pattern matching in that case.

#

You know that n = ...

#

If you know how n and k are related you can show the steps to get to n.

haughty mountain
fiery cosmos
#

i've been trying to adopt algmyr's n-1 hypothesis, then show that it must be true for n, but i mix myself up sometimes

haughty mountain
fiery cosmos
#

i'll get there. this is good practice

#

the n-1 case confuses me when like in this example, n = 2^k

opal oriole
fiery cosmos
#

although i guess it'd just be 2^(k-1)

haughty mountain
#

is n-1 even valid?

#

yeah

#

it would be 2^(k-1)

fiery cosmos
#

yep. then you could show that if its assumed to be true for 2^(k-1), it must also be true for 2^k

#

i'm gonna practice a few of these per day before i get into the coding side

haughty mountain
opal oriole
fiery cosmos
#

alright, what do we think here ladies + gents, this is in line 5 of a recursive algo on activity selection. the function is to return the greatest number of activities which can be done in a given space on a given day

#

my input arrays are one with start times, and one with finish times

#

here is the code so far:

s = [1,3,0,5,3,5,6,8,8,2,12]
f = [4,5,6,7,9,9,10,11,12,14,16]

def recursiveactivityselector(start_array,finish_array,k,n):
    m = k + 1
    while m<=n and s[m] < f[n]:
        m += 1
    if m<=n:
        return 
#

the equation i posted above must go in on the line with a single return statement now

#

so activity 0 begins at 1 and ends at 4, act 1 begins at 3 and ends at 5, and so on

#

so the return statement is to return a set with a single activity in it, plus the union to the set found with the recursive call

#

my discrete math book says that the union of two sets is the set that contains those elements that are in either A or in B, or in both.

fiery cosmos
#

i have a more general question regarding runtime complexity

#

i've generated a cover file for my randomized quicksort, and added up all the computations it did, which is equal to 42,743 lines executed on an input of a single array of 1000 numbers

#

so i would expect that this number could be upper bounded by the runtime complexity O(nlgn)

#

however, nlgn of 1000 is 9,965

#

therefore the O doesn't hold. therefore i must be misunderstanding something about the nature of runtime complexity vs. lines of code executed

agile sundial
#

you're forgetting the constant factor part

fiery cosmos
#

ah ok. so there is some low constant factor like 5 that can upper bound it

agile sundial
#

something like that yeah

fiery cosmos
#

hmm

#

what if i multiplied the runtime complexity by the amount of time it actually took to run

#

lms

#

no theres no formula nor association there so far as i can tell

#

which i guess is the point of O in the first place

#

to remove the machine specifics

agile sundial
#

right, the time complexity isn't going to be evident off 1 measurement

fiery cosmos
#

i'm just playing around with how can i take what i've learned and apply it to a formal analysis of say, one algorithm vs. the same with the random aspect, or say two sorting algorithms compared. is there a call that will sum the lines of the cover file and return it so that i don't have to do it manually? i guess i could write a script to do that?

#

looks like all the integers i want are preceded by the same character, :, that'll be helpful

haughty mountain
#

writing a script to sum the stuff is very simple yeah, though you run the risk of very misrepresentative data

#

e.g. these three sections have the same complexity

    1: A = []
 1001: for i in range(1000):
 1000:     A.append(i)

 1002: A = [i for i in range(1000)]

    1: A = list(range(1000))
fiery cosmos
#

hmm i'd like to pick your brain about that when i'm around

trim rover
#

basic question:

category, company_name, company_url, job_cost, linkedin_url, phone_number, logo_url, site_url = process_results_page(article, session)

write_to_mysql(category, company_name, company_url, job_cost, linkedin_url, phone_number, logo_url, site_url, cursor, conn)

What is the best way to shorten this list of variables? My favorite way is using a dictionary of course, but with MySQL it seems not very practical, since I need to put them in a tuple for the MySQL query...

haughty mountain
trim rover
#
    sql = "\
    INSERT INTO houzzscrap.houzz \
    (category, company_name, company_url, linkedin_url, phone_number, \
    job_cost, logo_url, site_url) \
    \
    VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
    val = (category, company_name, company_url, linkedin_url, phone_number, job_cost, logo_url, site_url)```
#

(the inside of the mysql function)

dark aurora
#

I was able to solve this but I am wondering why does my code work (on all but one case) when n, m are by one bigger then lengths of strings