如何解决hackerrank python 3 字符串操作,8个不同字符串的恢复功能
有 6 个测试用例,其中 5 个通过基于 python 3 的字符串操作问题,但 1 个测试用例从一开始就失败了。 请帮帮我。问题如下: 8 个字符串在一个函数中给出。
- 去除字符串两端的空格:first、second、parent、city
- 大写:第一,第二,父
- 打印带空格的字符串:first、second、parent、city
- 检查字符串:'phone' 是否只包含数字
- 检查电话号码是否以字符串 'start' 中的值开头并打印结果(真或假)
- 打印:总共没有。 'strfind' 出现在字符串中的次数:first、second、parent、city
- 打印:使用'string1'上的拆分函数生成的列表
- 在 'city' 中查找 'strfind' 的位置
我的代码如下:让我知道我做错了什么。 5/6 个测试用例通过,只有 1 个测试用例因未知原因失败。 :(
def resume(first,second,parent,city,phone,start,strfind,string1):
first = first.strip()
second = second.strip()
parent = parent.strip()
city = city.strip()
first = first.capitalize()
second = second.capitalize()
parent = parent.capitalize()
print(first + " " + second + " " + parent + " " +city)
print(phone.isdigit())
print(phone[0]==start[0])
res = first + second + parent + city
res_count = res.count(strfind)
print(res_count)
print(string1.split())
print(city.find(strfind))
解决方法
如果没有提供测试用例的详细信息,就不太确定。但是,数字 5 可能不正确,因为您只检查字符串的第一个值是否相同。这与检查“电话号码是否以字符串 'start' 中的值开头”不同。我建议改用以下代码:
print(phone.startswith(start))
此外,数字 6 似乎可能会导致一些重叠字符串的不匹配。相反,我建议使用:
print(first.count(strfind) + second.count(strfind) + parent.count(strfind) + city.count(strfind))
,
first = first.strip()
second = second.strip()
parent = parent.strip()
city = city.strip()
first = first.capitalize()
second = second.capitalize()
parent = parent.capitalize()
print(first + " " + second + " " + parent + " " +city)
print(phone.isnumeric())
print(phone.startswith(start))
res = first + second + parent + city
res_count = res.count(strfind)
print(res_count)
print(string1.split())
print(city.find(strfind))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。