Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从两个不同的整数中提取短数字序列并进行比较?_Python_Integer_Compare_Python 3.3_Digits - Fatal编程技术网

Python 如何从两个不同的整数中提取短数字序列并进行比较?

Python 如何从两个不同的整数中提取短数字序列并进行比较?,python,integer,compare,python-3.3,digits,Python,Integer,Compare,Python 3.3,Digits,我是Python新手,我的一项作业遇到了问题 因此,问题是: 我必须从用户那里获得两个正整数(一个长一个短)。然后我必须循环遍历较长的整数(从左到右),并检查较短的整数是否出现在较长的整数中。我必须报告比赛的位置和比赛的数量 *我不允许使用字符串和列表来完成此任务): 结果的例子应该是这样的: 例1输入一个正长整数:123456789 输入一个较短的正整数:123 在位置0处找到匹配项 结束:找到1个匹配项 例2.输入一个正长整数:123456789 输入一个较短的正整数:789 在位置6找到匹

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

因此,问题是: 我必须从用户那里获得两个正整数(一个长一个短)。然后我必须循环遍历较长的整数(从左到右),并检查较短的整数是否出现在较长的整数中。我必须报告比赛的位置和比赛的数量
*我不允许使用字符串和列表来完成此任务):

结果的例子应该是这样的:

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

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

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

例4.
输入一个正长整数:12331222
输入一个较短的正整数: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偏移几次,即可提取所有短整数长度的整数(如位移位,但幂为十而不是二)

结果:

6789 5678 4567 3456 2345 1234 123 12 一,


您可以按照以下思路做一些事情:

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

谢谢你说这是一项任务(很多人没有诚信)并发布所有可用信息。我不确定比较每个数字是否是最有效的解决方案:最好从longInt中提取子整数,并将其与shortInt进行比较。因此,我不能使用while循环来循环longInt?因为如果我使用while循环,但如果我不使用while循环,我不知道如何通过整数循环来提取数字。你知道算术移位吗:?这是相同的方法,但十次幂。我按照你的建议做了相应的操作,但它从右到左通过整数,所以位置是相反的。我能知道有没有办法o从左到右执行此操作?对于范围(0,longLength)中的偏移量:subInt=longInt//10**(offset)%10**(shortLength)如果(subInt==shortInt):打印(“在位置找到匹配项”,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?)
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