Skip to content

Instantly share code, notes, and snippets.

@paulvictor
Last active March 17, 2020 11:07
Show Gist options
  • Select an option

  • Save paulvictor/be91068400feb10637b990d9e51d9474 to your computer and use it in GitHub Desktop.

Select an option

Save paulvictor/be91068400feb10637b990d9e51d9474 to your computer and use it in GitHub Desktop.
@load "ordchr"
function asciiCount(str, result, k){
for (k=1; k<=length(str); k++){
ascii = ord(substr(str, k, 1))
if ((ascii > 64 && ascii < 91) || (ascii > 96 && ascii < 123)){
result ++;
}
}
return result;
}
function max(arr1, result, j){
for(j=1;j<=length(arr1); j++){
if (arr1[j] > result){
result = arr[j];
}
}
return result;
}
{
for(i=1;i<$NF;i++){
arr[i] = asciiCount($i);
}
max_value = 0;
max_index = 0;
for(i=1;i<$NF;i++){
if (arr[i] > max_value){
max_value = arr[i];
max_index = i;
}
}
printf("%s\t\t\t%s\n", $max_index, longestAsciiPrefix($max_index));
}
function isAscii(c, ascii){
ascii = ord(c);
return ((ascii > 64 && ascii < 91) || (ascii > 96 && ascii < 123));
}
# skip non a-zA-Z chars and take the first substring which is only a-zA-Z
function longestAsciiPrefix(str, result, j, encountered_ascii){
encountered_ascii = 0;
result = "";
for(j=1;j<=length(str);j++){
if(isAscii(substr(str,j,1)) ){
encountered_ascii = 1;
result = (result (substr(str, j, 1)));
} else if (!isAscii(substr(str,j,1)) && encountered_ascii == 0){
continue;
} else if (!isAscii(substr(str,j,1)) && encountered_ascii == 1){
return result;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment