程序的要求为:
In this exercise, you’ll create a function that takes in a name, validates that it’s in the correct format, and then adds it to a sorted array of names.
Create a function named validateAndAddName(name,nameAry):
- name is the new name that needs to be validated and added to the set
- Ensure name is of the form: “LastName,FirstNameInitials(s)”
- If the name is valid, add it to the set and QUIT with 1
- If the name is not valid, do not add it to the set and QUIT with 0
- nameAry is the set of sorted names
- The subscripts are the names, and the values are the number of times a particular name was entered.
- Remember that M automatically sorts Z before a. Use $$up() to convert the names to upper case so this isn’t an issue.
注意点
Mumps 本身没有 UP 函数,这个函数用于把字符串转换全部转换为大写。
但是不同的其他公司会提供不同的类库来进行大小转换。
如果你的代码中没有大写 UP 函数的话,可以使用下面的代码来进行大写函数转换:
up(str)
quit $TRANSLATE(str,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ")
测试用例
可用的测试用例
1 - Add a valid name to an empty list
*Test: s result=$$validateAndAddName^XCHR310EX4YCH("LOBLAW,BOB B",.nameAry)
*result has been incorrectly set to "" but expected 1
*nameAry at subscripts (0) has extra data in result with value "LOBLAW,BOB B"
that does not appear in expected
*nameAry at subscripts ("LOBLAW,BOB B") has no data in this node, but expected
data with value 1
2 - Add an invalid name to an empty list
*Test: s result=$$validateAndAddName^XCHR310EX4YCH("LOBLAW B",.nameAry)
*result has been incorrectly set to "" but expected 0
*nameAry at subscripts (0) has extra data in result with value "LOBLAW B" that
does not appear in expected
3 - Add a valid name to a non-empty list
*Data: nameAry("LOBLAW,BOB B")=1
*Test: s result=$$validateAndAddName^XCHR310EX4YCH("LOBLAW,BOB B",.nameAry)
*result has been incorrectly set to "" but expected 1
*nameAry at subscripts (0) has extra data in result with value "LOBLAW,BOB B"
that does not appear in expected
*nameAry at subscripts ("LOBLAW,BOB B") value has been incorrectly set to 1
but expected 2
4 - Add an invalid name to a non-empty list
*Data: nameAry("LOBLAW,BOB B")=1
*Test: s result=$$validateAndAddName^XCHR310EX4YCH("LOBLAW BOB",.nameAry)
*result has been incorrectly set to "" but expected 0
*nameAry at subscripts (0) has extra data in result with value "LOBLAW BOB"
that does not appear in expected
5 - Add a valid mixed-case name. Verify it was capitalized.
*Test: s result=$$validateAndAddName^XCHR310EX4YCH("Loblaw,Bob B",.nameAry)
*result has been incorrectly set to "" but expected 1
*nameAry at subscripts (0) has extra data in result with value "Loblaw,Bob B"
that does not appear in expected
*nameAry at subscripts ("LOBLAW,BOB B") has no data in this node, but expected
data with value 1
6 - Add a null value to the list
*Test: s result=$$validateAndAddName^XCHR310EX4YCH("",.nameAry)
*result has been incorrectly set to "" but expected 0
*nameAry at subscripts (0) has extra data in result that does not appear in
expected
程序会按照上面的测试用例来校验代码。
源代码
我们把提交的服务器上进行校验的代码贴在下面了。
这个程序的代码非常简单,就下面几句话。
S name="LOBLAW,BOB B"
DO validateAndAddName(name, .nameAry)
set name=""
f s name=$O(nameAry(name)) q:name="" w !,name,">",nameAry(name)
quit
validateAndAddName(name,nameAry)
n nameRet
S name=$$up(name)
;w !,name
if name'?1.U1",".1" "1.U.1(1" "1U) DO
. S nameRet=0
E DO
. SET nameAry(name)=nameAry(name)+1
. S nameRet=1
Q nameRet
UP(str)
quit $TRANSLATE(str,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ")
然后进行测试能够通过测试。
