【问题标题】:How do I extract short digit sequences from two different integers and compare them?如何从两个不同的整数中提取短数字序列并进行比较?
【发布时间】:2013-10-06 11:54:19
【问题描述】:

我是 Python 新手,我的一项作业遇到了问题。

所以问题是: 我必须从用户那里获得两个正整数(一个长一个短)。然后我必须遍历较长的整数(从左到右)并检查较短的整数是否出现在较长的整数内。我必须报告比赛的位置和比赛的数量。
*我不允许使用字符串和列表来做这个作业):

结果示例应该是这样的:

例如 1.
输入一个较长的正整数:123456789
输入一个较短的正整数:123
在位置 0 找到匹配项
结束:找到 1 个匹配项

例如 2.
输入一个较长的正整数:123456789
输入一个较短的正整数:789
在位置 6 找到匹配项
结束:找到 1 个匹配项

例如 3.
输入一个较长的正整数:12312312312231222
输入一个较短的正整数:22
在位置 10 找到匹配项
在位置 14 找到匹配项
在位置 15 找到匹配项
结束:找到 3 个匹配项

例如 4.
输入一个较长的正整数:12312312312231222
输入一个较短的正整数:55
结束:找不到任何匹配项

所以到目前为止我做了什么:

# Ask user for positve longer integer number
longInt = int(input("Input a positive longer integer: "))

# Ask user for positive shorter integer number 
shortInt = int(input("Input a positive shorter integer: "))

# Count number of digits in both longer and shorter integer numbers
import math
longLength = int(math.log10(longInt))+1
shortLength = int (math.log10(shortInt))+1

for i in range(0,longLength):
    for x in range(0,shortLength):
        while (longLength > 0):
            longDigit = longInt % 10 **(longLength) // 10**(longLength-1)
            longLength-=1
            print (longDigit)
        while (shortLength > 0):
            shortDigit = shortInt % 10**(shortLength) // 10**(shortLength-1)
            shortLength-=1
            print (shortDigit)

请帮忙!谢谢! (:

【问题讨论】:

  • 感谢您说这是一项任务(很多人没有这样做的诚信)并发布所有可用信息。我不确定比较每个数字是否是最有效的解决方案:最好从 longInt 中提取子整数并将其与 shortInt 进行比较。
  • 所以我不能使用while循环来循环longInt?因为如果我使用while循环,我有点卡在我必须放入两个整数的比较部分的地方,但是如果我不使用while循环,我不确定如何循环整数到提取数字。
  • 你知道算术移位:en.wikipedia.org/wiki/Arithmetic_shift 吗?这是相同的方法,但有几十的幂。

标签: python integer compare python-3.3 digits


【解决方案1】:

您只需将 longInt 偏移几次即可提取所有 shortInt 长度的整数(如位移,但使用十的幂而不是二的幂)。

import math

# Ask user for positve longer integer number
longInt = 123456789 

# Ask user for positive shorter integer number 
shortInt = 1234

# Count number of digits in both longer and shorter integer numbers

longLength  = int( math.log10(longInt) )+1
shortLength = int( math.log10(shortInt))+1

for offset in range(0, longLength):
  subInt = (longInt// 10**(offset)) % 10 **(shortLength) 
  print(subInt)

结果:

6789 5678 4567 3456 2345 1234 123 12 1

【讨论】:

  • 我按照你的建议做了相应的操作,但是它从右到左遍历整数,所以位置是相反的。我可以知道是否有办法从左到右吗? for offset in range(0,longLength): subInt = longInt // 10**(offset) % 10**(shortLength) if (subInt == shortInt): print ("Found a match at position ",offset)跨度>
  • 我尝试将其更改为范围内的偏移量 (longLength,0,-1),但它并没有真正起作用。如何让它从左到右穿过 longInt,而不是从右到左?
  • range(longLength,0,-1) 将以相反的顺序枚举从 longLength 到零的整数:换句话说,从 0 到 longLength ! (你做了一个双重翻转,它自己无效)。只需使用 range(0,longLength,-1) 或 range(longLength, 0)
  • 我之前也尝试过 range(0,longLength,-1) 和 range(longLength,0) ,它们也不起作用。这是因为我必须更改 subInt 的计算?
  • 还有其他方法可以反转 subInt 吗? (这样它会从左到右而不是从右到左循环 longInt 吗?)
【解决方案2】:

您可以按照以下方式做一些事情:

def i2l(i):
    rtr=[]
    while i:
        rtr.insert(0, i%10)
        i//=10
    return rtr

longer=int(input("Input a positive longer integer: "))    
shorter=int(input("Input a positive shorter integer: "))

ll=i2l(longer)   
ls=i2l(shorter)

answer=[]
for idx in [i for i,e in enumerate(ll) if e==ls[0]]:    
    try:
        all_good=all(ll[i+idx]==e for i,e in enumerate(ls))    
    except IndexError:
        all_good=False 
    if all_good:
        answer.append(idx)

if answer:
    s='index' if len(answer)==1 else 'indices'
    print '{} found in {} at {} {}'.format(shorter,longer,s,answer)
else:
    print '{} not found in {}'.format(shorter,longer) 

现在运行它:

Input a positive longer integer: 12312312312231222
Input a positive shorter integer: 22
22 found in 12312312312231222 at indices [10, 14, 15]

Input a positive longer integer: 12312312312231222
Input a positive shorter integer: 4
4 not found in 12312312312231222

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2016-09-16
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多