indexing - Calculate Scrabble word scores for double letters and double words MATLAB -


my goal here create code takes letters in string , gives out score in scrabble. has account double words scores (dictated !) , double letter score (dictated #).

example: if input 'hel#lo!', first 'l' played on double letter score space, , final 'o' played on             double word score space. output score 18. 

thanks rayryeng got read off score value if there no double words or double letters, can't seem figure out those. if have guidance in how go it, appreciate it.

doubleword = '#'; %// here labeled things doubleletter = '!'; doublew = strfind(word, doubleword-1); %// want find double words doublewb = 2; trouble = strfind(word, doubleletter-1); %// want find double letters troubleb = letterpoints*2; convert = word; stringtoconvert = lower(convert); ascii = double(stringtoconvert) - 96; origscore = (sum(values(ascii))); score = doublewb*(sum(origscore)+troubleb); 

the last line gives me trouble. array , nice clean number. furthermore, when try run simple string 'matlab', gives me error. or weird, not simple number. think need further indexing, i'm little confused. know supposed hardest problem, though i've been struggling them all. 'q#uar!tz#ifer#ous!' should give 220. 'ramblin!gwreck' should give 54.

why hello again! let's recreate lookup created in our previous post:

string1point = 'aeionrtlsu'; string2point = 'dg'; string3point = 'bcmp'; string4point = 'fhvwy'; string5point = 'k'; string8point = 'jx'; string10point = 'qz'; lookup = zeros(1,26); lookup(double(string1point) - 96) = 1; lookup(double(string2point) - 96) = 2; lookup(double(string3point) - 96) = 3; lookup(double(string4point) - 96) = 4; lookup(double(string5point) - 96) = 5; lookup(double(string8point) - 96) = 8; lookup(double(string10point) - 96) = 10; 

before else, need lowercase of letters in order access lookup table.

lowerword = lower(word); 

once this, let's figure out double letters , double words located. you've got right want find letters double letters (#) and/or tiles hit double words tiles (!). need move 1 outside of brackets can figure out actual letter is, not symbol located. in other words:

doublew = strfind(lowerword, '!') - 1; doublel = strfind(lowerword, '#') - 1; 

what need extract original string, without symbols. can by:

originalword = lowerword; originalword([doublew + 1 doublel + 1]) = []; 

i have add 1 each of locations because want access symbols located. use both locations of symbols are, use [] eliminate symbols. should give our original word. now, we're going access each letter worth in points in our lookup table , sum points. in other words:

ascii = double(originalword) - 96; basepoints = sum(lookup(ascii)); 

right now, amount of points have is:

basepoints =   8 

8 points how many points have before adjustment in score. now, let's tackle each case 1 @ time:

double letters

if remember before, doublew , doublel contain locations of letters subject increase in score.

first, let's figure out letters subject doubling point value, , figure out these in lookup table , add how score is:

doublelettersind = double(lowerword(doublel)) - 96; sumletters = sum(lookup(doublelettersind)); 

sumletters contain total score of of these letters subject doubled in value. here's gets tricky. i'm going next take score stored in sumletters , add on top of base score (which 8 in our case). take letters doubled , double them. remember, if add itself, doubling value. because in base score, i've added letters base score, if find letters doubled , scores of , add them on top of our original score, doubling score of corresponding letters:

doublepoints = basepoints + sumletters; 

double words

the last thing need count how many times have encountered double word tile. take number, , you'd have use exponent power of 2 show how many times need double. if hit double tile once, double score (i.e. 2^1 = 2). if hit double word tile twice, quadruple score (i.e. 2^2 = 4) , , on. in other words:

finalpoints = (2^numel(doublew))*doublepoints; 

numel counts how many elements in array or matrix. such, length of doublew tell how many times have encountered double word tile.

the ^ important, double correctly. ^ stands exponentiation, , handles talked when correctly doubling score. able handle case don't encounter double word tiles, 2^0 = 1.

once this, when word = 'hel#lo!';, above code, get:

finalpoints =   18 

with other examples, get:

.... cool? :)

also, above test cases, ramblin!gwreck, get:

finalpoints =   54 

for q#uar!tz#ifer#ous!, get:

finalpoints =   220 

to make nice can copy , paste editor can run it, here's code in single code block:

%// define word here word = 'ramblin!gwreck'; %word = 'q#uar!tz#ifer#ous!';  %// convert word lower case lowerword = lower(word);  %// create table string1point = 'aeionrtlsu'; string2point = 'dg'; string3point = 'bcmp'; string4point = 'fhvwy'; string5point = 'k'; string8point = 'jx'; string10point = 'qz'; lookup = zeros(1,26); lookup(double(string1point) - 96) = 1; lookup(double(string2point) - 96) = 2; lookup(double(string3point) - 96) = 3; lookup(double(string4point) - 96) = 4; lookup(double(string5point) - 96) = 5; lookup(double(string8point) - 96) = 8; lookup(double(string10point) - 96) = 10;  %// find letters either double word or double letter doublew = strfind(lowerword, '!') - 1; doublel = strfind(lowerword, '#') - 1;  %// original word without symbols originalword = lowerword; originalword([doublew + 1 doublel + 1]) = [];  %// base points of word ascii = double(originalword) - 96; basepoints = sum(lookup(ascii));  %// find letters need doubled in point value %// , double score doublelettersind = double(lowerword(doublel)) - 96; sumletters = sum(lookup(doublelettersind)); doublepoints = basepoints + sumletters;  %// finally, if need double word score, finalpoints = (2^numel(doublew))*doublepoints; 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -