Home > Mobile >  I need an infinite argument C# string
I need an infinite argument C# string

Time:08-12

so I need to create an infinite (or at least 1000) argument string. I just need to be able to hard code 1000 arguments.

I have tried var I = new List<string>("bob", "frank", and so on to 1000 arguments); and I can't find a way in C# so if I need a NuGet package thats fine.

CodePudding user response:

In general, asking someone to provide the full code without attempting yourself is frowned upon, but since you mentioned in a previous question today that you were 11, take this a learning opportunity for a few different concepts.

You'll need this at the top of your .cs file:

using System;
using System.Collections.Generic;
using System.Linq;

And try something like this:

public string GenerateOutput()
{
    // create a List of 1000 words
    var words = new List<string>(1000)
    {
        "bob",
        "frank",
        "windows",
        "test",
        "sample",
        "xylophone"
        // keep populating your list of words this way
    };
    
    int numberOfLettersToGet = 10;

    // keep getting #'numberOfLettersToGet' random letters until a word is found
    char[] randomLetters = new char[numberOfLettersToGet];
    dynamic match = null;
    while (match == null)
    {
        // select 10 random letters
        randomLetters = GetRandomLetters(numberOfLettersToGet);
        
        // get the word that has the most number of matching letters
        match = words.Select(w => new { Word = w, NumberOfMatchingLetters = w.ToCharArray().Intersect(randomLetters).Count() })
                     .OrderByDescending(o => o.NumberOfMatchingLetters)
                     .FirstOrDefault(o => o.NumberOfMatchingLetters > 0);
    }
    
    // return the results
    return $"{string.Join("", randomLetters)}: found \"{match.Word}\", {match.NumberOfMatchingLetters} matching letters";
}

public char[] GetRandomLetters(int numberOfLetters)
{
    // create an array of letters
    var letters = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
    
    // select random letters
    var rnd = new Random();
    var randomLetters = new char[numberOfLetters];
    for (var i = 0; i < numberOfLetters; i  )
    {
        randomLetters[i] = letters[rnd.Next(0, numberOfLetters)];
    }
    
    return randomLetters;
}

Here is a fiddle you can play around with: https://dotnetfiddle.net/GNE96w

CodePudding user response:

Load a Data Generator and use it accordingly.

  • Related