Longest common suffix: Difference between revisions
Content deleted Content added
SqrtNegInf (talk | contribs) m →{{header|Perl}}: small simplification |
Catskill549 (talk | contribs) added AWK |
||
Line 642: | Line 642: | ||
fix |
fix |
||
bar</pre> |
bar</pre> |
||
=={{header|AWK}}== |
|||
<lang AWK> |
|||
# syntax: GAWK -f LONGEST_COMMON_SUFFIX.AWK |
|||
BEGIN { |
|||
arr1[++n1] = "AAbcd,Abcd,abcd,bcd" |
|||
arr1[++n1] = "11Sunday,2Sunday" |
|||
arr1[++n1] = "Sunday,Monday" |
|||
arr1[++n1] = "Sunday,Monday,day" |
|||
arr1[++n1] = "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday" |
|||
arr1[++n1] = "crucifix,infix,prefix,suffix" |
|||
arr1[++n1] = "identical,identical" |
|||
arr1[++n1] = "," |
|||
arr1[++n1] = "this,has,nothing,in,common" |
|||
for (i=1; i<=n1; i++) { |
|||
n2 = split(arr1[i],arr2,",") |
|||
min_wid = 999 |
|||
for (j=1; j<=n2; j++) { |
|||
leng = length(arr2[j]) |
|||
if (min_wid > leng) { |
|||
min_wid = leng |
|||
min_col = j |
|||
} |
|||
} |
|||
cols = 0 |
|||
for (j=1; j<=min_wid; j++) { |
|||
delete arr3 |
|||
for (k=1; k<n2; k++) { |
|||
arr3[substr(arr2[k],length(arr2[k])+1-j)] = "" |
|||
arr3[substr(arr2[k+1],length(arr2[k+1])+1-j)] = "" |
|||
} |
|||
if (length(arr3) == 1) { |
|||
cols++ |
|||
} |
|||
} |
|||
printf("'%s' : '%s'\n",arr1[i],(cols == 0) ? "" : substr(arr2[min_col],length(arr2[min_col])+1-cols)) |
|||
} |
|||
exit(0) |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
'AAbcd,Abcd,abcd,bcd' : 'bcd' |
|||
'11Sunday,2Sunday' : 'Sunday' |
|||
'Sunday,Monday' : 'nday' |
|||
'Sunday,Monday,day' : 'day' |
|||
'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday' : 'day' |
|||
'crucifix,infix,prefix,suffix' : 'fix' |
|||
'identical,identical' : 'identical' |
|||
',' : '' |
|||
'this,has,nothing,in,common' : '' |
|||
</pre> |
|||
=={{header|BaCon}}== |
=={{header|BaCon}}== |