matlab - genetic code and zombies! -
in alien world, genetic codes of creatures in base-4 system(quartenary). pairs "13" , "22" considered genetic disorders. genetic code of lenght n, if there @ least n/4 disorders, creature becomes zombie!! example n=5, creature genetic code 01321 has disorder, not zombie while creature genetic code 22132 zombie( because has 2 disorders >n/4).
now need write matlab program , value n user, easy, , display number of creatures , how many of them zombies
here i've written far, can't figure out how determine creatures has genetic codes of zombie. i'd appreciate ideas , help.thank you
n=input('enter length of genetic sequence: '); while (n<4) || (mod(n,1))~=0 disp('invalid input!') n=input('enter length of genetic sequence: '); end nofcreatures=4^n; count=0; i=0:nofcreatures k=dec2base(i,4); end fprintf('there %g creatures , %g of them zombies.\n',nofcreatures,count);
i recommended in comment try regexp function. strfind suite better if want count overlaps, count '222' 2 disorders.
so need this:
k=dec2base(i,4,n); %# use n include trailing 0s, other possible types of disorders m = [strfind(k,'13') strfind(k,'22')]; if numel(m) > n/4 count = count+1; end in addition, can n=0 first line instead of duplicating input line. , correct for-loop end @ nofcreatures-1.
edit
for bonus vectorized solution:
nofcreatures=4^n; k=cellstr(dec2base(0:nofcreatures-1,4,n)); m = cellfun(@numel,strfind(k,'13')) + cellfun(@numel,strfind(k,'22')); count = sum(m > n/4); fprintf('there %g creatures , %g of them zombies.\n',nofcreatures,count);
Comments
Post a Comment