我是新来的,如果我没有问正确的问题,请原谅我。我正在尝试创建一个将采用字符串数组(药物)的函数,然后通过将其与其他数组进行比较来判断它是否属于某些类别。我试图用case switch方法实现这个目的。但它给了我错误"不能形成范围upperBound
代码如果有:
//This is list of medications a patient may be on. This array will be generated by user input.
var medicationArray = ["metoprolol", "Dulera", "Atrovastatin", "Albuterol", "lasix", "Sprinolactone", "Lisnopril","Aspirin","Apixaban"]
//Function to compare medications above to arrays of different categories of medications.
func medDetails(medications : [String]) {
//Arrays of list of different types of mjedications
let betaBlockerList = ["metoprolol", "carvedilol", "propanolol"]
let anticoagulantList = ["warfarin", "Apixaban","rivroxaban"]
var otherMedicationList : String = ""
// For loop to loop thru different medications patient is on.
for medication in medications {
//switch function to take the medication name and then comparing it against different arrays.
switch medication {
//comparing medication against the range of of elements of first array.
case anticoagulantList[0]...anticoagulantList[anticoagulantList.count-1]:
print("Patinet is on \(medication) for anticoagultion")
//comparing medication against the range of of elements of second array.
case betaBlockerList[0]...betaBlockerList[betaBlockerList.count-1]:
print("Patient is on \(medication) for betablocker")
//list of medications that do not fit any of the above two categorias.
default:
otherMedicationList = medication + ", "
if medication == medications[medications.count - 1]{
print("Patients other medications inculde \(otherMedicationList) .")
}
}
}
}
medDetails(medications: medicationArray
答案 0 :(得分:3)
let betaBlockerList = ["metoprolol", "carvedilol", "propanolol"]
" betaBlockerList"的开关案例工作良好。这是从" m"到" p"作为参数。这两个值按升序排列。
let anticoagulantList = ["warfarin", "Apixaban","rivroxaban"]
您的开关案例" anticoagulantList"由于"(w)arfarin"的非递增顺序而无法正常工作和"(r)ivroxaban"
这里的切换案例并不是将整个字符串作为参数。您的betaBlockerList案例也在执行以下所有值
var medicationArray = ["metoprolol", "n", "o"]
答案 1 :(得分:0)
我认为转换不是真正的最佳做法。一种好的方法是使用搜索功能,根据给定的前提搜索或过滤您的数组。但是如果你想实现一个更天真的解决方案,只需要做两个for循环。一个用于药物,另一个用于您正在比较的另一个阵列。然后在循环中添加一个if语句,检查药物是否属于该列表的一部分,如果是,您已找到答案,并且可以在此时打破循环。
答案 2 :(得分:0)
@Nirav已经对错误发表了评论,但问题是交换机可能不是您问题的最佳解决方案(例如,如果您有300个组,该怎么办?)
因此,这是一个只需要组定义的版本:
python col.py < text.txt
当像var medicationArray = ["metoprolol", "Dulera", "Atrovastatin", "Albuterol", "lasix", "Sprinolactone", "Lisnopril","Aspirin","Apixaban"]
func medDetails(medications: [String]) {
let input = Set(medications)
let betaBlockerList = Set(["metoprolol", "carvedilol", "propanolol"])
let anticoagulantList = Set(["warfarin", "Apixaban","rivroxaban"])
let groups = [
"betablocker": betaBlockerList,
"anticoagultion": anticoagulantList
]
// Get rid of any element from input that is present in groups
let unmatched = input.subtracting(groups.values.flatMap({$0}))
for medication in input {
for (groupName, groupValues) in groups {
if groupValues.contains(medication) {
print("Patient is on \(medication) for \(groupName)")
break
}
}
}
print("Patients other medications include: \(unmatched.joined(separator: ", "))")
}
打印时那样打印
患者服用美托洛尔治疗β受体阻滞剂
患者服用阿哌沙班进行抗凝治疗
患者其他药物包括:Sprinolactone,Atrovastatin,Dulera,Albuterol,Aspirin,Lisnopril,lasix