Solved

picking different combination of words from an array of words

Posted on 2011-09-21
4
159 Views
Last Modified: 2012-05-12
I have a string for example : apple pink lotus baby pen paper red

i wan extract     6 words then 5 words 4 words 3 words 2 words then 1 word from this array.. for example: apple pink lotus baby pen paper
apple pink lotus baby pen red
apple pink lotus baby paper red

and so on....
apple pink lotus baby pen
...

apple
pink
lotus
baby pen

Please help thanks. If there is code in VB.net that will be great
0
Comment
Question by:TrialUser
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36578344
I've implemented to get 6 random strings. Likewise write for other cases. Refer: http://www.techrepublic.com/article/working-with-random-numbers-in-net-applications/5663283
using System;
using System.IO;
using System.Xml.Linq;
using System.Xml;
using System.Collections;
using System.Collections.Specialized;

namespace TestApplication
{
    class Program
    {
        static void Main(string[] args)
        {

            

            string[] myStrings = new string[7];
            myStrings = "apple pink lotus baby pen paper red".Split(" ".ToCharArray());


            Random objRandom = new Random();
            StringCollection objCollection = new StringCollection();

            //To get 6 values randomly
            for (int i = 0; i < 6; )
            {
                int random = objRandom.Next(0, 7); 

                if (!objCollection.Contains(myStrings[random]))
                {
                    objCollection.Add(myStrings[random]);
                    i++;
                }

            }

            foreach(string s in objCollection)
              Console.WriteLine(s);

            Console.Read();

        }
    }
}

Open in new window

0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 36579474
here is a VB.NET function that will return the number of items that you require from a given string.
    Function getRandon(ByVal items As String, ByVal number As Integer) As String

        Dim objRandom As New Random
        Dim objCollection As New List(Of String)
        Dim objReturn As New List(Of String)
        Array.ForEach(items.Split(","), Sub(x) objCollection.Add(x))
        While objReturn.Count < number
            Dim random = objRandom.Next(0, number + 1)
            If Not objReturn.Contains(objCollection.ToList(random)) Then
                objReturn.Add(objCollection.ToList(random))
            End If
        End While
        Return String.Join(",", objReturn)
    End Function

Open in new window

call the function by:
Dim MyResult = getRandon(String.Join(",", {"apple", "pink", "lotus", "baby", "pen", "paper"}), 2)

Open in new window

0
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 500 total points
ID: 36580179
For results into a blank dictonary, do
Dim theList = {"apple", "pink", "lotus", "baby", "pen", "paper"}
Dim Result As New Dictionary(Of Integer, List(Of String))
For Each x In theList
    If Result.ContainsKey(x.Length) Then
        Result.Item(x.Length).Add(x)
    Else
        Result.Item(x.Length) = New List(Of String) From {x}
    End If
Next

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36582262
You have asked the same question 3 times now!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question