Byte Heist Home Leaderboard
Join the Heist (with Github)
Solve View

Cave Man Speak

A Cave man word is a word that is at most four letters and alternates vowels and consonants. Y is a vowel if and only only if it is the last letter of a word.

Print only the lines in the input that are cave man words.

Judge

(async function*(context: Context): Challenge {
  // For "Filter" Style challenges where the goal is to output all inputs that match some condition
	const testCases: [string, boolean][] = [
      ["Baby", true],
	  ["Aby", true],
	  ["Abed", true],
      ["Jedi", true],
	  ["A", true],
	  ["Y", true],
	  ["By", true],
	  ["Bye", false],
	  ["Eye", true],
	  ["Byte", false],
	  ["Babb", false],
	  ["Rule", true],
	  ["Growth", false],
	  ["Amy", true],
	  ["Yam", true],
	  ["Yma", false],
	  ["Finer", false],
	  ["Yy", true],
	  ["Miro", true],
	  ["Iran", true]
  ];
	
  yield* context.runFilterCases(testCases.flatMap(
	  ([i, j]: [string, boolean]) => {
		  if (!j || i==="Y") {
			  return [[i,j] as [string, boolean]];
		  } else {
			  let options;
			  switch (i[i.length - 1].toLowerCase()) {
				  case "a":
				  case "e":
				  case "i":
				  case "o":
				  case "u":
					  options = "aeiouy".split('');
					  break;
				  default:
					options = "qwrtpsdfghjklzxcvbnm".split('');
			  };
			  return [
				  [i+options[rand(0, options.length)], false],
				  [i+options[rand(0, options.length)], false],
				  [i,j]
			  ] as [string, boolean][];
		  }
	  }
  ).map(
      ([i,j]: [string, boolean]) => {
        return [
          i.replace(/./g,(k)=>Math.random()>0.5?k.toUpperCase():k.toLowerCase()),
          j
        ] as [string, boolean]
      }
    ));
  // Finally, the challenge is passed if no test cases failed
return context.noFailures();
})

Example Code

process.stdin.on('data', (data)=>{
    for (const line of (''+data).split(/\n/g)) {
		const cased = line.toLowerCase();
		const yremoved = cased.replace(/y$/,'e');
		const vowels = yremoved.replace(/[aeoiu]/g,'a').replace(/[^a]/,'b');
		if(vowels.length <= 4 && vowels.match(/^[^a]?([a][^a])*[a]?$/)) {
		    console.log(line);
		}
	}
})