Nugroho's blog.

Monday, April 10, 2017

Short Function.


Here's my implementation of function according to The Power of 10;

"Restrict functions to a single printed page."

As bonus, I didn't use global variable if possible. So if a function or procedure need a variable from others, it have to be passed using parameter on that function.

If we look at the code below, we know that it can be rewritten using a long single procedure or function. But according The Power of Ten, a function should be as short as possible so it could be printed in a single page.

So, instead one long multiple page function, I write/break it as several short-single-printed-page functions. :)





unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Edit3: TEdit;
Edit4: TEdit;
procedure proses;
function konversi(a:real;c,d:char):string;
function konversiC(a:real;d:char):string;
function konversiF(a:real;d:char):string;
function konversiR(a:real;d:char):string;
function konversiK(a:real;d:char):string;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
proses;
end;
procedure tform1.proses;
var
a:real;
s,b:string;
c,d:char;
begin
a:=strToFloat(edit1.Text);
s:=edit2.Text;
c:=s[1];
s:=edit4.Text;
d:=s[1];
b:=konversi(a,c,d);
edit3.Text:=b;
end;
function tform1.konversi(a:real;c,d:char):string;
begin
case c of
'C':konversi:=konversiC(a,d);
'F':konversi:=konversiF(a,d)
'R':konversi:=konversiR(a,d)
'K':konversi:=konversiK(a,d)
else konversi:='error';
end;
end;
function tform1.konversiC(a:real;d:char):string;
begin
case d of
'C':konversiC:=floatToStr(a);
'F':konversiC:=floatToStr(a*9/5+32);
'R':konversiC:=floatToStr(a*4/5);
'K':konversiC:=floatToStr(a+273);
else konversiC:='Error';
end;
end;
function tform1.konversiF(a:real;d:char):string;
begin
case d of
'C':konversiF:=floatToStr((a-32)*5/9);
'F':konversiF:=floatToStr(a);
'R':konversiF:=floatToStr((a-32)*4/9);
'K':konversiF:=floatToStr((a-32)*5/9+273);
else konversiF:='Error';
end;
end;
function tform1.konversiR(a:real;d:char):string;
begin
case d of
'C':konversiR:=floatToStr(a*5/4);
'F':konversiR:=floatToStr(a*9/4+32);
'R':konversiR:=floatToStr(a);
'K':konversiR:=floatToStr(a*5/4+273);
else konversiR:='Error';
end;
end;
function tform1.konversiK(a:real;d:char):string;
begin
case d of
'C':konversiK:=floatToStr(a-273);
'F':konversiK:=floatToStr((a-273)*9/5+32);
'R':konversiK:=floatToStr((a-273)*4/5);
'K':konversiK:=floatToStr(a);
else konversiK:='Error';
end;
end;

end.


.

Another Turtle in Circle

There's always another way to solve something.

So, I have another code for "Turtle in Circle" code, :)

In the script below, I use turtle position to determine if it's still inside circle or not. If it outside circle, instead of send it to zero position, I send it to random position inside circle.

import turtle
from random import uniform
import numpy as np

turtle.shape("turtle")
#turtle.speed(1)
x = 0
y = 0
rmax=40

for i in range (1,1000):
a = uniform (-90,90) #angle
turtle.left(a)
d = uniform (-75,75) #distance
x = turtle.xcor()+d*np.cos(a*np.pi/180)
y = turtle.ycor()+d*np.sin(a*np.pi/180)
r = np.sqrt(x*x+y*y)
if r>rmax:
turtle.setx(uniform(-rmax,rmax))
turtle.sety(uniform(-rmax,rmax))
x = 0
y = 0
else:
turtle.forward(d)

turtle.exitonclick()





.

Sunday, April 9, 2017

Turtle in Circle


I use previous code and improve it so the turtle could only move at certain circle area.

import turtle
from random import uniform
import numpy as np

turtle.shape("turtle")
#turtle.speed(1)
x = 0
y = 0

for i in range (1,1000):
a = uniform (-90,90) #angle
turtle.left(a)
d = uniform (-75,75) #distance
x += d*np.cos(np.pi*a/180)
y += d*np.sin(np.pi*a/180)
r = np.sqrt(x*x+y*y)
if r>40:
turtle.setx(0)
turtle.sety(0)
x = 0
y = 0
turtle.forward(d)

turtle.exitonclick()





.



Anti MainStream.

Saat booming sepatu roda, Alfa malah sibuk berlatih skateboard, :D .


Saturday, April 8, 2017

Random Turtle Movement.


I use turtle module, the standard module, in Python.

The turtle movement has random direction (angle), and random distance (forward/backward).

import turtle
from random import uniform

turtle.shape("turtle")
turtle.speed(1)
for i in range (1,100):
#random angle
a = uniform (-90,90)
turtle.left(a)
#random move
d = uniform (-100,100)
turtle.forward(d)

turtle.exitonclick()






.



The Power of 10



Rules for Developing Safety-Critical Code:


Avoid complex flow constructs, such as goto and recursion.


All loops must have fixed bounds. This prevents runaway code.


Avoid heap memory allocation.


Restrict functions to a single printed page.


Use a minimum of two runtime assertions per function.


Restrict the scope of data to the smallest possible.


Check the return value of all non-void functions, or cast to void to indicate the return value is useless.


Use the preprocessor sparingly.


Limit pointer use to a single dereference, and do not use function pointers.


Compile with all possible warnings active; all warnings should then be addressed before release of the software.



Friday, April 7, 2017

Prime Number on Python

We've done it using Delphi, how about Python? Easy, :)


for i in range (2,200):
prime = True
for j in range (2,i):
if i%j==0:
prime=False
if prime==True:
print i


.

323f (5) amp (1) android (12) apple (7) arduino (18) art (1) assembler (21) astina (4) ATTiny (23) blackberry (4) camera (3) canon (2) cerita (2) computer (106) crazyness (11) debian (1) delphi (39) diary (286) flash (8) fortran (6) freebsd (6) google apps script (8) guitar (2) HTML5 (10) IFTTT (7) Instagram (7) internet (12) iOS (5) iPad (6) iPhone (5) java (1) javascript (1) keynote (2) LaTeX (6) lazarus (1) linux (29) lion (15) mac (28) macbook air (8) macbook pro (3) macOS (1) Math (3) mathematica (1) maverick (6) mazda (4) microcontroler (35) mountain lion (2) music (37) netbook (1) nugnux (6) os x (36) php (1) Physicist (29) Picture (3) programming (189) Python (109) S2 (13) software (7) Soliloquy (125) Ubuntu (5) unix (4) Video (8) wayang (3) yosemite (3)