#algos-and-data-structs
1 messages · Page 18 of 1
i still could never figure out how to catch strings in the input. the way i im reading the data now perturbs that and i wasn't able to implement your proposed data reading strategy
hey my count function is messed up for when bucketsize is 3
right now it's this:
which was working for regular buckets of size 1
its like way wrong like im getting load factor of 8
its counting the correct number of elements
nvm, i just needed parenthesis 🤦♂️
i'm actually really surprised she doubled down on being wrong about the tablesize mod. its fine to be wrong, but the way you handle it matters
i wrote two completely separate programs
and neither are great, but at least i have a real load factor to work with
i have a vague feeling like the probe computation isn't what she wants, beyond just the modulo thing
oh yeah i see how quadratic probing is broken
oh wait this was chaining 🤔
somehow my chaining broke
for both programs. so weird
must have been some comparison logic i added
is this correct syntax?
if not var1 < var2:?
probably not
fwiw, not < is usually spelled >=
!e let me be the terrible person to throw nan at you as a counterexample
nan = float('nan')
print(not nan < nan)
print(nan >= nan)
@haughty mountain :white_check_mark: Your 3.11 eval job has completed with return code 0.
001 | True
002 | False
💯% right, and maybe thinking about that would clarify in someone's mind why they want not < over >=
gotta love floating point
x != x or x >= x
Hmm, now i understand that doing in-place operations just alters the list while doing something like
def test(nums):
list = x
x = x + [1] # x now references to a new list so there is no alteration in the original list
arr = [1,2,3]
print(test(arr))
`[1,2,3]
Hey can anyone help me understand how one is supposed to implement a backtracking problem iteratively??
Is it true that any problem can be solved both recursively and iteratively? Or are there certain instances where that isn't the case...
all recursive programs can be written iteratively and vice versa
hi all, i think my logic for my chaining insertion scheme could be incorrect
right, and this is not the same as what @inland crane had implied, that any problem can be solved recursively
not all problems have recursive structure
for my hashtable algorithm
is there some resources for a bit more intermediate algorithms n data structure? Especially for leetcode
and also some math courses? I'm kinda bad in math
for math you can do khan academy and schaums outlines (physical books) which are very good for getting back up to speed with things you may have forgotten. khan academy is invaluable. then you can move on to the pinned course in this channel
hi all,
if each insertion of a hashtable could be O(n) in worst case, and there are n elements, it theoretically could take n^2 time to insert them all, yeah?
so if i wanted to make a graph or tree data structure, with a bunch of objects as nodes and be able to access connected nodes via an attribute is that possible
of course. might be easier with an adjacency list though
for example using
for node in node5.children:
[do work on node objects]
node.children would have to contain a list of references. but python doesnt have references?
so how would you implement that
so typically a tree structure based on a node class just has pointers as attributes. eg, node.leftchild = node4
adjacency lists make everything easy, for graphs
trees and graphs can be quite different
yea thats how i would do it in c, using pointers
im specfically asking how to do it using object references not a adjecency chart
Everything in Python is a PyObject *.
= copies the pointer.
(Like in C)
So if you make a new_var = some_obj, they both reference the same object.
oh ive been out of python for a while i thought it did a copy
you'd recursively gather up all the children by calling a method on a node that would get all it's children first, then do something will all of the children you have gathered. it should be easier though if each node is the root of the subtree of all of its children
Deep copy requires the copy library, which is built in.
then you just say do something for the subtree with this node as the root
khanacademy comes a bit wierd to me like I currently finished a 2 year degree in computer programming but my highschool math wasn't totaly complete
yea i thought doing deep copy was the default my bad
I tried to look from there but some stuff are too basic. When I skip then again I skip too much material
i think khan academy is excellent one day i'd like to go through all of it and properly learn linear algebra and regression analysis
Yeah just remember PyObject *.
you literally have to painfully go through all of it
i've been there
i have 2 schaums outlines
one in rudimentary algebra and one in discrete
i took calc and stats in college and forgot all of it
i want to learn linear, as i said
like for example I know some stuff about till like functions in math. Idk the what exactly algebra and like discrete math is
and multiple linear regression
but my geometry was really bad
you don't know what algebra is?
like w the triangles
eh, you don't really need that
our entrance exam is based on that
mostly circles n triangles
for geometry
we just have like some topics
idk if it is in algebra or not
I also like to watch it in english
soo like because of that idk even what to watch in khanacademy
maybe thats why it was a bit bad experience for me
Eh, IDK if I can share that due to rules.
is pay to learn...
schaums outlines
ooh i should grab the linear algebra one
I am going to show you some stuff and you tell me if you think it's in there.
wow y'all are always on the ball, even on saturdays. bravo 👏 👏 👏
versin? exsec?? excsc??? wow, they really just keep making up functions :p
Algebra (derived from الجبر "al-jabr" (meaning "restoration" or "completion")) is "Calculation by Completion and Balancing". It's when you have equations such as 3x = 10. Them being equal to each other means they are balanced.
A set of rules for solving such algebraic equations is known as an "algorithm" (algorism) (name also given by same person, محمد بن موسی خوارزمی (Muhammad ibn Musa al-Khwarizmi)).
And in many algebra classes you memorize many such algorisms.
exsecant/exsec(θ) is sec(θ)-1
excosecant/excsc(θ) is csc(θ)-1
huh, never saw that - why not call them arcsec/arccsc like everything does?
ooooohh
algebra and algorithm
v curious
Its pretty much the same. But the name is not just joining func names
hey, so i had an interesting thought when it comes to string searching.
this might be the same as doing a regular check if a word appears at index n in a string, but:
actually, nevermind.
lol
👀
👁🗨
interesting that they used exscs and exsec which makes it very not obvious that the terms are related
external secant and external cosecant
they go with tan and cotan
thanks i hate it
versin is actually important
or was
for navigation
theres also the haversine
you would have tables of haversine (half versine)
right
The haversine formula determines the great-circle distance between two points on a sphere given their longitudes and latitudes. Important in navigation, it is a special case of a more general formula in spherical trigonometry, the law of haversines, that relates the sides and angles of spherical triangles.
The first table of haversines in Englis...
im pretty sure all of them can be derived from the base 3 trig functions
and tan can be derived from sin and cos
and cos can be derived from sin
its sin all the way down
yeah that about sums it up
I hope that joke landed
lol
The names don't really make sense and are only partially fixed in post. They are mistranslated through multiple languages over thousands of years.
yeah, most of the base names are terrible (looking at you sine) but there is a systematic naming on top of that
feels weird to hide the actually nice systematic part of it
The base needs to be accepted as its own made up words (slightly latin based), since that was translated incorrectly.
isn't sine the only base?
Yea after translation.
They needed a word for cosine, and were like, let's just add "co".
The older original words are related to construction.
Yea more or less, but the Latin root version.
Tan and cotan make more sense in that regard.
Cause if the total line is length say, 1, then cotan 1 - tan
So the complement.
Like in probability.
Sin and cos are a bit more complicated, related through the right triangle.
So not exactly complement.
(Unless you go by the squares)
it's complimentary in the sense of that one somewhat important trig identity, yes
iirc there is a nice symmetry of all the co- stuff
with the non co- stuff
the non co- version is based on the triangle on one side of the angle
the co is the same but for the other side of the angle
cofunc(θ) = func(π/2 - θ)
not that I actually know most of these terms off hand
if I need them I usually just represent them in the usual 3
I assume that's why they made me memorize that angles that add to π/2 are called "complementary"
er
fixed lol
complimentary is pi/2 i thought
lol, I was about to say
Wolfram math world to the rescue when you need it.
knew there was something wrong about that
before I saw the edit
External memory storage, don't need to remember everything, just the general idea and how it can be used.
so yeah, they are related to the complementary angle
guess I didn't memorize things as well as I thought
(wrt pi/2)
Or just 1 - theta if you are not using radians.
I recall in a bunch of US material more than the usual 3 functions
like cot scs and...what's the last one?
csc?
csc
if you are using degrees it would be 90° - θ
Degrees are for people using base 60.
what angle measurement is that even?
We can become French revolutionaries and use gradians.
4 somethings is a revolution?
I know there is a 400 degree thing
but I've not seen 4
Gradians ("new degree")
There are a whole bunch others, including 2pi = 1
it's quite simple, we define π = 2
aka a revolution is 1 revolution
radians are nice though
because the whole arc length thing
In Euclidean geometry, an angle is the figure formed by two rays, called the sides of the angle, sharing a common endpoint, called the vertex of the angle.
Angles formed by two rays lie in the plane that contains the rays. Angles are also formed by the intersection of two planes. These are called dihedral angles. Two intersecting curves may also...
yeah, I guess it's the more usual term
what's the relevant term from complex analysis...
I use all three at work (degrees, radians, turns)
degrees mostly for axis labels though
related to the residue theorem
winding number
I guess that doesn't translate to a nice term, huh?
Not sure. Knowing math there is probably a term for it.
well, winding is about the process of...winding something around the origin
I suspect turn could be the appropriate unit
revolution for sure pops up in practical applications
rpm and whatnot
someone should be terrible and shorten rad/s as rpm
but in the grand scheme radians is the "right" choice for mathematics
a bit like how e is the right choice for exponentials
It's kind of arbitrary, but results in better equations than something like degrees.
(and these are somewhat related, radians and e)
Which is subjective somewhat (shorter equations typically preferred).
well, that kinda removes the arbitrariness, doesn't it?
it removes a bunch of leading factors popping out when doing math on it
KInd of, because one can just collapse them often.
it can be arbitrary, but it can be practical too.
You minimize floating point error by doing math with all numbers that are more or less close to 1.0
Which is effectively the same as changing to radians or whatever.
Yeah in CS, which is on topic, it can show up better to no use radians, which many implementations do.
For by-hand, degrees can make sense if you use base 60, but we don't typically do that anymore.
yeah sure, but in math we would like to go for the thing that causes us less pain 😛
wrong message to reply to, but whatever
Gotta max out the lazy-ness, but also can just not care these days and let mathematica figure it out.
well, you still need to feed something sensible into mathematica
Yeah, but much less.
I can give some ridiculous stuff to it in 2022.
physicists do fun stuff as well, e.g. let c = 1
(And eventually when they use more ML, almost anything that is not complete nonsense)
Uh, nats?
planck units
is one version at least
is has some fun consequences for units relevant to us mortals though
"The universe does not care about what you prefer"
momentum is surprisingly human sized
it's the momentum of a single photon with planck wavelength 🥴
so it's, like, a basketball's worth of momentum in one photon
definitely enough to cook a chicken
(ignoring a factor of 2pi)
yea is in there
you generally wouldn't use quicksort for linked lists, since it requires random access
my guess is you can use insertion sort
the reason insertion sort is O(n^2) is because inserting in the middle in a vector (list) is O(n)
but for linked lists it is O(1)
so maybe you can take advantage of that
mergesort
regrettably, that is not correct
inserting at a cursor in a linked list is O(1)
but in an insertion sort you have to find the cursor first
insertion sort isn't N^2 because of actually inserting, it's N^2 because of how it selects the next element and swaps it into the correct spot
there is no efficient (O(n log n)) in-place sort on linked lists, but mergesort would work as a not-in-place one
treesort maybe
what about a heapsort?
if you're doing heapsort you might as well just collect the list into an array and do quicksort 😛
lol true
is remembering the code of the linked list or query important?
wdym query. generally you don't memorize code, just the ideas so that you can recreate it if necessary
need help in #help-broccoli if anyone is around
oh also i need help implementing the logic for removing items from my stack of free array index locations for a hashtable
any takers
what does this even mean?
why not do like I suggested and read all the non-header strings first and then convert?
pretty sure i got it. before i did not have the logic for removing an array index location from a free space stack after successful insertion for the chaining paradigm. eg, after successful insertion, i need to then go to my free space array and remove that location, so i don't select an occupied location and overwrite it
f.readline() # skip header
strings = f.read().split()
# ...convert strings to int
idk why you need that
don't you just pop from the stack when you actually need a new index?
its mostly to fulfill a requirement of the assignment, "use a stack to keep track of free space." stack here is just a list i am modifying as i go
i'd be glad to implement this approach if i had more info. i don't think this is enough for me to figure out. i will try rn
wdym?
just use readline to skip the first line
then read the rest and split on whitespace
some inputs have headers that are like 4 lines, and others dont
and the headers have nothing in common, like some marker?
maybe post an example
my inputs just begin with the data as integers. their input has some bullshit like:
`132.145 course name
required input
int1
int2
int3
...
`
i just need some conditionals within the try block, i think
to avoid headache i should just reformulate their required input lol
but they might test this w other files that are similar
is "required input" at the start of any input?
technically yes, it'd be as above, where it is the last thing i'd read before the keys
wedged in there between the keys and the course number line
I could add that line to my inputs, to normalize things
then i could readline until i come across "Minimal required input:"
i think
what would that code look like
so...does the data have that line or not?
their data does, my supplemental data does not, i could add it to make everything the same
i could add it to my inputs, yes
# Skip header
while True:
if f.readline().strip() == 'required input':
break
i'll try this now, ty
uhh
?
i'm really bad at coding, remember
i'm pretty good w the logic but my programming is severely lacking
ok that's working so far
just have to catch strings while casting them to ints? but they are all strings rn
ok
and why do you do the readline after the while loop?
i'd like you to assume that i am an extremely inexperienced programmer, and that any illogical series of things i post is due to lack of programmatic logic
i'm doing my best
ok so that is working to get all the keys
so during the process of casting them to int, i need to catch things that are alphabetic?
that's what i'm trying to do for error handling, anyhow
you can try-except that conversion
ok ok on it
it's more that I get the impression you're just throwing random stuff at the wall rather than thinking about what stuff does
i am already in a while loop, so i do not need another for string in strings? or i do
there is a bit of this, and scatterbrained-ness, that's why i drink a pot of coffee every day
like, the thing you want to do can be broken down into a few steps
# Skip the header lines
...
# Read the rest of the entries as strings
...
# Convert the strings to int with the error checking
...
can you fill in the code for the first two ...?
well i have both readline and read right now, and i don't know how they differ or can be used together, so i need to go read about that
readline reads the next line
The read() will read the whole file at once and then print out the first characters that take up as many bytes as you specify in the parenthesis versus the readline() that will read and print out only the first characters that take up as many bytes as you specify in the parenthesis
read without arguments will read the rest of the file
why would i want to break out of the while loop?
this tells me you don't really get the logic of what you're writing
well we know that
can you answer me this?
fill in the first ... part
we have a file object f
we want to skip past the header part
we already mentioned a way to detect the end of the header part before
this
yes
why would you want to break out of the loop you're using to parse the file
that's not the loop to parse the file
can you explain conceptually what the loop does?
only that first bit. it's going to continue reading the file via f.readline until it comes across that condition
which we know will exist
so it reads until the line we know is at the end of the header
correct
and then breaks the loop
so we need to begin a new while loop?
you'll need to read the rest of the file somehow
but there are easier ways to get the strings you're after
that's more sensible yes
note that rather than having 1 very complicated and error prone loop, this is 3 distinct and easy steps
oohh could i print the string that was detected to output???
breaking a complicated problem into simpler parts is important
wdym?
aren't you basically doing that already?
oh i see how
duh
oh this thing does not have scope of the output
this is a separate function outside of main()
i'll have to return some things from this to main()
will this work?
its not coloring like it will
no reason for the second as
i passed output to the getdata function
no need for args.output as output
here it is now
why do you have the second thing in the with?
oh its read by default huh
it's already an opened file
if you want to have the program exit if there is an error, why not just let an exception propagate through the program?
currently you catch an error, print as if you would stop the program, and then don't stop the program
ok so i can remove my syntax after except ValueError and then add a try block to my main()
and catch it there
ok so what are you suggesting
ok
then you know that's from you explicitly throwing such an exception
ah no i don't get it, they are all strings rn
wdym?
no
oof
you can create your own exception class easily
class MyException(Exception):
pass
outside of the getdata function?
why would you define the class inside?
idk, that's why i'm asking 😂
why would i want an exception that just passes
exception is a weird word
exce
it doesn't
the pass is just there since we don't define any methods or anything for the class
we can't just leave the body empty
because python
Anyone knows good study material/vids for ppl leaning DSA?
MyException inherits from Exception, so it has all the members and functions Exception has
so you can then do
def f():
try:
something()
except ValueError:
raise MyException('something went wrong')
def main():
try:
f()
except MyException as e:
...log e to file...
pins in this channel and youtube abdul bari
any exception not of type MyException will not be caught by the try in main
i think you are overestimating my abilities here
maybe I shouldn't bother trying to suggest good ways of doing things 🤷♂️
exceptions is such a core part of python, you should really learn them
they can simplify flow like this a bunch
what is the purpose of having your own exception class
have a guess
for custom exceptions
like, non ValueError or non TypeError
stuff
custom errors*
what's bad about wrapping my whole code in
try:
...
except ValueError:
...
bc ValueError could be many different things that may have gone wrong and its not explicit
yes, you will end up catching things you didn't intend to catch
and with your own exception class?
you can make it very specific to a single error or type of error
cool, that answers 
catching a generic error like ValueError is fine locally where you know what's going wrong
but having such a check globally will just end up hiding errors you didn't expect
better to have errors you don't explicitly handle terminate the program
that way coding errors will fail loudly rather than being silently ignored
and even in my example here I would do
def main():
try:
f()
except MyException as e:
...log e to file...
raise e
so that the program terminates with a failure, and with the exception printed to the terminal
so you can have your logging layer, but better to re-raise the error
alright let's try this bad boy
TypeError: catching classes that do not inherit from BaseException is not allowed
never seen this error before
ok this is what i have outside of main:
yes, you're not inheriting from Exception
oh i need a return
not an error, but lowercasing everything makes things hard to read
why do you implement logic in your class?
just inherit from Exception
wdym


what no
you define your own exception class
it doesn't need any additional logic than Exception has, it just needs to be its own type
then you can raise that exception just like you would any other
example here
that's what i was trying to do in the code i posted. i think i am not getting the inheritance thing
oh the inheritance is conferred by the raise keyword?
no
class MyException(Exception):
pass
MyException inherits from Exception
oh oh oh i see
i thought Exception was a placeholder for the exception i caught
what if i want to pass along the string i found in the input?
with open(input) as f, output: AttributeError: __enter__
you can pass whatever string you want to the exception
just remove output
that would require the class logic no?
no
Exception has that logic
(actually, maybe it's even BaseException, which Exception inherits from)
where does it take it as argument
huh?
no
when you inherit from class you get access to all its functions and similar
Exception has an __init__ that takes a string
!e
class BaseClass:
def f(self):
print(42)
class DerivedClass(BaseClass):
def g(self):
print('hi')
x = DerivedClass()
x.f()
x.g()
x = BaseClass()
x.f()
x.g() # does not work
@haughty mountain :x: Your 3.11 eval job has completed with return code 1.
001 | 42
002 | hi
003 | 42
004 | Traceback (most recent call last):
005 | File "<string>", line 15, in <module>
006 | AttributeError: 'BaseClass' object has no attribute 'g'
yeah what you wrote above makes total sense
so in my context, ValueError has access to the string which threw the error?
im not just seeing how to pass it along
the string which caused the ValueError
you have access to the variable with the string, no?
there is no variable associated with it yet, it's just this:
probably should have included this:
string is the string that caused the error, no?
yes
so what's the issue?
you probably want to add some formatting rather than just using the string as a message, but other than that you are using string as the message
I suspect you really want
except ValueError as e:
raise Myexception(string) from e
otherwise you'll get some weird looking errors
!e I mean compare this error
def f():
try:
int('not valid')
except ValueError:
raise Exception('other exception')
f()
@haughty mountain :x: Your 3.11 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "<string>", line 3, in f
003 | ValueError: invalid literal for int() with base 10: 'not valid'
004 |
005 | During handling of the above exception, another exception occurred:
006 |
007 | Traceback (most recent call last):
008 | File "<string>", line 7, in <module>
009 | File "<string>", line 5, in f
010 | Exception: other exception
!e to this
def f():
try:
int('not valid')
except ValueError as e:
raise Exception('other exception') from e
f()
@haughty mountain :x: Your 3.11 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "<string>", line 3, in f
003 | ValueError: invalid literal for int() with base 10: 'not valid'
004 |
005 | The above exception was the direct cause of the following exception:
006 |
007 | Traceback (most recent call last):
008 | File "<string>", line 7, in <module>
009 | File "<string>", line 5, in f
010 | Exception: other exception
this is technically fine, but I would either call exit with a non-zero argument (which means an error)
or re-raise the exception
ahh i see
i.e. you can do
try:
data = getdata(input, output)
except Myexception as e:
print(str(e) + ' was detected in the inputfile. Please check the inputfile and try again.', file = output)
raise e
it prints to console as well
exiting with non-zero error works fine too
oh wait nvm
exiting with non-zero at least signals something happens
the output file is wrong
is it?
re-raise the exception
then you can actually tell if that clause actually happens
(and this is why failing loudly is good)
i think this not being in my context manager is causing issue
that clause isn't in my context manager because the for datum in data loop is also not
is that wrong
share the relevant code
without context that stuff just seems weird
seems like output is a string there, which would be weird
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
well yeah
output is string there
it's before you open the file
(and the naming doesn't exactly help)
it's not an issue with argparse
the output should always be a named file keyed in at the command line
i thought it was the named text file, because just above i have
output = args.output
it's the name of a text file, sure
that's not what the file argument to print takes
ok so this whole thing goes in its own context manager?
or i put it in the one i already have
the whole try block
you could put things inside the context manager, sure
granted, I would probably break your main function apart a bit, it's starting to get quite long
bc the context manager comes after all of this
wait maybe not
maybe i could just put the try block? let me try 👀
nah that doesn't work
then data doesn't exist yet
weird it isn't exiting now
bruh. i am so bad at this
having the context manager wrap things wouldn't look so terrible if you only split things into functions
would you mind deleting this and DMing me
why?
so another student doesn't use it and make it look like we cheated or conspired or grabbed some random github code
if we turn in the same thing, one of us will be thought to be copying the other
you have the link already, right?
yes
ty
and rather than use this verbatim, i will study the logic
i feel like i am getting pretty close with my impl
it's just extracting parts into functions
so the main function doesn't get huge
that's fair enough
did you put this after the context manager or something?
i put the try block in its own context manager
separate from the one that exists later to write to out
well yeah
what did i do wrong
output is closed by the time you get to the except clause
because that's what context managers do
getting rid of the close() gives the same error
oh oh
you open the file with a context manager, that closes the file when the block is exited
i see what's wrong ok
ok think i got it
open opens as 'r' by default
yep
yeah i got it
and now it's writing to out. so now i have to test a non-error file
but at this point, you might as well put everything in one big with
yep, it's working fine
and then if that feels messy break things into functions
i could do that, eyah
yeah*
guess how many messages you've sent in here
that i return by searching for from: yourname
omega(10k)
right order of magnitude
in less than a year
I've been in this server since december last year
ah i see
do you like trivia?
i like trivia regarding large numbers. like random facts
its kind of remarkable you're able to go through and clean up code in the way you do. i guess you have a lot of practice
american lottery is up to US $1.9b.. 👀
You probably want (Euclidean) geometry, and algebra. Khan has courses for both.
I feel like everyone has the reaction "I kinda hate your code, I feel like I know better how it should look like" when seeing most other people's code
but yes, I've coded a lot, so doing small refactoring is kinda second nature
but then your default mindset is that of hatred. maybe you just see things for what they could be. an eternal optimist
i guess its the same with anything, when people get good at it.
eg, some pro golfer seeing someone else's swing and being like "this is crap and here is why and here is how you fix it"
eternal optimist feels like it misses the mark 😛
When working in a team an important thing to do is to realize that others will code in different ways and you kind of just have to let them, or go insane trying to correct them on everything.
I have opinions on roughly what I think good code should look like
oh i know lol i am painting it in a better light 😂
to some extent
@opal oriole are you in charge of software devs?
You can ofc have them run their code through a tool.
sometimes you should intervene in code reviews and say that the thing could be a lot cleaner if written differently
how do y'all feel about these coding tools like where AI will partially write something or auto-suggest
Yeah, but it's a balance, do it too much and it creates conflict.
like, I've had colleagues code that I genuinely didn't understand until I went through and re-wrote it
because it was a real mess of conditional logic
which could have been refactored into something way clearer
It's fine in concept, but legally, not so much.
if I can easily understand the code easily I rarely have strong opinions, unless it's doing something very silly
It's weird grey area that stuff.
Pretty much the idea, it's important to check oneself though, not bordering on OCD.
I'll be OCD about it only if you break our style guide 😛
outside that lies nuance
Yeah run through the tools.
style guide is not only tooling though
Then it's just the process and not so much personal conflict.
naming schemes, naming standards, restrictions on how you write stuff to keep things sane
Some things still require humans to look at it, but what is important is that the rules are written.
@sweet roost , @hearty python I had to go, but I thought yall would enjoy another way to parse lists:
def parse(text):
tokens = iter(
text
.replace(",", " ")
.replace("[", " [ ")
.replace("]", " ] ")
.split()
)
def _parse():
for token in tokens:
if token == "[":
yield list(_parse())
elif token == "]":
return
else:
yield int(token)
return next(_parse())
Both linear and recursive!
as an example, google's style guide for python
https://google.github.io/styleguide/pyguide.html
they list not only what the rules are, but the motivation behind them
so even if you disagree with some choice there is motivation behind the choice
(and if you really feel like it's wrong you can try to work to change the style guide by giving some motivation to the contrary)
it makes the style guide very long though 😛
Yeah, a written record of the changes and why is good.
It does take more work though.
and the google python guideline is still short compared to their C++ one
the C++ one has almost 30k words
the python one 12k
yeah, maintaining your own style guide only makes sense if your company is big enough
to warrant that extra cost
Otherwise, copy what you see already is generally the rule.
which works great if everyone was a good programmer 😛
actually, even with good programmers you have diverging styles
(And a big company will have varying skill levels)
i probably brought this up before, but do you all think anyone can code or it requires a certain type of thinking that is less learnable? like super left brained or whatever (if thats a real thing). could some masterful artist learn to code?
I think "anyone can code" is true at some basic level, but I don't think everyone has the capability of being a great programmer
people are quite different
Somewhat touchy topic. IMO anyone, and the reason why some seem to not be able is due to various other factors (their life situation).
"anyone" encompasses a lot
Probably the most important thing is just interest though.
nature and nurture
i don't think it needs to be a touchy subject. it's just a matter of inherent giftedness toward a particular topic. you'd probably not take a masterful programmer and make them some elite artist, as an example
yeah its just nature v nurture
you kinda need both to be "great"
Yeah and I think nature is pretty solid on average, humans are smart.
i feel this way as well about people
But not much can be done if i'm born in a place without food, let alone a computer.
the "average" part is very important there
your average person being capable doesn't mean everyone is
of course
I think the average person can learn to become a decent programmer, for sure
what separates a decent programmer from an elite one
I just don't like the "anyone" framing because there are always outliers
err, I feel that's a "I know it when I see it" kind of thing
fair enough
like, I do interviewing for my company, you can easily tell when people really know their stuff
like an example would be someone who wrote a great program or platform or app or something?
it was the same for me in biotech. surprisingly, most of them failed my easy question
I know google has numbers for number of applications, let me find them
one of those big techs puts a 6 month cooldown on you if you fail their interview
so this is from a few years back, they got ~3 million applications in a year
can you believe apple is now worth more than alphabet, meta, amzn, and another big tech combined??
jesus
and the acceptance rate is stated as 0.2%
that's still 6k people
Big companies get spammed.
Which is really annoying for those that know their stuff.
they have among the best percs though, so there is good reason. if you work at google and die your children get your salary until they turn 18
so order of magnitude thinking, 1/1000 gets hired, google has ~3 stages
CV screening (I'm guessing probably only 1/100 gets through that, it's very aggressive)
CV screening is automated?
phone interview (let's assume 1/10 gets through)
full interview (probably another 1/10)
are you tooting your own horn rn
the distribution is probably a bit different, but it feels about right
I'm just trying to put that number into context
many stages, pretty aggressive culling in each
well, lots of companies need programmers, even if they are mediocre
I have no clue how CV screening works at google, part of it is probably automated
data science to enable higher sales isn't exactly product breaking
considering 3M of them
data science at a tent selling company
realistically the CV screening is probably more aggressive than 1/100 
since anything after that point will involve a lot of costs
alright heres my CV: do i get through?
knows good stuff and such, and is a very stand-up individual
hired ✅
do you know the "you're hired" trick?
my CV is terrible looking and breaks so many "rules" people say you should follow
Automated screen is most likely keyword based.
(Some people tried just spamming keywords and it works)
thankfully I haven't had to go through a CV screening process
it's always been recruiters reaching out to me
ah man, i'm gonna need more python's and numpy's in there
In smaller companies it can often be someone reaching out to you too.
Especially if you have been posting things.
Doing your own interesting stuff.
the hired trick is this:
interviewer: "hello, what is your name?"
interveiwee: "hired."
interviewer: "you're hired?"
interveiwee: "great. see you Monday!"
ᶦ'ˡˡ ˢᵉᵉ ᵐʸˢᵉˡᶠ ᵒᵘᵗ
oh one more question
when trying to remove list indices from my stack
wait what are you trying to do?
i was unexpectedly getting p is not in list
calling remove is a big red flag to me
i'm trying to go and remove that array location to make the stack of free elements accurate
i have some degree of confidence it works, because i am inserting all the keys in my test case. let me try a larger input..
why do you need to remove the array location?
oh no
like you know when you need to add a new node
i just tried an execution and entered a forever loop 😭
ok so a smaller input size worked, keys = 36
what's that code even doing?
stack is a list of bucket locations
you try to insert at p (the primary hash) self.tablesize times?
sorry that's not the whole snippet
1s
oh duh
it only failed bc i didn't update my headers
bc they want that "stack" to represent the free bucket locations
unless you decided to use a deque for some reason
no, i still have a list named stack
it's O(n)
this was for k = 84 keys
correctness isn't the issue
and chaining
it's just super dumb from a performance point
is there a better way to pop from front?
pop() which pops from the end is O(1)
why do you want to pop from the front?
if you pop from the front and insert in the back it's not a stack even
no i'm aware. there is no insertion
if you just want to start picking low indices start with it being a reversed range
and pop from the end
granted, you are dealing with so small tables basically nothing matters performance wise
which kinda sucks
it's ok, i know why is it wrong
but going back to this, why would you remove?
i wonder if popping from the back would place things outside the artifical modulo constraint?
or sorry
popping the highest index first
so i am guaranteed not to pull that same index again
yeah, it does
what, how?
only for chaining. there is no probe computation
i could make the stack only up the the modulo integer..
oh, because you add table_size elements to the stack
right
they "become" usable
i am aware, don't remind me
had a total meltdown last night over it
doing hacks that shouldn't be needed to accommodate their terrible choice isn't a great idea
it only makes things more confusing
i just wonder if i should let the thing 'overflow' into using the indices beyond the modulo constraint
or just only make the stack elements up to that
so, I see why you need to special case the primary hash, but that's pretty terrible
correct
i've tried debating w prof over this for hours, she does not understand how its supposed to work
its painfully obvious
an O(n) cost for every insertion
huh
remove is O(n)
actually, I feel you can solve this nicer
instead of doing the remove, why not, when getting a new bucket, pop until you get a bucket that you know is free?
This is something I have run into before, where I have some idea in my head of how this kind of thing is normally done, but then they need some weird requirements which trips me up. I have to kind of clear my mind and assume that it's all broken.
given an index you could pretty easily check if the bucket is empty
uhh right now i have bigger fish to fry, i'm getting IndexError: pop from empty list
that is, of course, when i try an input that is one too big for the tablesize
i tried key = 121, tablesize 120
buckets size 1
i was just making sure my error checking still worked, it does not
wait, the chained one is the one exception where you can actually use all the buckets
by virtue of it being in the list, it is free. as long as i remove while inserting
yes
yes, that removal is very expensive
no i get that, but can we first solve my error
line 131, in chaininsert new_p = self.stack.pop() IndexError: pop from empty list
so weird
keep in mind, this is only for when there are too many keys to fit in the table
lms where i thought i was catching that
so you're inserting the 121th key and it fails?
ah i think i've got it. i had the error catching in one prog and not the other
CRAP
i got it working to catch the error but now it's not working for legitimate input. everything beneath my first context man is greyed out. 1s
is there some proper way to close a context manager maybe i dont know about?
huh?
what are you even doing?
the point of context managers are that they handle resources
i put my error catching logic in the same with open as the one we just developed
and it worked great, but now the code is never running past it, as i can tell from everything being greyed out
am i maybe missing a continue or pass or break or close() or something
i tried like output.close(), close(output) etc
why would you need to close it?
that's the point of the context manager
that you don't need to
i'll pastebin
it's clearly either some infinite loop or you unconditionally exiting in some way
dm'd
so option number 2
hmm so i am printing but also throwing a command line error
what if you want to avoid command line errors from displaying?
in what case?
so i'm not showing that ugly pop from empty list error
in the case that the keys > tablesize
that's an error in your code, no
yes
fair enough. i just feel like its cleaner if it runs successfully and they check the output and see the error message
rather than both
if anything, the command line is the place where errors for sure should go
maybe with a copy also logged to file
i'll just leave it, i've already spent way too much time on this today. i just need to be sure the core functionality is up and running
also i have not yet eaten and its 7:00pm
so, i need to do that.
ah crap
it doesn't work for the tablesize = 40 and bucketsize 3 case. let me check
so weird
linearinsert and quadinsert being basically copies of each other is...not great
they have one difference which probably is a bug
i don't understand why this isn't working for the tablesize = 40 and bucketsize = 3 case
i'm computing the size of the table as (hashtable.tablesize*hashtable.bucketsize)
think i got it
btw, your linearinsert and quadinsert code doesn't make sense to me
why do you have like 4 calls to try_insert in it
in a sane world the whole linear insert loop is
h = primary_hash(key, self.primary_hash_int)
for i in range(self.tablesize):
j = (h + i) % self.table_size
if self.table[j].try_insert(key):
self.count += 1
break
```minor difference in the bad reality, different mod and a terrible if check
does this first try the initial hash location when i = 0?
you have to try the primary hash location first, before computing a probe
h + 0 is h yes
probing is to resolve a collision
but then i am modding by primary_hash_int again
i just dont know if that'll first check the h location
x % m % m is the same as x % m
ok
do you know what the mod operation does?
returns the remainder after dividing the two numbers
well yeah
but it puts the number in the range [0, m)
by adding/subtracting a multiple of m to put it in that range
if you're already in that range, nothing happens
hence why modding twice doesn't change anything
can i ask you something
how does the chaining ever get like a pointer to an empty location
i feel like that'll never happen
oh i does when we pop from the stack
but when we first arrive at a bucket, it'll never have a pointer to an open location
so why are we checking that
why are we following that and trying to insert rather

i'm trying to implement this now without screwing up my if h is outside the table check
the logic is just
- can I insert here? if so I'm done
- otherwise is there a link to follow, if so follow it
- otherwise we need a new bucket, link to a new empty bucket and follow the link
- go to 1.
i don't see any chains every occurring though
i don't think we'll ever arrive at a bucket that has a pointer. the only pointing we do is instant during our stack pop
wdym?
we assign pointers when we add buckets
if you have a collision you'll follow the link
i can't see it, i could be wrong
i cannot see it in the logic, yes. i'll check again
i just feel that when we first arrive to a bucket, it'll never point to an empty bucket. maybe that isn't the point. the point is so that we can follow the chain for lookups
it's more relevant when doing lookups for sure
in insertion it's just the terminating case
though it catches both the first insertion (primary bucket is empty)
and the insertion when we need to add a bucket
no need for the first if
i need it, there is a tablesize = 40 and modulo 41 case
you don't need the first if
second sure, because of that case
but not the first one
how
like, why can't we get something beyond the tablesize in that scenario
cause it'll mod to 40 in the highest case?
why would the first if matter for anything?
other way around
the only difference between those two is the probing
you could re-use the same code for both
right
(h + probing_fun(i)) % mod
where probing_fun(i) is i for the linear case and i*(i+1)//2 for the quadratic case
i'm moving slow bc i am updating two different scripts
right, you pointed that out before i recall
interesting that there is a case where we can go past the modulo constraint with chaining
i guess its "less broken"? idk
still, load factors have been a headache
i just calculate both
tysm for your help!!!!!!!!!!
huh. its no longer printing that stack error
ooh that was only for chaining, i am running quad probing nvm
slightly
the primary hash mod is still dumb
i still wonder if they want to overflow into that area, i will ask
oh its horrific
i totally did not have a full meltdown over this
the only way to get to the extra few buckets is chaining
right. at least there is that. but like, the notion of creating a bigger table is totally out the window w linear and quad
doesn't matter if you make a bigger table or not 😂
i know you still don't like that O(n) insert, i can fix that before due date
you just have to modify the way you get an empty bucket
you can easily check if a bucket is empty, right?
so you can pop until you actually get an empty bucket
that removes the need for the .remove
i would need the remove afterward though no?
its supposed to keep track of the free space, not the potentially free space
but yeah, its a great point considering the whole point of a hashtable is those O(1) operations..
I don't know of a way to keep a stack of free buckets that allows removals like the ones you would need
maybe a deque?
without getting these expensive removal costs
why would that help for removal?
you still need to find the element to remove
which is O(n)
i think list lookups are O(1) and linear search is O(n)?
oh i see why
bc im not removing by index, im literally finding and removing the element
ok ok
removing by index would be O(n) for deque too
deque is basically a linked list complexity-wise
removing by index is O(n) for a list?
even indexing is O(n) for a list deque
well
O(distance from either end to the index)
right ok
err
but then wouldn't hash tables not be as fast?
deque, not list
we're doing the same thing w a hashtable (indexing into the array)
indexing into a list if O(1)
then removal should be O(1) if i am removing by index?