#algos-and-data-structs

1 messages · Page 28 of 1

molten osprey
#

not from that line

haughty mountain
#

cool, you have a bunch of vectors as 2d arrays of different orientations

#

make them consistent

molten osprey
#

😭

#

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

haughty mountain
#

y1 is the culprit

#

numpy handled actual 1d arrays differently

#

and it doesn't generally mix well with 2d arrays

molten osprey
#

I see

#

y1.values is 1 dimensional tho right?

haughty mountain
#

yes, that's the issue

molten osprey
#

is y2's dimension good ?

#

how would I convert y1 so its exactly in the same format as y1

haughty mountain
#

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))
molten osprey
#

The first one looks good

haughty mountain
#

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.

molten osprey
#

delta2 looks like that now

haughty mountain
#

I'm betting one of w2 and a1 is 5 by something

molten osprey
#

let me check

#

(5, 580) is w2.T
(5, 5) is a1

#

delta2 is 580, 1

haughty mountain
#

cool, now make the matrix operations make sense

#

transposing matrices if you need to

molten osprey
#

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?

haughty mountain
#

you know better what you're trying to calculate

molten osprey
#

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?

haughty mountain
haughty mountain
#

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

molten osprey
#

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]
.....```
haughty mountain
#

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

molten osprey
#

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

haughty mountain
#

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

molten osprey
#

got it

#

I might have to start again, its gotten too messy

molten osprey
#

@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/
haughty mountain
#

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?

molten osprey
#

y1 is the actual output

haughty mountain
keen skiff
#

guys

def foo(n):
 n += 1
#

why sometimes the function changes the value of n and sometimes it doesnt

agile sundial
#

that would depend what n is

keen skiff
#
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

agile sundial
#

it depends on what n is

keen skiff
#

@agile sundial what data type it is ?

agile sundial
#

yes. n could have defined __iadd__ to mutate itself

keen skiff
#

so how do I know when it changes and when it doesnt

agile sundial
#

you need to know the type of n

keen skiff
#

Which types change ?

agile sundial
#

i don't think any std lib type would be mutated with += 5, though

keen skiff
#

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

agile sundial
#

what is there to study? just the operators? global variables?

agile sundial
keen skiff
keen skiff
agile sundial
#

i'm not sure of any articles that talk about that

agile sundial
keen skiff
#

So how do I mutate it

agile sundial
#

it needs to be mutable

keen skiff
#

so how do I make it mutable

agile sundial
#

you can't change the mutability of something, it's a property of the type

keen skiff
#

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

agile sundial
#

generally, you just don't want to do this

keen skiff
#

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

agile sundial
#

yes, no

keen skiff
#

@agile sundial are objects of classes we create mutable ?

agile sundial
#

yes

keen skiff
#

thx

tranquil junco
outer rain
tranquil junco
#

create a dictionary like this:
{[1:5],[0:6],[1:12],[0:8],[1:5]}

naive oak
#

what is it a dictionary of?

tranquil junco
#

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

outer rain
tranquil junco
#

that will support key duplication?

outer rain
#

Oh wait

#

Alright

#

You can append to list pairs instead

#

res.append((key, value))

tranquil junco
#

and this will support key duplication right? 🙂

outer rain
#

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.

tranquil junco
#

thank you for that, I will test in a minute

tranquil junco
outer rain
#

No, sorry, you want to have result be a list. Do result = [] just as you had before

tranquil junco
outer rain
#

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]:

tranquil junco
outer rain
#

print i after the loop 🙂

tranquil junco
#

I am sorry.

outer rain
#

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

tranquil junco
#

this the output

outer rain
#

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

tranquil junco
#

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)
outer rain
tranquil junco
#

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)
outer rain
#

It should be just <

tranquil junco
outer rain
#

Weird. Works for me.

tranquil junco
#

I forgot to empty out the result array.

#

I just throw my code into repl.

#

works like charm

quaint perch
#

what can i do to make this solution better
it doesn't seem optimal

outer rain
quaint perch
#

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)
haughty mountain
#

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 pithink

#

the formatting is kinda botched on mobile

#

yeah ok, looks like what you're doing

mystic fjord
#

yo

#

sup

modern bison
#

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

naive oak
#

#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

outer rain
#

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

keen skiff
#

Does every recursive algorithm has also an iterative solution

tranquil junco
#

If it was a question. If it was not then ignore my response.

keen skiff
#

@tranquil junco It was a question thx

#

does Every iterative solution have a recursive solution too

naive oak
#

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

outer rain
#

True

tranquil junco
#

very basicaly.

outer rain
#

me trying to control myself to not say word "monad" 😓 💦 😓

naive oak
#

just say "monoid in the category of endofunctors"

keen skiff
#

Just to make sure
every recursive function can be turned into iterative function
and every iterative function can be turned into recursive function

tranquil junco
keen skiff
#

thx

tranquil junco
#

Even digged out my old uni notes for this 😄

keen skiff
#

did u finish uni

tranquil junco
#

years ago 🙂

keen skiff
#

how's the job market for python

fiery cosmos
keen skiff
#

what's the most popular language for jobs

fiery cosmos
tranquil junco
keen skiff
#

Here the job market is for website

#

@tranquil junco what about where you from

tranquil junco
#

imho if you can code, understand basic structures, know oop, then the language is secondary.
(maybe c++ and assembly is an exeption)

outer rain
#

I don't think c++ is an exception, but functional languages are

keen skiff
#

what's a functional language

outer rain
#

And it's super easy to learn a second language so the choice of first lang does not even matter too much

tranquil junco
#

and I am located at hungary, which is basically the india of the EU

keen skiff
#

@tranquil junco for what hardware do you guys work

naive oak
tranquil junco
#

right now digi xbee3 and rp2040

#

both micropython

keen skiff
#

What are they used on

tranquil junco
#

sorry that I can't answare.

outer rain
naive oak
#

comparatively easy sounds better yeah

#

definitely much smoother if you already have some experience

#

rather than going in raw

keen skiff
#

lol

fiery cosmos
#
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

keen skiff
#

@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

fiery cosmos
#
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)
keen skiff
#

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

agile sundial
keen skiff
#

what

odd bloom
fiery cosmos
# odd bloom 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)

tranquil junco
#

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

vocal gorge
#

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.

tranquil junco
#

the second one of each tuple.
min_value = min(array, key=lambda x: x[1])

#

that is the best I found.

quaint perch
#

for example

keen skiff
outer rain
#

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

outer rain
#

There is no reason to talk about all of this unless "recursive" and "iterative" algorithms are formally defined though

quaint perch
outer rain
#

ok this is wrong and right at the same time on so many levels I won't even start

devout aspen
#

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.

regal tartan
#

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?

jolly mortar
#

its supposed to read "2^(9n) <= c for all n >n0"

regal tartan
#

what is n0?

jolly mortar
#

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

regal tartan
#

what is LHS and RHS?

jolly mortar
#

left hand side and right hand side

#

LHS is 2^(9n) and RHS is c

regal tartan
#

what is the threshold and c?

#

is it random numbers?

jolly mortar
#

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

regal tartan
#

if n = 1 and c = 0 then this holds true

#

🤔

jolly mortar
#

"2^(9n) <= 0 for all n > 1" is not true

regal tartan
#

oh

#

2^(9) <= 9999999 for all 1 > n0

#

how does this not hold true?

#

n = 1 and c = 99999999

jolly mortar
#

you arent choosing n

regal tartan
#

why did you make n0 = 1?

jolly mortar
#

you are choosing n0 and c

regal tartan
#

oh

#

alright

#

2^(9n) <= 999999999999 for all n >= 1

#

does this not hold true?

jolly mortar
#

no it doesnt

regal tartan
#

how not?

#

nvm

#

i am stupid

#

didnt notice all n

#

well thanks

#

i understand now

jolly mortar
#

np

fathom sedge
#

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?

quartz mirage
#

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

fathom sedge
#

Thank you for that reply

agile sundial
haughty mountain
#

which is usually good enough, in practice I don't really care about n0

vocal gorge
#

Is the structure maybe something simple like "there's always exactly one JSON object per line"?

fathom sedge
#

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

haughty mountain
#

an example: null is a valid value in json

upper hearth
#

I want to start learning Data Structures and Algorithms in python where do I start?

haughty mountain
#

the channel pins have some resources

fiery cosmos
#

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?

gusty isle
#

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...

gusty isle
strange anvil
#

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...)

outer rain
#

huh?

#

oh

#

!pypi sized-ints

halcyon plankBOT
outer rain
#

an ugly, but perhaps better alternative is to use ctypes

strange anvil
#

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

haughty mountain
naive oak
#

or use numpy types

robust dagger
#

Or use array.array objects of length 1.

steep basin
#

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

snow anvil
#

Question so hard to read is that how it reads originally?

fiery cosmos
crude galleon
#
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)

agile sundial
#

track the value of i on each iteration

crude galleon
#

btw does i represent operations ?

agile sundial
#

i is a variable in the code

crude galleon
#

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 ?

agile sundial
#

it's not a specific variable, it's the things your code does

vocal gorge
#

the statement "time complexity is n" doesn't really make sense. n is just a common variable name, no other meaning attached.

snow anvil
# crude galleon but what represents the "operations"

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))

agile sundial
#

O(however many times loop is ran)
it depends on the operations inside the loop

snow anvil
#

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

haughty mountain
#

the amount of work done doesn't scale with your input n

potent nymph
#

hey, Can I put a repository about a math algo in here?

haughty mountain
#

can a bipartite graph have a triangle?

haughty mountain
haughty mountain
#

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

snow anvil
#

well first of all it would error but I assume there is like a pass after the 2nd loop

agile sundial
#

that's just a placeholder for the inside of the inner loop

snow anvil
#

oh ic

#

makes sense

haughty mountain
#

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

snow anvil
#

true

#

yeah as I said my explanation was rly ass

haughty mountain
#

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

agile sundial
#

and recurrences !!

haughty mountain
#

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 😛

snow anvil
#

limits 🔥

haughty mountain
#

asymptotics is a lot about how much you can sweep under the rug without affecting the end result

outer rain
#

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

haughty mountain
outer rain
#

oh I see

haughty mountain
#

or stuff like when you can upper bound without changing results

outer rain
#

Well, I guess you just need to manually compute a few difficult big Ohs and get an intuition

haughty mountain
#

well, not wrong

#

but not tight

snow anvil
#

if it's not right it's wrong 😩

haughty mountain
haughty mountain
naive oak
#

ok good

#

where does p series come in then? or am I missing some context

tranquil junco
#

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?

outer rain
#

or with generator expression:

max_item = max(y for x, y in temp if x == 0)
tranquil junco
outer rain
#

I would say the generator expressions (the second one) is more widely used in python because lambda syntax is honestly 🤮

jolly mortar
#

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

crude galleon
#

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 }
snow anvil
#

it would be bad for the algorithm to run indefinitely if there is no match

keen skiff
tight briar
#

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?

haughty mountain
#

in terms of time complexity

#

but it generally doesn't, you need to consider the series itself

regal tartan
#

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?

agile sundial
#

mathematically

regal tartan
#

how?

#

they all tend to infinity

agile sundial
#

but not at the same rate

regal tartan
#

🤔

#

i will try

agile sundial
#

if one function is linear, and one function is quadratic, the quadratic one grows faster. that's what big O is measuring

regal tartan
#

yes but i am not really good at log thing

#

i will go watch a youtube tutorial about it

regal tartan
#

@agile sundial i took your advice

#

I actually solved it correctly and passed

agile sundial
#

👍

regal tartan
#

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?

agile sundial
#

do you know the time complexity of merging two sorted arrays?

regal tartan
agile sundial
#

just the merge step of mergesort, not sorting in general

regal tartan
#

no

agile sundial
#

huh?

regal tartan
#

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?

agile sundial
#

you don't need to find the time complexity of merge sort. you only need to care about the merging part

regal tartan
#

I got this from chatgpt 💀

agile sundial
#

well

regal tartan
#
# 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```
agile sundial
#

indeed

regal tartan
#

worst case 3n^2 * k?

agile sundial
#

i'm not sure how you would come to that conclusion

#

what are n and k

regal tartan
#

k sorted arrays, each with n elements

agile sundial
#

oh, you're talking about the problem. i was just asking about the merge part of merge sort

agile sundial
#

well yes, but n and k are defined for the problem, not the merge part

regal tartan
#

yea, I think I understand now

#

Thanks thumbsupparrot

haughty mountain
#

it's O(n k^2) right?

#

n^2 k doesn't make sense

#

e.g. merging k=1 lists isn't n^2

agile sundial
#

they also give a pretty suboptimal method, lol

haughty mountain
#

for sure

#

it's almost like merging unbalanced sized lists is a terrible idea

hollow lintel
#

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

hollow lintel
#

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

lunar sparrow
#

Would anyone be able to help me answer question 1 please 😬😭

agile sundial
#

it's just asking if there is some input that will give true

#

it's just the SAT problem

lunar sparrow
#

Something like this I’m guessing

#

ik how to do it on paper but I’m not quite sure how to code it

fiery cosmos
#

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

  1. 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)

  1. 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)

  1. calculation of the optical thickness from the extinction and the retrodiffusion
    extinction = ds.['extinction']
    retrodiff = ds.['retrodiff']
    optical_depth = -np.log(extinction/retrodiff)

  2. calculation of the lidar ratio knowing the backscatter and the extinction
    backscatter = []

extinction = []

lidar_ratio = backscatter / extinction

gloomy talon
#

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?

gloomy talon
#

There is no way to have scoped variables inside if statement?

fickle spruce
#

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

regal tartan
#

has anyone taken coursera course?

#

it is so weird

#

the assignment is so hard

#

like how the hell am i supposed to solve this?

robust dagger
#

!codeblock

halcyon plankBOT
#
Formatting code on discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

fiery cosmos
#

@robust dagger I mean ds['extinction']
Sorry
.sel to select

haughty mountain
#

do you know how to multiply two numbers by hand (digit based)?

#

if so you should be able to make it into an algorithm

dusky trellis
#

many people don't know how

snow anvil
#

are u supposed to write karazuba?

haughty mountain
dusky trellis
#

how to multiple by hand

haughty mountain
#

isn't this something you learn at very early age in school?

outer rain
#

they taught us FFT in the second grade

haughty mountain
#

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

crude galleon
#

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 ?

outer rain
#

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

crude galleon
#

so B-trees are what used in actual business projects ?

outer rain
crude galleon
#

is it red black in Java due to TreeMap class?

outer rain
#

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

crude galleon
#

gotcha

outer rain
#

You will have to some something very specific if standard library implementation does not work for you

haughty mountain
#

stuff like pointer stability

outer rain
haughty mountain
#

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

#

and they break different parts of the STL constraints

#

in order to have better performance

molten osprey
#

@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

robust dagger
# haughty mountain (because that means you basically can't ever change that)

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.

haughty mountain
robust dagger
#

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.

haughty mountain
#

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

outer rain
robust dagger
# haughty mountain idk, if we found a new sort of hash table that improves cpu usage by X% and memo...

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.

haughty mountain
#

flat hash tables vs node based hash tables

robust dagger
#

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.

dusky trellis
#

well, sorta

robust dagger
#

Yes, that's what I was saying.

dusky trellis
#

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)

dusky trellis
robust dagger
#

Yeah, there's a special case for when all the keys are strings, but I didn't want to bring that up.

dusky trellis
#

that's the usual case though 🙂

#

I can count on one hand the times I didn't use simple strings as keys

robust dagger
#

It's kinda a weird case because it's so intertwined with how Python actually works. Keyword arguments, that sort of thing.

robust dagger
#

Tuples, frozensets, hashable objects...

dusky trellis
#

really? interesting

#

you livin' on the edge!

robust dagger
#

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__.)

dusky trellis
#

it has? since what python version?

#

holy shit, why did I not know about this?

robust dagger
#

Yeah, since 3.6. When I was able to transition to that, my life got better.

#

Metaclasses are overpowered for most applications.

dusky trellis
#

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!

dense lake
#

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)```
dusky trellis
atomic bane
#

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?

atomic bane
#

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.

outer rain
solemn star
#

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.

agile sundial
#

just look at a typical undergraduate DSA course

agile sundial
#

my prof keeps saying "the big O-ness of x"

haughty mountain
#

🥴

#

you should ask out loud

did you mean to say "asymptotics"?

#

(or "asymptotic behavior", but that's less convenient)

agile sundial
#

yeah I mean. he knows what he's talking about, it's just amusing

vocal gorge
haughty mountain
#

(assuming it's actually a tight bound)

amber tusk
#

I end up using gigs of ram when only 10mb is allowed

#

All i know is that its doable with pyhon

ionic locust
#

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

outer rain
ionic locust
#

Yeah I know about those, can you give a concrete example? thanks

outer rain
# ionic locust 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

agile sundial
#

that would be ambiguous right?

outer rain
agile sundial
#

fun

haughty hollow
#

but dicts are fine too

#

kind of depends on how big its gonna be

#

for just a few entries a dict might be simpler

ionic locust
leaden forge
#

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?

vast adder
#

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.

agile sundial
#

topological sort

outer rain
#

Yes, and the "flatten it back to a list" sounds loke topological sorting

agile sundial
#

it would need to be acyclic, in python at least

outer rain
#

You can also look up dominator trees if the structure contains loops, or you need some complex analysis

agile sundial
#

oh, you don't mean actual modules, nevermind

leaden forge
#

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

neon nimbus
#

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?
jolly mortar
#

yes

neon nimbus
#

do you know what name I can google to learn more about it?

#

thanks!

agile sundial
#

broadcasting

jolly mortar
#

broadcasting perhaps

neon nimbus
#

ahhhhh

#

thanks a bunch

#

❤️

haughty hollow
#

The foods are really just labels for the rows and columns

agile sundial
outer rain
#

but the larger the dataset the more hash collisions you will have...

agile sundial
#

well you have a fixed number, you can make a perfect hash function

outer rain
#

how do you assign the hash to strings then? just guess the function and use it?

agile sundial
#

with a dictionary of course!

mapping = {
  "pizza": 0,
  "wasabi": 1,
  "snowball": 2,
  "shells": 3,
}
outer rain
#

sounds like we have gone full circle but ok

agile sundial
#

yeah i mean my original statement was joking. it's basically constructing a hash map

outer rain
#

I guess one hash table + array lookup is better than one big hash table + no lookup

agile sundial
#

but actually, you can create a perfect hash function since the universe of keys is finite (and feasibly small)

crude galleon
#

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.

haughty mountain
agile sundial
haughty mountain
#

that's what will end up being propagated all the way up, as the function is written currently

agile sundial
#

this doTree function is quite strange tbh

haughty mountain
#

it is

crude galleon
agile sundial
#

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

queen geyser
#

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?

agile sundial
#

it's a microoptimization

queen geyser
#

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?

haughty forge
#

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)?

agile sundial
queen geyser
crude galleon
haughty mountain
agile sundial
haughty mountain
ionic kraken
vast adder
#

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

ionic kraken
#

I modified the code last time and is new_cap

#

But exactly the same error again

vast adder
#

did you execute the cell after you modified the code?

forest tundra
#

@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

vast adder
#

nice!

forest tundra
#

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

vast adder
#

hopefully it's fast enough 😉

forest tundra
#

should be

#

if it's not i'll use your numpy function

#

thanks for the talk! it was very interesting

vast adder
#

np 😄

pulsar moth
#

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]
?

haughty hollow
#

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

vagrant perch
#

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 !

agile sundial
haughty hollow
#

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

agile sundial
haughty hollow
agile sundial
#

huh?

agile sundial
undone oracle
#

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

haughty mountain
#

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)

haughty hollow
#

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?

leaden barn
haughty hollow
#

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

agile sundial
#

sounds like an LRU cache

haughty hollow
#

yeah thats pretty much exactly what im trying to do

undone oracle
onyx hearth
#

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

fiery cosmos
#
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

fiery cosmos
haughty mountain
#

it sounds like you're talking about a single list, but then there is no "corresponding" element

haughty mountain
fiery cosmos
#

ooooh i thought it wasn't a support channel lmao

#

thx

haughty mountain
coarse sorrel
fiery cosmos
agile sundial
fiery cosmos
#

ye its used for gen_move

#

which basically generate all the possible moves

agile sundial
#

well how is that gonna work for knights

haughty mountain
#

kneen

#

or quight

haughty mountain
#

granted, idk what should be responsible for piece movement checks in a design like this pithink

agile sundial
#

yeah I'm actually confused. moves property and gen_moves?

midnight raptor
#

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.

jolly mortar
#

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))
midnight raptor
#

@jolly mortar Oh, that's absolutely ideal. Thank you so much.

naive oak
#

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)
magic holly
#

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?

agile sundial
#

they make their own functions

vocal gorge
# magic holly Can't figure out this question,if there is no build-in function to create graphi...

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.

lyric burrow
#
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?

vocal gorge
#

continue?

#

oh, skip the next iteration... set some flag, I guess, and at the start of the loop do

if flag:
    flag = False
    continue
lyric burrow
haughty mountain
#

no

vocal gorge
#

To skip the next iteration after the current one? no.

haughty mountain
#

if you need to skip around I would use a while loop

#

or maybe some iterator shenanigans

vocal gorge
haughty mountain
haughty mountain
#

and deal with the special case outside the loop

lyric burrow
# haughty mountain I don't get this loop. Why don't you just iterate over indices in `range(len(s) ...
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
magic holly
haughty mountain
vocal gorge
haughty mountain
#

which is what you want, as long as you move the special case outside the loop

magic holly
vocal gorge
#

Not sure what you mean

haughty mountain
magic holly
haughty mountain
#

is checks on small strings are...unreliable

#

!e

s = 'III'
print(s[1] is s[2])
halcyon plankBOT
#

@haughty mountain :white_check_mark: Your 3.11 eval job has completed with return code 0.

True
lyric burrow
haughty mountain
#

check the code I executed above

lyric burrow
haughty mountain
#

the is checks if they point to the same object

#

but strings can be cached

#

especially small ones

lyric burrow
#

should i make it == instead?

haughty mountain
#

no

#

why not use the index?

#

fwiw, I don't even know why you want that if and break

lyric burrow
#

i could try not using enum and just use index, yes

haughty mountain
#

!d itertools.pairwise

halcyon plankBOT
#

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)
```...
lyric burrow
#
    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
haughty mountain
#

that's not what I meant

#

something like this, if anything

if idx+1 == len(s)-1:
  break
lyric burrow
#

oh yes, that's a very clever way to do it

haughty mountain
#

but regardless what you really want to do is loop over pairs, no?

haughty mountain
lyric burrow
#

thank you

uneven vine
#

.

agile sundial
#

@fiery cosmos

#

<@&831776746206265384>

oblique panther
#

!cleanban 531843417207209985 porn

halcyon plankBOT
#

:incoming_envelope: :ok_hand: applied ban to @sage spire permanently.

agile sundial
#

sorry that other guy i pinged

keen hearth
agile sundial
#

😔

nocturne patrol
#

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))

haughty mountain
#

figure out how that None came about

#

and you'll find the bug

nocturne patrol
#

let me look into this further, thanks

haughty mountain
#

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)

agile sundial
#

or rather, what is being returned

haughty mountain
tepid pivot
#

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

  1. Leakage value 1.2 for circuit 1 is greater than the standard

  2. Capacitance is huge in circuit 3

  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 ?

lean walrus
solemn star
#

What should be the list of topics that are relevant for a Data Structures and Algo product interview. I am learning Python from scratch

nocturne patrol
proper salmon
#

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?

vast adder
#

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.

eager marten
#

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...'].

vocal gorge
eager marten
#

thank you

wild fox
#

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!

dull summit
#

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?

robust drum
eager marten
stark copper
#

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

pulsar light
#

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

gritty wyvern
#

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

fiery cosmos
kindred swift
#

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..

pulsar light
willow vapor
#

guys

#

I cant understand this

#

what does this mean?

haughty mountain
#

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

primal tangle
#

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

regal tiger
#

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

stiff quartz
#

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)

agile sundial
#

karatsuba, maybe

#

though maybe that's not simple

outer rain
#

karatsuba for polynomials is pretty simple

outer rain
# stiff quartz Any simple example to demonstrate big O is only valid if the constant terms are ...

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.

stiff quartz
stiff quartz
agile sundial
#

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

outer rain
vestal steppe
#

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

vagrant perch
#

Can anyone tell me why this passes? Its O(N) worst case

outer rain
#

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.

agile sundial
#

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

outer rain
agile sundial
#

¯_(ツ)_/¯

vagrant perch
agile sundial
#

you can binsearch on the first column too, actually

vagrant perch
#

wdym?

#

isnt that wat i said

agile sundial
#

oh. i misread. yes

vagrant perch
#

youd need to do both surely, incase arrays are really long

tawny vapor
#
    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```
haughty mountain
haughty mountain
#

as written creating a TreeNode would just recurse forever (until it dies from the recursion depth)

tawny vapor
#

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??

haughty mountain
haughty mountain
#

(though it should really be Optional[TreeType])

tawny vapor
haughty mountain
#

just None

tawny vapor
#

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

burnt trellis
#

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

fallen sequoia
#

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)```
sullen shore
#

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?

snow anvil
agile sundial
#

reducing the number of variables makes it inaccurate as a measurement of speed

snow anvil
#

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

snow anvil
#

I guess if you use something for a thing it wasn't designed to do you'll obtain subpar results

haughty mountain
#

sometimes trade-offs are worth it 😛

prime oar
#

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

solemn star
#

Which is the leetcode link that has a collection of questions for the Data Structures question for product company interviews

austere sparrow
#

Anyone knows a non-bruteforce algorithm for solving nonograms that can account for this puzzle?

thick vine
# austere sparrow Anyone knows a non-bruteforce algorithm for solving nonograms that can account f...

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

austere sparrow
#

I was just exploring the source of sgtpuzzles and it listed this puzzle as something its solver can't take on

fallen sequoia
#

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?

wicked cloud
#

what is the best way to get started

twin vapor
#

a set and stack r too slow

#

what would be faster dsa to use

agile sundial
#

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

twin vapor
#

its my friends java assignment but sicne it's DSA i assumed i could get a idea of what to do

agile sundial
#

the most straightforward solution just reads the data and uses a set to check which numbers you've output already

twin vapor
#

@agile sundial in reverse order though?

#

wouldn that be slow

agile sundial
#

it's linear

#

since you have to read all the numbers that's pretty much as best as you can get

magic chasm
#

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?

brisk fiber
#

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.

velvet ledge
lean walrus
agile sundial
haughty mountain
#

sounds like a reasonable thing

agile sundial
#

why do you need a deque? I was thinking just read it all in and loop in reverse

haughty mountain
agile sundial
#

how would you do it?

haughty mountain
#

actually, let me think if a deque would actually do the right thing pithink

haughty mountain
#

I suspect you can, but I need to think a bit :/

agile sundial
#

actually you don't even need a deque at that point

haughty mountain
#

since you can actually read from the back

agile sundial
#

you can?

haughty mountain
#

files have O(1) seek, don't they?

agile sundial
#

¯_(ツ)_/¯

#

either way, that is kinda boring

haughty mountain
#

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
agile sundial
#

actually i think you can just use a deque and set

haughty mountain
#

my first idea needs some annoying book keeping

agile sundial
#

indeed

#

hmm. i wonder if OrderedDict can be useful here

#

!d collections.OrderedDict

halcyon plankBOT
#

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.
haughty mountain
#

wait, did I mess up ordering in my thing?

agile sundial
#

hm? oh i was thinking we could use ordereddict instead of a set and deque

haughty mountain
agile sundial
#

huh

#

!d itertools.pairwise

halcyon plankBOT
#

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)
```...
haughty mountain
#

my output is [2, 8, 8, 4, 8, 8, 8, 8, 8, 8]

#

which seems off

agile sundial
#

indeed 🤔

#

should just be the first half of that

haughty mountain
#

actually, maybe it's correct

#

it ends up being a bunch of 8s at the end

agile sundial
#

what's your input? the sample input doesn't give that output

haughty mountain
#

oh, I added the last 3 lines

#

which is the answer part

#

[2, 8, 8, 4, 8, 8, 8, 8, 8]

agile sundial
#

o

haughty mountain
#

oh wait

#

I'm dumb

#

only the first 8 lines are the sequence 🤡

agile sundial
#

😔

haughty mountain
#

I can do it with a heap, but that feels more expensive than needed

#

O(n log x) time

#

O(x) memory

agile sundial
#

but a heap doesn't give you the desired order, does it?

haughty mountain
#

heap with index as key and value as value

#

so you can easily reconstruct the right order

spiral raptor
#

does anyone know some good resource for learning algorithms and data structures in Python?

haughty mountain
#

see pins

thick hazel
#

for dijkstras does the graph need to be acyclic

haughty mountain
#

no

#

but you can't have negative cycles

outer rain
#

no, but it will work in O(2^n*nm) with negative cycles

haughty mountain
#

it won't terminate with negative cycles

outer rain
haughty mountain
#

how? the correct answer is -∞

#

and it will just work towards that

outer rain
#

oh ok, if the answer is -inf it won't of course, right, im dumb

thick hazel
#

right

outer rain
#

well then just check how many iterations it did and if it's a lot terminate it 🤡

thick hazel
#

no negative weights yea

agile sundial
haughty mountain
#

i.e. bellman ford

agile sundial
#

though negative cycles are somewhat impossible in real life

outer rain
#

they are possible, just not in the graph problems

agile sundial
#

depending on what you model of course

haughty mountain
#

very much depends on your model

outer rain
#

i mean, in problems which are not about graphs at first, but which reduce to graph problems 🙂

haughty mountain
#

sometimes they exist and then you might want to realize a negative cycle exists

#

and that then the answer is -∞

brisk fiber
#

that is like the solution that comes out of my brain and makes sense

agile sundial
#

pop(0) takes linear time

#

just don't use that

brisk fiber
#

how could I do it instead then?

agile sundial
#

use two indexes to keep track of where you are in the lists

brisk fiber
#

oh yea i was gonna do that for the dividing but for merging too?

#

ok

#

i can give it a try

grave pumice
#

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?

high flower
#

Anyone ever run into a python algorithm or package for ranking job applicants against ideal candidates?

shadow spear
#

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.

lament totem
#

Iirc you also have to add CUDA and CUDNN to path

#

did you do that too? @shadow spear

fiery cosmos
#

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

rose glacier
#

(et ton anglais n'est pas si mauvais, on comprend)

fiery cosmos
#

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é

rose glacier
#

yup!

fiery cosmos
#

j'ai presque finis la vidéo, après j'attaque le tp je crois que je comprends tout désormais !

rose glacier
#

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!