C
C#2y ago
malkav

✅ Reorder letters in a string and group per...

So I've been puzzling with this for a while now, and I am not sure if this actually is what I am looking for but before I dive into what I have, let me explain the premise: Say I have a text (say a lorem of 200 words) and I strip all punctuations and spaces and newlines and whatever to turn the entire lorem into a single string without any spaces/punctuation-marks/newlines (\r\n) Now I group all of these by 6 characters into a single array (done) Here's the tricky part that I am puzzling with: I have to turn the whole string into something that looks like this:
lor psu lor ame nse
emi mdo sit tco cte

tur
--- (etc)
lor psu lor ame nse
emi mdo sit tco cte

tur
--- (etc)
So basically, group by 6 characters, and write 5 of those groups in a single "line" (which is obviously 2 lines) But actually I have to reorder these letters before I do so. Now the reordering is easy, and the grouping by 6 is easy. It's the actual "how do I write them on two lines with the indexes 0, 1, and 2 on the first line, and 3, 4, 5 on the second line. Here's what I have to begin with:
private static IEnumerable<string> WriteByLine(this string[] groups, string order, int startIndex = 0)
{
string text = "";
int groupIndex = 0;

foreach (string item in groups)
{
// TODO: Change this next line to the actual order of items instead of just the first or second three items
text += item.Reorder(order).Substring(startIndex, Math.Min(3, item.Length));
if (groupIndex == 4)
{
text += "\n";
groupIndex = 0;
}
else
{
text += " ";
}

groupIndex++;
}

return text.Split("\n");
}
private static IEnumerable<string> WriteByLine(this string[] groups, string order, int startIndex = 0)
{
string text = "";
int groupIndex = 0;

foreach (string item in groups)
{
// TODO: Change this next line to the actual order of items instead of just the first or second three items
text += item.Reorder(order).Substring(startIndex, Math.Min(3, item.Length));
if (groupIndex == 4)
{
text += "\n";
groupIndex = 0;
}
else
{
text += " ";
}

groupIndex++;
}

return text.Split("\n");
}
but I fear it's not entirely doing what I wanted... please help me out?
1 Reply
malkav
malkavOP2y ago
here's the result I'm getting at the moment:
moL dsp iol cma tsnier omu tsr ote ecedut iip teg edeiar ncs sil ioddsu rme icn tnutom iop ddi luteba odt nme ulaero rlo aga aqiitU nda ivm sqmmne iim ane niuuso cxe oat mludrt ire nit calolo ssi itu xiurba iin qla eepmca nod tqe uiuomo soc Dau tasrie rod rrn eeheur iol epe rdnvti aul ive cseoni ttp tle iesdll eol auf aunomu uer tig plltra eEr set ctnuai pcx iru acocce tip pnt niouta aad rno tedius auc fiu eicntn qpl ifo sdatre tlo din btsmnu ail eim oal-ur --m
moL dsp iol cma tsnier omu tsr ote ecedut iip teg edeiar ncs sil ioddsu rme icn tnutom iop ddi luteba odt nme ulaero rlo aga aqiitU nda ivm sqmmne iim ane niuuso cxe oat mludrt ire nit calolo ssi itu xiurba iin qla eepmca nod tqe uiuomo soc Dau tasrie rod rrn eeheur iol epe rdnvti aul ive cseoni ttp tle iesdll eol auf aunomu uer tig plltra eEr set ctnuai pcx iru acocce tip pnt niouta aad rno tedius auc fiu eicntn qpl ifo sdatre tlo din btsmnu ail eim oal-ur --m
which is all on a single line, while I would've preferred this to be like I showed earlier. per two lines 5 groups of 6 characters and if I replace text += "\n" with text += "\r\n" and console log that text (before returning) I get this:
Lor psu lor ame nse
emi mdo sit tco cte
tur pis gel edd
adi cin its oei
usm emp nci unt
odt ori did utl
abo tdo ema ali
ree lor gna qua
Ute adm mve mqu
nim ini nia isn
ost exe tat ull
rud rci ion amc
ola isn uta uip
bor isi liq exe
aco doc equ uis
mmo ons atD aut
eir dol nre hen
ure ori pre der
iti lup eve ess
nvo tat lit eci
llu lor fug nul
mdo eeu iat lap
ari rEx teu nto
atu cep rsi cca
eca pid tno oid
tcu ata npr ent
sun cul uio cia
tin paq ffi des
eru oll nim stl
ntm ita ide abo
rum ---
Lor psu lor ame nse
emi mdo sit tco cte
tur pis gel edd
adi cin its oei
usm emp nci unt
odt ori did utl
abo tdo ema ali
ree lor gna qua
Ute adm mve mqu
nim ini nia isn
ost exe tat ull
rud rci ion amc
ola isn uta uip
bor isi liq exe
aco doc equ uis
mmo ons atD aut
eir dol nre hen
ure ori pre der
iti lup eve ess
nvo tat lit eci
llu lor fug nul
mdo eeu iat lap
ari rEx teu nto
atu cep rsi cca
eca pid tno oid
tcu ata npr ent
sun cul uio cia
tin paq ffi des
eru oll nim stl
ntm ita ide abo
rum ---
Which is almost there, but after the first group of 5, it only gives me a group of 4 instead)
Want results from more Discord servers?
Add your server