filter a list of strings in C# -
i'm learning c# reading books , other online tutorials (homeandlearn.co.uk)
i've managed fizzbuzz excercise struggling below excercise. much appreciated.
please explain in detail can learn aswell.
excercise
filter list of strings should pass 6 letter strings composed of 2 concatenated smaller strings in list.
for example, given list
acks, top, cat, gr, by, bar,lap, st, ely, ades
the list should return
stacks, laptop, grades, barely
because these concatenation of 2 other strings:
st + acks = stacks
lap + top = laptop
gr + ades = grades
bar + ely = barely
there lot of ways this. here's 1 uses pairing:
//grab possible pairings in 1 data structure list<keyvaluepair<string, string>> pairs = new list<keyvaluepair<string, string>>(); string[] list = { "acks", "top", "cat", "gr", "by", "bar", "lap", "st", "ely", "ades" }; foreach (string first in list) { foreach (string second in list) { pairs.add(new keyvaluepair<string, string>(first, second)); } } //test each pairing length , whatever else want list<string> sixletterwords = new list<string>(); foreach (keyvaluepair<string, string> pair in pairs) { string testword = pair.key + pair.value; if (testword.length == 6) { sixletterwords.add(testword); } }
Comments
Post a Comment