PHP
내가 바랐던 것처럼 성공하지 못한 내 시도는 문자열 길이와 특정 문자 배치를 사용하여 상태 이름에서 약어를 추출합니다. 아마도 이름 제거에 대한 더 나은 시퀀싱이 가능할 것입니다.
function findAbb ($state) {
$first = substr($state, 0, 1);
$last = substr($state, -2,1);
$state = strtolower($state);
if (strlen($state) < 4) {
return strtoupper($state);
}
if (strpos($state, ' ')) { //if it's a space, return the first letter of each word.
$space_index = strpos($state, ' ');
$state = explode(' ', $state);
return strtoupper(substr($state[0], 0, 1) . substr($state[1], 0, 1));
}
if (startsWith($state, 'io')) { //iowa is annoying, get rid of it.
return strtoupper($first . $last);
}
if (startsWith($state, 'w,i')) { //if it starts with a W, return the first 2.
return strtoupper(substr($state, 0, 2));
}
if (strlen($state) < 7 && strpos($state, 'm')===false) { //matches texas, ohio, and utah.
return strtoupper($first . substr($state, -4,1));
}
if (strlen($state) < 7 && substr($state, 0, 1) > 'j' && substr($state, 0, 1) < 'n') { //matches maine, kansas, and hawaii
return strtoupper($first . $last);
}
if (startsWith($state, 'c,d,k,l,p,v,g,h')) { //some unique states
return strtoupper($first . $last);
}
if (strpos($state, 'sk')) {
return strtoupper ('ak');
}
if (startsWith($state, 'k,l', 1)) {
return strtoupper(substr($state, 0, 2));
}
if (startsWith($state, 'n')) {
return strtoupper($first . substr($state, 2, 1));
}
if (startsWith($state, 'n', 2) || startsWith($state, 'z', 3)) { //montana, tennessee, minnesota, and arizona
return strtoupper($first . substr($state, 3, 1));
}
if (startsWith($state, 'm') && ($last == 's') || ($last == 'n')) {
return strtoupper(substr($state, 0, 2));
}
if (strpos($state,'o')) {
return strtoupper($first . 'o');
}
if (strpos($state,'y')) {
return strtoupper($first . 'd');
}
if (strpos($state,'r')) {
return strtoupper($first . 'r');
}
if (strpos($state,'ss')) {
return strtoupper($first . 's');
}
return $state; //otherwise return the name of the state (it was mispelled).
}
function startsWith ($state, $letters, $index = 0) { //takes a comma separated array and finds contents.
$letters = split(',',$letters);
for ($q = 0; $q<count($letters); $q++) {
if (strpos($state,$letters[$q]) === $index) {
return true;
}
}
return false;
}
물론 골프도 할 수 있습니다. 이것은 나의 첫 번째 골프 시도이므로 통찰력을 높이 평가합니다. (911)
function t($s){$s=u($s);$f=b($s,0,1);$l=b($s,-2,1);
if(strlen($s)<4)return $s;if(strpos($s,' '))$s=split(' ',$s);
return b($s[0],0,1).b($s[1],0,1);
if(w($s,'IO'))return $f.$l;
if(w($s,'W,I'))return b($s,0,2);
if(strlen($s)<7 && strpos($s,'M')===false)return $f.b($s,-4,1);
if(strlen($s)<7 && b($s,0,1)>'I' && b($s,0,1)<'N')return $f.$l;
if(w($s,'C,D,K,L,P,V,G,H'))return $f.$l;if(strpos($s, 'SK'))return 'AK';
if(w($s,'K,L',1))return b($s,0,2);if(w($s,'N'))return $f.b($s,2,1);
if(w($s,'N',2) || w($s,'Z',3))return $f.b($s,3,1);
if(w($s,'M') && ($l=='S') || ($l=='N'))return b($s,0,2);
if(strpos($s,'O'))return $f.'O';
if(strpos($s,'Y'))return $f.'D';if(strpos($s,'R'))return $f.'R';
if(strpos($s,'SS'))return $f.'S';return $s;}function w($s,$l,$i=0){$l=split(',',$l);
for($q=0;$q<count($l);$q++)if(strpos($s,$l[$q])===$i)return 1;return 0;}
function u($z){return strtoupper($z);}
function b($v,$x,$y){return substr($v,$x,$y);}