C-ns-r-d Str-ngs (Code Challenge)

  • Page Owner: Not Set
  • Last Reviewed: 2019-10-11

C-ns-r-d Str-ngs

Someone has attempted to censor my strings by replacing every vowel with a , lk* th*s. Luckily, I've been able to find the vowels that were removed.

Given a censored string and a string of the censored vowels, return the original uncensored string.

Example

uncensor("Wh*r* d*d my v*w*ls g*?", "eeioeo") ➞ "Where did my vowels go?"

uncensor("abcd", "") ➞ "abcd"

uncensor("*PP*RC*S*", "UEAE") ➞ "UPPERCASE"

Notes

The number of vowels will match the number of * characters in the censored string.

JavaScript Tests

Feedback requested - If you use these, please let me know if this is a good format?

Test = (x, y) => x == y ? console.log("Pass") || true : console.log("Fail") || false;
uncensor = x=>x;

Test(uncensor('Wh*r* d*d my v*w*ls g*?', 'eeioeo'), 'Where did my vowels go?')
Test(uncensor('abcd', ''), 'abcd', 'Works with no vowels.')
Test(uncensor('*PP*RC*S*', 'UEAE'), 'UPPERCASE', 'Works with uppercase')
Test(uncensor('Ch**s*, Gr*mm*t -- ch**s*', 'eeeoieee'), 'Cheese, Grommit -- cheese', 'Works with * at the end')
Test(uncensor('*l*ph*nt', 'Eea'), 'Elephant', 'Works with * at the start')

CSharp Tests

Feedback requested - If you use these, please let me know if this is a good format?

public object Uncensor(string input, string vowels) {
	return false;
}

void Main()
{
	Test(Uncensor("Wh*r* d*d my v*w*ls g*?", "eeioeo"), "Where did my vowels go?");
	Test(Uncensor("abcd", ""), "abcd");
	Test(Uncensor("*PP*RC*S*", "UEAE"), "UPPERCASE");
	Test(Uncensor("Ch**s*, Gr*mm*t -- ch**s*", "eeeoieee"), "Cheese, Grommit -- cheese");
	Test(Uncensor("*l*ph*nt", "Eea"), "Elephant");
}


public bool Test(object x,object y)
{
	if (x == y)
	{
		Debug.WriteLine("Pass");
		return true;
	}
	else
	{
		Debug.WriteLine("Fail");
		return false;
	}
}

Additional Posts

public string Uncensor(string input, string vowels)
{
	int i = 0;
	string returnString = string.Empty;
	foreach (var c in input)
	{
		if (c.Equals('*'))
		{
			returnString += vowels[i];
			i++;
		}
		else
			returnString += c;
	}
	return returnString;
}
void Main()
{
	Uncensor("Wh*r* d*d my v*w*ls g*?", "eeioeo").Dump(); // ➞ "Where did my vowels go?"
	Uncensor("abcd", "").Dump(); // ➞ "abcd"
	Uncensor("*PP*RC*S*", "UEAE").Dump(); // ➞ "UPPERCASE"
}

public string Uncensor(string original, string missingVowelString)
{
	if(original.Count(c => c == '*') != missingVowelString.Count())
	{
		throw new ArgumentException("Missing vowel count mismatch.");
	}
	
	var vowels = new Queue<Char>(missingVowelString.ToArray());
	return new String(original.Select(c => c == '*' ? vowels.Dequeue() : c).ToArray());
}

My actual solution portion is 128 characters.

Test = (x, y) => x == y ? console.log("Pass") || true : console.log("Fail") || false;

uncensor = (x, y) => {
    var letters = y.split("");
    return x.split("").map(w => w == "*" ? letters.shift() : w).join("")
}

Test(uncensor('Wh*r* d*d my v*w*ls g*?', 'eeioeo'), 'Where did my vowels go?')
Test(uncensor('abcd', ''), 'abcd', 'Works with no vowels.')
Test(uncensor('*PP*RC*S*', 'UEAE'), 'UPPERCASE', 'Works with uppercase')
Test(uncensor('Ch**s*, Gr*mm*t -- ch**s*', 'eeeoieee'), 'Cheese, Grommit -- cheese', 'Works with * at the end')
Test(uncensor('*l*ph*nt', 'Eea'), 'Elephant', 'Works with * at the start')