Abbreviations, easy: Difference between revisions

m (added related tasks.)
Line 1,016:
{{out}}
<pre>Please enter your command to verify: riG rePEAT copies put mo rest types fup. 6 poweRin RIGHT REPEAT *error* PUT MOVE RESTORE *error* *error* *error* POWERINPUT</pre>
 
=={{header|Nim}}==
{{trans|Kotlin}}
This is a translation of Kotlin solution with several modifications.
<lang Nim>
import sequtils
import strutils
 
const Commands = "Add ALTer BAckup Bottom CAppend Change SCHANGE CInsert CLAst COMPress COpy " &
"COUnt COVerlay CURsor DELete CDelete Down DUPlicate Xedit EXPand EXTract Find " &
"NFind NFINDUp NFUp CFind FINdup FUp FOrward GET Help HEXType Input POWerinput " &
"Join SPlit SPLTJOIN LOAD Locate CLocate LOWercase UPPercase LPrefix MACRO " &
"MErge MODify MOve MSG Next Overlay PARSE PREServe PURge PUT PUTD Query QUIT " &
"READ RECover REFRESH RENum REPeat Replace CReplace RESet RESTore RGTLEFT " &
"RIght LEft SAVE SET SHift SI SORT SOS STAck STATus TOP TRAnsfer Type Up"
 
#---------------------------------------------------------------------------------------------------
 
proc validate(words, commands: seq[string]; minLens: openArray[int]): seq[string] =
 
if words.len == 0:
return
 
for word in words:
var matchFound = false
for i, command in commands:
if word.len notin minLens[i]..command.len:
# Word too short or too long.
continue
if minLens[i] == command.len and word != command:
# For commands with no lowercase character, no abbreviation is allowed.
continue
if command.toUpper.startsWith(word.toUpper):
result.add(command.toUpper)
matchFound = true
break
if not matchFound:
result.add("*error*")
 
#---------------------------------------------------------------------------------------------------
 
var commands = Commands.splitWhitespace()
var minLens = newSeq[int](commands.len) # Count of uppercase characters.
for idx, command in commands:
minLens[idx] = command.countIt(it.isUpperAscii)
 
while true:
 
try:
stdout.write "Input? "
let words = stdin.readline().strip().splitWhitespace()
let results = words.validate(commands, minLens)
stdout.write("\nUser words: ")
for i, word in words:
stdout.write(word.alignLeft(results[i].len) & ' ')
stdout.write("\nFull words: ")
for result in results:
stdout.write(result & ' ')
stdout.write("\n\n")
 
except EOFError:
echo ""
break
</lang>
{{out}}
<pre>
Input? riG rePEAT copies put mo rest types fup. 6 poweRin
 
User words: riG rePEAT copies put mo rest types fup. 6 poweRin
Full words: RIGHT REPEAT *error* *error* MOVE RESTORE *error* *error* *error* POWERINPUT
 
Input? set
 
User words: set
Full words: *error*
 
Input? SET
 
User words: SET
Full words: SET
 
Input?
 
User words:
Full words:
 
Input?
</pre>
 
=={{header|OCaml}}==
Anonymous user