【问题标题】:Printing numbers that contain only odd digits in a given range打印给定范围内仅包含​​奇数位的数字
【发布时间】:2022-10-14 19:45:12
【问题描述】:

我发现打印给定范围内仅包含​​奇数数字的数字的任务有些困难。 f.e:第一个数字是 2345,第二个数字是 6789。还有一件事 - 打印的数字应该仅限于根据数字位置 2 到 6 (3,5)、3 到 7(3,5) 的范围,7), 4 to 8(5,7), 5 to 9(5,7,9) - 所以这意味着第一个数字应该是 3355,3357,3359,3375,3377,3379,3555,3557.. ..

代码不会按照输出应有的方式执行它:

number_one=int(input())
number_two=int(input())
list_one=[]
list_two=[]
number_one=str(number_one)
number_two=str(number_two)

for i in number_one:
    if int(i)==0 or int(i)%2==0:
        i=int(i)+1
    list_one.append(int(i))

for i in number_two:
    list_two.append(int(i))

a=0
b=0
c=0
d=0

for j in range(list_one[0],list_two[0]+1):
    if j%2==1:
        a=j
    for p in range(list_one[1],list_two[1]+1):
        if p%2==1:
            b=p
        for x in range(list_one[2],list_two[2]+1):
            if x%2==1:
                c=x
            for y in range(list_one[3],list_two[3]+1):
                if y%2==1:
                    d=y
                    print(f"{a}{b}{c}{d}",end=" ")

我想避免输出中有很多重复。

先感谢您!

【问题讨论】:

  • 数字总是相同的长度吗?

标签: python for-loop numbers repeat


【解决方案1】:

可能这不是最佳解决方案。 但这适用于具有相同长度的正整数。

number_one=int(input())
number_two=int(input())

if len(str(number_one)) != len(str(number_two)):
    raise Exception("numbers should be of same length")

def print_num(num_one, num_two):
    res = []
    for i,j in zip(num_one, num_two):
        next_odd_for_i = int(i) + (not (int(i)%2))
        prev_odd_for_j = int(j) - (not (int(j)%2))
        temp_str = ""
        for i_next in range(next_odd_for_i, prev_odd_for_j+1, 2):
            temp_str += str(i_next)
        res.append(temp_str)
    return res


def print_perm(li_of_str):
    if len(li_of_str) == 1:
        return [li_of_str[-1]]
    res = []
    first = li_of_str[0]
    for j in first:
        tmp = [j+k for n in print_perm(li_of_str[1:]) for k in n ]
        res.append(tmp)
    return res
print(print_num(str(number_one), str(number_two)))
print(print_perm(print_num(str(number_one), str(number_two))))

【讨论】:

    【解决方案2】:

    解决这个问题的一种方法是递归。此函数接受两个表示数字的字符串,并返回满足您指定条件的所有奇数(作为字符串):

    def odd_digits(num1, num2):
        # split off first digit of string
        msd1, rest1 = int(num1[0]), num1[1:]
        # make the digit odd if required
        msd1 += msd1 % 2 == 0
        # split off first digit of string
        msd2, rest2 = int(num2[0]), num2[1:]
        # make the digit odd if required
        msd2 -= msd2 % 2 == 0
        # if no more digits, just return the values between msd1 and msd2
        if not rest1:
            return [str(i) for i in range(msd1, msd2+1, 2)]
        # otherwise, append the results of a recursive call to each 
        # odd digit between msd1 and msd2
        result = []
        for i in range(msd1, msd2+1, 2):
            result += [str(i) + o for o in odd_digits(rest1, rest2)]
        return result
    
    print(odd_digits('2345', '6789'))
    

    输出:

    [
     '3355', '3357', '3359',
     '3375', '3377', '3379',
     '3555', '3557', '3559',
     '3575', '3577', '3579',
     '3755', '3757', '3759',
     '3775', '3777', '3779',
     '5355', '5357', '5359',
     '5375', '5377', '5379',
     '5555', '5557', '5559',
     '5575', '5577', '5579',
     '5755', '5757', '5759',
     '5775', '5777', '5779'
    ]
    

    如果您想使用整数值,只需使用(例如)

    print(list(map(int, odd_digits(str(2345), str(6789)))))
    

    输出将如上所示,但所有值都是整数而不是字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 2020-06-20
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 2020-11-25
      相关资源
      最近更新 更多