Results 1 to 3 of 3

Thread: Contest 11: Sorting Characters - [.paul.]

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Contest 11: Sorting Characters - [.paul.]

    code.cs

    couldn't save in dotnetfiddle. submitting a text file...

    code.cs

    Source:
    Code:
    using System;
    using System.Linq;
    					
    public class Program
    {
    	public static void Main()
    	{
    		Console.WriteLine("Enter message...");
    		String input = Console.ReadLine();
    		
    		Console.WriteLine(string.Concat(input.ToLower().Where(c => char.IsLetter(c)).OrderBy(c => c).ToArray()));		
    	}
    }
    Fiddle: https://dotnetfiddle.net/wXlZen
    Last edited by dday9; Jan 20th, 2017 at 11:18 AM. Reason: Added source and fiddle.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Contest 11: Sorting Characters - [.paul.]

    Moderator's notes:
    The time taken for 100,000 iterations of "Wow! Whenever you go outside, it is actually a very pretty world." (as per the contest description) on my computer was:
    IDE: 8432 ms

    Comments on the submission: The algorithm that sorts the text is only 1 line. The algorithm uses LINQ to do the sorting which takes a performance hit in favor of a concise code.

    There is only one minor change that could be made so that multiple variables are not declared and that is to treat the LINQ as a query:
    Code:
    Console.WriteLine(string.Concat((from c in input.ToLower() where char.IsLetter(c) orderby c select c).ToArray()));
    Though the difference to the speed on my computer was minuscule.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Contest 11: Sorting Characters - [.paul.]

    It was certainly concise, and I'd much rather program in C# than any of the other languages used, but it illustrates that a one liner is possible with LINQ but not possible in most other languages. For that reason, it would be useful to have categories in the contests, but with just four entrants, that wouldn't be very practical...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width