BookmarkSubscribe
Choose Language Hide Translation Bar
gzmorgan0
Super User

Re: JSL Programming Challenge: Convert from string of Ys and Ns to matrix of 1s and 0s, and back aga

These are all very cool solutions. PMROZ pointed out that I misread that the secanrio is to start with a list. He asked for my revised solution.  Below converts a list or a string of YN's to a vector of 10's and a vector of 10's to a string of YN's.

 

To be consistent with my previous post, I just converted the string to a list with the words() function using an empty string delimiter.

 

exmat = J(25,1, RandomInteger(0,1));
exlst = Insert({}, {"Y", "N"}[2-exmat]);
exstr = ConcatItems(Insert({}, {"Y", "N"}[2-exmat]), "");

convert_yn = Function({usr}, {rslt=empty(), ynList ={"Y","N"} },
 if(IsMatrix(usr), 
	rslt = ConcatItems(insert({},ynList[2-usr]),"")
 , //else
    IsList(usr),
     rslt = J(nitems(usr),1, 1);
     rslt[loc(usr,"N")]=0
 , //else
    IsString(usr),
     rslt = J(length(Trim(usr)),1,1);
     rslt[loc(words(usr,""),"N")]=0    
  );  //end if
  rslt
); //end Function

show(exmat, convert_yn(exmat), exlst, convert_yn(exlst), exstr, convert_yn(exstr) );
pmroz
Super User

Re: JSL Programming Challenge: Convert from string of Ys and Ns to matrix of 1s and 0s, and back aga

Such cool solutions.  I can't believe the variety of approaches used to solve this relatively simple problem.  Thanks again all.

0 Kudos