Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Sample ChE 26 Codes

Problem 19 Typhoon Eye

function [H1 H2]=typhooneye(A,B,R1,R2,V)


%problem 19 MEx

%distance of luneta from trajectory of the typhoon center


alpha=atan(B/A);
d=sin(alpha)*A;

%det of the length of the chord with distance D inside circles 1 and 2
%outer circle
if d>R1
C1=0;
else
beta1=acos(d/R1);
C1=2*R1*sin(beta1);
end

%inner circle
if d>R2
C2=0;
else
beta2=acos(d/R2);
C2=2*R2*sin(beta2);
end

H1=(C1-C2)/V;
H2=C2/V;
end

Problem 24 Missing Profiles

function F=missingprofiles(p1,p2,p3,p4)
%problem 24 - MEx

I=1:10; F=zeros(1,6); j=0;


for i=1:10
if I(i)~=p1 && I(i)~=p2 && I(i)~=p3 && I(i)~=p4
j=j+1;
F(j)=I(i);
end
end
end

Problem 75 Missing Profiles

function personality(A,n)
% Problem 75 machine exercise
%Extra. Error checking.
if length(A)>5 || length(A)<5
error('Invalid length of string input.\n')
end
B=zeros(1,length(A));
for i=1:length(A)
if A(i)=='A'
B(i)=1;
else
B(i)=0;
end
end

m=zeros(1,n); m(1:length(A))=B;
for k=6:n
ss=sum(m(k-5:k-1));
if ss>=3
m(k)=0;
else
m(k)=1;
end
end

if m(n)==1
fprintf('Andy would be Roger''s personality on day %.0f.\n',n)
else
fprintf('Bob would be Roger''s personality on day %.0f.\n',n)
end

% Extra only. To check if Roger will be healed.


kk=6;
mm=zeros(1,n); mm(1:5)=B;
sss=1;
while sss~=5 || sss~=0
sss=sum(mm(kk-5:kk-1));
if ss>=3
mm(kk)=0;
else
mm(kk)=1;
end
kk=kk+1;
if kk==10000
ind=1;
fprintf('Roger will never have one personality.\n')
break
end
end

if ind~=1
fprintf('Roger will have one personality after %.0f days.\n',kk)
end
Problem 80 Jack is Patient Zero

function F=patientzero(N,P)
%problem 80 - MEx
tic
k=0; count=1;
PP=zeros(1,N); PP(1)=P;
while k~=1
count=count+1;
PP(count)=mod(2*PP(count-1),N+1);
for i=1:count-1
if PP(count)==PP(i)
k=1;
break
end
end
end
F=count-1;
toc
end

Problem 7 Degrees Franklin

function F = franklin(X1,Y1,X2,Y2,T)
C = [X1 1;X2 1]\[Y1;Y2];
F = C(1)*T + C(2);
end

NOTE: If you have a system of linear equations involving n variables, you can
use “/” in order to solve it.

Example:

(1) x+2y=5 (2) 3x – y = 12


solution = [1 2; 3 -1;]\[5;12]

Problem 10 Safety Levels

function G=safetylevels(N,R,C)
%prob 10 - MEx
G=zeros(N); G(R,C)=1;
for i=1:N
for j=1:N
G(i,j)=max(abs(i-R),abs(j-C))+G(R,C);
end
end
end

Problem 45 Mean Sequences

%sample code 1
function meanseq(X,Y,M)
if mod(Y - X,M - 1) == 0
disp('YES');
else
disp('NO');
end
end

%sample code 2

function meanseq(x,y,m)

% Prob 45 - MEx
G=zeros(1,m); G(1,1)=x; G(1,m)=y;
d=(y-x)/(m-1);
for i=2:m-1;
G(1,i)=G(1,i-1)+d;
if rem(G(1,i),1)~=0
ind=1;
fprintf('NO\n')
break
else
ind=0;
end
end

if ind~=1
fprintf('YES\n')
end
end

Problem 69 Bacon Pancake Delivery

%sample code 1

function F = deliver(A,B,C,D,E,F)
p = [0 0;A B;C D;E F]; d = zeros(4);
v = [2 4 10 12;3 4 7 8;2 3 8 12]';
for j = 1:4,
for k = 1:4,
d(j,k) = hypot(p(j,2) - ...
p(k,2),p(j,1) - p(k,1));
end
end
F = min(sum(d(v)));
end

%sample code 2

function F=baconpancakes(x1,y1,x2,y2,x3,y3)
%problem 69 - MEx
C=[0 x1 x2 x3; 0 y1 y2 y3]; D=zeros(1,6);
%[0-1 0-2 0-3 1-2 1-3 2-3]
%D1=0-1 D2=0-2 D3=0-3 D4=1-2 D5=1-3 D6=2-3
%[0-1 0-1 0-2 0-2 0-3 0-3] = [D1 D1 D2 D2 D3 D3]
%[1-2 1-3 2-1 2-3 3-1 3-2] = [D4 D5 D4 D6 D5 D6]
%[2-3 3-2 1-3 3-1 1-2 2-1] = [D6 D6 D5 D5 D4 D4]
%[3-0 2-0 3-0 1-0 2-0 1-0] = [D3 D2 D3 D1 D2 D1]

%[D1 D4 D6 D3] = [D1 D3 D4 D6]


%[D1 D5 D6 D2] = [D1 D2 D5 D6]
%[D2 D4 D5 D3] = [D2 D3 D4 D5]

%[D1 0 D3] [D1 D2 0] [0 D2 D3]


%[D4 0 D6] [0 D5 D6] [D4 D5 0]

k=0; CM=zeros(1,3);
for i=1:3
for j=i+1:4
k=k+1;
D(k)=(((abs(C(1,i)-C(1,j)))^2)+((abs(C(2,i)-C(2,j)))^2))^0.5;
end
end

for i=1:3
CM(i)=sum(D)-D(1,i)-D(1,7-i);
end
F=min(CM);
end

Problem 13 Christmas Trees

%sample code 1

function F = tree(N)
c(1:N+2,1:2*N+3) = '.';
c(N+2,N+2) = '*';
for j = 1:N+1
c(j,N+3-j:N+1+j) = '*';
end
F = c;
end

%sample code 2

function G=xmastree(N)
%problem 13 - MEx
G=zeros(N+2,(2*N)+3); G(N+2,:)=46; G(N+2,ceil(N+1.5))=42; G(1,:)=G(N+2,:);
for i=2:N+1
G(i,:)=46;
G(i,(ceil(N+1.5)-i+1):(ceil(N+1.5)+i-1))=42;
end
G=char(G);
end

Problem 12 Unit Fraction Expansion

function F = unitfrac(D,N)
M = 1;
while N > 0
M = 10*mod(M,D);
N = N - 1;
end
F = floor(M/D);
end

Problem 35 Henyo Strategy Part 1

w = input('Input word: ','s');


c = w - 'A'; n = [26*26,26,1];
v = dec2bin(1+sum(n.*c(1:3)));
for j = 2:length(v)
if v(j) == '0', fprintf('Y');
else fprintf('YN'); end
end
fprintf('M\n');

Problem 37 Chess Knight Moves

function F = knightmov(R,C)
b(1:15,1:15) = '.'; b(8,8) = 'K';
x = [-2 -2 -1 1 2 2 -1 1] + 8;
y = [-1 1 2 2 -1 1 -2 -2] + 8;
for j = 1:8, b(x(j),y(j)) = '*'; end
F = b(9-R:16-R, 9-C:16-C);
end

Problem 85 Center of Weight

function F = cenWeight(C,D)
X = 0; Y = D; N = 0;
while abs(Y - X) > 0.01
Z = 0.5*(Y + X);
if C == Z, break;
elseif C < Z, Y = Z;
else X = Z;
end
N = N + 1;
end
F = N;
end

Problem 8 Heating Value of Coal

function F = dulong(W,X,Y,Z)
D = [33801 144158 -144158*...
0.125 9413];
MW = [12 1 16 32];
G = [W X Y Z];
F = sum(D.*(MW.*G/sum(MW.*G)));
end

Problem 26 Folding the Manila Paper

function F = manilapaper(L,W)
c = 0;
while L >= 1 || W >= 1
c = c + 1; a = min(L,W);
L = max(L,W)/2; W = a;
end
F = c;
end

Problem 3 Single Card Comparison

%sample code 1

N = input('Enter the number of cards: ');


val = '3456789TJQKA2';
sut = 'CSHD';
maxPos = -1;
for j = 1:N
fprintf('Input card %d',j);
c = input(': ','s');
v = find(val == c(1));
s = find(sut == c(2));
pos = 4*(v - 1) + (s - 1);
if pos > maxPos
maxPos = pos;
end
end
v = floor(maxPos/4) + 1;
s = mod(maxPos,4) + 1;
disp(['Highest card: ' val(v) sut(s)]);

%sample code 2

%MEx 3 -Single Card Comparison


N=input('Input number of cards:');
V=zeros(1,N); S=V; val='3456789TJQKA2'; suit='CSHD';
for i=1:N
fprintf('Input card %d',i); k=input(':','s');
V(i)=find(val==k(1)); S(i)=find(suit==k(2));
end
vmx=max(V); VV=find(V==vmx);
if length(VV)>1
Sin=S(VV(1)); ind=VV(1);
for i=2:length(VV)
if S(VV(i))>Sin
Sin=S(VV(i)); ind=VV(i);
end
end
else
ind=VV;
end
fprintf('Highest card: %s%s\n',val(V(ind)),suit(S(ind)))

Problem 51 Complementary DNA

s = input('Enter string: ','s');


a = s;
a(s == 'A') = 'T';
a(s == 'T') = 'A';
a(s == 'G') = 'C';
a(s == 'C') = 'G';
disp(a);

Problem 73 Gold Piece Balance

%sample code 1

function F = goldpiece(x1,x2,x3,x4,x5)
x = [x1 x2 x3 x4 x5]; w = sum(x);
for j = 0:31
t = dec2bin(j) - '0';
w = min(w,abs(sum(x) ...
- 2*t*x(1:length(t))'));
end
F = w;
End

%sample code 2

function w=GPB(A,B,C,D,E)
%MEx - 73
x=[A B C D E]; tw=sum(x); iz=zeros(1,5); w=tw;
%0 - Lupin, 1 - Kaito Kid
for i=0:31
t=dec2bin(i)-'0';
iz((6-length(t)):5)=t;
coor=find(iz==0);
kk=abs(tw-sum(x(coor)));
wn=abs(kk-sum(x(coor)));
if wn<w
w=wn;
end
end
end

Problem 77 Hyper Room

function F = hyperRoom(S)
g = S(1);
for j = 2:length(S)
g = gcd(g,S(j));
end
F = g;
end

You might also like