Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active November 10, 2020 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/336f57ab4715cfa24239cafcfdf480d7 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/336f57ab4715cfa24239cafcfdf480d7 to your computer and use it in GitHub Desktop.
Just PHP FUN 147.
<?php
# https://www.codewars.com/kata/57fe5b7108d102fede00137a Hungarian Vowel Harmony (harder).
function instrumental(string $w): string {
$wp = [
"a" => "á",
"e" => "é",
"i" => "í",
"o" => "ó",
"u" => "ú",
"ö" => "ő",
"ü" => "ű",
"á" => "á",
"é" => "é",
"í" => "í",
"ó" => "ó",
"ú" => "ú",
"ő" => "ő",
"ű" => "ű",
]; $x = strlen($w) - 1;
preg_match('/(.*)(.)$/u',$w, $matches);
$prefix = $matches[1];
$last = $matches[2];
if(preg_match('/[aáoóuú]$/',$w)){
return $prefix. $wp[$last] . 'val';
}
if(preg_match('/[eéiíöőüű]$/',$w)){
return $prefix. $wp[$last] . 'vel';
}
if(preg_match('/((sz)|(zs)|(cs))$/',$w)){
preg_match('/(.*)(.)(.)(.)$/u',$w, $matches);
$l1 = $matches[4];
$l2 = $matches[3];
$l3 = $matches[2];
$l4 = $matches[1];
if(preg_match('/[aáoóuú]$/',$l3)){
return $l4.$l3.$l2.$l2.$l1.'al';
}
if(preg_match('/[eéiíöőüű]$/',$l3)){
return $l4.$l3.$l2.$l2.$l1.'3l';
}
}
if(preg_match('/[^aáoóuúeéiíöőüű]{2}$/',$w)){
echo "$w \n";
preg_match('/(.*)(.)(.)(.)$/u',$w, $matches);
$l1 = $matches[4];
$l2 = $matches[3];
$l3 = $matches[2];
$l4 = $matches[1];
if(preg_match('/[aáoóuú]$/',$l3)){
$ans = $l4.$l3.$l2.$l1.$l1.'al';
}
if(preg_match('/[eéiíöőüű]$/',$l3)){
$ans = $l4.$l3.$l2.$l1.$l1.'el';
}
echo "$ans \n";
return $ans;
}
if(preg_match('/[aáoóuú].$/',$w)){
preg_match('/(.*)(.)(.)$/u',$w, $matches);
$last = $matches[3];
$prev = $matches[2];
$prefix = $matches[1];
return $prefix.$prev.$last.$last."al";
}
if(preg_match('/[eéiíöőüű].$/',$w)){
preg_match('/(.*)(.)(.)$/u',$w, $matches);
$last = $matches[3];
$prev = $matches[2];
$prefix = $matches[1];
return $prefix.$prev.$last.$last."el";
}
throw new Exception("Unhandled case. $w;");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment