Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Challenge (Out-of-Date)
New Posts  All Forums:Forum Nav:

Programming Challenge (Out-of-Date) - Page 11  

Poll Results: Are you interested in participating in and/or helping organise and post these programming challenges?

 
  • 100% (2)
    I want to participate.
  • 0% (0)
    I want to contribute by helping posting and organise these challenges.
  • 0% (0)
    I'll only take part if other people are willing to participate.
  • 0% (0)
    I can help and participate - I love programming!
  • 0% (0)
    I do not wish to participate or help.
2 Total Votes  
post #101 of 306
Here's my C# implementation of the "convert dollar amount" challenge:

Code:
using System;

namespace WrittenAmount
{
    public class ConvertDollar
    {
        public string ToWrittenAmount(string dollarAndCentsAmount)
        {
            // Strip off the leading '$'.
            string cleanDollarAndCentsAmount = ExtractDollarAmount(dollarAndCentsAmount);

            // Separate the dollars and cents.
            string[] dollarAndCents = ExtractCentsAmount(cleanDollarAndCentsAmount);
            if (dollarAndCents.Length == 0)
            {
                return string.Empty;
            }

            // Just dollars?
            if (dollarAndCents.Length == 1)
            {
                return DollarsAndCentsToText(dollarAndCents[0], "0");
            }

            // Dollars and (possibly nul) cents.
            return DollarsAndCentsToText(dollarAndCents[0], 
                    String.IsNullOrEmpty(dollarAndCents[1]) ? "0" : dollarAndCents[1]);
        }

        private string DollarsAndCentsToText(string dollars, string cents)
        {
            // ConvertDollarResources.DollarsAndCentsFormat fetches the format
            // string from the .resx file.
            return String.Format(ConvertDollarResources.DollarsAndCentsFormat, NumberToText(dollars), NumberToText(cents));
        }

        //
        // This is the "meat" of the conversion process.
        //
        private string NumberToText(string number)
        {
            string text = "";
            char[] digits = number.ToCharArray();
            for (int i = 0; i < digits.Length; ++i)
            {
                string thisNumber;

                // Ugh. Special case the teens (10 through 19).  :(
                if (IsTensPlace(i, digits.Length) && IsTeenNumber(digits[i].ToString() + digits[i + 1].ToString()))
                {
                    thisNumber = TeenNumberToText(digits[i].ToString() + digits[i + 1].ToString());
                    i += 2;
                }
                else
                {
                    thisNumber = NumberToText(digits[i].ToString(), digits.Length - i);
                }

                if (thisNumber.Length > 0)
                    text = String.Concat(text, thisNumber, " ");
            }

            return text;
        }

        private bool IsTensPlace(int currentDigit, int totalDigits)
        {
            // 1112.00
            //   ^
            //   +-- Are we here?
            if ((totalDigits - currentDigit) == 2)
                return true;
            return false;
        }

        private bool IsTeenNumber(string possibleTeen)
        {
           if (Convert.ToInt16(possibleTeen) >= 10 && Convert.ToInt16(possibleTeen) <= 19)
                return true;
            return false;
        }

        private string TeenNumberToText(string teen)
        {
            string resourceKey = "N" + teen;
            return GetResourceString(resourceKey);
        }

        private string NumberToText(string digit, int place)
        {
            // i.e. "N9_4" is equivalent to "nine thousand"
            string resourceKey = "N" + digit + "_" + place.ToString();
            return GetResourceString(resourceKey);
        }

        private string ExtractDollarAmount(string dollarAmount)
        {
            int indexOfDollarSign = dollarAmount.IndexOf('$');
            if (indexOfDollarSign < 0)
            {
                return dollarAmount;
            }

            return dollarAmount.Remove(indexOfDollarSign, 1);
        }

        private string[] ExtractCentsAmount(string amount)
        {
            return amount.Split(new Char[] { '.' });
        }

        private string GetResourceString(string key)
        {
            return ConvertDollarResources.ResourceManager.GetString(key);
        }


    }
}
NUnit test fixture:
Code:
using System;
using NUnit.Framework;
using WrittenAmount;

namespace WrittenAmount.UnitTest
{
    [TestFixture]
    public class ConvertDollarTests
    {
        [Test]
        public void ToText_0()
        {
            WrittenAmount.ConvertDollar converter = new WrittenAmount.ConvertDollar();
            string text = converter.ToWrittenAmount("$0.0");
            StringAssert.AreEqualIgnoringCase("zero dollars and zero cents", text);
        }

        [Test]
        public void ToText_0_16()
        {
            WrittenAmount.ConvertDollar converter = new WrittenAmount.ConvertDollar();
            string text = converter.ToWrittenAmount("$0.16");
            StringAssert.AreEqualIgnoringCase("zero dollars and sixteen cents", text);
        }

        [Test]
        public void ToText_16_0()
        {
            WrittenAmount.ConvertDollar converter = new WrittenAmount.ConvertDollar();
            string text = converter.ToWrittenAmount("$16.0");
            StringAssert.AreEqualIgnoringCase("sixteen dollars and zero cents", text);
            text = converter.ToWrittenAmount("$16.00");
            StringAssert.AreEqualIgnoringCase("sixteen dollars and zero cents", text);
        }

        [Test]
        public void ToText_1_03()
        {
            WrittenAmount.ConvertDollar converter = new WrittenAmount.ConvertDollar();
            string text = converter.ToWrittenAmount("$1.03");
            StringAssert.AreEqualIgnoringCase("one dollars and three cents", text);
        }

        [Test]
        public void ToText_1_23()
        {
            WrittenAmount.ConvertDollar converter = new WrittenAmount.ConvertDollar();
            string text = converter.ToWrittenAmount("$1.23");
            StringAssert.AreEqualIgnoringCase("one dollars and twenty three cents", text);
        }

        [Test]
        public void ToText_10_23()
        {
            WrittenAmount.ConvertDollar converter = new WrittenAmount.ConvertDollar();
            string text = converter.ToWrittenAmount("$10.23");
            StringAssert.AreEqualIgnoringCase("ten dollars and twenty three cents", text);
        }

        [Test]
        public void ToText_12_34()
        {
            WrittenAmount.ConvertDollar converter = new WrittenAmount.ConvertDollar();
            string text = converter.ToWrittenAmount("$12.34");
            StringAssert.AreEqualIgnoringCase("twelve dollars and thirty four cents", text);
        }

        [Test]
        public void ToText_112_34()
        {
            WrittenAmount.ConvertDollar converter = new WrittenAmount.ConvertDollar();
            string text = converter.ToWrittenAmount("$112.34");
            StringAssert.AreEqualIgnoringCase("one hundred twelve dollars and thirty four cents", text);
        }

        [Test]
        public void ToText_123_45()
        {
            WrittenAmount.ConvertDollar converter = new WrittenAmount.ConvertDollar();
            string text = converter.ToWrittenAmount("$123.45");
            StringAssert.AreEqualIgnoringCase("one hundred twenty three dollars and fourty five cents", text);
        }

        [Test]
        public void ToText_1234_56()
        {
            WrittenAmount.ConvertDollar converter = new WrittenAmount.ConvertDollar();
            string text = converter.ToWrittenAmount("$1234.56");
            StringAssert.AreEqualIgnoringCase("one thousand two hundred thirty four dollars and fifty six cents", text);
        }

        [Test]
        public void ToText_9999()
        {
            WrittenAmount.ConvertDollar converter = new WrittenAmount.ConvertDollar();
            string text = converter.ToWrittenAmount("$9999");
            StringAssert.AreEqualIgnoringCase("nine thousand nine hundred ninety nine dollars and zero cents", text);
        }

        [Test]
        public void ToText_9999_()
        {
            WrittenAmount.ConvertDollar converter = new WrittenAmount.ConvertDollar();
            string text = converter.ToWrittenAmount("$9999.");
            StringAssert.AreEqualIgnoringCase("nine thousand nine hundred ninety nine dollars and zero cents", text);
        }
    }
}
post #102 of 306
Is anyone up for some NTLM hash work?
Burning Phoenix
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q9550 Gigabyte EP45-UD3P eVGA GTX 260 Core 216 SuperClocked 4GB G.Skill DDR2-1000 5-5-5-15 
Hard DriveOptical DriveOSMonitor
3 WD 80GB RAID0, Seagate 500GB, WD 1TB Caviar BLCK Lite-On LH-20A1S Windoze 7 Professional Dual Dell E207WFP 20.1" Widescreen LCD's 
KeyboardPowerCaseMouse
Logitech G15 Silverstone OP750 Lian-Li PC-A10B Logitech G5 
Mouse Pad
Harley Davidson 
  hide details  
Burning Phoenix
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q9550 Gigabyte EP45-UD3P eVGA GTX 260 Core 216 SuperClocked 4GB G.Skill DDR2-1000 5-5-5-15 
Hard DriveOptical DriveOSMonitor
3 WD 80GB RAID0, Seagate 500GB, WD 1TB Caviar BLCK Lite-On LH-20A1S Windoze 7 Professional Dual Dell E207WFP 20.1" Widescreen LCD's 
KeyboardPowerCaseMouse
Logitech G15 Silverstone OP750 Lian-Li PC-A10B Logitech G5 
Mouse Pad
Harley Davidson 
  hide details  
post #103 of 306
Just wondering, can we start this again?
Damit
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x4 965 Black MSI 790FX-GD70 Asus ATI 5850 4x2 GBs RipJaw DDR3 1066 MHz 
Hard DriveOptical DriveOSMonitor
RAID 0 500 GB WD Black Lite-On Blue Ray, Samsung DVD Arch Linux/Ubuntu 10.04 25.5" Samsung 
KeyboardPowerCaseMouse
PS2 by Compaq 750 Watt COOLER MASTER Elite RC-332-KKN1-GP Death Adder 
Mouse Pad
Custom 
  hide details  
Damit
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x4 965 Black MSI 790FX-GD70 Asus ATI 5850 4x2 GBs RipJaw DDR3 1066 MHz 
Hard DriveOptical DriveOSMonitor
RAID 0 500 GB WD Black Lite-On Blue Ray, Samsung DVD Arch Linux/Ubuntu 10.04 25.5" Samsung 
KeyboardPowerCaseMouse
PS2 by Compaq 750 Watt COOLER MASTER Elite RC-332-KKN1-GP Death Adder 
Mouse Pad
Custom 
  hide details  
post #104 of 306
i'd like to join too.
Pepe Silvia
(13 items)
 
  
CPUMotherboardGraphicsRAM
255 @ 4 Ghz MSI 870-C45 GT 520 @ 1Ghz Crucial Rendition 4GB DDR3 
Hard DriveOSMonitorPower
Hitachi 1TB Win 7 19" 420W 
  hide details  
Pepe Silvia
(13 items)
 
  
CPUMotherboardGraphicsRAM
255 @ 4 Ghz MSI 870-C45 GT 520 @ 1Ghz Crucial Rendition 4GB DDR3 
Hard DriveOSMonitorPower
Hitachi 1TB Win 7 19" 420W 
  hide details  
post #105 of 306
Apparently no one is interested?
Damit
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x4 965 Black MSI 790FX-GD70 Asus ATI 5850 4x2 GBs RipJaw DDR3 1066 MHz 
Hard DriveOptical DriveOSMonitor
RAID 0 500 GB WD Black Lite-On Blue Ray, Samsung DVD Arch Linux/Ubuntu 10.04 25.5" Samsung 
KeyboardPowerCaseMouse
PS2 by Compaq 750 Watt COOLER MASTER Elite RC-332-KKN1-GP Death Adder 
Mouse Pad
Custom 
  hide details  
Damit
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x4 965 Black MSI 790FX-GD70 Asus ATI 5850 4x2 GBs RipJaw DDR3 1066 MHz 
Hard DriveOptical DriveOSMonitor
RAID 0 500 GB WD Black Lite-On Blue Ray, Samsung DVD Arch Linux/Ubuntu 10.04 25.5" Samsung 
KeyboardPowerCaseMouse
PS2 by Compaq 750 Watt COOLER MASTER Elite RC-332-KKN1-GP Death Adder 
Mouse Pad
Custom 
  hide details  
post #106 of 306
this sounds like fun..good thing I checked the date before trying any of the challenges

Someone should start this up again
Main computer
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Pentium E2180 ASUS P5N-D Radeon HD 4870 4GB 
Hard DriveOptical DriveOSMonitor
250GB Lightscribe Windows 7(x64)/Ubuntu 9.10(x32) dual boot Dell 1901FP 
KeyboardPowerCaseMouse
HP 675w ATX X-K1 Series HP 
Mouse Pad
asus 
  hide details  
Main computer
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Pentium E2180 ASUS P5N-D Radeon HD 4870 4GB 
Hard DriveOptical DriveOSMonitor
250GB Lightscribe Windows 7(x64)/Ubuntu 9.10(x32) dual boot Dell 1901FP 
KeyboardPowerCaseMouse
HP 675w ATX X-K1 Series HP 
Mouse Pad
asus 
  hide details  
post #107 of 306
I'll see if I can come up with anything.
Damit
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x4 965 Black MSI 790FX-GD70 Asus ATI 5850 4x2 GBs RipJaw DDR3 1066 MHz 
Hard DriveOptical DriveOSMonitor
RAID 0 500 GB WD Black Lite-On Blue Ray, Samsung DVD Arch Linux/Ubuntu 10.04 25.5" Samsung 
KeyboardPowerCaseMouse
PS2 by Compaq 750 Watt COOLER MASTER Elite RC-332-KKN1-GP Death Adder 
Mouse Pad
Custom 
  hide details  
Damit
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x4 965 Black MSI 790FX-GD70 Asus ATI 5850 4x2 GBs RipJaw DDR3 1066 MHz 
Hard DriveOptical DriveOSMonitor
RAID 0 500 GB WD Black Lite-On Blue Ray, Samsung DVD Arch Linux/Ubuntu 10.04 25.5" Samsung 
KeyboardPowerCaseMouse
PS2 by Compaq 750 Watt COOLER MASTER Elite RC-332-KKN1-GP Death Adder 
Mouse Pad
Custom 
  hide details  
post #108 of 306
could do some old ACM competition questions.
    
CPUMotherboardOSMonitor
2500k P8P67 Windows 7 Ultimate x64 22" phillips 
KeyboardPowerMouseMouse Pad
Cheap Logitech Antec Earthwatts 650W Razer Deathadder Razer eXactMat 
  hide details  
    
CPUMotherboardOSMonitor
2500k P8P67 Windows 7 Ultimate x64 22" phillips 
KeyboardPowerMouseMouse Pad
Cheap Logitech Antec Earthwatts 650W Razer Deathadder Razer eXactMat 
  hide details  
post #109 of 306
Quote:
Originally Posted by serge2k View Post
could do some old ACM competition questions.
Do you have access to those?
Damit
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x4 965 Black MSI 790FX-GD70 Asus ATI 5850 4x2 GBs RipJaw DDR3 1066 MHz 
Hard DriveOptical DriveOSMonitor
RAID 0 500 GB WD Black Lite-On Blue Ray, Samsung DVD Arch Linux/Ubuntu 10.04 25.5" Samsung 
KeyboardPowerCaseMouse
PS2 by Compaq 750 Watt COOLER MASTER Elite RC-332-KKN1-GP Death Adder 
Mouse Pad
Custom 
  hide details  
Damit
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x4 965 Black MSI 790FX-GD70 Asus ATI 5850 4x2 GBs RipJaw DDR3 1066 MHz 
Hard DriveOptical DriveOSMonitor
RAID 0 500 GB WD Black Lite-On Blue Ray, Samsung DVD Arch Linux/Ubuntu 10.04 25.5" Samsung 
KeyboardPowerCaseMouse
PS2 by Compaq 750 Watt COOLER MASTER Elite RC-332-KKN1-GP Death Adder 
Mouse Pad
Custom 
  hide details  
post #110 of 306
http://cm.baylor.edu/ICPCWiki/Wiki.jsp?page=Problems

archive seems to be down though. The questions for the finals work but the input files don't seem to be there.

Their is also the UVA online judge which has similar problems.
http://uva.onlinejudge.org/

I was looking at old google code jam problems last night. A lot are simple but still might be interesting.
    
CPUMotherboardOSMonitor
2500k P8P67 Windows 7 Ultimate x64 22" phillips 
KeyboardPowerMouseMouse Pad
Cheap Logitech Antec Earthwatts 650W Razer Deathadder Razer eXactMat 
  hide details  
    
CPUMotherboardOSMonitor
2500k P8P67 Windows 7 Ultimate x64 22" phillips 
KeyboardPowerMouseMouse Pad
Cheap Logitech Antec Earthwatts 650W Razer Deathadder Razer eXactMat 
  hide details  
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
This thread is locked  
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Challenge (Out-of-Date)