Last active
August 29, 2015 14:09
-
-
Save archer884/c37a3849146e90ca0581 to your computer and use it in GitHub Desktop.
Comments for YuEnDee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
namespace EasyChallenge188_Dates | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// I never bother with type names unless I have to. | |
// Also, we can skip all the streamreader crap with this: | |
var RawDates = File.ReadLines(@"../../Input/Dates.txt").ToList(); | |
var CorrectedDates = new List<string>(); | |
// I really hate regular expressions, but you gotta | |
// go with what works, right? | |
// Initialize each Regex. | |
Regex FirstForm = new Regex(@"\d{4}-\d{2}-\d{2}"); | |
Regex SecondForm = new Regex(@"\d{2}/\d{2}/\d{2}"); | |
Regex ThirdForm = new Regex(@"\d{2}#\d{2}#\d{2}"); | |
Regex FourthForm = new Regex(@"\d{2}\*\d{2}\*\d{4}"); | |
Regex FifthForm = new Regex(@"[a-zA-Z]{3} \d{2}, \d{2}"); | |
Regex SixthForm = new Regex(@"[a-zA-Z]{3} \d{2}, \d{4}"); | |
// Process each raw date. | |
foreach (string date in RawDates) | |
{ | |
// IsMatch() will return true if it succeeds | |
// We can do this because you don't use the | |
// match result anywhere else. | |
if (FirstForm.IsMatch(date)) | |
CorrectedDates.Add(date); | |
// else if prevents more than one form matching | |
// any given date--just in case | |
else if (SecondForm.IsMatch(date)) | |
{ | |
string year = date.Substring(6, 2); | |
if (Convert.ToInt32(year) < 50) | |
{ | |
year = "20" + year; | |
} | |
else | |
{ | |
year = "19" + year; | |
} | |
string month = date.Substring(0, 2); | |
string day = date.Substring(3, 2); | |
// I'm not using a variable to store the corrected | |
// date before adding it to the list: | |
CorrectedDates.Add(String.Format("{0}-{1}-{2}", year, month, day)); | |
} | |
else if (ThirdForm.IsMatch(date)) | |
{ | |
string year = date.Substring(3, 2); | |
if (Convert.ToInt32(year) < 50) | |
{ | |
year = "20" + year; | |
} | |
else | |
{ | |
year = "19" + year; | |
} | |
string month = date.Substring(0, 2); | |
string day = date.Substring(6, 2); | |
CorrectedDates.Add(String.Format("{0}-{1}-{2}", year, month, day)); | |
} | |
else if (FourthForm.IsMatch(date)) | |
{ | |
string year = date.Substring(6, 4); | |
string month = date.Substring(3, 2); | |
string day = date.Substring(0, 2); | |
CorrectedDates.Add(String.Format("{0}-{1}-{2}", year, month, day)); | |
} | |
else if (FifthForm.IsMatch(date)) | |
{ | |
// Large blocks of repeated code are a great way | |
// to introduce new and interesting bugs when | |
// making changes. | |
string month = GetMonthFromAbbreviation(date); | |
string day = date.Substring(4, 2); | |
string year = date.Substring(8, 2); | |
if (Convert.ToInt32(year) < 50) | |
{ | |
year = "20" + year; | |
} | |
else | |
{ | |
year = "19" + year; | |
} | |
CorrectedDates.Add(String.Format("{0}-{1}-{2}", year, month, day)); | |
} | |
else if (SixthForm.IsMatch(date)) | |
{ | |
// Reuse the method from FifthForm | |
string month = GetMonthFromAbbreviation(date); | |
string day = date.Substring(4, 2); | |
string year = date.Substring(8, 4); | |
CorrectedDates.Add(String.Format("{0}-{1}-{2}", year, month, day)); | |
} | |
} | |
// This part is totally unnecessary: | |
// Clear output file. | |
//if (File.Exists(@"../../Output/CorrectedDates.txt")) | |
//{ | |
// File.WriteAllText(@"../../Output/CorrectedDates.txt", String.Empty); | |
//} | |
// Again, skip streamwriter/reader nonsense whenever possible | |
File.WriteAllLines(@"../../Output/CorrectedDates.txt", CorrectedDates); | |
// If you have to use sw/sr, using blocks are the shit: | |
// Output all of the corrected dates into a file. | |
//using (var writer = new StreamWriter(@"../../Output/CorrectedDates.txt")) | |
//{ | |
// foreach (string correctDate in CorrectedDates) | |
// { | |
// writer.WriteLine(correctDate); | |
// } | |
//} | |
/* Important: | |
* ========== | |
* | |
* If you decide not to use this using pattern, you have to close the streamwriter | |
* or reader manually because, otherwise, there's really no telling what's gonna | |
* happen on exit. It's entirely possible that *nothing* will have been written to | |
* disk, or that the last part of your output will have been lost to the * nether, | |
* and all just because the buffer didn't get flushed. | |
* */ | |
// No need to return from a void method | |
//return; | |
} | |
private static string GetMonthFromAbbreviation(string date) | |
{ | |
string month = date.Substring(0, 3); | |
switch (month) | |
{ | |
case "Jan": | |
month = "01"; | |
break; | |
case "Feb": | |
month = "02"; | |
break; | |
case "Mar": | |
month = "03"; | |
break; | |
case "Apr": | |
month = "04"; | |
break; | |
case "May": | |
month = "05"; | |
break; | |
case "Jun": | |
month = "06"; | |
break; | |
case "Jul": | |
month = "07"; | |
break; | |
case "Aug": | |
month = "08"; | |
break; | |
case "Sep": | |
month = "09"; | |
break; | |
case "Oct": | |
month = "10"; | |
break; | |
case "Nov": | |
month = "11"; | |
break; | |
case "Dec": | |
month = "12"; | |
break; | |
} | |
return month; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment