2 bobobo1127 bobobo1127 于 2016.09.23 08:29 提问

Python 问题--希望各位高手前来解答

Suppose you are given two strings named s1 and s2 (containing only lowercase letters). The string s1 is called
a “good” string if it contains s2, i.e., s2 is a substring of s1. For example, if s1 is smart and s2 is art, then
s1 is a good string because smart contains art. The string s1 is called “almost good” if it is not a “good”
string but inserting one character into it would make it a “good” string. For example, if s1 is smart and s2
is mark, then s1 is not a good string because mark is not a substring of smart. But s1 is an “almost good”
string because inserting the letter k in s1 (between r and t) makes it smarkt which is a “good” string because
it contains mark. The string s1 is called a “bad” string if it is neither a “good” nor an ”almost good” string.
You need to write two functions called isGood(s1, s2) and isAlmostGood(s1, s2) that return true if
and only if s1 is “good” and “almost good”, respectively.
Your program must ask the user to enter two input strings: first input string will be s1 and the second input
string will be s2. Both strings must be converted to lowercase letters. Then, the program must call the two
functions to determine whether s1 is a “good”, “almost good” or “bad” string. The result must be printed

3个回答

hi_chen_xingwang
hi_chen_xingwang   Rxr 2016.09.23 09:19

你这属于伸手要代码的吧..你读下题目就知道该怎么做了啊,一到算法题,想到于求两个字符串集合的关系, 子集===>good, 有交集===>almost good, 无交集 ===>bad

hi_chen_xingwang
hi_chen_xingwang 回复Queen_hz: 必须的
大约一年之前 回复
u011249920
u011249920 你这昵称亮了~
大约一年之前 回复
hi_chen_xingwang
hi_chen_xingwang   Rxr 2016.09.23 10:12
#!/usr/bin/python
import sys
print 'please input two string:'
strs = []
for i in sys.argv:
    print 'now start -->'
    print i
    if i == "test_string.py":
        continue
    strs.append(i)

print strs

str1=strs[0]
str2=strs[1]
if len(str1)>len(str2):
    pass
else:
    tmp = str1
    str1 = str2
    str2 = tmp
if str1.find(str2) >=0:
    print 'good'
    sys.exit(0)
else:
    for m in xrange(0,len(str2)-1):
        # print m
        for n in xrange(m+1,len(str2)):
            # print n
            check_str = str2[m:n]
            # print check_str
            if str1.find(check_str) >=0:
                print 'almost good'
                break
            else:
                # print '%s is not contained in %s' %(check_str,str1)
                if m == len(str2)-2 and n == len(str2)-1:
                    print 'bad'

hi_chen_xingwang
hi_chen_xingwang   Rxr 2016.09.23 10:14

调用方法是 python filename.py 字符串1 字符串2

Csdn user default icon
上传中...
上传图片
插入图片