<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: JSL Programming Challenge: Convert from string of Ys and Ns to matrix of 1s and 0s, and back aga in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57396#M32113</link>
    <description>&lt;P&gt;Such cool solutions.&amp;nbsp; I can't believe the variety of approaches used to solve this relatively simple problem.&amp;nbsp; Thanks again all.&lt;/P&gt;</description>
    <pubDate>Fri, 18 May 2018 20:03:31 GMT</pubDate>
    <dc:creator>pmroz</dc:creator>
    <dc:date>2018-05-18T20:03:31Z</dc:date>
    <item>
      <title>JSL Programming Challenge: Convert from string of Ys and Ns to matrix of 1s and 0s, and back again</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57158#M32067</link>
      <description>&lt;P&gt;Hi JSL programmers,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I had a little problem as part of a larger application and thought I'd share it with you, and see if there's a way to do it better/slicker.&lt;/P&gt;
&lt;P&gt;Basically I have a vector of 1s and 0s (i.e. [1, 1, 0, 0, 1]) that I need to convert to a string of Ys and Ns (i.e. "YYNNY").&amp;nbsp; Then I need to do the reverse, that is convert a string of Ys and Ns back to a vector of 1s and 0s.&lt;/P&gt;
&lt;P&gt;Here are my quick and dirty functions - let's see if you can come up with something beyond my feeble brainpower!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;// Convert a vector of 1s and 0s to a string with no separators
matrix_10_to_yn = function({matrix_10}, {default local},
	yn_txt = "";
	if (nrows(matrix_10) &amp;gt; 0,
// Strip out square brackets and commas
		yn_txt = substitute(char(matrix_10), "[", "", "]", "", ", ", "");
// Now convert 1s to Y and 0s to N
		yn_txt = substitute(yn_txt, "1", "Y", "0", "N");
	);
	yn_txt;
);

// Convert a YN string with no separators to a vector of 1s and 0s 
yn_to_matrix_10 = function({yn_txt}, {default local},
	n = length(yn_txt);
	matrix_10 = j(n, 1, 0);
	for (i = 1, i &amp;lt;= n, i++,
		if (substr(yn_txt, i, 1) == "Y",
			matrix_10[i] = 1;
		);
	);
	matrix_10;
);
&lt;BR /&gt;// Test thing out
a = yn_to_matrix_10("YYYNNNN"); show(a);
b = matrix_10_to_yn(a);			show(b);

c = yn_to_matrix_10("");		show(c);
d = matrix_10_to_yn(c);			show(d);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 May 2018 19:57:14 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57158#M32067</guid>
      <dc:creator>pmroz</dc:creator>
      <dc:date>2018-05-17T19:57:14Z</dc:date>
    </item>
    <item>
      <title>Re: JSL Programming Challenge: Convert from string of Ys and Ns to matrix of 1s and 0s, and back aga</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57169#M32074</link>
      <description>&lt;P&gt;Whatever works, works!&amp;nbsp; Here is one alternative.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;exmat = J(25,1, RandomInteger(0,1));
exlst = Insert({}, {"Y", "N"}[2-exmat]);

convert_yn = Function({usr}, {rslt, ynList ={"Y","N"} },
 if(IsMatrix(usr), 
	rslt = insert({},ynList[2-usr])
 , //else
     rslt = J(nitems(usr),1, 1);
     rslt[loc(usr,"N")]=0
  );  //end if
  rslt
); //end Function

show(exmat, convert_yn(exmat), exlst, convert_yn(exlst));&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 May 2018 22:49:52 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57169#M32074</guid>
      <dc:creator>gzmorgan0</dc:creator>
      <dc:date>2018-05-17T22:49:52Z</dc:date>
    </item>
    <item>
      <title>Re: JSL Programming Challenge: Convert from string of Ys and Ns to matrix of 1s and 0s, and back aga</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57170#M32075</link>
      <description>&lt;P&gt;Another!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to here(1);
yto1 = function({str}, 
	{DEFAULT LOCAL}, 
	list = words(str, "");
	design(list, {"Y"});
);
oneToY = function({mat}, 
	list = substitute(as list(mat), 1, "Y", 0, "N");
	Concat items(list, "");
);
str = "YYYNNNNYYNY";
nums = yto1(str);
show(nums);
str2 = oneToY(nums);
show(str2, str2==str);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 May 2018 23:54:47 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57170#M32075</guid>
      <dc:creator>vince_faller</dc:creator>
      <dc:date>2018-05-17T23:54:47Z</dc:date>
    </item>
    <item>
      <title>Re: JSL Programming Challenge: Convert from string of Ys and Ns to matrix of 1s and 0s, and back aga</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57207#M32085</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This won't extend to three values or missing/error values without some extra effort. It converts string--blob--matrix and does the Y and N detection using the number of the ASCII character in the matrix. The BlobToMatrix and MatrixToBlob functions are told to use 1-byte little-endian integers. The endianness is not critical for 1-byte numbers, big works just as well. It does depend on the characters having one byte representations.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;source = "YYNNYQ";
// if you are uncomfortable with hard coded constants...
Yvalue = Blob To Matrix( Char To Blob( "Y" ), "int", 1, "little" )[1]; // 89
Nvalue = Blob To Matrix( Char To Blob( "N" ), "int", 1, "little" )[1]; // 78
// forward
numbers = Blob To Matrix( Char To Blob( source ), "int", 1, "little" ) == Yvalue;
// reverse
string = Blob To Char( Matrix To Blob( Nvalue + numbers * (Yvalue - Nvalue), "int", 1, "little" ) );
// display
Show( source, numbers, string );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;source = "YYNNYQ";&lt;BR /&gt;numbers = [1, 1, 0, 0, 1, 0];&lt;BR /&gt;string = "YYNNYN";&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2018 11:13:47 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57207#M32085</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2018-05-18T11:13:47Z</dc:date>
    </item>
    <item>
      <title>Re: JSL Programming Challenge: Convert from string of Ys and Ns to matrix of 1s and 0s, and back aga</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57209#M32087</link>
      <description>&lt;P&gt;you can do the string to matrix conversion in a single line using Char to Blob and Blob to Matrix. My contribution:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;names default to here(1);

a=[1,0, 1, 1, 1, 0, 0, 1, 0, 1];
b="YNYYYNNYNY";

f_YNString_to_IndicatorVector=Function({s},
	blob to matrix(char to blob(d),"int", 1, "little", 1)==89;
);

f_IndicatorVector_to_YNString=Function({v},{vv},
	vv=As List(v);
	vv[loc(v)]="Y";
	vv[loc(!v)]="N";
	concat items(vv,"");
);

show(a);
show(b);
Show(f_YNString_to_IndicatorVector(b));
Show(f_IndicatorVector_to_YNString(a));
Show(f_IndicatorVector_to_YNString(f_YNString_to_IndicatorVector(b)));


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 May 2018 11:59:25 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57209#M32087</guid>
      <dc:creator>MathStatChem</dc:creator>
      <dc:date>2018-05-18T11:59:25Z</dc:date>
    </item>
    <item>
      <title>Re: JSL Programming Challenge: Convert from string of Ys and Ns to matrix of 1s and 0s, and back aga</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57210#M32088</link>
      <description>&lt;P&gt;Oops, I didn't see Craig's reply...his version is better, I think.&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2018 12:00:16 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57210#M32088</guid>
      <dc:creator>MathStatChem</dc:creator>
      <dc:date>2018-05-18T12:00:16Z</dc:date>
    </item>
    <item>
      <title>Re: JSL Programming Challenge: Convert from string of Ys and Ns to matrix of 1s and 0s, and back aga</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57212#M32090</link>
      <description>&lt;P&gt;Wow these are amazing.&amp;nbsp; Thanks all!&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2018 12:14:20 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57212#M32090</guid>
      <dc:creator>pmroz</dc:creator>
      <dc:date>2018-05-18T12:14:20Z</dc:date>
    </item>
    <item>
      <title>Re: JSL Programming Challenge: Convert from string of Ys and Ns to matrix of 1s and 0s, and back aga</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57274#M32091</link>
      <description>&lt;P&gt;and now I need to study the design() function!&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2018 12:28:38 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57274#M32091</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2018-05-18T12:28:38Z</dc:date>
    </item>
    <item>
      <title>Re: JSL Programming Challenge: Convert from string of Ys and Ns to matrix of 1s and 0s, and back aga</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57361#M32094</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/4550"&gt;@pmroz&lt;/a&gt;,&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Here is my take/attempt at this. Thanks for offering this interesting challenge. I know I am using a loop which I don't know if it will scale well over large matrices, but I thought, some others in the community can come up with a faster way.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Clear Log(); Clear Globals(); 

ex = Associative Array({1,0} ,{"Y", "N"});

Test = [1,0, 1, 1, 1, 0, 0, 1, 0, 1];

Res = {}; 

for(i = 1, i &amp;lt;= N Rows(Test), i++,
		Insert Into(Res, ex &amp;lt;&amp;lt; Get Value(Test[i])); 
   ); 

Show(Res);


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 May 2018 13:24:40 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57361#M32094</guid>
      <dc:creator>uday_guntupalli</dc:creator>
      <dc:date>2018-05-18T13:24:40Z</dc:date>
    </item>
    <item>
      <title>Re: JSL Programming Challenge: Convert from string of Ys and Ns to matrix of 1s and 0s, and back aga</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57371#M32097</link>
      <description>&lt;P&gt;I use a generic 'mapply' function in most projects, so normally I would only need the last two lines:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to here(1);

// Utility function to make for loops, combo 'map' and 'apply'
// this is a 'fake' map/apply function because it allows side
// effects (like apply) and it also returns a value (like map)
mapply = function( {x, foo, arg1 = "__missing", arg2 = "__missing"}, { i, r = {}, x, foo, arg1, arg2, arg1missing, arg2missing },
	if( N Items( x ) &amp;lt; 1,
		return({}), //empty list supplied, return an empty list
		for( i = 1, i &amp;lt;= N Items(x), i++,
			arg1missing = 0; arg2missing = 0;
			If( type(arg1) == "String", If( arg1 == "__missing", arg1missing = 1 ) );
			If( type(arg2) == "String", If( arg2 == "__missing", arg2missing = 1 ) );
			if( 
				arg1missing == 1,
				r = Insert( r, foo( x[i] ) ),
				arg2missing == 1,
				r = Insert( r, foo( x[i], arg1 ) ),
				r = Insert( r, foo( x[i], arg1, arg2 ) )
			)
		)
	);
	return(r);
);

// Solution:
str = "YYYNNNNYYNY";
(numbers = mapply( words(str,""), function( {x}, if( x=="Y", 1, 0) ) ) )
mapply( as list(numbers), function( {x}, if( x==1,"Y", "N") ) )&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 May 2018 14:28:11 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57371#M32097</guid>
      <dc:creator>ih</dc:creator>
      <dc:date>2018-05-18T14:28:11Z</dc:date>
    </item>
    <item>
      <title>Re: JSL Programming Challenge: Convert from string of Ys and Ns to matrix of 1s and 0s, and back aga</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57379#M32102</link>
      <description>&lt;P&gt;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.&amp;nbsp; 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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To be consistent with my previous post, I just converted the string to a list with the words() function using an empty string delimiter.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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) );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 May 2018 16:41:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57379#M32102</guid>
      <dc:creator>gzmorgan0</dc:creator>
      <dc:date>2018-05-18T16:41:26Z</dc:date>
    </item>
    <item>
      <title>Re: JSL Programming Challenge: Convert from string of Ys and Ns to matrix of 1s and 0s, and back aga</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57396#M32113</link>
      <description>&lt;P&gt;Such cool solutions.&amp;nbsp; I can't believe the variety of approaches used to solve this relatively simple problem.&amp;nbsp; Thanks again all.&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2018 20:03:31 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Programming-Challenge-Convert-from-string-of-Ys-and-Ns-to/m-p/57396#M32113</guid>
      <dc:creator>pmroz</dc:creator>
      <dc:date>2018-05-18T20:03:31Z</dc:date>
    </item>
  </channel>
</rss>

