Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # CVX
- import cvxpy as cp
- import numpy as np
- A=np.array([[1,1,1],[1,2,3]])
- b=np.array([6,5])
- c=np.array([-1,-2,-4])
- n=3
- x=cp.Variable(n,integer=True)
- P=cp.Problem(cp.Minimize(c@x),[A@x<=b,x>=0])
- P.solve(cp.ECOS_BB)
- print(x.value)
- print(P.value)
- # scipy
- import numpy as np
- import scipy as sp
- from scipy.optimize import linprog
- a=np.array([[2,1],[-4,5],[-1,2]])
- b=np.array([20,10,-2])
- c=np.array([1,2])
- LB=np.array([0,0])
- res=linprog(-c,A_ub=a,b_ub=b)
- print(res)
- # Classes
- import numpy as np
- class Point:
- def __init__(self,id,x=0,y=0):
- self.id = id
- self.x = x
- self.y = y
- def setId(self,id):
- self.id = id
- def setX(self,x):
- self.x = x
- def setY(self,y):
- self.y = y
- def getId(self):
- print(f'the id is {self.id}')
- def getX(self):
- print(f'the x of this point is {self.x}.')
- def getY(self):
- print(f'the y of this point is {self.y}.')
- def deplacer (self,dx,dy):
- self.x = self.x + dx
- self.y = self.y + dy
- def dist (self,point):
- d=np.sqrt((self.x-point.x)**2+(self.y-point.y)**2)
- print(f'the distance between 2 points is {d}.')
- # tower of hanoi
- def hanoi(n, source, helper, target):
- if n > 0:
- # move tower of size n - 1 to helper:
- hanoi(n - 1, source, target, helper)
- # move disk from source peg to target peg
- if source:
- target.append(source.pop())
- # move tower of size n-1 from helper to target
- hanoi(n - 1, helper, source, target)
- source = [3,2,1]
- target = []
- helper = []
- hanoi(len(source),source,helper,target)
- print(source, helper, target)
- # PGCD
- def pgcd(a, b):
- a, b = b % a, a
- if a == 0:
- # BASE CASE
- return b
- else:
- # RECURSIVE CASE
- return pgcd(a, b)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement