#python help plz

1 messages · Page 1 of 1 (latest)

frank granite
#

python help thread cause im stupid ;-;

austere crest
#

fuck python

frank granite
#

^

#

I agree

austere crest
#

but i sent you a video that explains it

rustic void
frank granite
#

im watching it

austere crest
#

also shows you why copy 1 and 3 are shallow

#

and 2 and 4 are deep

frank granite
#

hmm I need to go, mom is yelling at me to do shit .-. but im in the middle of trying to solve this

#

this is only 1 /3 questions

#

question 2 is scary

#

using python for OOP

austere crest
#

why is everything python

#

when you applied for java/c#

frank granite
#

python is not made for OOP .-.

#

IDK

austere crest
#

do you only have python on your github or smth

frank granite
#

ASK the damn recruiter

#

nope

#

most of my github is c#

austere crest
#

i mean you can ask to confirm this is the right things. like The document you sent me only references python. Is this correct as I applied for a position referencing Java and C#

#

not like people never make mistakes

frank granite
#

also... how do I make classes in python? this seems strange

like interfaces and whatnot

#

ive never made interface in python before

rustic void
#

you dont

frank granite
#

but

#

interface

austere crest
molten bramble
#

theres no interface in python

austere crest
#

at this step latest i would ask if python was the right lang

molten bramble
#

only classes. but you can treat them as abstract classes

frank granite
#

apparently
from abc import abstractmethod, ABCMeta

#

so AbstractBase Class is an Interface

austere crest
#

well

#

no

frank granite
#

I hate python

molten bramble
#

you hate it because you dont understand it :p

frank granite
#

I hate trying to use OOP in it .-.

#

its not designed for OOP

molten bramble
#

it does oop just fine

frank granite
#

but how if you cant make interfaces

molten bramble
#

why do you need an interface

frank granite
#

Q2 needs ones .-..

austere crest
#

read the 2 pictures

#

they expect lana to implement an interface in python

molten bramble
#

ah right

#

yeah.. several options according to a quick search engine search

frank granite
#

ABC

#

im triyng to solve Q1 first though

#

im getting an error that copy is not defined

austere crest
#

code

frank granite
#
import copy

# Original object
document = [[1, 2, 3], [4, 5, 6]]

# Shallow copy
copy1 = copy.copy(document)
copy1[1][2] = 15
print("name=Original list= ", document)
print("name=Copy 1 list= ", copy1)

# Another shallow copy
copy2 = copy.copy(document)
copy2[0][2] = 3
print("name=Original list= ", document)
print("name=Copy 2 list= ", copy2)

# Deep copy
copy3 = copy.deepcopy(document)
copy3[1][0] = '456'
copy3[1][1] = 7
copy3[1][2] = 15
copy3[1][3] = 16
print("name=Original list= ", document)
print("name=Copy 3 list= ", copy3)

# Another deep copy
copy4 = copy.deepcopy(document)
copy4[1][0] = '789'
print("name=Original list= ", document)
print("name=Copy 4 list= ", copy4)
#

i asked AI for this to see what it came up with

molten bramble
#

whats the error

austere crest
#

still have no idea why you do a non change on copy2

frank granite
#

that AI did that, imma change it one sec

#

oh now I have list assignment out of range on line 23, which is copy3[][] = 16

#

yep makes sense

austere crest
#

just so its in here

frank granite
#

still not sure what to put in the document for the detailed explaination they want

austere crest
#

explaining why the changes happened as they did

#

since you only ever edit the copies

frank granite
#

copy makes a copy of the list, and it effects the original if you change the copy
deepcopy makes a copy of the list as a new object and therefor does not effect the original

#

thats the wwhole explaination

austere crest
#

so just explain that detailed

frank granite
#

...

#

ah yes, put my bullshittery skills to good use

austere crest
#

[screenshot of copy1 vs original]
since we did X and its a Y the change on ZZZ results in the original also changing

#

its not bullshiterry

#

its showing you understand why the output is what it is

frank granite
#

so 1 and 3 are shallow copies...

austere crest
#

yes

#

id just explain the question in the word document show the example output, make up a new original list in your code, make up new changes (all after you know it does what it should), post the code and output as screenshots, then explain the code detailed so this is the original list as we can see its a list of lists being XYZ originally, next screenshot of cop1 vs original this is after we did the first copy being a shallow copy and editing these and those indexes with these and those numbers. as you can see it also changes the subentries of the list as its a shallow copy blablablabla

#

ofc write it nicely

frank granite
#

Im guessing "Clone an object call document" is a typo?

austere crest
#

called

#

and yes

frank granite
#

yeh

austere crest
#

theres more typos

frank granite
#

seems the recruiter is indian so that makes sense

austere crest
#

doesnt explain code typos lol

frank granite
#

LOL

austere crest
#

unless its an actually empty entry

frank granite
#

I wouldnt doubt that

austere crest
#

but yeah wouldnt matter since the same line has a change that makes it a deep copy

bitter kernel
#

python.poggers

austere crest
#

no

#

after this thread mentioning python = ban

#

make it official mansen

bitter kernel
#

ban me, tickets dont get answered havo

frank granite
#

xd

#

so got to name the original list "document"

#

aghhh why

austere crest
#

because the question says so

frank granite
#

oh, I need to modify copy1 before printing it

molten bramble
#

traling commas are fine

frank granite
#

ok I got this

#

the wording makes it worse than it seems .-.

austere crest
#

thats not a word document

frank granite
#

yeh I know but I need to make the code first

austere crest
#

also id just name them copy1 - copy4 as the question asks

frank granite
#

maybe I should

molten bramble
#

if you want to be really cool you can assert yuor expectations :p

frank granite
#

oh

austere crest
#

insert image of you t-posing to assert your expectations

frank granite
#

LOL

#

need to figure out how to insert a string into a specific index of a loosely typed list .-.

molten bramble
#

just insert it

#

let go of the worries of type

frank granite
#

nah...I like knowing what type my data is at all times

#

Having the interpreter intepret it based on assumptions is not nice LOL

#

well, it works most of the time I guess

molten bramble
#

everything in python has a type, and its well defined

frank granite
#

wait how do I add this item in copy3??

#

its at the start of the second list

molten bramble
#

🦆

austere crest
#

google

molten bramble
#

use [0] instead of [1]?

#

i assume youre rubberducking ebcause you done provide us with a proper question

frank granite
#
shallowCopy2 = copy.copy(document)
shallowCopy2[1].insert(0, '456')
shallowCopy2[1][1] = 7
shallowCopy2[1][2] = 15
shallowCopy2[1][3] = 16

print("Shallow Copy 2:", shallowCopy2)
print("Original List: ", document, "\n")
molten bramble
#

that is incomplete

austere crest
#

looks complete to me

molten bramble
#

what is document.
what is the output
what output is expected

#

how did that sticker get there

frank granite
frank granite
#

Ugh, question 2 now

molten bramble
#

Nothing for over an hour, so must be going well

frank granite
#

I am writing the document for q1

#

And procrastinating slightly cause I'm scared of q3

molten bramble
#

Did they give an time estimate for the questions?

frank granite
#

I have until Sunday

#

Idk why Sunday cause they won't be working on Sundays

molten bramble
#

Well it's good excersice

frank granite
#

Yeah

#

If I can figure it out....

frank granite
#

Create a custom class called Value that will hold a number. Then add decorators that allow addition (Add) and subtraction (Sub) to a number (Value). The Add and Sub decorators can accept integers directly, a custom Value object or other Add and Sub decorators. Add, Sub and Value all implement the IValue interface and can be used recursively

#

what does it mean by "Can be used recursively"?

molten bramble
#

this is a weird exercice.

frank granite
#

Yep

molten bramble
#

sure, a class with a value. sure.
a decorator.. is not an easy thing, but quite interesting technique.

#

does it say how to use the decorators?

#

because decorators themselves are applied to functions/classes

frank granite
#

nope

#

this is the UML he gave with that descrition

#

idk how to use Decorators

#

I got my code to work but Add() and Sub() are not using Decorators AFAIK

molten bramble
#

decorators are fun. I found them hard to understand at first, but its is fairly simple.

#

a decorator does something with calling a function. before, after or both

#

so it can change the input of the function, or alter the output.

#

or just log for example

#

i have no idea what they mean with a decorator

#

chatgpt does something i would not call a decorator hehe

frank granite
#

this is the code phind gave me when I asked it a bunch of questions about the code I wrote already

from abc import ABC, abstractmethod

class IValue(ABC):
  @abstractmethod
  def __int__(self):
      pass

  @abstractmethod
  def __add__(self, other):
      pass

  @abstractmethod
  def __sub__(self, other):
      pass

def add_decorator(func):
   def wrapper(self, other):
       result = func(self, other)
       return Add(result)
   return wrapper

def sub_decorator(func):
   def wrapper(self, other):
       result = func(self, other)
       return Sub(result)
   return wrapper

class Value(IValue):
  def __init__(self, value):
      self.value = value

  def __int__(self):
      return self.value

  @add_decorator
  def __add__(self, other):
      return self.value + other.value

  @sub_decorator
  def __sub__(self, other):
      return self.value - other.value

class Add(IValue):
  def __init__(self, value):
      self.value = value

  def __int__(self):
      return self.value

  def __add__(self, other):
      return Add(self.value + other.value)

  def __sub__(self, other):
      return Sub(self.value - other.value)

class Sub(IValue):
  def __init__(self, value):
      self.value = value

  def __int__(self):
      return self.value

  def __add__(self, other):
      return Add(self.value + other.value)

  def __sub__(self, other):
      return Sub(self.value - other.value)

value1 = Value(int(input("Please give the largest number: ")))
value2 = Value(int(input("Please give the smallest number: ")))

print("\nAddition: ", int(value1 + value2))
print("Subtraction: ", int(value1 - value2))
print("Adding 50 to first number then subtracting the second: ", int((value1 + 50) - value2))
#

I asked it to make sure these are Decorators in my code

#

said it was!

#

idk if these are implemented right but it functions....

#

wait....

molten bramble
#

te add_decorator and sub are decorators

frank granite
#

the UML wants strings not ints

#

does he not want the addition done????

#

how will that work??

#

__str__self()

molten bramble
#

thats a function on the class

austere crest
#

__str__ is a function for console output formatting

frank granite
#

o

#

.-.

austere crest
#

also why does your add and sub have the other function

#

like add has sub as well

#

and sub has add as well

molten bramble
frank granite
#

beause each class has to implement the functions in IValue

austere crest
#

doesnt pythons print call __str__ implicitly?

austere crest
molten bramble
frank granite
austere crest
#

thats not shown in the UML

frank granite
#

it says so in the description

molten bramble
#

the problem statement is so bad.. if that was done better it'd be a better assignment

austere crest
#

it doesnt even mention an add or sub class

frank granite
#

oh wait

austere crest
#

the fuck is this assignment

frank granite
#
from abc import ABC, abstractmethod

class IValue(ABC):
 @abstractmethod
 def __str__(self):
     pass

class Value(IValue):
 def __init__(self, value):
     self.value = value

 def __str__(self):
     return str(self.value)

class Add(IValue):
 def __init__(self, left, right):
     self.left = left
     self.right = right

 def __str__(self):
     return str(self.left) + " + " + str(self.right)

class Sub(IValue):
 def __init__(self, left, right):
     self.left = left
     self.right = right

 def __str__(self):
     return str(self.left) + " - " + str(self.right)

value1 = Value(int(input("Please give the largest number: ")))
value2 = Value(int(input("Please give the smallest number: ")))

print("\nAddition: ", Add(value1, value2))
print("Subtraction: ", Sub(value1, value2))
print("Adding 50 to first number then subtracting the second: ", Add(value1, Sub(Value(50), value2)))
#

fixed it

austere crest
#

has no decorators

molten bramble
#

yeah, but where are the decorators?

austere crest
#

also wouldnt you just use the deocrators themselves on the variable?

#

like the code before didnt really use the decorators efficiently either

molten bramble
#

no it was weird

austere crest
#

or can python only decorate funcs

molten bramble
#

the problem is weird

#

uh, classes and funcs i believe

frank granite
#

wat

#

this doesnt have decorators?

#

did the last one?

molten bramble
#

By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.

frank granite
#

I dont get it ;-;

molten bramble
#

neither do i

frank granite
#

well, I get it but IDK how to code it

molten bramble
#

oh really? i dont understand the problem

austere crest
#

same

frank granite
#

here is the whole problem

#

this is all the info I got

austere crest
#

the assignment sounds like an unbeatable problem id have nightmares about

#

it doesnt make sense but in a dream id accept it

frank granite
#

:/

molten bramble
#
def this_is_the_function_it_wraps():
  ...something
frank granite
#
def Add(func):
   def wrapper(left, right):
       return func(left) + func(right)
   return wrapper

def Sub(func):
   def wrapper(left, right):
       return func(left) - func(right)
   return wrapper

@Add
def add_values(value1, value2):
   return value1.value + value2.value

@Sub
def sub_values(value1, value2):
   return value1.value - value2.value

value1 = Value(int(input("Please give the largest number: ")))
value2 = Value(int(input("Please give the smallest number: ")))

print("\nAddition: ", add_values(value1, value2))
print("Subtraction: ", sub_values(value1, value2))
print("Adding 50 to first number then subtracting the second: ", add_values(value1, Sub(Value(50), value2)))
#

this? but I need the IValue too UGH

molten bramble
#

if that assignment was given to me, i'd write something else (and more fun) to demonstrate that i understand decorators lol

frank granite
#

I dont understand them 😢

#

and IDK enough about python to do this properly

#

if this was Java it would be a lot easier for me

molten bramble
#

how would you do this in java?

#

are decorators different in java?

#

as in.. the meaning of decorators?

frank granite
#

AAGHHH

#

so you cant have Interface AND Decorators

#

yet this questions wants both

austere crest
#

dont use ai

#

easy fix

frank granite
#

im just asking it shit about the code I wrote myself

#

to see if it is done right

molten bramble
#

the problem seems to require a java implementation as it looks like decorators have a different meaning in java

frank granite
#

.-.

molten bramble
#

like really different

frank granite
#

i have to use Python :/

molten bramble
#

you cant

frank granite
#

then idk

#

whats the reason I cant?

molten bramble
#

maaybe
a decorator can alter a class and add add and sub functions to it.
but you should really use inheritance for that.

#

i dont know anymore, ive spend too many brain cycles on this

#

and need to sleep

frank granite
#
from abc import ABC, abstractmethod

class IValue(ABC):
  @abstractmethod
  def __call__(self):
      pass

class Value(IValue):
  def __init__(self, value):
      self.value = value

  def __call__(self):
      return self.value

def Add(func):
  def wrapper(left: IValue, right: IValue):
      return func(left) + func(right)
  return wrapper

def Sub(func):
  def wrapper(left: IValue, right: IValue):
      return func(left) - func(right)
  return wrapper

def main():
  value1 = Value(int(input("Please give the largest number: ")))
  value2 = Value(int(input("Please give the smallest number: ")))

  add_values = Add(Value)(value1, value2)
  sub_values = Sub(Value)(value1, value2)

  print("\nAddition: ", add_values())
  print("Subtraction: ", sub_values())
  print("Adding 50 to first number then subtracting the second: ", Add(Value)(Value(50), Sub(Value)(value1, value2))())

if __name__ == "__main__":
  main()
#

hhmmm

#

would this work?????

austere crest
#

try it

frank granite
#

fuckong phone call .-.

#

crashes

#

add_values = Add(Value)(value1, value2)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "Q2-Abstract-AI.py", line 17, in wrapper
return func(left) + func(right)
~^~~
TypeError: unsupported operand type(s) for +: 'Value' and 'Value'

#
from abc import ABC, abstractmethod

class IValue(ABC):
 @abstractmethod
 def __call__(self):
    pass

class Value(IValue):
 def __init__(self, value):
    self.value = value

 def __call__(self):
    return self.value

def Add(func):
 def wrapper(left: IValue, right: IValue):
    return func(left) + func(right)
 return wrapper

def Sub(func):
 def wrapper(left: IValue, right: IValue):
    return func(left) - func(right)
 return wrapper

@Add
def add_values(value1: IValue, value2: IValue):
   return value1() + value2()

@Sub
def sub_values(value1: IValue, value2: IValue):
   return value1() - value2()

def main():
 value1 = Value(int(input("Please give the largest number: ")))
 value2 = Value(int(input("Please give the smallest number: ")))

 print("\nAddition: ", add_values(value1, value2))
 print("Subtraction: ", sub_values(value1, value2))
 print("Adding 50 to first number then subtracting the second: ", add_values(Value(50), sub_values(value1, value2)))

if __name__ == "__main__":
 main()

I just get error
Traceback (most recent call last): File "Q2-Abstract-AI.py", line 42, in <module> main() File "Q2-Abstract-AI.py", line 37, in main print("\nAddition: ", add_values(value1, value2)) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "Q2-Abstract-AI.py", line 17, in wrapper return func(left.__call__()) + func(right.__call__()) ^^^^^^^^^^^^^^^^^^^^^ TypeError: add_values() missing 1 required positional argument: 'value2'

molten bramble
#

You're not using the decorators

frank granite
#

how use?

molten bramble
#

Oh wait you are

frank granite
#

also, the UML calls for only one value being used....
do we just hardcode a second value?

molten bramble
#

But they don't provide 2 args to the called function. And what are they even doing

frank granite
#

there is no sample output

molten bramble
#

Ok, really out now

frank granite
#

huh?

#

im done for today ....
havent eaten since breakfast, tummy hurts a lot

frank granite
#

ah...total clusterfuck of function calls

austere crest
#

gotta love functional programming

frank granite
#

ok fuck this question..need to try the next one :/

#

Suppose we want to buy a stock for some amount, at some given price, and we have to strategize to decide, depending upon the strategy, we will change the amount for which we want to buy the stock. And we will change the price at which we would like to buy the stock. Assume that we have two strategies, one is aggressive, and one is passive. If we are moving with aggressive strategy, then what we will do is we will invest our whole amount at the current price of the stock. And if we are moving with the passive strategy, then what we will do is we would invest only 50% of the amount and that to at 90% of the current price. So, we will wait in this case so that the stock price comes down to the 90% of its current one, and then only we will invest, and we will invest only half of our amount i.e., 50%. So, these are the two strategies which we would like to use and whenever we are buying the stock, we will specify with which strategy we are moving with. Use python to demonstrate above concept of strategy design pattern.

austere crest
#

i really wonder why your java/c# position only has python questions

frank granite
#

SAME

#

.-.

#

maybe setting up the IDE to run this code would take too much time for them .-. instead of just runing a single .py file at terminal

austere crest
#

i mean youd also use an ide for python

#

like the jetbrains one

frank granite
#

Im am using PyCharm

frank granite
#

Why does Python want two lines of blank space between function defs?

austere crest
#

thats not python

#

but pylint prob

molten bramble
#

yes pylint, and its the pep8 standard

#

deal with it

frank granite
#

How do I turn these into BCNF tables???
They should already be 3NF

Customer (custNo, custName) Outlet (outletNo, outletLoc) Car (carReg, make, model) CustomerCar (custNo, id) Car (carReg, id): Hire (hireNo, hireDate, custNo, carReg, outletNo) HireOutlet (hireNo, outletNo)

austere crest
#

wow now its database engineering

#

i thought this was a java/c# job

frank granite
#

different job offer

austere crest
#

did you learn any database engineering?

frank granite
#

yes

#

but up to 3NF

#

BCNF seems to be 3NF but with whatever "X should be a superkey for every functional dependency (FD) X−>Y in a given relation. " means

outer wadi
#

bump