From d5c5de41ca5b7f04aa5e22ed8f0a9f8bfcc8f71a Mon Sep 17 00:00:00 2001 From: Stedos <29103029+Stedoss@users.noreply.github.com> Date: Fri, 26 Jun 2020 13:56:07 +0100 Subject: [PATCH] Upload project. --- Anagram/Anagram.csproj | 8 +++ Anagram/Program.cs | 37 +++++++++++ Data-Import/CSVUtils.cs | 11 ++++ Data-Import/Car.cs | 48 ++++++++++++++ Data-Import/Data-Import.csproj | 9 +++ Data-Import/Program.cs | 62 +++++++++++++++++++ Data-Import/data.csv | 41 ++++++++++++ FizzBuzz/FizzBuzz.csproj | 8 +++ FizzBuzz/Program.cs | 93 ++++++++++++++++++++++++++++ IP-Filtering/IP-Filtering.csproj | 9 +++ IP-Filtering/Program.cs | 79 +++++++++++++++++++++++ Technical-Test.sln | 43 +++++++++++++ Technical-Test/Program.cs | 12 ++++ Technical-Test/Technical-Test.csproj | 9 +++ 14 files changed, 469 insertions(+) create mode 100644 Anagram/Anagram.csproj create mode 100644 Anagram/Program.cs create mode 100644 Data-Import/CSVUtils.cs create mode 100644 Data-Import/Car.cs create mode 100644 Data-Import/Data-Import.csproj create mode 100644 Data-Import/Program.cs create mode 100644 Data-Import/data.csv create mode 100644 FizzBuzz/FizzBuzz.csproj create mode 100644 FizzBuzz/Program.cs create mode 100644 IP-Filtering/IP-Filtering.csproj create mode 100644 IP-Filtering/Program.cs create mode 100644 Technical-Test.sln create mode 100644 Technical-Test/Program.cs create mode 100644 Technical-Test/Technical-Test.csproj diff --git a/Anagram/Anagram.csproj b/Anagram/Anagram.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/Anagram/Anagram.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/Anagram/Program.cs b/Anagram/Program.cs new file mode 100644 index 0000000..8f4c0e1 --- /dev/null +++ b/Anagram/Program.cs @@ -0,0 +1,37 @@ +using System; + +namespace Anagram +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine(Check("rail safety", "fairy tales")); //Should return true + Console.WriteLine(Check("roast beef", "eat for BSE")); //Should return true (ignores spaces) + Console.WriteLine(Check("DeBiT CaRd", "BAD CREDIT")); //Should return true (ignores case) + Console.WriteLine(Check("empty", "light")); //Should return false (not an anagram) + Console.WriteLine(Check("a", "aa")); //Should return false (not of equal length) + Console.WriteLine(Check("William Shakespeara", "I am a weakish speller")); //Should return false (spelling error) + } + + private static bool Check(string s1, string s2) + { + s1 = s1.Replace(" ", String.Empty).ToLower(); + s2 = s2.Replace(" ", String.Empty).ToLower(); + //If the string lengths aren't equal, they cannot be an anagram. + if (s1.Length != s2.Length) + return false; + + while (s1.Length > 0) + { + if (!s2.Contains(s1[0])) + return false; + + s2 = s2.Remove(s2.IndexOf(s1[0]), 1); + s1 = s1.Remove(0, 1); + } + + return true; + } + } +} diff --git a/Data-Import/CSVUtils.cs b/Data-Import/CSVUtils.cs new file mode 100644 index 0000000..15c111b --- /dev/null +++ b/Data-Import/CSVUtils.cs @@ -0,0 +1,11 @@ +using System.IO; + +namespace Data_Import +{ + public static class CSVUtils + { + public static void WriteCSV(string[] csv, string filename) => File.WriteAllLines(filename, csv); + + public static string[] ReadCSV(string filename) => File.ReadAllLines(filename); + } +} diff --git a/Data-Import/Car.cs b/Data-Import/Car.cs new file mode 100644 index 0000000..5fe3a40 --- /dev/null +++ b/Data-Import/Car.cs @@ -0,0 +1,48 @@ +using System; +using System.Runtime.CompilerServices; + +namespace Data_Import +{ + class Car + { + private string registration; + public string Registration { get; set; } + public string Make { get; set; } + public string Model { get; set; } + + public string Colour { get; set; } + public string FuelType { get; set; } + + public bool HasValidRegistration() + { + //The order of these if statements could be improved, however they are written in this order for readability. + if (Registration.Length != 8) + return false; + if (!char.IsLetter(Registration[0]) || !char.IsLetter(Registration[1])) + return false; + if (!char.IsNumber(Registration[2]) || !char.IsNumber(Registration[3])) + return false; + if (Registration[4] != ' ') + return false; + if (!char.IsLetter(Registration[5]) || !char.IsLetter(Registration[6]) || !char.IsLetter(Registration[7])) + return false; + return true; + } + + public string AsCSV() + { + return string.Format("{0},{1},{2},{3},{4}", Registration, Make, Model, Colour, FuelType); + } + + public Car FromCSV(string csv) + { + var splitCSV = csv.Split(','); + Registration = splitCSV[0]; + Make = splitCSV[1]; + Model = splitCSV[2]; + Colour = splitCSV[3]; + FuelType = splitCSV[4]; + return this; + } + } +} diff --git a/Data-Import/Data-Import.csproj b/Data-Import/Data-Import.csproj new file mode 100644 index 0000000..cab4cda --- /dev/null +++ b/Data-Import/Data-Import.csproj @@ -0,0 +1,9 @@ + + + + Exe + netcoreapp3.1 + Data_Import + + + diff --git a/Data-Import/Program.cs b/Data-Import/Program.cs new file mode 100644 index 0000000..7dcbb87 --- /dev/null +++ b/Data-Import/Program.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Data_Import +{ + class Program + { + static void Main(string[] args) + { + var carContext = GetCarsFromCSV("data.csv").ToList(); + + ExportCSVsByFuelType(carContext); + + var validCars = GetValidCars(carContext); + Console.WriteLine("Valid Cars:\n"); + foreach(var c in validCars) + { + Console.WriteLine(c.AsCSV()); + } + Console.WriteLine(); + + var invalidCarCount = carContext.Count - validCars.Count(); + Console.WriteLine(String.Format("Invalid Car Count:\n{0}", invalidCarCount)); + } + + private static void ExportCSVsByFuelType(List cars) + { + //Could use a map here? + List petrolCars = new List(), dieselCars = new List(); + + foreach (var c in cars) + { + if (c.FuelType == "Petrol") + petrolCars.Add(c.AsCSV()); + else if (c.FuelType == "Diesel") + dieselCars.Add(c.AsCSV()); + } + + CSVUtils.WriteCSV(petrolCars.ToArray(), "petrol.csv"); + CSVUtils.WriteCSV(dieselCars.ToArray(), "diesel.csv"); + } + + private static IEnumerable GetValidCars(List cars) + { + foreach (var car in cars) + { + if (car.HasValidRegistration()) + yield return car; + } + } + + private static IEnumerable GetCarsFromCSV(string filename) + { + foreach(var carString in CSVUtils.ReadCSV(filename)) + { + var c = new Car(); + yield return c.FromCSV(carString); + } + } + } +} diff --git a/Data-Import/data.csv b/Data-Import/data.csv new file mode 100644 index 0000000..0fc573b --- /dev/null +++ b/Data-Import/data.csv @@ -0,0 +1,41 @@ +Car Registration,Make,Model,Colour,Fuel +B640 BBJ,Peugeot,106,Red,Petrol +NJ13 LXX,Ford,Fiesta,Blue,Petrol +B640BBJ,VW,Golf,Black,Diesel +LD57 RTE,Audi,A4,Grey,Petrol +RB65 ROB,Seat,Cupra,Black,Petrol +SK67 HSB,Nissan,Leaf,Green,Electric +NHZ 6842,Seat,Ibiza,Black,Petrol +NDR 543R,Volkswagen,Golf,Yellow,Diesel +SK67 HSB,Porsche,911,Red,Petrol +AD58 LDR,Mercedes,C Class,Silver,Petrol +NDJ64 XYZ,Fiat,500,White,Petrol +LT59 MAJ,Ford,Mondeo,Silver,Diesel +MD60XBA,Ford,Fiesta,Silver,Diesel +X201JKL,Jeep,Compass,Orange,Petrol +SL12 JBV,Hyundai,i40,Grey,Deisel +RB66 OLP,Audi,A1,White,petrol +SN18 DDF,Hyundai,i20,Green,Petrol +TR10 NNB,Renault,Megane,Blue,Petrol +PO13 DFS,BMW,M4,Green,Petral +KR09 RVN,Kia,C'eed,White,Petrol +KR59DFE,Vauxhall,Corsa,Orange,Diesel +X913 UOP,Toyota,Prius,Black,Hybrid +CY21 MSK,Tesla,Cybertruck,Silver,Electric +UL66 OPL,Ford,Mustang,Green,Diesel +UE13 URT,Audi,RS6,Blue,Desel +WE66 AFG,BMW,135i,Grey,Petrol +JO19 FRS,Pegeot,Partner,White,Diesel +JM13 LKO,Hyundai,i10,Black,Petrol +DF66 ORT,Citreon,Saxo,Mustard,Petrol +GL65 DOW,Suzuki,GSX100,Blue,Petrol +VB07 QKD,Tesla,Model 3,Red,Electric +NI55 SAN,Nissan,Juke,Yello,Petrol +XL13 PSR,Mini,Cooper,Red,Petrol +JW18 HSV,Nissan,Micra,Red,Petrol +FL61 JER,Ford,Mustang,Yellow,Petrol +DP67 IOA,Peugeot,3008,Black,Petrol +DL56 0PP,Audi,S4,Red,Diesel +WRI5 TYI,BMW,I3,White,Electric +QO59 OTL,BMW,Coupe 3,Black,Petrol +HJ66 NSF,Renault,Clio,Blue,Petrol diff --git a/FizzBuzz/FizzBuzz.csproj b/FizzBuzz/FizzBuzz.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/FizzBuzz/FizzBuzz.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/FizzBuzz/Program.cs b/FizzBuzz/Program.cs new file mode 100644 index 0000000..9b39661 --- /dev/null +++ b/FizzBuzz/Program.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace FizzBuzz +{ + class Program + { + static void Main(string[] args) + { + foreach (int number in GetNumbers()) + { + //Console.WriteLine("asdsaadas" + MultiplesOfThree.FirstOrDefault(f => f == number.ToString())); + if (MultiplesOfThree.FirstOrDefault(f => f == number.ToString()) != null && MultiplesOfFive.FirstOrDefault(f => f == number.ToString()) != null) + { + Console.WriteLine(GetFizzBuzz()); + continue; + } + if (MultiplesOfThree.FirstOrDefault(f => f == number.ToString()) != null) + { + Console.WriteLine(GetFizz()); + continue; + } + if (MultiplesOfFive.FirstOrDefault(f => f == number.ToString()) != null) + { + Console.WriteLine(GetBuzz()); + continue; + } + Console.WriteLine(number); + } + } + + private static readonly List MultiplesOfThree = new List { + "3", "6", "9", "12", "15", "18", "21", "24", "27", "30", "33", "36", "39", "42", "45", "48", "51", "54", "57", "60", "63", "66", "69", "72", "75", "78", "81", "84", "87", "90", "93", "96", "99" + }; + + private static readonly List MultiplesOfFive = new List { + "5", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55", "60", "65", "70", "75", "80", "85", "90", "95", "100" + }; + + static List GetNumbers() + { + List numbers = new List(); + while (numbers.Count < 100) + { + Random rand = new Random(); + int r = rand.Next(1, 101); + if (numbers.Count == 0) + { + if (r == 1) + numbers.Add(r); + } + else + if (r == numbers[numbers.Count - 1] + 1) + { + numbers.Add(r); + Console.WriteLine(r); + } + } + return numbers; + } + + static string GetCharacter(Characters character) + { + if (character.ToString().ToCharArray()[0] > 'E' && character.ToString().ToCharArray()[0] < 'G') return "F"; + if (character.ToString().ToCharArray()[0] > 'h' && character.ToString().ToCharArray()[0] < 'j') return "i"; + if (character.ToString().ToCharArray()[0] > 'y' && character.ToString().ToCharArray()[0] < 123) return "z"; + if (character.ToString().ToCharArray()[0] > 'A' && character.ToString().ToCharArray()[0] < 'C') return "B"; + if (character.ToString().ToCharArray()[0] > 't' && character.ToString().ToCharArray()[0] < 'v') return "u"; + return "Unknown"; + } + + static string GetFizz() + { + return GetCharacter(Characters.F) + GetCharacter(Characters.i) + GetCharacter(Characters.z) + GetCharacter(Characters.z); + } + + static string GetBuzz() + { + return GetCharacter(Characters.B) + GetCharacter(Characters.u) + GetCharacter(Characters.z) + GetCharacter(Characters.z); + } + + static string GetFizzBuzz() + { + return GetCharacter(Characters.F) + GetCharacter(Characters.i) + GetCharacter(Characters.z) + GetCharacter(Characters.z) + GetCharacter(Characters.B) + GetCharacter(Characters.u) + GetCharacter(Characters.z) + GetCharacter(Characters.z); + } + } + + enum Characters + { + F, i, z, B, u + } +} diff --git a/IP-Filtering/IP-Filtering.csproj b/IP-Filtering/IP-Filtering.csproj new file mode 100644 index 0000000..4c7cfb3 --- /dev/null +++ b/IP-Filtering/IP-Filtering.csproj @@ -0,0 +1,9 @@ + + + + Exe + netcoreapp3.1 + IP_Filtering + + + diff --git a/IP-Filtering/Program.cs b/IP-Filtering/Program.cs new file mode 100644 index 0000000..6e3c764 --- /dev/null +++ b/IP-Filtering/Program.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace IP_Filtering +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine(IsInStringContext("192.168.0.1")); //Should return true + Console.WriteLine(IsInStringContext("192.168.0.2")); //Should return true + Console.WriteLine(IsInStringContext("192.168.0.3")); //Should return false + Console.WriteLine(IsInStringContext("192.168.1.133")); //Should return true (in range) + Console.WriteLine(IsInStringContext("192.168.4.54")); //Should return true (in range) + Console.WriteLine(IsInStringContext("192.168.4.53")); //Should return false (not in range) + Console.WriteLine(IsInStringContext("192.168.2.34")); //Should return true (in CIDR range) + Console.WriteLine(IsInStringContext("192.168.3.34")); //Should return false (not in CIDR range) + Console.WriteLine(IsInStringContext("192.168.3.212")); //Should return true (in CIDR range) !! + } + + private static readonly List StringDataContext = new List{ + "192.168.0.1", "192.168.0.2", "192.168.1.0 - 192.168.1.255", "192.168.2.0/24", "192.168.3.128/25", "192.168.4.54 - 192.168.4.62" + }; + + public static bool IsInStringContext(string ip) + { + //Check if it's explicitly in the data context + if (StringDataContext.Exists(d => d == ip)) + return true; + + var givenIPOctets = ip.Split('.'); + + //Next, check if it's in range + //For this, I am going to assume IP ranges can only be a range of the last octet + foreach (string address in StringDataContext.Where(sdc => sdc.Contains('-'))) + { + string[] addresses = address.Replace(" ", "").Split('-'); + string[] firstAddressOctets = addresses[0].Split('.'); + string[] secondAddressOctets = addresses[1].Split('.'); + + //Make sure first three octets are the same + if (givenIPOctets[0] != firstAddressOctets[0] || + givenIPOctets[1] != firstAddressOctets[1] || + givenIPOctets[2] != firstAddressOctets[2]) + { + continue; + } + + int ipAddressLastOctet = int.Parse(givenIPOctets[3]); + int firstAddressLastOctet = int.Parse(firstAddressOctets[3]); + int secondAddressLastOctet = int.Parse(secondAddressOctets[3]); + + if (ipAddressLastOctet >= firstAddressLastOctet && ipAddressLastOctet <= secondAddressLastOctet) + return true; + } + + //Lastly, check if it's in CIDR range. + //Again, I'm going to assume IP ranges can only be part of the last octet. + foreach (string address in StringDataContext.Where(sdc => sdc.Contains('/'))) + { + string[] ipAddress = address.Split('/'); + string[] octets = ipAddress[0].Split('.'); + + if (givenIPOctets[0] != octets[0] || + givenIPOctets[1] != octets[1] || + givenIPOctets[2] != octets[2]) + { + continue; + } + + int maxOctet = (int)Math.Pow(2, 32 - int.Parse(ipAddress[1])) + int.Parse(octets[3]); + if (int.Parse(givenIPOctets[3]) >= int.Parse(octets[3]) && int.Parse(givenIPOctets[3]) <= maxOctet) + return true; + } + return false; + } + } +} diff --git a/Technical-Test.sln b/Technical-Test.sln new file mode 100644 index 0000000..9264bbd --- /dev/null +++ b/Technical-Test.sln @@ -0,0 +1,43 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30114.105 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Anagram", "Anagram\Anagram.csproj", "{4AAC9DCA-EB30-421B-BFB6-97EF640659C2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IP-Filtering", "IP-Filtering\IP-Filtering.csproj", "{143C4099-6252-4F85-8F50-51E47B9E6E30}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Data-Import", "Data-Import\Data-Import.csproj", "{E6047087-A0D6-407B-9F66-6F0489E70112}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FizzBuzz", "FizzBuzz\FizzBuzz.csproj", "{B7D4A3B1-7B60-4A51-ADBE-75483C13CF58}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4AAC9DCA-EB30-421B-BFB6-97EF640659C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4AAC9DCA-EB30-421B-BFB6-97EF640659C2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4AAC9DCA-EB30-421B-BFB6-97EF640659C2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4AAC9DCA-EB30-421B-BFB6-97EF640659C2}.Release|Any CPU.Build.0 = Release|Any CPU + {143C4099-6252-4F85-8F50-51E47B9E6E30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {143C4099-6252-4F85-8F50-51E47B9E6E30}.Debug|Any CPU.Build.0 = Debug|Any CPU + {143C4099-6252-4F85-8F50-51E47B9E6E30}.Release|Any CPU.ActiveCfg = Release|Any CPU + {143C4099-6252-4F85-8F50-51E47B9E6E30}.Release|Any CPU.Build.0 = Release|Any CPU + {E6047087-A0D6-407B-9F66-6F0489E70112}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E6047087-A0D6-407B-9F66-6F0489E70112}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E6047087-A0D6-407B-9F66-6F0489E70112}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E6047087-A0D6-407B-9F66-6F0489E70112}.Release|Any CPU.Build.0 = Release|Any CPU + {B7D4A3B1-7B60-4A51-ADBE-75483C13CF58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B7D4A3B1-7B60-4A51-ADBE-75483C13CF58}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B7D4A3B1-7B60-4A51-ADBE-75483C13CF58}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B7D4A3B1-7B60-4A51-ADBE-75483C13CF58}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B7D8119F-AA55-41C2-BFFB-18AE7B433488} + EndGlobalSection +EndGlobal diff --git a/Technical-Test/Program.cs b/Technical-Test/Program.cs new file mode 100644 index 0000000..9b78167 --- /dev/null +++ b/Technical-Test/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace Technical_Test +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/Technical-Test/Technical-Test.csproj b/Technical-Test/Technical-Test.csproj new file mode 100644 index 0000000..f92332a --- /dev/null +++ b/Technical-Test/Technical-Test.csproj @@ -0,0 +1,9 @@ + + + + Exe + netcoreapp3.1 + Technical_Test + + +