Created
January 30, 2019 20:47
-
-
Save m4ss1m0g/1251592297400f7410034e54762a7435 to your computer and use it in GitHub Desktop.
Italian Fiscal Code - LastName and FirstName
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
class FiscalCode | |
{ | |
private static readonly string[] VOC = new string[] { "a", "e", "i", "o", "u" }; | |
public static string Name(string lastName, string firstName) | |
{ | |
return $"{Lastname(lastName)}{FirstName(firstName)}"; | |
} | |
private static string Lastname(string value) | |
{ | |
var name = value | |
// Consonants | |
.Where(p => !VOC.Contains(p.ToString(), StringComparer.OrdinalIgnoreCase)).Take(3) | |
// Vocals | |
.Concat( | |
value.Where(p => VOC.Contains(p.ToString(), StringComparer.OrdinalIgnoreCase)).Take(3) | |
) | |
// Filler | |
.Concat( | |
new char[] { 'X', 'X', 'X' } | |
) | |
.Take(3) | |
.Select(p => p.ToString().ToUpper()); | |
return string.Join("", name); | |
} | |
private static string FirstName(string value) | |
{ | |
var cons = value.Where(p => !VOC.Contains(p.ToString(), StringComparer.OrdinalIgnoreCase)).ToArray(); | |
if (cons.Count() > 3) | |
{ | |
// First, Third and Forth charter | |
return string.Join("", new char[] { cons[0], cons[2], cons[3] }.Select(p => p.ToString().ToUpper())); | |
} | |
else | |
{ | |
// Get all consonants plus all vocals | |
var withVocs = cons.Concat( | |
value.Where(p => VOC.Contains(p.ToString(), StringComparer.OrdinalIgnoreCase)).Take(3) | |
) | |
// Filler | |
.Concat( | |
new char[] { 'X', 'X', 'X' } | |
) | |
.Take(3) | |
.Select(p => p.ToString().ToUpper()); | |
return string.Join("", withVocs); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment