August Code Challenge Winner

by Dan Murphy September 1, 2009 09:12 - commentComments (0)

It's offical, another winner!

Just to recap, here is our latest challenge:

Using ASP.NET, C#, and LINQ to Objects create a web page with a form that accepts a comma delimited list of integers and outputs them into their word representation followed by a line break, with an option to sort by word, integer, or original order. Also, there needs to be an option to output 10, 20, 30, or All values.

We received a lot of good solutions but one man has seperated himself from the pack.  That guys name is Nate S. from Greenville, IN.  Nate's solution hits all the high points of the requirement and outputs a very clean reponse to the user.  Great job Nate!  Your gift certificate should be in your inbox within the hour.

We truly appreciate all of our participants for taking part and stay tuned for our September challenge!

Nate's winning solution!

<!--Start of Default.aspx-->
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Aug2009CodeChallenge._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Aug2009CodeChallenge</title>
<style type="text/css">
body { font-size: 12px; font-family:Arial; padding:0px; margin:10px; border:0px;}
p { padding:0px; margin:0px; padding-bottom:5px; }
div { padding:0px; margin:0px; }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Insert a list of comma delimited numbers anywhere between -79228162514264337593543950335 and 79228162514264337593543950335</p>
<p><asp:TextBox ID="txtInts" runat="server" Width="770" /> </p>
<p>Select the number of results to return:
<asp:DropDownList ID="ddlNumberOfOutputs" runat="server">
<asp:ListItem Selected="True" Value="All" />
<asp:ListItem Value="10" />
<asp:ListItem Value="20" />
<asp:ListItem Value="30" />
</asp:DropDownList></p>
<p>Select the order in which to return:
<asp:DropDownList ID="ddlOrderToReturn" runat="server">
<asp:ListItem Selected="True" Value="Original Order" />
<asp:ListItem Value="Word" />
<asp:ListItem Value="Integer" />
</asp:DropDownList></p>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_OnClick" Text="Submit" />
<div>
<asp:DataGrid ID="dgResults" runat="server" >
<Columns>
<asp:BoundColumn HeaderText="" />
</Columns>
</asp:DataGrid>
</div>
</div>
</form>
</body>
</html>
<!--Start of Default.aspx.cs-->
using System;
using System.Linq;
using System.Collections.Generic;
namespace Aug2009CodeChallenge
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_OnClick(object sender, System.EventArgs e)
{
//TODO: Add some input validation
string[] numArray = txtInts.Text.Split(',');
int takeNum = (ddlNumberOfOutputs.SelectedItem.ToString().Equals("All")) ? numArray.Count() : int.Parse(ddlNumberOfOutputs.SelectedItem.ToString());
List<Number> numList = (from num in numArray select new Number(num)).Take(takeNum).ToList();
dgResults.DataSource = from num in numList orderby this.GetOrderBy(ddlOrderToReturn.SelectedValue, num) select num.word;
dgResults.DataBind();
}
private object GetOrderBy(string orderToReturn, Number num)
{
if(orderToReturn.Equals("Word"))
return num.word;
if(orderToReturn.Equals("Integer"))
return num.digit;
return null;
}
}
}
<!--Start of Number.cs-->
using System;
namespace Aug2009CodeChallenge
{
public partial class Number
{
public decimal digit { get; set; }
public string word { get; set; }
public Number(string d)
{
digit = decimal.Parse(d);
word = this.wordify((Decimal)digit);
}
private string wordify(decimal v)
{
if (v == 0) return "zero";
var units = "|one|two|three|four|five|six|seven|eight|nine".Split('|');
var teens = "|eleven|twelve|thir#|four#|fif#|six#|seven#|eigh#|nine#".Replace("#", "teen").Split('|');
var tens = "|ten|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety".Split('|');
var thou = "|thousand|m#|b#|tr#|quadr#|quint#|sex#|sept#|oct#".Replace("#", "illion").Split('|');
var g = (v < 0) ? "minus " : "";
var w = "";
var p = 0;
v = Math.Abs(v);
while (v > 0)
{
int b = (int)(v % 1000);
if (b > 0)
{
var h = (b / 100);
var t = (b - h * 100) / 10;
var u = (b - h * 100 - t * 10);
var s = ((h > 0) ? units[h] + " hundred" + ((t > 0 | u > 0) ? " and " : "") : "")
+ ((t > 0) ? (t == 1 && u > 0) ? teens[u] : tens[t] + ((u > 0) ? "-" : "") : "")
+ ((t != 1) ? units[u] : "");
s = (((v > 1000) && (h == 0) && (p == 0)) ? " and " : (v > 1000) ? ", " : "") + s;
w = s + " " + thou[p] + w;
}
v = v / 1000;
p++;
}
return g + w;
}
}
}
Share or Bookmark this post…
  • del.icio.us
  • Furl
  • LinkedIn
  • Technorati
  • TwitThis
  • StumbleUpon
  • Sphinn
  • Reddit
  • Propeller
  • NewsVine
  • Mixx
  • Ma.gnolia
  • Google
  • Facebook
  • DZone
  • Digg
  • DotNetKicks

Comments

Add comment




  Country flag

biuquote