VBA 使えるソースコード

つたないソースコードを載せます。これは、他人の書いたソースコードを読む練習に最適です。初心者の方は、どうしたらきれいになるかなど考えながら活用してください。

Python 数列から知りたい数字を探索しよう

import random

size = 10
max = 8
low = 0

high = size - 1
key = random.randint(1,max)
list_num = []
while size > 0:
list_num.append(random.randint(1,max))
size -= 1
list_num.sort()
flag = False
while low <= high:
middle = round*1
if key == list_num[middle]:
flag = True
break
elif key < list_num[middle]:
high = middle - 1
else:
low = middle + 1
print(list_num)
print("key = " + str(key))
if flag:
print(str(key) + " is found")
else:
print(str(key) + " is not found")

*1:low + high) / 2)
print( "low = " + str(low) + ",middle = " + str(middle) + ",high = " + str(high

Python お絵描きツールで必要なクラス

class SimpleCircle:
def __init__(self,x,y,wid,len,color):
self.y = x
self.x = y
self.wid = wid
self.len = len
self.color = color

def draw(self,canvas):
print("drawメソッドによる描画処理");
x1 = self.x - self.wid/2
y1 = self.y - self.len/2
canvas.create_oval(x1,y1,x1 + self.wid, y1 + self.len, outline = self.color)

 

class SimpleRectangle:
def __init__(self,x,y,wid,len,color):
self.y = x
self.x = y
self.wid = wid
self.len = len
self.color = color

def draw(self,canvas):
print("drawメソッドによる描画処理");
x1 = self.x - self.wid/2
y1 = self.y - self.len/2
canvas.create_rectangle(x1,y1,x1 + self.wid, y1 + self.len, outline = self.color)

 

class Shape:
def __init__(self,x,y,wid,len,color):
self.x = x
self.y = y
self.wid = wid
self.len = len
self.color = color

 

from Shape import Shape

class Circle(Shape):

def draw(self,canvas):
print("Circleクラスのdrawメソッドによる描画処理")
x1 = self.x-self.wid/2
y1 = self.y-self.len/2
canvas.create_oval(x1,y1,x1+self.wid,y1+self.len,outline=self.color)

 

from Shape import Shape

class Line(Shape):

def draw(self,canvas):
print("Lineクラスのdrawメソッドによる描画処理")
x1 = self.x-self.wid/2
y1 = self.y-self.len/2
canvas.create_line(x1,y1,x1+self.wid,y1+self.len,fill=self.color)

 

from Shape import Shape

class Rectangle(Shape):

def draw(self,canvas):
print("Rectangleクラスのdrawメソッドによる描画処理")
x1 = self.x-self.wid/2
y1 = self.y-self.len/2
canvas.create_rectangle(x1,y1,x1+self.wid,y1+self.len,outline=self.color)

 

 

 

 

 

Python お絵描きしてみよう2

from Circle import Circle
from Rectangle import Rectangle
from Line import Line
import tkinter
import random

class AddShapePanelv4:

def __init__(self):
print("AddShapePanelv4オブジェクトの作成")
self.create_window()
self.listOfObject=[]
self.IDforCircleRed = 1
self.IDforRectangleBlue = 2
self.IDforLineGreen = 3

def create_window(self):
self.window = tkinter.Tk()
self.canvas = tkinter.Canvas(self.window, bg = "white", width = 320, height = 240)
self.canvas.pack()

circle_button = tkinter.Button(self.window, text = "NewCircle1", command = lambda:self.createShape(self.IDforCircleRed))
circle_button.pack(side = tkinter.LEFT)

retangle_button = tkinter.Button(self.window, text = "NewRetangle1", command = lambda:self.createShape(self.IDforRectangleBlue))
retangle_button.pack(side = tkinter.LEFT)

line_button = tkinter.Button(self.window, text = "Newline1", command = lambda:self.createShape(self.IDforLineGreen))
line_button.pack(side = tkinter.LEFT)


def createShape(self, drawID):
print("createShapeメソッドの実行")
x = random.randint(0,320)
y = random.randint(0,240)
wid = random.randint(0,90) + 10
len = random.randint(0,90) + 10

if(drawID == self.IDforCircleRed):
color ="Red"
self.listOfObject.append(Circle(x,y,wid,len,color))
elif(drawID == self.IDforRectangleBlue):
color = "Blue"
self.listOfObject.append(Rectangle(x,y,wid,len,color))
elif(drawID == self.IDforLineGreen):
color = "Green"
self.listOfObject.append(Line(x,y,wid,len,color))
self.repaint()

def repaint(self):
print("repaintメソッドの実行:画面をクリアしてからpaintメソッドを呼び出す")


self.canvas.delete("all")
self.paint()

def paint(self):
print("paintメソッドの実行")
for drawObject in self.listOfObject:
drawObject.draw(self.canvas)

def run(self):
self.window.mainloop()

AddShapePanelv4().run()

f:id:yoronx:20200302135709p:plain

 

必要なクラスはここから!

 

yoronx.hatenablog.com

 

 

Python お絵描きしてみよう1

from SimpleCircle import SimpleCircle
import tkinter
import random

class AddShapePanel:

def __init__(self):
print("AddShapePanelオブジェクトの作成")
self.create_window()
self.listOfObject=[]
self.IDforCircleRed = 1
self.IDforCircleBlue = 2

def create_window(self):
self.window = tkinter.Tk()
self.canvas = tkinter.Canvas(self.window, bg = "white", width = 320, height = 240)
self.canvas.pack()

circle_button = tkinter.Button(self.window, text = "NewCircle1", command = lambda:self.createShape(self.IDforCircleRed))
circle_button.pack(side = tkinter.LEFT)

retangle_button = tkinter.Button(self.window, text = "NewCircle2", command = lambda:self.createShape(self.IDforCircleBlue))
retangle_button.pack(side = tkinter.LEFT)

def createShape(self, drawID):
print("createShapeメソッドの実行")
x = random.randint(0,320)
y = random.randint(0,240)
wid = random.randint(0,90) + 10
len = random.randint(0,90) + 10

if(drawID == self.IDforCircleRed):
color ="Red"
self.listOfObject.append(SimpleCircle(x,y,wid,len,color))
elif(drawID == self.IDforCircleBlue):
color = "Blue"
self.listOfObject.append(SimpleCircle(x,y,wid,len,color))
self.repaint()

def repaint(self):
print("repaintメソッドの実行:画面をクリアしてからpaintメソッドを呼び出す")
self.canvas.delete("all")
self.paint()

def paint(self):
print("paintメソッドの実行")
for drawObject in self.listOfObject:
drawObject.draw(self.canvas)

def run(self):
self.window.mainloop()

AddShapePanel().run()

 

必要なクラスはここから!

 

yoronx.hatenablog.com

 

Python 行列の積

def m_product(m1,m2):


m = #returnするやつ


for i in range(len(m1)): #m1の行を固定
r = #各行を入れるところ


for j in range(len(m2[0])): #m2のどの列なのかを決める
v = 0


for k in range(len(m2)): #m2の列の数
v += m1[i][k] * m2[k][j] #m1のi行目のk列、m2のk行目のj列
r.append(v)
m.append(r)
return m

m1 = [[1,2],[3,4]]
m2 = [[5,6],[7,8]]
print(m_product(m1,m2))

Python 線形計画問題 その2

# -*- coding: utf-8 -*-

from pulp import LpProblem, LpVariable, LpMaximize, LpStatus

problem = LpProblem('sample', LpMaximize)

x1 = LpVariable('x1', lowBound=0,upBound=None,cat='Continuous')
x2 = LpVariable('x2', lowBound=0,upBound=None,cat='Continuous')

problem += (15*x1 + 20*x2, 'Objective function')

problem += (3*x1 + x2 <= 156, 'Constraint1')
problem += (x1 + 3*x2 <= 136, 'Constraint2')
problem += (8*x1 + 7*x2 <= 486, 'Constraint3')

print(problem)

result = problem.solve()

print(LpStatus[result])
print('objective function value', problem.objective.value())
print('x1 = ',x1.value())
print('x2 = ',x2.value())

 

Python 線形計画問題 その1

# -*- coding: utf-8 -*-

from pulp import LpProblem, LpVariable, LpMaximize, LpStatus

problem = LpProblem('sample', LpMaximize)

x1 = LpVariable('x1', lowBound=0, upBound=2,cat='Continuous')
x2 = LpVariable('x2', lowBound=0, upBound=3,cat='Continuous')

problem += (5*x1 + 2*x2, 'Objective function')

problem += (2*x1 + x2 <= 5, 'Constraint')

print(problem)

result = problem.solve()

print(LpStatus[result])
print('objective function value', problem.objective.value())
print('x1 = ',x1.value())
print('x2 = ',x2.value())