Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This could be cleaner, but it works:</p> <pre><code>Dim fruits As New List(Of String) From { "apple", "orange", "pear", "banana" } Dim input As String = "a,p,l,e,r" dim inputLetters = from letter in input.Replace(",", "") group by letter into Group select group.first, Group.Count dim result = fruits.where( function(fruit) dim fruitcounts = from letter in fruit group by letter into Group select group.first, group.count dim res = from fc in fruitcounts, inputs in inputletters where fc.first = inputs.first andalso fc.count &lt;= inputs.count select fc.first return res.count = fruit.count end function ) </code></pre> <p><strong>edit</strong> - I removed some unneeded order by clauses, and simplified the grouping</p> <p><strong>edit again</strong> - after some more thinking, here's a version that has more lines, but is much clearer and better factored:</p> <pre><code>Sub Main Dim fruits As New List(Of String) From { "apple", "orange", "pear", "banana" } Dim input As String = "a,p,l,e,r" dim matchingFruits = from fruit in fruits where CanBeMadeFrom(fruit, input) End Sub Function StringToFrequencyTable(input as string) as Dictionary(of Char, Integer) dim freqTable = from letter in input group by letter into Group select letter, Group.Count() return freqTable.ToDictionary(function(g) g.Letter, function(g) g.Count) end function Function CanBeMadeFrom(candidate as string, letters as string) as boolean dim inputLetters = StringToFrequencyTable(letters.replace(",", "")) dim IsCharInFrequencyTable = function(x) (from entry in inputLetters where entry.Key = x.Key andalso entry.Value &gt;= x.Value).Any() return StringToFrequencyTable(candidate).All( IsCharInFrequencyTable ) end function </code></pre> <p>If I did anything else to this, I would make CanBeMadeFrom and StringToFrequencyTable into extension methods.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload