/* solves the problem posted at http://www.hackerrank.com/challenges/fraud-prevention */ using System; using System.Linq; using System.Collections.Generic; using System.Text; class FraudPrevention { static void Main(String[] args) { var file = new System.IO.StreamReader(@"C:\path\file.txt"); Console.SetIn(file); int numRecords = int.Parse(Console.ReadLine()); var FraudulentOrders = new HashSet(); var Orderz = new Dictionary(); for (int i = 0; i < numRecords; i++) { var o = new Order(Console.ReadLine().Split(',')); Orderz[o.OrderID] = o; } foreach (var deal in Order.DealIDs) { if (deal.Value.Count > 1) { var byDeal = new List(); //create a list of orders (small list) foreach (var id in deal.Value) //foreach item with this deal ID { byDeal.Add(Orderz[id]); //create a list of objects } var byEmail = byDeal.GroupBy(x => x.Email).Where(x => x.Count() > 1).SelectMany(x => x); var byAdds = byDeal.GroupBy(x => x.Address).Where(x => x.Count() > 1).SelectMany(x => x); var byE_CC = byEmail.GroupBy(x => x.ccNum).Where(x => x.Count() >= 1).SelectMany(x => x); var byA_CC = byAdds.GroupBy(x => x.ccNum).Where(x => x.Count() == 1).SelectMany(x => x); foreach (var item in byE_CC) { FraudulentOrders.Add(item.OrderID); } foreach (var item in byA_CC) { FraudulentOrders.Add(item.OrderID); } } } var answer = new StringBuilder(); foreach (var item in FraudulentOrders.OrderBy(x => x)) { answer.Append(item + ","); } Console.WriteLine(answer.ToString().Substring(0, answer.Length - 1)); } /// /// the order class /// public class Order { /// /// a unique collection of orderids /// private HashSet OrderIDs = new HashSet(); //ensures orderID uniqueness /// /// a collection of deal ids /// public static Dictionary> DealIDs = new Dictionary>(); /// /// the id for the order /// public int OrderID { get; set; } /// /// the id for the deal /// public int DealID { get; set; } /// /// the full email /// public Tuple Email { get { return new Tuple(this.User, this.Domain); } } /// /// the user /// public string User { get; set; } /// /// the email domain /// public string Domain { get; set; } /// /// a tuple representation of the address /// public Tuple Address { get { return new Tuple(this.Street, this.City, this.State, this.Zip); } } /// /// the city /// public string City { get; set; } /// /// the street address /// public string Street { get; set; } /// /// the state /// public string State { get; set; } /// /// the zip code /// public int Zip { get; set; } /// /// the credit card number /// public long ccNum { get; set; } /// /// constructor which takes a string array /// /// public Order(string[] s) { if (s.Length != 8) { throw new Exception("Wrong Number of fields in record"); } try { var orderID = int.Parse(s[0]); if (!this.OrderIDs.Add(orderID)) { throw new Exception("Order ID is not Unique!"); } else { this.OrderID = orderID; } } catch (Exception e) { throw new Exception("Order ID creation Error:", e); } try { this.DealID = int.Parse(s[1]); if (DealIDs.ContainsKey(this.DealID)) { DealIDs[this.DealID].Add(this.OrderID); } else { DealIDs.Add(this.DealID, new List()); //add key and initialize list DealIDs[this.DealID].Add(this.OrderID); //and add this to the list } } catch { throw new Exception("Deal ID creation Error"); } try { var email = s[2].ToLower(); int pos = email.IndexOf("@"); if (pos < 0) { throw new Exception("Must contain @"); } var user = email.Substring(0, pos); //split var domain = email.Substring(pos + 1); if (!domain.Contains(".")) { throw new Exception("Domain Error, no '.' found"); } while (user.Contains(".")) { pos = user.IndexOf("."); //find them user = user.Remove(pos, 1); //and remove them } if (user.Contains("+")) { pos = user.IndexOf("+"); //find the first user = user.Remove(pos); //and remove everything after } this.User = user; this.Domain = domain; } catch (Exception e) { throw new Exception("Invalid Email Address:", e); } try { var street = s[3].ToLower(); var city = s[4].ToLower(); var state = s[5].ToLower(); var zip = s[6]; int pos = zip.IndexOf('-'); if (pos > 0) { zip = zip.Remove(pos, 1); } try { this.Zip = int.Parse(zip); } catch { throw new Exception("Zipcode Error"); } if (street.Contains("street")) { street = street.Replace("street", "st."); } if (street.Contains("road")) { street = street.Replace("road", "rd."); } switch (state) { case "illinois": state = "il"; break; case "new york": state = "ny"; break; case "california": state = "ca"; break; //todo: implement additional states switches } this.Street = street; this.City = city; this.State = state; } catch (Exception e) { throw new Exception("Problem with Address:", e); } var cc = s[7].Trim(); try { this.ccNum = long.Parse(cc); } catch { throw new Exception("Credit Card Number contains invalid characters"); } } /// /// Overrides the ToString() method to provide additional information for debugging /// /// public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append("================================" + "\n Order Details:" + "\n================================" + "\nOrderID = " + this.OrderID + "\nDealID = " + this.DealID + "\nEmail = " + this.User + "@" + this.Domain + "\nccNum = " + this.ccNum + "\nAddress = " + this.Street + "\n " + this.City + ", " + this.State.ToUpper() + ", " + this.Zip); return sb.ToString(); } } }