Problem Statement
Write a program to process the elements of a given string array, get an integer from the process, and return the sum of these integers.
Given class WidgetProcessor:
class WidgetProcessor {
/**
* @see processName().
*
* @return BufferedWriter to which you can write names for processing.
*/
public BufferedWriter getBufferedWriter();
/**
* Relies on the BufferedWriter returned by getBufferedWriter().
* Processes whatever you have written to the BufferedWriter since
* the instantiation of this object or, if applicable, since the
* last time you called processName().
*
* @return integer between 0 and 16383, inclusive.
*/
public int processName();
}
DEFINITION
You must define public class WidgetNames with public method int checksum(String[] documents, String delimiterChars, String nameChars).
Each element of documents contains two or more non-overlapping substrings of the form <delimiter><name><delimiter>.
Each <delimiter> is any one of the characters found in delimiterChars, while <name> consists of one or more characters found in nameChars. A character cannot appear in both delimiterChars and nameChars.
Instantiate the WidgetProcessor class and obtain a BufferedWriter object by calling getBufferedWriter(). Traverse the elements of documents in order, starting at index 0. From each element, extract the second name. Write the second name to the BufferedWriter, and call processName() to obtain an integer.
If processName() throws a NullPointerException, discard the integer and ignore the remaining elements of documents. If processName() throws any other runtime exception, discard the integer, and move to the next element (if any) of documents.
Otherwise, add the integer to the sum of all previous integers obtained in this manner. Return the final sum.
Definition
- Class:
- WidgetNames
- Method:
- checksum
- Parameters:
- String[], String, String
- Returns:
- int
- Method signature:
- int checksum(String[] documents, String delimiterChars, String nameChars)
- (be sure your method is public)
Constraints
- The only valid characters are 'a' to 'z' (lowercase letters), '_' (underscore), ',' (comma), ';' (semicolon), ':' (colon), and '.' (period).
- delimiterChars, nameChars, and each element of documents will include valid characters only.
- documents will contain between 1 and 50 elements, inclusive.
- Each element of documents will consist of between 20 and 50 characters, inclusive.
- Each element of documents will contain two or more names as described in the problem statement.
- delimiterChars will consist of between 1 and 5 unique characters, inclusive.
- nameChars will consist of between 1 and 30 unique characters, inclusive.
- No character will appear in both delimiterChars and nameChars.
Examples
{"hello,_world,_how_are_you", "_hello,_world,_how_are_you"}"_,"
"abcdefghijklmnopqrstuvwxyz"
Returns: 16610
The first string in documents contains exactly two substrings of the form <delimiter><name><delimiter> as described in the problem statement. The first is "_world," and the second is "_how_". We ignore the first one and extract the <name> portion from the second one, yielding "how". We write "how" to the BufferedWriter returned by getBufferedWriter(). Now we call processName() and obtain the integer 4272. In the second element of documents, there are three substrings of the required form: "_hello,", "_world,", and "_how_". We extract "world" from the second one, write it to the BufferedWriter, and get 12338 from processName(). The sum of the integer values is 16610.{";;a;;_;;b;;_;;c;;_;;d;;_;;e;;", ".....a.....b.....c.....d....."}"_."
";abcdef"
Returns: 8702
The first string contains the names ";;b;;" and ";;d;;". We write ";;d;;" to the BufferedWriter and obtain 8604 from processName(). The second string contains the names "a", "b", "c", "d", and "e". We write out "b" and obtain 98. The sum of 8604 and 98 is 8702.{".a.b.c.........................", ".d.e.f.........................", ".g.h.i........................."}"."
"abcdefghi"
Returns: 207
After we have written "c" to the BufferedWriter, processName() throws a runtime exception (but not a NullPointerException). Then "f" yields the value 102 and "i" yields 105.{"aczdgu.hkclpc,obqq,pccdzzzz.ocobc:lqeoipcpnb;z", "cbohpdupkeqqdocdcznoornwddgwprc_.lp", "cnwphgkodcq_cd:nreeozczc::p,i::ecekcznqikdhncwea,", "vuc:edehlggzcjncwluehggh,c,ccwelow..wbcu", "cpee,cchprkcuc:gugcpxwu,z", "pceb.rg:f:rdczcgkhlzp.dlrcg,", "pcubh:h,ndu.bezwcp:ciou:o::urfd:wdc_s", "cr.fzkb,nnlrgehuzccihhoez.kwulegizocoia.b", "cu,n:czcd:rhceiclehqcncoi,,cqfzuqfj", "cohcceicuac:hcc.ncoor;,iykn", "fck.rolzfpicecfuqqhefoqcvacfnuqrwqnlcievf", "oclcrcgcdfchcl;crclls", "ckfclcgqczncgnc,j:dj", "co.bgfpcgc,pug.hcvncolqelecvxcie:up,cl;ln"}"c"
"lnhbfprq,iwd.eg:uozk"
Returns: 110234
If processName() throws a NullPointerException, we ignore all remaining strings.{"zfjlz,_:ds,m;_jalrlvyafnpwjygrl.nogk;xkf", "pvggxznx,;fbj_,ymcqca;,rpwdhkua,v;hpamnxshzyp,llg,", "i;;daqs_g_vcnh,__gnso,l;hyj.l.fc,kgreoeuculvca,p", "ewp,.rrrmxqujv;on_xvfhwnjg", "kw;uf_kyoshfex.pjmdycn_yhgqz_;rplnmcmwkzynyvjrxy", "ujydukg.u_pfjdhchldzqjbcz", "l;ekumsqh;spkqy_zahph;e.gdm:k;hjrwcg_sf;ky:_", "lppqkos_,:knpjdc_hegyym;e;rswuzezosprfpnv", "jw.l;;jskyp;fho;rqf.g", "prhsvmzgnmchquzdv;dj:xkrrrvle_hlsalljt.g", ";ll:unumpjkvakhfzjgajun_qxg.;i;gquy.r,jppmnl", "pwgaopppevkajmz;okhm;fcvdb", "wprdoho,ptn;qxy,sy;tp_f_q:f;ppyumy_.;s;gsbf", ";qlmxcawmawfdpm;hkkmd_kcgwyk;jc:mvz.sh:reapnjfwzd", "jscod:cou;;pknvw:_flp;c,u_mxncpgm.e._l", ";yapx_pkfp;eqjpvbm;v", "jfuuyr:pnh;cd_,fe;tj;rwoas,pzxk", "jrduhq;jgvc_rpc;zfyff;cs;zxgfx;z.,gqlsd", "prvhu_ar:h.r,jjnwdanzrockv,j:j.f,rfnscaf.u;xi_it", ":p_fam;jqwwmj.a;kwh.jjsyr:pvinpaawu", "xajmug;ujefkp:ipxqn;hm", "px:kvpljfqaa;;ywokjdgpouwa", "jyuza,ly;pjykyfresj;d;_yqclw,jdtp:ee:xkajkkc", "epyw.xedfpd;vzm:scu;hq:", "pfgl.rysrrowoau;v,pdhnm:rwsk._qku;ju", "fqjujpc;aa;r;fc;djksh", "juhmcxup;dxkyhl;gej.:_.us;_mm:", "q:;fxrhrhq,vnus_jpg.woznmvxcscrpehz", "aspuu:okd:,mzj;uvqovld_,vpgp:sae_.qzlwj;y;", "abjqfhmj;d,omprv;rlua;cq;rvqn;okwt", "hcpzayswd;q;elwqfmpo;_gknhej:.", "nprchrgqz;wpzyssz.,pjs,lq,cuppmsa;", ";qa_nlkk,y.g;pyxnhckygf.sjix;p", ";rk,sl.j;mwoxuwp;vnlaaq;hc;uwon_qjmdzk.whwf", "xspqumrhkjpc.e:afjgh;hf,.fmjjocuzu.pztq,u:", "oj:carkysguwzuuualyaru;ipxwyf,ymgqnlfuz_grd__;djp", "ipmmwcqxvsajwojzqxhrnna:;r", ";,qgszhgcppchzc_kcgjjyw_q,yrcjcjj_wl,mrlojbpsrle", "mpscc,sdxav.ccdjt;f_yrh_le,no,o;;n", "cpc:ochf.:zqn.z..agpphvmwafkravxqloqo,poci", ";ykzjph:xj;ecsp;gtbm;ut", ";fqrnynvxoolsyf;p,lgkg:yqoqscxypl;v:", "dp,_lk,phjnad.nju:pshg,q;h:", ";czfkveomwcvypjgxeso,cv,l:xp:gpllxe::kdwmnkppzhi", "jx._k.;opfuwldjpjmoo::pefopny", "i,pfa:_p;whn:jotjgzwq;wapsckdjrizmp", "cljvxw:oez:d,ajjcf,f_xeym:zj;ii"}"p;j"
"yu.dwloxr:,nkfc_zgqmvsaeh"
Returns: 331057
See if you can handle this large case correctly.