카테고리 없음

[python] 내 단어 점수 기능 문제

필살기쓰세요 2021. 2. 17. 07:05

당신이 사용할 수 있다고 가정하면 set, dict또는 Counter의 문자를 하나 개의 방법이 반복 될 것 string1하나를 사용하여 하나 str.find가에서 찾을 수 있는지 확인하는 string2. 문자가 발견되면 1결과에 추가 string2하고 일치 색인 앞뒤의 슬라이스를 결합하여 새로 생성합니다.

def word_score(string1, string2):
    if not string1 or not string2:
            return 0
            
                index = string2.find(string1[0])
                    if index != -1:
                            return 1 + word_score(string1[1:], string2[:index] + string2[index+1:])
                                else:
                                        return word_score(string1[1:], string2)
                                        
                                        TEST_CASES = [
                                            ['always', 'walking'],
                                                ['recursion', 'excursion'],
                                                    ['diner', 'syrup'],
                                                        ['always', 'bananas']
                                                        ]
                                                        
                                                        for s1, s2 in TEST_CASES:
                                                            print('word_score({}, {}) -> {}'.format(s1, s2, word_score(s1, s2)))
                                                            

산출:

word_score(always, walking) -> 3
word_score(recursion, excursion) -> 8
word_score(diner, syrup) -> 1
word_score(always, bananas) -> 3

최신 정보

가정하면 str쉽게 자신이 가정 검색을 구현할 수 있습니다 방법을 사용할 수없는 rangelen사용할 수 있습니다 :

def word_score(string1, string2):
    if not string1 or not string2:
            return 0
            
                for i in range(len(string2)):
                        if string1[0] == string2[i]:
                                    return 1 + word_score(string1[1:], string2[:i] + string2[i+1:])
                                    
                                        return word_score(string1[1:], string2)
                                        
-------------------

"긍정적 일치"가 발생하면 두 번째 단어의 문자를 "지우기"할 수 있습니다. 이렇게하면 중복 문제가 사라집니다.

-------------------

목록에서 str을 변환 한 다음 중복을 제거하고 교차 길이를 확인하기 위해 집합에서 변환 할 수 있습니다.

def word_score(string1, string2):
    '''Returns the word score for two strings'''
        string1=list(string1)
            string2=list(string2)
                string1=set(string1)
                    string2=set(string2)
                        if len(string1) == 0 or len(string2) == 0:
                                return 0
                                    else:
                                            return len((string1.intersection(string2)))
                                            

편집 : 당신이 교차하지만, 사용하지 못할 목록을 하고 있는 동안 당신이 사용할 수 있습니까?

def word_score2(string1, string2):
    '''Returns the word score for two strings'''
        string_temp1=list(string1)
            string_temp2=list(string2)
                if len(string_temp1) == 0 or len(string_temp2) == 0:
                        return 0
                            else:
                                    if string_temp1[0] in string_temp2:
                                                while string1[0] in string_temp1:
                                                                string_temp1.remove(string1[0])
                                                                            return 1 + word_score2(string_temp1, string_temp2)
                                                                                    else:
                                                                                                return word_score2(string_temp1[1:], string_temp2)
                                                                                                


출처
https://stackoverflow.com/questions/39940044