|
![](/i/fill.gif) |
On 3/12/2012 2:54 AM, Orchid Win7 v1 wrote:
> On 02/12/2012 03:06 PM, Warp wrote:
>> Orchid Win7 v1<voi### [at] dev null> wrote:
>>> Here's a challenge - write me a C# function (or even just some
>>> psuedocode) which will take a string containing space-delimited positive
>>> integers, and return them in ascending order.
>>
>> If it were C++, I could write a few lines of code that does that
>
> Haskell:
>
> foobar :: String -> [Int]
> foobar = sort . map read . words
>
>
> C#:
>
> public List<Int> foobar(String s)
> {
> string[] words = s.Split(' ');
> List<Int> list = new List<Int>();
> foreach (String word in words) {list.add(int.Parse(word));}
> list.Sort();
> return list;
> }
>
>
> Not exactly taxing, eh?
Of course there are many ways to approach this in C# and most other
languages. Using LINQ and and working in decimal datatype without error
handling:
private List<decimal> ParseAndSortValuesFromString1(string text)
{
return text.Split(' ').Select(s => Decimal.Parse(s)).OrderBy(s =>
s).ToList();
}
Post a reply to this message
|
![](/i/fill.gif) |