【问题标题】:How to check largest possible palindrome integer如何检查最大可能的回文整数
【发布时间】:2018-09-24 09:40:33
【问题描述】:

我正在处理一个要求我使用函数的赋值,该函数带有两个参数:一个字符串形式的整数,例如 '1921',以及一个整数 k,例如 2。该函数返回最大可能的回文整数可以通过将输入数字的最多 k 位更改为 0-9 的任何其他数字来找到。

例如,最多对 1921 进行两次更改,最大可能的回文数是 1991(一次更改 2 -> 9)。如果我们进行三个更改,我们将得到 1921 到 9999,这是可能的最高四位数整数值。

我相信我使用的代码会检查数字是否为回文,但并没有给出我能得到的最大可能的回文。

使用的代码:

def Is_palindrome(str):
    x = 0 
    for i in range (len(str)/2):
        if (str[x]) == (word[len(str)-x-1]):
            x+=1
        if x == (len(str)/2):
            return True
    return False

def longest_palindrome(str):
    lst = str.split() 
    palindromes = [] #List that contains the palindromes
    long_len = 0 #Length of the longest palindrome
    longest = "" #The actual longest palindrome
    for i in lst: 
        if Is_palindrome(i): #If the str is a palindrome
            palindromes.append(i) #Add it to the palindrome list
    for i in palindromes: #Loop through the palindrome list
        if len(i) > long_len: #If the palindrome is longer than the 
longest one
            longest = i #Set it as the longest one
            longest_len = len(i) # Set the length of the longest one to 
the length of this one
    return longest

如果您有任何建议,请告诉我!!提前致谢。

【问题讨论】:

  • 嗨。我不明白:在您的文字中,您说您的函数有两个参数:字符串和 k;但是我只能看到字符串之一。这正常吗?
  • 您好,我是 python 新手,在课堂上我被教导只能单独使用字符串。我不确定如何编写一个可以同时使用字符串和 k 的函数。这就是为什么我认为我的功能不完整并且不适用于我给定的问题。

标签: python palindrome


【解决方案1】:

请尝试以下代码:

import sys

def maximumPalinUsingKChanges(str, k):
    palin = []

    a = 0
    b = len(str)

    while (a < b):
        palin.append(str[a])
        a = a + 1

    # Iinitialize l and r by leftmost and
    # rightmost ends
    l = 0
    r = len(str) - 1

    #  first try to make string palindrome
    while (l < r):
        # Replace left and right character by
        # maximum of both
        if (str[l] != str[r]):
            palin[l] = max(str[l], str[r])
            palin[r] = max(str[l], str[r])
            k = k - 1
        l = l + 1
        r = r - 1    

    # If k is negative then we can't make
    # string palindrome
    if (k < 0):
        print("Not possible")
        return "Not possible"

    l = 0
    r = len(str) - 1

    while (l <= r):
        # At mid character, if K>0 then change
        # it to 9
        if (l == r):
            if (k > 0):
                palin[l] = '9'

        # If character at lth (same as rth) is
        # less than 9
        if (palin[l] < '9'):
            #If none of them is changed in the
            #  previous loop then subtract 2 from K
            # and convert both to 9 
            if (k >= 2 and palin[l] == str[l] and palin[r] == str[r]):
                k -= 2
                palin[l] = '9'
                palin[r] = '9'

            #  If one of them is changed in the previous
            #  loop then subtract 1 from K (1 more is
            #  subtracted already) and make them 9  
            elif (k >= 1 and (palin[l] != str[l] or palin[r] != str[r])):
                k = k - 1
                palin[l] = '9'
                palin[r] = '9'

        l = l + 1
        r = r - 1

    print(''.join(palin))
    return ''.join(palin)

确保 k 是整数而非字符串

【讨论】:

  • 做得很好,我之前看过这个并想通了必须先解决边缘问题,但我很难解决某些更改和 K 组合的可能性,干得好这是我的朋友
  • @vash_the_stampede 谢谢。乐意效劳。很高兴它成功了。
  • @vash_the_stampede 没关系,人会有很多机会
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多