#algos-and-data-structs
1 messages · Page 28 of 1
cool, you have a bunch of vectors as 2d arrays of different orientations
make them consistent
😭
this is only getting worse
delta2 : [[-0.00901958 0.01363654 -0.00146754 ... -0.01657163 0.0060845
0.02118858]
[-0.00940553 0.01323033 -0.00186024 ... -0.01695081 0.00568505
0.02077562]
[-0.00972705 0.01289152 -0.00218753 ... -0.01726658 0.005352
0.02043104]
...
[-0.00863173 0.01404424 -0.00107307 ... -0.01619038 0.00648558
0.0216029 ]
[-0.0094346 0.01319971 -0.00188983 ... -0.01697937 0.00565494
0.02074448]
[-0.00923187 0.01341318 -0.00168352 ... -0.01678021 0.00586483
0.02096152]]
w2 : [[0.38559615 0.42632125 0.37463161 ... 0.38468813 0.38713751 0.37752896]
[0.61714355 0.58645799 0.6268056 ... 0.60550274 0.61035616 0.6136253 ]
[0.47664232 0.5118125 0.48848415 ... 0.47984483 0.49998998 0.48995297]
[0.55637761 0.55994256 0.53588313 ... 0.56815227 0.55365194 0.55356773]
[0.41303066 0.44085103 0.4182787 ... 0.40267915 0.41795279 0.41178166]]
a1 : [[-0.03224576 -0.02904151 0.26345452 -0.11011576 0.0447146 ]
[ 0.29515814 0.2046597 0.37707235 -0.01133226 0.37961229]
[-0.32958044 0.30341332 -0.18244315 -0.07614565 -0.14522167]
[-0.35581132 0.30099153 -0.2283503 0.10371445 -0.37295197]
[-0.29651709 0.16144836 -0.1430846 0.39429631 -0.35032075]]```
everything except delta2 is inconsistent
delta2 is what we made y1 and y2 out of
y1 is the culprit
numpy handled actual 1d arrays differently
and it doesn't generally mix well with 2d arrays
yes, that's the issue
is y2's dimension good ?
how would I convert y1 so its exactly in the same format as y1
pick your poison
x[:, None]
x[:, np.newaxis]
x[None].T
x[np.newaxis].T
np.vstack(x)
np.array([x]).T
np.reshape(x, (len(x), 1))
The first one looks good
it's almost like you can search for basically your terms and get helpful stuff
https://en.wikipedia.org/wiki/Longest_common_substring
In computer science, a longest common substring of two or more strings is a longest string that is a substring of all of them. There may be more than one longest common substring. Applications include data deduplication and plagiarism detection.
these commands worked, y1 now is same as y2 but its still the same error ``ValueError: shapes (580,1) and (5,580) not aligned: 1 (dim 1) != 5 (dim 0)
[ 0.04889463]
[ 0.03298588]
[ 0.01796647]
[ 0.04818468]
[-0.0042836 ].....```
delta2 looks like that now
I'm betting one of w2 and a1 is 5 by something
cool, now make the matrix operations make sense
transposing matrices if you need to
does this make sense
delta1 = np.dot(w2.T, delta2).T * a1 * (1 - a1)```
does it achieve a different result?
@haughty mountain what do you think?
you know better what you're trying to calculate
does it perform the same function ?
I switched the order so the dimensions match
delta1 = np.dot(w2.T, delta2).T * a1 * (1 - a1)
delta1 = np.dot(delta2, w2.T) * a1 * (1 - a1)
have the same effect?
wdym? does one fail and the other doesn't?
if they are the same both should work or both should fail
and in this case both should be the same because of propetry 3 above
perfect thanks a lot
Ill try it
@haughty mountain one last problem 😭
delta2 and delta1 are different shapes
is it even possible to fix that
[ 0.91820069 0.05568194 -0.08495439 0.59877995 -0.31689019]
[-0.19139255 0.30183401 -0.28960028 -0.19520619 -0.42749143]
[-0.20138682 -0.11724268 -0.35015292 0.64441971 -0.25101793]
[-0.38085224 -0.03375838 -0.37037521 -0.32735989 -0.23145478]]
delta2 is [[ 4.79704108e-05]
[ 2.26191211e-02]
[ 7.99302688e-03]
[-7.39296118e-03]
.....```
no, these are just not compatible at all
go back and look through the formula you're trying to calculate and figure out what shapes should be, and then verify your data actually matches that
just trying to correct random shape errors without knowing what it's supposed to be is a fool's errand
yeah your right
I dont understand the formula properly either thats another issue
this is a backward pass algorithm for a ANN, do you happen to know about that
I do, but it might be better for you to just go through and try to understand the formula and the linear algebra going on
@haughty mountain hey, could I ask you something about the equation?
y2 is the supposed to be the predicted output
but which part of y2 is the input cell/output cell
def forward_pass(x1, y1, w1, w2):
# multiply input by first set of weights
z1 = np.dot(x1, w1)
# apply activation function (e.g. sigmoid)
a1 = 1 / (1 + np.exp(-z1))
# multiply hidden layer by second set of weights
z2 = np.dot(a1, w2)
# apply activation function to get output
y2 = 1 / (1 + np.exp(-z2))
# return predicted output and mean squared error
print("the length is", len(y2))
return y2, a1
``` does this look right for the forward pass/
idk if the comments make sense
in particular
# multiply hidden layer by second set of weights
so I'm gathering you have two layers, you apply one, apply activation function, apply the other, apply activation function
if so that looks ok, but idk why you want to return a1
also, you're not returning any mean squared error, are you?
# return predicted output and mean squared error
actually wait, what's y1?
y1 is the actual output
it's not being used anywhere
guys
def foo(n):
n += 1
why sometimes the function changes the value of n and sometimes it doesnt
that would depend what n is
n = 10
def foo(n):
for i in range(10):
n += 1
foo(n)
print(n)
this time n doesnt change
it still stay's 10
@agile sundial so when does it change
it depends on what n is
@agile sundial what data type it is ?
yes. n could have defined __iadd__ to mutate itself
so how do I know when it changes and when it doesnt
you need to know the type of n
Which types change ?
i don't think any std lib type would be mutated with += 5, though
can you point me to an article or wikipage so I can study this more in depth
I don't know how to google this question
what is there to study? just the operators? global variables?
if you're passing an int, n will never change outside the function
I wanna know exactly when does a variable change when we put it inside the function's argument
So what do I need to pass in for it to change
i'm not sure of any articles that talk about that
in order for changes in the object to be seen outside the function, it must be mutable, and you need to mutate it. with ints, += does not mutate the object, it returns a new int
So how do I mutate it
it needs to be mutable
so how do I make it mutable
you can't change the mutability of something, it's a property of the type
so what's the solution
n = 10
def foo(n):
for i in range(10):
n += 1
foo(n)
print(n)
this time n doesnt
what do I use instead of int
or is there no solution
generally, you just don't want to do this
doesn't C++ have a feature where you can pass in a reference to a variable inside of a function's argument
does python have something equivalent to that
yes, no
@agile sundial are objects of classes we create mutable ?
yes
thx
hello there,
I need some help. I want to process this bit array, but my aproach has a some syntax error.
The output should be something like this {[1:5],[0:6],[1:12],[0:8],[1:5]} So I would like to have the same keys multiple times
https://pastebin.com/chBbSpmp
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.
What are you trying to achieve at the last line?
create a dictionary like this:
{[1:5],[0:6],[1:12],[0:8],[1:5]}
what is it a dictionary of?
Well, it is a little bit complicated. I am listening a digital input, sampleing it fast.
This dictionary would be middle step. the output is a manchester code.. something like that: 10101001
based on that middle step i could decide if those bits means one or two digit
Then add to dictionary. Your res is a list. You need to initialize it as {} and do res[key] = value instead of res.append(key: value)
that will support key duplication?
and this will support key duplication right? 🙂
This does not contain keys at all in traditional sense, it's just tuples, one element of which we call "key" and another we call "value"
And of course the list will preserve order. I think this is the only property of it you care about.
yep, that is right.
thank you for that, I will test in a minute
nope, I got an error for that.
AttributeError: 'dict' object has no attribute 'append'
result = {} this is the declaration.
result.append((x, y)) that is the append
No, sorry, you want to have result be a list. Do result = [] just as you had before
hmmm okay, new error. index out of range.
https://pastebin.com/KmYEW6gB
Probably this part: bits[x+i]
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.
yeah, you iterate i with no upper bound
just add i + x < len(bits) into while condition and you should be fine
while i + x < len(bits) and bits[x] == bits[x+i]:
yes that is an issue too, but smth else is off also. i stay 0 somehow
https://pastebin.com/ka1pKPB8
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.
print i after the loop 🙂
I just noticed that you add to the list for every x, but you might want to only add element if the value differs
you can turn top level for into while and instead of adding 1 to x every time just add the length of the segment to x: x += i
But also, think something is not qute right.
https://pastebin.com/Zeqp4DJm
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.
this the output
seems good to me. I mean, it does what it does: for every bit starting from first 1 counts how many bits of the same value there are after it
yeah, the while cycle should give the next iteration of the for.
so step into the for, run until the numbers are the same, then contine the for from there
that is the algoritmy, but I am not sure python allow me to modify the cycle variable inside the loop.
for x in range(bits.index(1),len(bits)):
i = 0
while x + i < len(bits) and bits[x] == bits[x+i]:
i += 1
result.append((bits[x], i))
x += i
print("i: "+str(i)+" x: "+str(x))
print(result)
True, it does not. Formally it allows you to, but the change will be forgotten on the next iteration. Kinda like if range just returned a list of integers and you are iterating over it one by one.
You will have rewrite the outer loop using while.
something like this, but now index is out of range again.
x = bits.index(1)
while x <= len(bits):
i = 0
while x + i < len(bits) and bits[x] == bits[x+i]:
i += 1
result.append((bits[x], i))
x += i
print("i: "+str(i)+" x: "+str(x))
print(result)
Uhmm.. why x <= len(bits)? If x == len then accessing bits is a range error.
It should be just <
my mistake... but it still not as it inteded.
one pair should represent one constant chain. it should look like {(1:6),(0:7),(1:6),....}
https://pastebin.com/b8qFHDGw
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.
Weird. Works for me.
I forgot to empty out the result array.
I just throw my code into repl.
works like charm
what can i do to make this solution better
it doesn't seem optimal
instead of reflecting the whole tree, compare the left subtree of root with it's reflected right subtree inplace (don't create a new tree, you don't need it)
so```py
Definition for a binary tree node.
class TreeNode:
def init(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
return self.equal_reflect(root.left, root.right)
def equal_reflect(self, root1, root2):
'''basically equal(root1, reflect(root2))'''
if not (root1 or root2):
return True
elif not (root1 and root2):
return False
elif root1.val != root2.val:
return False
else:
return self.equal_reflect(root1.left, root2.right) and \
self.equal_reflect(root1.right, root2.left)
you can just dfs, no?
dfs in left subtree, make the opposite choice in the same tree, if leaves don't always occur at the same time it's not symmetric
(+ check corresponding values, but that's trivial to check as you go)
wait, maybe that's what you do 
the formatting is kinda botched on mobile
yeah ok, looks like what you're doing
wassup guys
ignore my messages
I am just trying to talk with others in room
but as you know I have to send at least 50 messages
to gain this feature
#c-extensions message
just wanted to add something to this, multiplication of complex numbers actually comes in handy when you need to rotate your vectors, usually by 90° etc, because that corresponds to multiplication by 1j (sign depends on how your board is setup)
so it isn't wrong necessarily wrong to multiply with complex numbers
another issue with using it is floats, which isn't really an issue for most purposes but once you get large enough or precise enough you'll lack precision
I doubt for most cases you'd care, but a tuple of ints has that over complex numbers
@robust dagger @lilac cradle
If you can calculate something over ints, go for it. Rotation by 90° is just (-y, x) or (y, -x) (depending on the orientation), so you can do that with ints too!
I tried to generalize geometry primitives in Rust. Point is represented by an array of objects, vector is array of group elements (and therefore if point elements are in the group they can be converted to vectors), if vectors element is the element of the ring then the scalar product is defined, and so on. I gave up eventually. The two useful systems I found is f64 for everything (just evaluate everything in floats) or i32/i64/f64 (store vectors as pairs of i32, calculate scalar products in i64, calculate lengths and angles in f64). Alternative to the last one is i64/i128/f64 or i64/f64/f64
Does every recursive algorithm has also an iterative solution
yes
If it was a question. If it was not then ignore my response.
@tranquil junco It was a question thx
does Every iterative solution have a recursive solution too
I usually just use complex for all my 2d mapping things
I've gotten used to it from AoC and now I think naturally in them
if I do need to I can implement a quick Coord/Point class for use but complex is just there most of the time so I use it
True
yes.
basicaly recursion replaces iteration.
very basicaly.
me trying to control myself to not say word "monad" 😓 💦 😓
just say "monoid in the category of endofunctors"
Just to make sure
every recursive function can be turned into iterative function
and every iterative function can be turned into recursive function
well, if there is an iteration, you can replace it with recursion.
and if there is a recursion you can replace it with iteration.
thx
Even digged out my old uni notes for this 😄
did u finish uni
years ago 🙂
how's the job market for python
it is okay but theres a lot more to a job than only python
what's the most popular language for jobs
prob python or anything web related
well that is depends on a lot of things.
What is your experties, where are you located....
imho if you can code, understand basic structures, know oop, then the language is secondary.
(maybe c++ and assembly is an exeption)
I don't think c++ is an exception, but functional languages are
what's a functional language
And it's super easy to learn a second language so the choice of first lang does not even matter too much
I am totaly familiar with the job market, I only focused on embedded stuff.
Here is c++ and python
and I am located at hungary, which is basically the india of the EU
@tranquil junco for what hardware do you guys work
depends on the language I feel
What are they used on
sorry that I can't answare.
I will stand by comparatively "easy", but I guess the frustration will depend on the languages
comparatively easy sounds better yeah
definitely much smoother if you already have some experience
rather than going in raw
lol
import json
import colours
single_survival = {
# difficulty
'enemy_num': 20,
'tracking_enemy_num': 1,
'size': 15,
'player_speed': 10,
'max_player_health': 100,
'tracking_enemy_speed': 10,
'tracking_enemy_health': 20,
'enemy_min_speed': 5,
'enemy_max_speed': 8,
'enemy_damage': 5,
'tracking_enemy_damage': 5,
'prey_heal': 10,
# colours
'background_colour': colours.black,
'player_colour': colours.white,
'enemy_colour': colours.red,
'tracking_enemy_colour': colours.dark_green,
'health_bar_active_colour': colours.red,
'health_bar_inactive_colour': colours.dark_red,
'timer_colour': colours.grey,
# appearances
'player_trail': 50,
'player_trail_fade': 0.1,
'enemy_trail': 0,
'enemy_trail_fade': 0.1,
'tracking_enemy_trail': 50,
'tracking_enemy_trail_fade': 0.1
}
with open('survival_settings.json', 'w') as settings_file:
json.dump(single_survival, settings_file)
TypeError: Object of type Color is not JSON serializable
apparently i can't use pygame.Colors in a json file, how do i work around this
@fiery cosmos Idk exactly how JSON works but I reckon you can store an array of colors, [r, g, b]
and if you can't store an array just store the R,G,B as ints
ye that's a good idea thanks, but im looking for a way to store the objects directly because i don't want to change my code
import pygame
# neutral colours
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
grey = pygame.Color(238, 238, 238)
champagne = pygame.Color(250, 234, 203)
brown = pygame.Color(128, 96, 77)
# bright colours
red = pygame.Color(255, 0, 0)
orange = pygame.Color(255, 165, 0)
yellow = pygame.Color(255, 255, 0)
green = pygame.Color(0, 128, 0)
blue = pygame.Color(0, 0, 255)
indigo = pygame.Color(75, 0, 130)
violet = pygame.Color(238, 130, 238)
# dark colours
dark_red = pygame.Color(152, 0, 0)
dark_orange = pygame.Color(152, 69, 0)
dark_yellow = pygame.Color(167, 127, 3)
dark_green = pygame.Color(12, 137, 0)
dark_blue = pygame.Color(0, 75, 131)
dark_grey = pygame.Color(70, 70, 70)
in that case write a function that does this for you automatically
def get_array(color):
arr = []
for val in color
arr.append(val)
return arr
and use the function on every variable
I don't think you can store objects in json
isn't this just the list function
what
If you need the objects themselves, why don't you use the Pickle module?
import pygame
colours = {
# neutral colours
'black': pygame.Color(0, 0, 0),
'white': pygame.Color(255, 255, 255),
'grey': pygame.Color(238, 238, 238),
'champagne': pygame.Color(250, 234, 203),
'brown': pygame.Color(128, 96, 77),
# bright colours
'red': pygame.Color(255, 0, 0),
'orange': pygame.Color(255, 165, 0),
'yellow': pygame.Color(255, 255, 0),
'green': pygame.Color(0, 128, 0),
'blue': pygame.Color(0, 0, 255),
'indigo': pygame.Color(75, 0, 130),
'violet': pygame.Color(238, 130, 238),
# dark colours
'dark red': pygame.Color(152, 0, 0),
'dark orange': pygame.Color(152, 69, 0),
'dark yellow': pygame.Color(167, 127, 3),
'dark green': pygame.Color(12, 137, 0),
'dark blue': pygame.Color(0, 75, 131),
'dark grey ': pygame.Color(70, 70, 70)
}
dw i solved this issue
(kinda)
I need to finetune my algo.
[(1, 6), (0, 7), (1, 6), (0, 7), (1, 7), (0, 7), (1, 6), (0, 7), (1, 7), (0, 14), (1, 7), (0, 7), (1, 6), (0, 8), (1, 5), (0, 8), (1, 6), (0, 53)]
this is the input.
How can I find the highest and lowest number? (ofc i can loop through on it and do min or max search)
Is there one line solution? There is min() and max(), but those are funky in this usecaes
highest and lowest out of which numbers though?
either way, min or max with the key argument, e.g. something like key=lambda tup: tup[1] to search by second element of each tuple.
the second one of each tuple.
min_value = min(array, key=lambda x: x[1])
that is the best I found.
no, you can't convert this into iteration```py
def traverse_tree(tree):
acc = [tree.data]
if tree.left:
acc += traverse_tree(tree.left)
if tree.right:
acc += traverse_tree(tree.right)
return acc
for example
Who's right here
you can with extra memory
def traverse_tree(tree):
stack = [tree]
acc = 0
while len(stac) > 0:
node = stack.pop()
acc += tree.data
if tree.right:
stack.push(tree.right)
if tree.left:
stack.push(tree.left)
return acc
but more generally you can simulate any program both on a turing machine (which is iterative only), and in lambda calculus (which does not have imperative operations at all and therefore there are no low-level iterative implementations at all) so those are fully equivalent
also "extra memory" is the same memory recursive algorithm uses for stack frames, so they both consume the same amount of memory asymptotically
There is no reason to talk about all of this unless "recursive" and "iterative" algorithms are formally defined though
recursion is iteration with stack
ok this is wrong and right at the same time on so many levels I won't even start
^
is there any reason to use onehot over dummy encoding with categorical variables for machine learning
i know you need dummy encoding to work with a linear regression model, does it have adverse effects on decision tree, random forest, etc.
what the hell is this
I understand Big Oh notation
i just dont understand how to prove if a function is the Big Oh notation of something
like in 2^9n <= c An >= n0 what is c and reversed An and n0?
its supposed to read "2^(9n) <= c for all n >n0"
what is n0?
the big O definition is, roughly, "f(n) is O(g(n)) if f(n) is eventually bounded by a constant multiple of g(n)"
the "eventually" means that the condition holds for all n above some threshold, the threshold is labelled n0
in this case no matter what n0 you choose, "2^(9n) <= c for all n >n0" can never be true because the LHS grows unbounded while the RHS is a finite constant
what is LHS and RHS?
it depends on the function
youre proving that in this case theres no way to choose n0 and c which work
or, rather, whatever n0 and c you choose you always run into a false statement, meaning your assumption that 2^(10n) is O(2^n) is false
"2^(9n) <= 0 for all n > 1" is not true
oh
2^(9) <= 9999999 for all 1 > n0
how does this not hold true?
n = 1 and c = 99999999
you arent choosing n
why did you make n0 = 1?
you are choosing n0 and c
no it doesnt
np
Hello, I have a task to be done that inclues working with json files.
The problem in this task is that I cannot use the 'json module'. I am only allowed to use 'collections' and 'itertools' modules.
And here is my next problem - these files are fairly big. I believe, this script will have to handle 20GB json file. I tried to use 'eval()' method, but I think it is a dead end, unsuitable for this task.
Could you help me with ideas?
Can you even read this large json file without dividing it first?
you are going to want some kind of outer structure, iterator over smaller pieces
you don' want to represent a 20 GB json file in memory all at once
Thank you for that reply
list(color) does the same thing
the version I have in my head is usually g(n) is in O(f(n)) if g(n)/f(n) ≤ c as n → ∞
which is usually good enough, in practice I don't really care about n0
That sounds very cursed - maybe they want you to implement a JSON parser from scratch?
Is the structure maybe something simple like "there's always exactly one JSON object per line"?
Thank you, I've did it already by using yield generator. Then I am finding indexes of brackets and using eval on that chunk of string
Sorry for my english im exhausted and im not native xd
eval doesn't generally work
an example: null is a valid value in json
I want to start learning Data Structures and Algorithms in python where do I start?
the channel pins have some resources
Parent class:
class GameMode(ABC):
@abstractproperty
def _settings_json(self): pass # settings file name
Child class:
class Survival(GameMode):
@abstractproperty
def _settings_json(self): pass # settings file name
Child of child class
class Single(Survival):
settings_json = 'single_survival_settings.json'
do i have to repeat the _settings_json variable in the child class?
I wonder... I bet I could figure this one out really Quickly...
What library are you using?
Oh I see I have it opened in the python documentation...
Optionally You don't have to if the object is going to return something but in this scenario you have to implement the method if the object is abstract (Rewrite the _settings_json variable multiple times).
not sure if this the best place to ask this, but I am wondering if anyone knows of any packages that can be used to fake c++ data types in python? I am converting some c++ code to python and need to utilise the fact that the data is a uint32. I whipped up a simple class myself class uint32(int) which overrides things like __add__ to keep the number less than 0xFFFFFFFF, but I was wondering if there already exists a package that does something similar? A google search got me nowhere (probably because the keywords match too many other things...)
!pipy sized-ints
huh?
oh
!pypi sized-ints
an ugly, but perhaps better alternative is to use ctypes
cheers, looks good enough. If I need to do more than just the simple bit of code I need it for I'll use it
you could just use python types and & 0xffffffff where needed
or use numpy types
Or use array.array objects of length 1.
I need help with this question:
Robert is given a string that ends with a character or a number. he has to check the length of the given string and append it to the end in such a way that if the original string ends with a number then the new string with the appended value must have the length of the string as the last two characters. if the string ends with a character then append the total length as the last character. also, the number to be appended must be a single positive digit. write a code to implement the given scenario.
Input format:
Input consists of a string
Output format:
The output displays the single-digit value to be appended. If the value is more than one digit or no digit is to be appended, then print −1.
Sample Testcase:
Input - abcdefghijklmnop1
Output - 6
Question so hard to read is that how it reads originally?
https://beautifulideas.net/arbitrary-transform-affine-profile/ does anything like this exist already
program idea - a bit beyond my skill level
- using a virtual machine to find and record a measure of support for deterministic transformation of any matrix to any other matrix
Take any deterministic transform- be it orthogonal or not. Constant Q or not. Reversible or not. Linear or not.
public static int compute(int n) {
if (n < 10)
return n;
else {
int number = 0;
for (int i = 1; i < 1000; i = i * 2) {
number += i;
}
return number;
}
}
why is this code O(log n ) ? is it because of i = i * 2 ?
this feels like a basic iteration until the for condition becomes false
feels like O(n)
track the value of i on each iteration
btw does i represent operations ?
i is a variable in the code
hmm, I know time complexity is n / time(or operations)
figuring out n in the code is easy
but what represents the "operations"
i * 2 ?
it's not a specific variable, it's the things your code does
the statement "time complexity is n" doesn't really make sense. n is just a common variable name, no other meaning attached.
Loops and recursion generally take O(however many times loop is ran)
Things like array length addition multiplication and a lot of other things are described as O(1) mostly unless the programming language functions in such a way that they are not
Then stuff like eval() or sort() depends on how it's implemented by the language these are bad examples but you get the point.
You get the total time complexity by multiplying any nested loops or recursion equivalents adding non nested loops and then taking out any constants and sums that are always less than the largest term
(The stuff about loops also applies to language functions that take more than O(1))
O(however many times loop is ran)
it depends on the operations inside the loop
The best way to understand it is to look at the mathematical definition. Tbf my explaination is kinda ass
Yes I go on to say that in a shitty way
I was talking about a loop without any operations inside it or constant times ones since O(n * 1 * 1...) Remains O(n)
Should have made that more clear
this is O(1) 
the amount of work done doesn't scale with your input n
hey, Can I put a repository about a math algo in here?
can a bipartite graph have a triangle?
I don't really like (just multiply nested loop) part, it's not generally true, e.g. consider
for i in range(n):
for j in range(2**i):
# do O(1) work
you didn't answer my question, can a bipartite graph have a triangle?
oh, I can't read 😅
wdym compare?
all subgraphs of a bipartite graph are bipartite, if that's of any interest
All aspects of being bipartite also applies to subgraphs, so you won't have much help from that
idk what other metrics could be relevant
connectedness? average degree?
idk
I may be stupid but this does not look like it would do O(1) work
well first of all it would error but I assume there is like a pass after the 2nd loop
that's just a placeholder for the inside of the inner loop
yeah, it's a placeholder for how expensive the loop body is
multiplying loops it's tempting to say
O(n) * O(2^n)
so O(n 2^n)
but in reality it's just O(2^n) overall
I mean, shortcuts are useful, you just need to know where they are applicable 😄
i.e. most simple rules don't work in general, you need to know when they break down
at the end of the day time complexity just boils down to a counting game
so you'll end up having to deal with sums of series and all manners of math fun
and recurrences !!
and that's what kinda dictates when you can take shortcuts
if you don't at least know the underlying math, you don't know when you can cheat and get away with it 😛
limits 🔥
asymptotics is a lot about how much you can sweep under the rug without affecting the end result
the only shortcuts that do work are master theorem and the fact that 1^k + 2^k + ... + n^k = 1/(k + 1) * n^(k + 1) + O(n^k) I think...
There are a lot of other small facts like harmonic series, but those are very limited
I mean shortcuts like what parts are fine to just ignore
oh I see
or stuff like when you can upper bound without changing results
Well, I guess you just need to manually compute a few difficult big Ohs and get an intuition
e.g. here upper bounding gives you the wrong thing
well, not wrong
but not tight
if it's not right it's wrong 😩
is that just 2^(n+1)-1 loops?
exactly, you need to do the dirty work a few times before you have solid intuitions
yep which is O(2^n)
hey there,
Can I ask some help with syntax?
max_item = max(temp, key=lambda item: item[1])
I an array, what looks like this: [(0,5),(1,7),(0,6),(1,2),(1,8),(0,10),(0,12)....]
How can I modify my lamdba so that only took the max from where item[0] is 0?
Filter out everything you don't need
max_item = max(filter(lambda x: x[0] == 0, temp), key=lambda item: item[1])
or with generator expression:
max_item = max(y for x, y in temp if x == 0)
What is your opinion? whichone is more accepted? I mean for code look, readabilyt?
I would say the generator expressions (the second one) is more widely used in python because lambda syntax is honestly 🤮
https://www.hackerrank.com/contests/algomaniac-2023-prelims/challenges/playing-with-numbers-18
this was my solution which TLEd on some of the test cases
from math import factorial
t = int(input())
M = 998244353
arr = [[0 for _ in range(6000)] for _ in range(200)]
for s in range(6000):
arr[0][s] = 1
for n in range(199):
arr[n+1][n+1] = 1
for a in range(n+2, 6000):
arr[n+1][a] = arr[n+1][a-1] + arr[n][a-1] - arr[n][a-1-(n+1)]
for _ in range(t):
n, S = [int(i) for i in input().split()]
print((arr[n][S] * pow(factorial(n),-1,M)%M))
is the algorithm suboptimal or is using python the bottleneck
thanks
so I have a question to see if I understand an algorithm correctly? Specifically at line 13/15 we have a recursion where either the **low ** or high is replaced by mid -1 / +1, and because it is a recursion the program starts running again from line 6 right? and basically recursion occurs until line 10 is true? did I understand it right ?
public static boolean binarySearch(int[ ] data, int target, int low, int high) {
6 if (low > high)
7 return false; // interval empty; no match
8 else {
9 int mid = (low + high) / 2;
10 if (target == data[mid])
11 return true; // found a match
12 else if (target < data[mid])
13 return binarySearch(data, target, low, mid − 1); // recur left of the middle
14 else
15 return binarySearch(data, target, mid + 1, high); // recur right of the middle
16 }
17 }
yesn't as far as I understand this this will also possibly terminate on line 6-7 if no match is found
it would be bad for the algorithm to run indefinitely if there is no match
are if statements somehow more suitable for any task, than a match statement?
eg, can I use match all over the place, and replace if entirely with it?
the context is basically me answering to a statement that basically says "loops multiply"
in terms of time complexity
but it generally doesn't, you need to consider the series itself
guys I have plotted these functions on a graph but they are all very close to each other, i still listed them in order and the website still gives me "wrong answer" label
whats the right way of checking growth speed of a function?
mathematically
but not at the same rate
if one function is linear, and one function is quadratic, the quadratic one grows faster. that's what big O is measuring
yes but i am not really good at log thing
i will go watch a youtube tutorial about it
👍
I got 1 question wrong though
this
I got no idea how i am supposed to solve it
is there a way to calculate this or am i supposed to memorize the answer?
do you know the time complexity of merging two sorted arrays?
now yes, nlog(n)
just the merge step of mergesort, not sorting in general
no
huh?
the code is kinda confusing
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]
merge_sort(left_half)
merge_sort(right_half)
i = j = k = 0
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1
while i < len(left_half):
arr[k] = left_half[i]
i += 1
k += 1
while j < len(right_half):
arr[k] = right_half[j]
j += 1
k += 1
return arr```
how can i figure out the time complexity of this?
you don't need to find the time complexity of merge sort. you only need to care about the merging part
I got this from chatgpt 💀
well
# this seems to be the merging code
while i < len(left_half) and j < len(right_half): # n
if left_half[i] < right_half[j]: #n
arr[k] = left_half[i] #n
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1
while i < len(left_half):
arr[k] = left_half[i]
i += 1
k += 1
while j < len(right_half):
arr[k] = right_half[j]
j += 1
k += 1
return arr```
indeed
worst case 3n^2 * k?
k sorted arrays, each with n elements
oh, you're talking about the problem. i was just asking about the merge part of merge sort
this is the merge part 
well yes, but n and k are defined for the problem, not the merge part
it's O(n k^2) right?
n^2 k doesn't make sense
e.g. merging k=1 lists isn't n^2
they also give a pretty suboptimal method, lol
hi
i have a question
why doesnt this code run anything
import arcade
import random
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
COLORS = [arcade.color.BLUE, arcade.color.FANDANGO_PINK,
arcade.color.FRENCH_ROSE, arcade.color.GOLDEN_POPPY]
random.choice(COLORS)
class Cercle:
def init(self, radius, centre_x, centre_y, color):
self.radius = radius
self.centre_x = centre_x
self.centre_y = centre_y
self.color = color
def draw(self):
arcade.draw_circle_filled(self.centre_x, self.centre_y, self.radius, self.color)
class MyGame(arcade.Window):
def init(self, width, height):
super().init(width, height, "fenetre")
self.list = []
def setup(self):
for _ in range(20):
rayon = random.randint(10, 50)
center_x = random.randint(0 + rayon, SCREEN_WIDTH - rayon)
center_y = random.randint(0 + rayon, SCREEN_HEIGHT - rayon)
color = random.choice(COLORS)
cercle = Cercle(rayon, center_x, center_y, color)
self.list.append(cercle)
def on_draw(self):
arcade.start_render()
for cercle in self.list:
cercle.draw()
def main():
my_game = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT)
my_game.setup()
arcade.run()
nvm its all good
i do have another problem tho
import arcade
import random
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
COLORS = [arcade.color.BLUE, arcade.color.FANDANGO_PINK,
arcade.color.FRENCH_ROSE, arcade.color.GOLDEN_POPPY]
random.choice(COLORS)
class Cercle:
def init(self, radius, centre_x, centre_y, color):
self.radius = radius
self.centre_x = centre_x
self.centre_y = centre_y
self.color = color
def draw(self):
arcade.draw_circle_filled(self.centre_x, self.centre_y, self.radius, self.color)
def draw_circle_filleddis
class MyGame(arcade.Window):
def init(self, width, height):
super().init(width, height, "fenetre")
self.list = []
def setup(self):
for _ in range(20):
rayon = random.randint(10, 50)
center_x = random.randint(0 + rayon, SCREEN_WIDTH - rayon)
center_y = random.randint(0 + rayon, SCREEN_HEIGHT - rayon)
color = random.choice(COLORS)
cercle = Cercle(rayon, center_x, center_y, color)
self.list.append(cercle)
def on_draw(self):
arcade.start_render()
for cercle in self.list:
cercle.draw()
def main():
my_game = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT)
my_game.setup()
arcade.run()
main()
i have this code which just generates 20 circles
and now i need to do that if i right click on one of the circle, it changes its color and if i left click, it erases it
it's just asking if there is some input that will give true
it's just the SAT problem
Something like this I’m guessing
ik how to do it on paper but I’m not quite sure how to code it
Hello
I work with a netcdf file and data from a lidar, I have to calculate some parameters all parameters have 2 dimension (time and height) with time = 2543 and height = 324
I'm a beginner in python and xarray, I'm a bit lost, I know about the structure of xarray data. Can someone help me to complete the script.
mol_retrodiff and part_retrodiff there are nan values
- calculation of the extinction by knowing the molecular and particulate backscatter using
mol_retrodiff = mol_data['mol_retro'].sel(time=time, height=height)
part_retrodiff = part_data['part_retro'].sel(time=time, height=height)
extinction = -np.log(mol_retrodiff + part_retrodiff)
- calculation of the extinction coefficient knowing the retrodiffusion
#Selection of the backscatter variables and the distance using the sel() method in the path of the ds file
range = ds.range.sel(range=slice(0, max_range))
retrodiff = ds.total_backscatter.sel(range=slice(0, max_range))
extinction = -np.gradient(np.log(retrodiff), range)
-
calculation of the optical thickness from the extinction and the retrodiffusion
extinction = ds.['extinction']
retrodiff = ds.['retrodiff']
optical_depth = -np.log(extinction/retrodiff) -
calculation of the lidar ratio knowing the backscatter and the extinction
backscatter = []
extinction = []
lidar_ratio = backscatter / extinction
Hey folks, I'm new to Python, coming from JavaScript
I have the following code:
result = {"today": "RED", "tomorrow": "WHITE"}
if any(item in ("WHITE", "RED") for item in result.values()):
payload = {
"today":
"tomorrow":
}
I'd like to have "today" and "tomorrow" keys filled again with their respective values
How would I approach this?
There is no way to have scoped variables inside if statement?
Do you know if there are some libraries that I can use to verify whether a mathematical expression is valid or not without executing it?
For example,
1 + 1 * 4 is good
1 ++ 3 is bad
1 +3) is bad
sin(10/3) + ((10)) is good
xyz(10) + 11 is bad since xyz is not known mathematical function
has anyone taken coursera course?
it is so weird
the assignment is so hard
like how the hell am i supposed to solve this?
It's not clear what you're stuck on. Some of the code you included is not valid Python syntax (ds.['extinction']), and some of the other code seems to be using a package I don't recognize (what is .sel?). It would help if you post your whole script. Format it properly, and tell us what's wrong with it.
!codeblock
@robust dagger I mean ds['extinction']
Sorry
.sel to select
you mean just writing code to multiply the numbers?
do you know how to multiply two numbers by hand (digit based)?
if so you should be able to make it into an algorithm
many people don't know how
are u supposed to write karazuba?
how?
how to multiple by hand
isn't this something you learn at very early age in school?
they taught us FFT in the second grade
of course I didn't mean karatsuba
but the naive O(n²) algo
i.e. the thing that boils down to
123*456 =
100*456 + 20*456 + 3*456 =
45600 +
9120 +
1368 =
56088
though usually written differently
i do not understand why someone would use binary tree instead of red black tree
is it just "this program is small enough that I feel too lazy to write some extra code" or something ?
Red black trees are subset of binary trees (so every red black tree is a binary tree). In reality literally no one except interviewers use binary tree as a data structure for ordered set. But generally binary trees can be used to, for example, represent mathematical expressions such as 2 + 2 * 2:
+
2 *
2 2
basically plain binary trees are not used for ordered sets
and even for ordered sets usually B-Trees perform better than red-black/avx/treap/splay-... trees just because B-Trees use cache more efficiently because they are not binary
so B-trees are what used in actual business projects ?
B-Trees or thier variations are used in databases, but in production the standard-library implementation is used. In C++ it's red-black tree for historical reasons, in Rust it's BTree, in Java it's also red-black
is it red black in Java due to TreeMap class?
I meant that you usually don't have to reimplement tree at all and just use standard library one, which is TreeMap in java, which is red black tree
gotcha
You will have to some something very specific if standard library implementation does not work for you
there are some guarantees that break if you switch from red-black to B-tree
stuff like pointer stability
True, but if you need pointer stability there are other ways to achieve it. Vector also does not guarantee it. As I said, you can't swap red black tree with B-Tree in C++ how because it will break everything, but generally as an abstract data structure "Ordered Set", the B-Tree and Red Black tree are equivalent and """"in theory"""" interchangeable.
the the case of C++ you can't change it (in part) because the standard overconstrains the implementation
(also why I really don't like python specifying that dicts are in insertion order)
(because that means you basically can't ever change that)
abseil has a bunch of map/set types that are interesting
An open-source collection of core C++ library code
and they break different parts of the STL constraints
in order to have better performance
@haughty mountain I'd like some help in implementing momentum to my algorithm, are you free?
I have a function
def update_params(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha):
W1 = W1 - alpha * dW1
b1 = b1 - alpha * db1
W2 = W2 - alpha * dW2
b2 = b2 - alpha * db2
return W1, b1, W2, b2
this is where I update my parameters
the momentum equation is
I have some sympathy for your point here, but I also feel like if the underlying implementation matters that much, then the code is probably performance-critical, so it's probably best done in something other than pure Python (like Cython or an extension module). But I'd be interested to hear a counterargument.
dict code is in C, or wdym?
Most uses of dictionaries are in the middle of a whole lot of Python stuff: Using them as basic key-value data stores, using they for keyword arguments, that sort of thing. And the overall performance of your Python script matters, sure, so the dict implementation needs to be fast. But if the difference between one dict implementation and another really makes a huge difference to you, then you are probably doing something that is better done in some other language.
idk, if we found a new sort of hash table that improves cpu usage by X% and memory usage by Y% but that doesn't preserve order, that would suck for python
free gains for basically all code that you cannot get
you could ask why there even was a change to the current tables if perf and memory indeed don't matter
Which is not that impossible, who knows what new processor architectures will offer. Hardware hash tables are definitely a possibility.
It seems extremely unlikely to me that there are big fundamental gains to be had, especially ones incompatible with preserving insertion order. Remember that the hash table part of a dict (dk_indices) currently stores only an index into an array of entries (dk_entries). Preserving insertion order is done by preserving the ordering of the entry array. It would be a very strange data structure where you couldn't store an index into an array.
that does assume you actually have an array of entries, for other types of hash tables this is generally not the case
flat hash tables vs node based hash tables
Given that we're talking about Python dicts, ultimately, you need to store at a minimum two PyObject *s, one for the key and one for the value. The hash also gets stored, too. I think storing indices and keeping all that data in a separate array is likely to remain a good idea, regardless of the underlying data structure.
that's actually what happens
well, sorta
Yes, that's what I was saying.
python raw dict implementation is pretty tricksy. lots of optimizations. and it's even better in 3.11 (and yet better again in 3.12)
no, I mean you don't necessarily get objects for the keys
Yeah, there's a special case for when all the keys are strings, but I didn't want to bring that up.
that's the usual case though 🙂
I can count on one hand the times I didn't use simple strings as keys
It's kinda a weird case because it's so intertwined with how Python actually works. Keyword arguments, that sort of thing.
I use things that aren't strings as keys all the time.
Tuples, frozensets, hashable objects...
I think everyone has different expectations. I remember someone once telling me that I was writing some really sophisticated code because I was writing classes with dunder methods. I guess he didn't notice the metaclass...
(The metaclass has thankfully since been replaced by __init_subclass__.)
Yeah, since 3.6. When I was able to transition to that, my life got better.
Metaclasses are overpowered for most applications.
cool beans. TIL something! I have one set of classes that I use frequently that inherit from a metaclass. hopefully this will make my code simpler. thanks man!
hello can someone help me with my code? im having an error using sortby with pandas
i have this function but it doesnt read the column from the input and returns an error
df = pd.read_csv('list.csv').fillna('')
sorted_df = df.sort_values(by=[column])
print(sorted_df)```
have you considered that it might help if you say what the error is?
I'm trying to understand why the inner for loop of the DP function must be decrementing.
The DP problem is return True/False whether you can reach the target by summing elements in the List. Each element can be used at most once.
def can_sum(nums: List[int], target: int) -> bool:
dp = [False] * (target + 1) # DP table representing whether it can sum up to value of index i
dp[0] = True # base case
for num in nums:
for i in range(target, num-1, -1):
dp[i] = dp[i] or dp[i-num]
return dp[target]
The part that left me questioning is for i in range(target, num-1, -1): why doesn't incrementing for i in range(num, target+1): work the same way?
My best guess is that if we increment the i, we end up using the same num more than once to calculate the sum which is not allowed. I just have hard time intuitively understanding this.
Consider nums = [1] and targert = 3
If the inner loop was iterating in the incrementing order after one iteration the dp would look like [True, True, False, False], but on the second iteration when i = 2 it will set dp[2] to True. Not what we want. The decrementing order makes sure that each number from nums is only used once. In my example we will just end up with the wrong answer.
Topic list for DSA interview :-
Pls help in outlining the list of topics from Python for a Data Structures and Algorithm interview. I am a fresher software developer aiming to get into MAANG companies.
just look at a typical undergraduate DSA course
my prof keeps saying "the big O-ness of x"
🥴
you should ask out loud
did you mean to say "asymptotics"?
(or "asymptotic behavior", but that's less convenient)
yeah I mean. he knows what he's talking about, it's just amusing
alternatively, say "akchtually, it's called the big Θ-ness of x"
(assuming it's actually a tight bound)
Can anybody solve this: https://pctc.cuttle.org/index.php?action=user_question&grq_id=635 ?
Ive tried a bunch but cant get it to work
I end up using gigs of ram when only 10mb is allowed
All i know is that its doable with pyhon
What's the best way to represent something like this in python? I was thinking a 2d array but I'd need some way to identify which foods associate with which indexes. e.g wasabi x pizza is at row 1 column 2, pizza x wasabi is at row 2 column 1, etc. and you can't really do that with a 2d array
I would use dictionaries from pairs of strings to floats. Learn about dictionaries and tuples if those are new too you, they are extremely useful
Yeah I know about those, can you give a concrete example? thanks
Sure, the one on the picture translates to:
exchange_rates = {
("Pizza Slice", "Pizza Slice"): 1.0,
("Pizza Slice", "Wasabi Root"): 0.5,
("Pizza Slice", "Snowball"): 1.45,
...
}
# use [...] to get or update the values:
snowball_to_wasabi = exchange_rates["Snowball", "Wasabi Root"]
exchange_rates["Snowball", "Wasabi Root"] = 3.0
# technically you should have written it like this:
snowball_to_wasabi = exchange_rates[("Snowball", "Wasabi Root")]
exchange_rates[("Snowball", "Wasabi Root")] = 3.0
# but python allows ommitting parenthesis in most contexts, indexing using tuples is one of them
# so the first one (without parenthesis) is shorter and more preferable
note that you cannot omit parenthesis in dictionary definition by the way
idk, just a piece of trivia
that would be ambiguous right?
Yes, otherwise you would have to do infinite lookahead to differentiate sets from dicts with tuple keys
fun
this is effectively a matrix so a 2d array is what I'd go for. preferrably numpy.
but dicts are fine too
kind of depends on how big its gonna be
for just a few entries a dict might be simpler
And how would you represent the food x food relationships with a 2d array?
Is there a good 'multi node linked list' / graph type? To expand my problem:
I have a list of modular analysis objects, some of them depend on the output of others (there is a dictionary passed to each module as it iterates once through the list). Each module has a name, and a list of the other module names that need to come before it in the list
I want to order the list of analysis modules by their dependencies of each other. To me, it makes sense to build what's essentially a dependency graph then flatten it back to a list rather than shuffling a list iteratively. Any general hints for a data structure to look up?
sounds like a directed acyclic graph / DAG to me
though i am not sure if it's actually acyclic in your case, but definitely directed.
topological sort
Yes, and the "flatten it back to a list" sounds loke topological sorting
it would need to be acyclic, in python at least
You can also look up dominator trees if the structure contains loops, or you need some complex analysis
oh, you don't mean actual modules, nevermind
yep, it will need to be acyclic
perfect, this is very helpful! Plenty of things to look up. Thank you!
DAG with topological sorting is exactly what I am looking for! Winner
hello I have a very simple question about a numpy.ndarray and was hoping to get some clarification on the behavior.
Although this is indeed not a numpy forum it's a common enough lib that I'm hopeful someone will know the answer.
I have this function
def f(x):
return 3*x**2 - 4*x + 5
```
I can call it like so
```
f(3.0) # output = 20
```
but I see I can also do the following
```
xs = np.arange(-5, 5, 0.25)
# where xs equals [-5. -4.75 -4.5 -4.25 -4. -3.75 -3.5 -3.25 -3. -2.75 -2.5 -2.25
# -2. -1.75 -1.5 -1.25 -1. -0.75 -0.5 -0.25 0. 0.25 0.5 0.75
# 1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. 3.25 3.5 3.75
# 4. 4.25 4.5 4.75]
ys = f(xs)
print(ys)
# output equals [100. 91.6875 83.75 76.1875 69. 62.1875 55.75 49.6875
44. 38.6875 33.75 29.1875 25. 21.1875 17.75 14.6875
12. 9.6875 7.75 6.1875 5. 4.1875 3.75 3.6875
4. 4.6875 5.75 7.1875 9. 11.1875 13.75 16.6875
20. 23.6875 27.75 32.1875 37. 42.1875 47.75 53.6875]
```
what I don't understand is where is the iteration happening? is it builtin to numpy?
yes
broadcasting
broadcasting perhaps
Map the names to integer indices
The foods are really just labels for the rows and columns
yeah. i would recommend mapping them with a hash function, then use the result of the hash function as an index
but the larger the dataset the more hash collisions you will have...
well you have a fixed number, you can make a perfect hash function
how do you assign the hash to strings then? just guess the function and use it?
with a dictionary of course!
mapping = {
"pizza": 0,
"wasabi": 1,
"snowball": 2,
"shells": 3,
}
sounds like we have gone full circle but ok
yeah i mean my original statement was joking. it's basically constructing a hash map
I guess one hash table + array lookup is better than one big hash table + no lookup
but actually, you can create a perfect hash function since the universe of keys is finite (and feasibly small)
i want to hit my wall because of recursion
public int doTree(TreeMap<Integer, Integer> tree, int n) {
tree.put(n, n);
if(n <= 0) {
return n;
}
else {
return doTree(tree, n - 2);
}
}
can someone help me understand why this returns -1 and not 9 when the basefall is met. If the initial n is set to 9 ?
my understanding is that it is put 9,9
then 7,7 ; 5,5 ; 3,3 ; 1,1 ; -1,-1
9,9 would be the bottom of the stack, and -1, -1 is the top right ? so we take away the top until we reach the bottom of the stack. Then the program executes.
that's a meta joke that is subtle enough to just whoosh over most people's head, neat
are you familiar with factorial?
what is n when the base case triggers?
that's what will end up being propagated all the way up, as the function is written currently
this doTree function is quite strange tbh
it is
i was some time ago, then I forgot it all 😄
it's just a recursively defined function
f(0) = 1
f(n) = n * f(n - 1)
i brought it up because i thought it might help you understand how doTree works
im going through the regular expression code, just wondering is there any reason other than readability that itemsappend = items.append and sourcematch = source.match exist?
it's a microoptimization
interesting, so it sort of caches the method calls so the interpreter doesn't have to check if source and items have those respective methods each call?
I'm working on an assignment and we're given graphs in the format C = [[1,2],[3],[3],[]] where node 0 would have children nodes 1 and 2, node 1 would have child node 3, etc. Is there any simple way I'm not thinking of to get all parents from a given node? i.e. given node 3, is there a way to simply find all parents of 3 (which would be 1 and 2)?
pretty much. every . operation is a dict lookup, which is like a few us each
not without either changing the data structure, or checking every node
so basically the method returns -1 through each stage because that was the n when basefall was achieved ?
dang, no shortcuts for me
the base case returns -1, the other returns return whatever the function returned
try to implement factorial recursively. i think it will help a lot to understand this
so by extension you just return -1 all the way down
Hi, I have this error in my code I don't understand
https://github.com/D4NyWoW/Algorithms-Data-Strcutures/blob/main/dynamic_arrays.ipynb
error in cell 11 i presume?
your DynamicArray remains to be the one defined in cell 1 (which is now nowhere to be seen)
i accidentally removed two arrows oops..
look at the two purple-ish arrows, you can see there is a typo
did you execute the cell after you modified the code?
@vast adder this is the final state after adapting it to what i needed it for ```py
def get_cells_at_radius(radius: int):
for n in range(radius):
abso = abs(n - radius)
yield center_cellx + cell_width * n, center_celly + cell_height * abso
yield center_cellx + cell_width * abso, center_celly + cell_height * -n
yield center_cellx + cell_width * -n, center_celly + cell_height * -abso
yield center_cellx + cell_width * -abso, center_celly + cell_height * n
nice!
it's still compatible with the rotating approach i think | actually, maybe not, you can't rotate the pixel because of things that are not what i said lol triple edit
hopefully it's fast enough 😉
should be
if it's not i'll use your numpy function
thanks for the talk! it was very interesting
np 😄
how would i write a code that checks for duplicates in a list, and for every item thats a pair, returns the pair plus one? for example, if i had [flower flower flower flower pig pig cow cow] it would return: [flower flower flower flower flower flower (extra 2 because tehre are 4 flowers aka 2 pairs) pig pig pig cow cow cow]
?
Not sure if your trying to make a joke here or are being serious. Ife we just went with a dict there are problems: If you just use a dict of lists, how do you index the list? If youre trying to make a dict of dicts... its going to be very tedious to create and wastes lots of memory.
Also if you use a 2darray there is the added benefit that you can pass around the indices as numbers ("foodID" or something) and save computation time by indexing directly
What did i do wrong?
this is meant to be 0n
it specifically fails the full array test case
Im thinking python thinks that -1 is +1
i gotta start from i=1 right?
voila !
well you would use a dict of dicts. the memory usage is the same asymptotically. it's probably easier to create because you don't need to do any mapping to indexes yourself. the computation time of using strings vs ints is also asymptotically the same
Asymptotically yes but that's not all that matters. Also it's just much cleaner to represent a matrix as a 2d array. In the end both is fine and up to preference but I much prefer the array, at least for large data sets
it's cleaner if you have indexes, but you don't. and there's no simple one to one mapping in this case
how would you add another item to your nested dicts? You'd have to loop over the entire thing, then add the new entry to each dict. That does not seem very clean at all. Your duplicating the same data n times for n entries
huh?
to add a new item, you need to add conversions for the existing items. you would need to do this for any representation
how do i get into learning algos?
bc my cs teacher told me to compete at the swiss olympiad of informatics and i am not that good with algos yet
usaco might be an interesting resource
it's the US olympiad site
they have a bunch of training resources
(and yes, you are allowed to register even if not in the US)
is there a data structure in python that works like a queue (FIFO), can be limited to a maximum length (will remove excess items) and when an item is enqueued thats already present in the data structure it will be moved to the start of the queue?
Yes, python has collections.deque that can do just that - you can do .append() or .appendleft() and it supports having a maxlen
For more information you can see it here https://docs.python.org/3/library/collections.html#collections.deque
but deques dont normally move an element to the start if its inserted twice
I guess im just gonna check wether its already there and remove it
sounds like an LRU cache
yeah thats pretty much exactly what im trying to do
thank you
I've got a question
can somebody help with a simple problem I'm stuck on
Essentially, I'm trying to get a max value from a list and find a corresponding element in the same row index of that list
i know how to get the max value
but I'm not sure how to find a corresponding row element
class Piece(ABC):
@abstractproperty
def moves(self): return self._moves
@abstractproperty
def image(self): return self._image
i want to enforce the users to define moves and images in child classes, is this the way to do it?
^ pls help
sorry what do you mean by a corresponding row element
code example of how your data looks? it's clear what you mean exactly
it sounds like you're talking about a single list, but then there is no "corresponding" element
I think yes? But it might be better to ask in #python-discussion rather than here
for small questions general is fine, and this for sure isn't the right channel 😛
Since you say "child classes" this means you will need to define those child classes, so make some new classes that inherit from your Piece class.
@property
def image(self) -> pygame.Surface:
return self._image
@property
def moves(self) -> list[tuple[int, int]]:
return [(1, 0), (-1, 0), (0, 1), (0, -1), (1 ,1), (1, -1), (-1, -1), (-1, 1)]
i have done this for the queen class
it works, but kinda dumb ill ask in #python-discussion
the queen can move more than one square
well how is that gonna work for knights
same question for pawns
granted, idk what should be responsible for piece movement checks in a design like this 
yeah I'm actually confused. moves property and gen_moves?
Sorry to ask such a super basic question but... I'm scraping a site and I've decided to use a dataclass decorator for convenience to store the values. Now sometimes the data is a string but is really an int "the number here is 3 buried in some text" sort of thing.
In a pythonesque game engine script I use as a hobby Godot, I'm able to use setters / getters to parse this sort of data. So I could do the super convenient equivalent of number: int with the method def set_number(self, num): where I parse the str into an int within the dataclass. The end result is that I can put my_dataclass.number = "the number is 3" and print(my_dataclass.number) outputs the int 3.
It's really handy, I don't need to worry about any processing after the fact.
I suddenly realised that I'm not sure how best to do this in python...
Obviously I could just call the method my_dataclass.set_number("my_string") - but, it's less elegant than setting the attribute directly and having the class handle it.
sthn like this should work
from dataclasses import dataclass, field
@dataclass
class A:
_number: int = field(init=False)
@property
def number(self):
return self._number
@number.setter
def number(self, val):
self._number = int(re.search("\d+", val).group(0))
@jolly mortar Oh, that's absolutely ideal. Thank you so much.
in attrs you can just do
from attrs import define, field
import re
@define
class Foo:
number: str = field(converter=lambda s: int(re.search(r'\d+', s).group()))
x = Foo('my number is 123')
print(x)
x.number = 'my number is 456'
print(x)
Can't figure out this question,if there is no build-in function to create graphic object,then how pyplot does it using functions which consist of build-in functions?
they make their own functions
Ultimately, by calling various system-specific APIs (see win32api, say), but that happens way deeper - pyplot just uses a graphics library depending on the backend. Like, let's say matplotlib is set to use the tkinter backend, which is the interactive default. Tkinter itself is python bindings for Tk/Tcl, a C GUI toolkit (https://github.com/tcltk/tk). Somewhere in the depths of Tk, there must be calls to platform-specific functions to create windows and draw on them, but that's two layers of abstraction removed from matplotlib.
for idx, ele in enumerate(s):
if idx+1 < len(s) and translate[s[idx+1]]>translate[ele]:
sum+=translate[s[idx+1]]-translate[ele]
if s[idx+1] is s[-1]:
break
else:
sum+=translate[ele]
i want idx and ele to skip the next iteration if the first if statement is satisfied. how could i achieve that?
continue?
oh, skip the next iteration... set some flag, I guess, and at the start of the loop do
if flag:
flag = False
continue
is there no inbuilt method to do that?
no
To skip the next iteration after the current one? no.
if you need to skip around I would use a while loop
or maybe some iterator shenanigans
well, I guess one cursed way comes to mind:
it = iter(enumerate(s))
for idx, ele in it:
if ...:
# skip next iteration:
try:
next(it)
except StopIteration:
pass
😛
I don't get this loop. Why don't you just iterate over indices in range(len(s) - 1)?
and deal with the special case outside the loop
s = "MCMXCIV"
def romanToInt(self, s: str) -> int:
sum = 0
translate = {
'I' : 1,
'V' : 5,
'X' : 10,
'L' : 50,
'C' : 100,
'D' : 500,
'M' : 1000
}
for idx, ele in enumerate(s):
if idx+1 < len(s) and translate[s[idx+1]]>translate[ele]:
sum+=translate[s[idx+1]]-translate[ele]
if s[idx+1] is s[-1]:
break
else:
sum+=translate[ele]
return sum
print(romanToInt('',s))```
here's the code and testcase i'm debugging for the roman to integer leetcode problem
so,tkinter is a python library which uses Tcl language ?
there is also itertools.pairwise
i have no idea if it has any code in tcl-the-language, but it uses tcl-the-library, sure
which is what you want, as long as you move the special case outside the loop
tcl-the-library is a set of tcl language code isn't it?
Not sure what you mean
I also don't get the inner if, what is that even meant to check?
if it uses tcl library then it uses tcl language code,as tcl library is just a set of tcl language code sounds logic
@haughty mountain :white_check_mark: Your 3.11 eval job has completed with return code 0.
True
the last element, if the next element is the last element, it breaks the loop
that's not really what that's checking
check the code I executed above
do i not remember i had written this like 3 weeks ago 
the is checks if they point to the same object
but strings can be cached
especially small ones
should i make it == instead?
no
why not use the index?
fwiw, I don't even know why you want that if and break
i could try not using enum and just use index, yes
!d itertools.pairwise
itertools.pairwise(iterable)```
Return successive overlapping pairs taken from the input *iterable*.
The number of 2-tuples in the output iterator will be one fewer than the number of inputs. It will be empty if the input iterable has fewer than two values.
Roughly equivalent to:
```py
def pairwise(iterable):
# pairwise('ABCDEFG') --> AB BC CD DE EF FG
a, b = tee(iterable)
next(b, None)
return zip(a, b)
```...
for idx in s:
if idx+1 < len(s) and translate[s[idx+1]]>translate[s[idx]]:
sum+=translate[s[idx+1]]-translate[s[idx]]
if s[idx+1] is s[-1]:
break
else:
sum+=translate[s[idx]]```
like this
that's not what I meant
something like this, if anything
if idx+1 == len(s)-1:
break
oh yes, that's a very clever way to do it
but regardless what you really want to do is loop over pairs, no?
so you can use this
.
!cleanban 531843417207209985 porn
:incoming_envelope: :ok_hand: applied ban to @sage spire permanently.
sorry that other guy i pinged
I mean, if you're going to use that as your nickname... 😄
😔
Can you someone help me what is my mistake, struggling to make this program run successfully.
<ipython-input-1-99eedd1ab845> in merge(list1, list2)
9 i = 0
10 j = 0
---> 11 while (i < len(list1) and j < len(list2)):
12 if list1[i] < list2[j]:
13 list3.append(list1[i])
TypeError: object of type 'NoneType' has no len()
Merge Sort
'''
Given an unsorted list, sort using merge sort
Split by mid & repeat until the final condition is met
Merge the elements of sorted array recursively.
'''
def merge (list1, list2):
list3 = [] # Merged List
i = 0
j = 0
while (i < len(list1) and j < len(list2)):
if list1[i] < list2[j]:
list3.append(list1[i])
i = i + 1
else:
list3.append(list2[j])
j = j + 1
if i == len(list1) and j < len(list2):
list3.extend(list2[j:])
if i < len(list1) and j == len(list2):
list3.extend(list1[i:])
return list3 # Return Merged List
def mergeSort (ul):
Error Checking
if len(ul) == 0:
return None
def helpx(ul): #unsorted list
if len( ul ) <= 1: # Either 0 or 1 elements in an array, which is sorted as there is only a signle element
return ul
else:
mid = len(ul)// 2
#split the array
leftpart = helpx(ul[: mid])
rightpart = helpx(ul[mid:])
print(leftpart, rightpart)
merge(leftpart, rightpart)
helpx(ul)
Driver Code
ul = [7,3,5,88,44,55,67,77]
print(mergeSort(ul))
one of list1 and list2 is None
figure out how that None came about
and you'll find the bug
let me look into this further, thanks
I see two things immediately
this is not the None you see, but why are you doing return None in the len 0 case? that's not a sensible result
the None that you're hitting I'm pretty sure comes from helpx
look at what's being returned from there
(or rather what's not being returned)
or rather, what is being returned
||or rather, what should have a return but doesn't||
I have a log files, it might contain an error lines or not, i want to make a code that can understand each error line and print just a unique from each no need to duplicate
Example
Input file :
Leakage value 1.2 for circuit 1 is greater than the standard
1)Leakage value 0.9 for circuit 2 is greater than standard
2)Capacitance is huge in circuit 3
3)Capacitance is huge in circuit 4
4)Capacitance is huge in circuit 5
5)Capacitance is huge in circuit 6
6)High delay in circuit 7
Output: must be the unique ignoring instance information like circuit number or certain value
-
Leakage value 1.2 for circuit 1 is greater than the standard
-
Capacitance is huge in circuit 3
-
High delay in circuit 7
The log file may contain over than 10000 errors but not ,however its might be just 10 unique errors as shown in the output ,,,
Anyone can suggest a library, or a place to start from?is it possible to make code clever enough to determine these things ?
<@&831776746206265384> crosspost ( #algos-and-data-structs #unix #data-science-and-ml #type-hinting )
What should be the list of topics that are relevant for a Data Structures and Algo product interview. I am learning Python from scratch
Thank you @agile sundial adding "return" to the function helped! Also thank you @haughty mountain
Anyone can help for this
what's the best free resource to learn DS&A in python within a week in most easiest way possible if I already know OOPs?
if you haven't gotten an answer, could you please post again but with an example illustrating what is the expected input and output over at #data-science-and-ml ?
usually pandas question goes there, and they do usually get solved.
hi, i have a dataframe about movies, where the cast is separated by "-". so the cast column for the movie "The Dark Knight" looks like:
Christian Bale-Heath Ledger-Michael Caine-Gary Oldman-Aaron Eckhart-Maggie Gyllenhaal-Morgan Freeman
jupyter notebook doesnt display the entire cell, which is understandable.
the thing is when i say
x = df[df["title"] == "The Dark Knight"]["credits"].to_string().split("-")
x becomes
['357 Christian Bale', 'Heath Ledger', 'Michael Caine', 'Gary...'].
.to_string()? are you sure you weren't looking for .str.split("-")?
shit i was doing .str().split("-") and it wasn't working 😭 . now i can do list(x) to get all the values.
thank you
Is anyone interested to do leetcode questions together starting from easy level?
We can do by our own approaches and then have a discussion on concepts!
Let me know if anyone is interested!
Dm me
so i was wondering is it good to use 4 processors then 1? cause i saw a yt video saying it boosts your performance and stuff so i went to run and typed in msconfig and then i went to “Boot” and selected “Advanced options” where it said”Numbers of processors” do i have it on max or 1?
Could you not just read the log files text line by line and like add it into a set? Should work if I understand the problem
it just affects your boot, does not affect your performance while actually using the computer
was wondering if i could get some help with a simple string output that i need to do for my class?
It needs me to print a border around the text:
+---+
| h |
| i |
+---+
but i dont undestand how im supposed to do this?
not asking anyone to do it for me, just wondering how i'm supposed to wrap text in a border
can someone please give me advice on problem solving approach and logic building?
When I try to solve a problem, even if it's easy one i am sometimes unable to and that makes me feel like stupid and breaks my confidence. I already feel kinda insecure about it and there's always this "fear to start" that I am trying to overcome. Any advice should help. 🥲
Thank you
Hello
Does anyone know how I do it in the binance future api with python to stop the order.
That is, for example, let's say that you have profited 10 dollar. Oh I want to stop, what is the function of the python api to stop.
I've looked everywhere and tested and I can't find it. I looked on the GPT site but it doesn't say either.
I'm creating a bot to profit penny by penny, but it needs to stop the order when it starts to lose. That's what I'm not finding.
Here's the api documentation site:
https://python-binance.readthedocs.io/en/latest/binance.html
Dont worry. When you solve relatively large number of problems and some complicated ones. You will grow confidence. It actually used to happen to me too.
search examples on github
what channel would be the one to ask/discuss vscode+python. using those to in combination with a remote docker container via ssh to do development. have some questions regarding git integration..
Ohh okay. Thanks for the hope 🥺
a triple is when you pick three different elements from the array (e.g. the elements at index i, j and k)
the question is for how many i, j, k the values at the index sum to 0
i.e. how many choices of i, j, k has that a[i] + a[j] + a[k] = 0
(i < j < k)
e.g. a[0] + a[1] + a[6] = 30 + -40 + 10 = 0
Anyone have advice or resources for implementing symbolic algebra?
Specifically term rewriting on a graph structure
Even more specifically, what graph library to use or how to build a good one myself
Hey I need help with something please
How do you use the for loop to print the pattern??
I tried so many different ways but whenever i run my program, it always appears in like a straight line
Looking for an example where big O notation are counter intuitive for small n
But here (is ˋaˋ in list or set), even with small n (n=1), O(1) ‘wins’
Any simple example to demonstrate big O is only valid if the constant terms are not too bad?
(Without using time.sleep(1) as a simulation of a long constant term)
karatsuba for polynomials is pretty simple
it's generally difficult to do in python because the performance of simple instructions is cancelled by a poor general performance of executing a bytecode instruction. In compiled langs you can just show how linear scan beats hashtables or something. So maybe you can find this imbalance of O(1) instructions performance in python? For example you can show how hand-written binary search, which is O(log n), does worse compared to numpy np.any(a == x), which is O(n)? Feels a little like cheating though.
Well does feel like cheating if I’m using numpy which is C right?
Im gonna look it up
well that's another failing of Big O as a speed measurement. it doesn't take into account processor speed, overhead and stuff like that
Implementation of numpy is in C (and another relevant language), but the interface is in python. Idk, even if we don't consider it cheating, I would not have shown that either way. It may raise too many questions.
Manhattan is a square grid of streets. But on some streets (we remember which ones) roadworks are being carried out, so you can't drive through them. One day, his car broke down - now he can only turn right. So at each intersection he can go straight or right into the street. Thus, if we are currently traveling north on the street, we can either continue north or turn west onto the street. we can't turn east, and we can't turn south.
Devise an algorithm for us to find the shortest route from his position to his mechanic with his car broken down like this. Both locations are at the intersection. His car at the beginning is heading north.
how would you guys find a linear solution to this algorithm
i though about at each vertex stroing the amounts of times we turned
or we could orient the graph so each side is like --> <-- and we can only pass once and then just do bfs/dfs
Can anyone tell me why this passes? Its O(N) worst case
Because leetcode is trash. Verify your code yourself before submitting.
Ok, the real answer is because on leetcode your solution only runs once, and since data creation takes O(n), your solution also can run in O(n). If they did O(n) sample and O(q) requests, then naive O(nq) would have been too slow. But for some reason leetcode does not do that. Probably to emulate a "real" coding interview problem or something.
i don't see why this is a leetcode issue. they just don't have large enough test cases or the problem constraints are too easy. it's impossible to verify what time complexity a solution has automatically
which i guess is a leetcode issue, but not because of how they run the tests
Codeforces solved that by having "large enough test cases". I'm sure even on leetcode they are all procedurally generated anyway, just dump the generator into the checker and your tests suddenly become useful.
¯_(ツ)_/¯
would the true solution be to check tthe first value of every array, and binary search that, and then binary search the chosen array?
you can binsearch on the first column too, actually
oh. i misread. yes
youd need to do both surely, incase arrays are really long
def __init__(self, e):
self.element = e
self.left = TreeNode(e)
self.right = TreeNode(e)
self.parent = TreeNode(e)```
I have a TreeNode class like this, and I need to modify my insert BST method and in a way that it updates the parentNode, and I don't understand it at all. I tried to google or chatGTP myself out but nothing helped. It all tells me that I need self.left,right and parent to be none, but for the assigment it has to be TreeNode(e) 🥲
```def insert(self, e):
if self.root == None:
self.root = self.createNewNode(e) # Create a new root
else:
# Locate the parent node
parent = self.root
current = self.root
while current != None:
if e < current.element:
parent = current
current = current.left
elif e > current.element:
parent = current
current = current.right
else:
return False # Duplicate node not inserted
# Create the new node and attach it to the parent node
if e < parent.element:
parent.left = self.createNewNode(e)
else:
parent.right = self.createNewNode(e)
self.size += 1 # Increase tree size
return True # Element inserted```
or just binary search treating the thing as a (n*m) long 1d array
left, right and parent should be None in TreeNode yeah
as written creating a TreeNode would just recurse forever (until it dies from the recursion depth)
I know that left,right and parent should be none, but we were literally told to make it TreeNode
thats why I'm totally lost
or did I misunderstand it??
you can do this in O(1) if you don't have to provide the full path
that says they should have TreeNode type
(though it should really be Optional[TreeType])
Like TreeNode(None) ??
just None

so they were being extra
you're telling me that I've been crying my eyes out just to find out that my code was totally fine until now
given i have a string "31131123521" how can i make it so i multiply each letter of the string equaling the mathematical expression 3 * 1 * 1 * 3 * 1 * 1 * 2 * 3 * 5 * 2 * 1 (this is equal to 540)
i cant seem to come up with a pattern or something of that sort for this specific case
i scaled with multiple features and the return is only one value, so it throws an error
ValueError: non-broadcastable output operand with shape (1,1) doesn't match the broadcast shape (1,6)
scaler = MinMaxScaler(feature_range=(0, 1))
scaler.fit(data)
# Get the last day closing price and scale it
last_day = data[-61:-1].values
last_day_scaled = scaler.transform(last_day)
# Reshape the data to be compatible with the model
last_day_60 = np.array([last_day_scaled])
last_day_60 = last_day_60.reshape(1, 60, 6)
# Make a prediction for the next day opening price
next_day_open = model.predict(last_day_60)
# Inverse transform the prediction to get the actual price
next_day_open = scaler.inverse_transform(next_day_open)
# Print the result
print('Next day opening price:', next_day_open)```
Hello. I'm a complete noob but I managed to get the terminal to print out two balances from an exchange and convert one of those balances into £. What I'd like to do is to add a line of code that will make an action based upon the percentages of those two currencies. How would I go about doing this?
I don't see that as a failing I think reducing the amount of variables is good as long as zou know what big O actually is about and know the contraints
reducing the number of variables makes it inaccurate as a measurement of speed
it's an aproximation ofc it will be inacurate
and I am pretty sure big O was designed spicifically to evaluate algorithms regardless of hardware
you don't say a big failing of a car as a means of tranport is that it can't fly
I guess if you use something for a thing it wasn't designed to do you'll obtain subpar results
on one hand: you can actually do computations sensibly
on the other hand: not 1:1 match to the real world
sometimes trade-offs are worth it 😛
Can somebody explain, how linked lists work on Python. I tried to solve leetcode 206, but don't really get, how exactly the thing is supposed to work
Which is the leetcode link that has a collection of questions for the Data Structures question for product company interviews
Anyone knows a non-bruteforce algorithm for solving nonograms that can account for this puzzle?
https://asset-pdf.scinapse.io/prod/2026240824/2026240824.pdf
https://fse.studenttheses.ub.rug.nl/15287/1/Master_Educatie_2017_RAOosterman.pdf
I've just had a quick read through these two papers, seems like they might be what you're looking for?
second paper goes into complexities and analysis of uniqueness which is quite interesting
Oh thanks, it seems like it's what I want!
I was just exploring the source of sgtpuzzles and it listed this puzzle as something its solver can't take on
I'm creating a LSTM regressor. The older the start date, the less accurate the results are.
Example:
df = yf.download(symbol, start='2021-01-01') =36%to 40% with 0.8 test
df = yf.download(symbol, start='1980-01-01') =0% to 7% with 0.8 test
I thought that LSTM solved the problem of, well short/long term memory.
Btw this isn't for trading, mostly understanding all models and how performance changes if i change parameters
Can you help me guys?
what is the best way to get started
don't you just need one variable to store the last number
oh they need to be unique, so you also need a set
the prompt doesn't make sense tbh. they ask you to read the file in one line at a time, but also the last x distinct values, which you can't know unless you read the entire file
its my friends java assignment but sicne it's DSA i assumed i could get a idea of what to do
the most straightforward solution just reads the data and uses a set to check which numbers you've output already
it's linear
since you have to read all the numbers that's pretty much as best as you can get
Hi guys. I am just studying. I have task to read and open all markdown files in folder. Then open them. Then edit this files. Then convert to pdf
import os
import markdown
import pdfkit
Define a directory with a folder of markdown files
dir_path = "C:\Users\vladimir\PycharmProjects\pythonProject2"
Get all markdown files from a directory
md_files = [f for f in os.listdir(dir_path) if f.endswith(".md")]
Look at each markdown file
for md_file in md_files:
# Reading markdown files
with open(os.path.join(dir_path, md_file), "r") as f:
markdown_text = f.read()
Edit markdown files
edited_text = markdown_text.replace("#", "##")
Convert markdown files to html
html = markdown.markdown(edited_text)
Define the name of the pdf format
pdf_file = os.path.splitext(md_file)[0] + ".pdf"
pdf_path = os.path.join(dir_path, pdf_file)
Convert html to pdf
pdfkit.from_string(html, pdf_path)
so can you help me to do working programm on Python?
Im trying to implement a merge sort algorithm myself in py and I have this so far:
def merge_sort(arr: list[int]) -> list[int]:
if len(arr) == 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
new_arr = []
while left and right:
if left[0] < right[0]:
new_arr.append(left.pop(0))
else:
new_arr.append(right.pop(0))
new_arr += left + right
return new_arr
it works, however I make a new array each time instead of modifying the arr in place to sort it which I was told is "ideal", but IM not sur ehow to do that with my implementation.
hi everybody, if someone can help me debug/understand my error, I appreciate https://discord.com/channels/267624335836053506/1091013005124309012 Thx
you are also making two new arrays there
the first optimization is to merge in O(n), not the O(n^2) you're currently doing
deque + set
sounds like a reasonable thing
why do you need a deque? I was thinking just read it all in and loop in reverse
you could do that, though a deque is a nice streaming approach
how would you do it?
actually, let me think if a deque would actually do the right thing 
the question is if you can do this in O(x) memory I guess
I suspect you can, but I need to think a bit :/
reverse the file, then use a deque and set, heh
actually you don't even need a deque at that point
err, I guess "given a file" this is kinda boring
since you can actually read from the back
you can?
files have O(1) seek, don't they?
for fun, assume streaming input
big stream, you care only about a few values
so small x
the divisibility part is boring
you can just
def divisible(seq):
for last, x in itertools.pairwise(seq):
if x % last == 0:
yield x
```and you have a sequence where you want the last `k` unique values
actually i think you can just use a deque and set
my first idea needs some annoying book keeping
indeed
hmm. i wonder if OrderedDict can be useful here
!d collections.OrderedDict
class collections.OrderedDict([items])```
Return an instance of a [`dict`](https://docs.python.org/3/library/stdtypes.html#dict "dict") subclass that has methods specialized for rearranging dictionary order.
New in version 3.1.
wait, did I mess up ordering in my thing?
hm? oh i was thinking we could use ordereddict instead of a set and deque
no, I mean I get weird stuff with 
itertools.pairwise(iterable)```
Return successive overlapping pairs taken from the input *iterable*.
The number of 2-tuples in the output iterator will be one fewer than the number of inputs. It will be empty if the input iterable has fewer than two values.
Roughly equivalent to:
```py
def pairwise(iterable):
# pairwise('ABCDEFG') --> AB BC CD DE EF FG
a, b = tee(iterable)
next(b, None)
return zip(a, b)
```...
what's your input? the sample input doesn't give that output
oh, I added the last 3 lines
which is the answer part
[2, 8, 8, 4, 8, 8, 8, 8, 8]
o
😔
I can do it with a heap, but that feels more expensive than needed
O(n log x) time
O(x) memory
but a heap doesn't give you the desired order, does it?
heap with index as key and value as value
so you can easily reconstruct the right order
does anyone know some good resource for learning algorithms and data structures in Python?
see pins
for dijkstras does the graph need to be acyclic
no, but it will work in O(2^n*nm) with negative cycles
it won't terminate with negative cycles
it will, it's just has to
oh ok, if the answer is -inf it won't of course, right, im dumb
well then just check how many iterations it did and if it's a lot terminate it 🤡
no negative weights yea
or just use a different algorithm lol
i.e. bellman ford
though negative cycles are somewhat impossible in real life
they are possible, just not in the graph problems
depending on what you model of course
very much depends on your model
i mean, in problems which are not about graphs at first, but which reduce to graph problems 🙂
sometimes they exist and then you might want to realize a negative cycle exists
and that then the answer is -∞
i guess i can fix the slicing part by using 2 pointers for indices instead but im not sure how to optimize the merging
that is like the solution that comes out of my brain and makes sense
how could I do it instead then?
use two indexes to keep track of where you are in the lists
oh yea i was gonna do that for the dividing but for merging too?
ok
i can give it a try
Examine the provided code and drawing. Apply the delete method to each of the four orange nodes. Comment into the code, describing how each of the four combinations of children are handled. Draw the resulting tree afterwards.
How would I answer this question?
Anyone ever run into a python algorithm or package for ranking job applicants against ideal candidates?
Hello guys, I have some kind of problem with Tensorflow. She says that I do not have Nvidia, but I know that I have it, I also installed (CUDA, cudnn). But it still says that it is not there and does not find the video card.
Iirc you also have to add CUDA and CUDNN to path
did you do that too? @shadow spear
Not really a algo and datastructure question though, probably better to ask in #data-science-and-ml
Hi, I follow a sery of helpvideos for learning python since 5 days, but I have a problem, today it is very difficult : I understand nothing in the last (french) video but I have tried to do the practical work whose learn our the methods. Please can you help me ? Can I give you the correcion and, you explain me every line because it is ununderstanding ? Sorry for the bad quality of my english I am a french people
salut, française ici, peux-tu expliquer un peu plus ?
(et ton anglais n'est pas si mauvais, on comprend)
haha merci, voilà le message français que j'ai mis sur un autre serveur, que veux tu d'autre comme spécifications ?
Je suis bloqué sur une vidéo de graven à propos d'une série ayant pour but de nous faire apprendre les codes. Ici c'étaient les méthodes. J'ai suivi toutes les vidéos précédentes mais là impossible de comprendre quoi que ce soit...
Puis-je vous envoyer la correction du tp qu'il avait proposé pour que vous m'expliquiez le but de chaque ligne ? merci
c'est la vidéo 7 de tuto mais laisse moi une demi heure, je retente le truc
si vraiment je n'y arrive pas je t'appellerai désolé
yup!
j'ai presque finis la vidéo, après j'attaque le tp je crois que je comprends tout désormais !
super !! n'hésite pas à demander ; le truc self/init ça paraît abstrait au départ, mais quand on l'utilise soi-même avec un but en tête ça devient assez intuitif. Bon courage!
