Mumps Name Search 函数

需要创建的函数要求为:

In this exercise, you’ll create a function that returns a set of names matching a search string.

Create a function named findNames(search,nameAry,matches):

  • search is the search string.
    • Names that begin with the search string are considered a match
    • Remember to use $$up(str) to capitalize strings as needed
  • nameAry is the set of sorted names
    • The names are the subscripts in the array, in all caps
    • The value is the number of times that name was placed in nameAry
  • matches is an array of matching names.
    • The structure of matches is the same as nameAry, but matches only includes names from nameAry that start with search
  • QUIT from findNames with the total number of matches
    • Remember, some names are in nameAry more than once

测试用例

下面的代码使用了可用的测试用例。

我们可以使用测试用例来对代码进行校验。

1 - Exact match, single result
  *Data: nameAry("LOBLAW,BOB B")=1
  *Test: s result=$$findNames^XCHR310EX5YCH("LOBLAW,BOB B",.nameAry,.matches)
  *Run Error: <COMMAND>searchNamesTestHelper+8^XCHR310TESTS *Function must
    return a value at findNames+7^XCHR310EX5YCH

2 - Exact match, two duplicate results
  *Data: nameAry("LOBLAW,BOB B")=2
  *Test: s result=$$findNames^XCHR310EX5YCH("LOBLAW,BOB B",.nameAry,.matches)
  *Run Error: <COMMAND>searchNamesTestHelper+8^XCHR310TESTS *Function must
    return a value at findNames+7^XCHR310EX5YCH

3 - Multiple non-exact unique matches, one leading and one trailing non-match
  *Data: nameAry("ALGEBRA,B B")=1,nameAry("LOB,B B")=1,nameAry("LOBLAW,BOB
    B")=1,nameAry("ZEBRA,B B")=1
  *Test: s result=$$findNames^XCHR310EX5YCH("LOB",.nameAry,.matches)
  *Run Error: <COMMAND>searchNamesTestHelper+8^XCHR310TESTS *Function must
    return a value at findNames+7^XCHR310EX5YCH

4 - Exact match, lower-case search string
  *Data: nameAry("LOBLAW,BOB B")=1
  *Test: s result=$$findNames^XCHR310EX5YCH("loblaw,bob b",.nameAry,.matches)
  *Run Error: <COMMAND>searchNamesTestHelper+8^XCHR310TESTS *Function must
    return a value at findNames+7^XCHR310EX5YCH

5 - Multiple non-exact duplicate matches, one leading and one trailing non-match
  *Data: nameAry("ALGEBRA,B B")=3,nameAry("LOB,B B")=2,nameAry("LOBLAW,BOB
    B")=5,nameAry("ZEBRA,B B")=3
  *Test: s result=$$findNames^XCHR310EX5YCH("LOB",.nameAry,.matches)
  *Run Error: <COMMAND>searchNamesTestHelper+8^XCHR310TESTS *Function must
    return a value at findNames+7^XCHR310EX5YCH

源代码

下面是本函数使用的源代码。

  SET nameAry("ALGEBRA,B B")=1
  SET nameAry("ALGEBRA,B B")=1
  SET nameAry("LOB,B B")=1
  SET nameAry("LOBLAW,BOB B")=1
  SET nameAry("ZEBRA,B B")=1
  ;SET nameAry(3)="Tom"
  ;SET nameAry(4)="Carrie"
  ;SET nameAry(5)="Tomcat"
  ;SET nameAry ="t"
  S search="LOB"
  DO findNames(search,.nameAry,.matches)
  SET name=""
  ;f  s name=$O(array(name)) q:name=""  w !,array(name)
  f  s name=$O(matches(name)) q:name=""  w !,name
  QUIT
findNames(search,nameAry,matches)
 n name
 n matchCount
 S search=$$up(search)
 S name=""
 S matchCount=0
 FOR  SET name=$O(nameAry(name)) Q:name=""  D
 . If ($FIND($$up(name),search)>0) D
 . . S matches(name)=nameAry(name)
 . . S matchCount=matchCount+nameAry(name)
 w !,matchCount
 QUIT matchCount
up(str)
 QUIT $TRANSLATE(str,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ")