Reputation: 289
I'm new to Python and I'm having trouble with one of my assignment.
So the question is:
I have to obtain two positive integers from the user (one longer and one shorter). Then I have to loop through the longer integer (from left to right) and to check to see if the shorter integer appears inside the longer integer. And I have to report the position of the matches and the number of matches.
*I am not allowed to use strings and list to do this assignment ):
Examples of the result is supposed to be like this:
Eg 1.
Input a positive longer integer: 123456789
Input a positive shorter integer: 123
Found a match at position 0
End: found 1 matches
Eg 2.
Input a positive longer integer: 123456789
Input a positive shorter integer: 789
Found a match at position 6
End: found 1 matches
Eg 3.
Input a positive longer integer: 12312312312231222
Input a positive shorter integer: 22
Found a match at position 10
Found a match at position 14
Found a match at position 15
End: found 3 matches
Eg 4.
Input a positive longer integer: 12312312312231222
Input a positive shorter integer: 55
End: cannot find any match
So what I did so far:
# 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)
Please do help! Thanks! (:
Upvotes: 2
Views: 270
Reputation: 103844
You could do something along these lines:
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)
Now run it:
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
Upvotes: 0
Reputation: 11002
You just need to offset your longInt several times to extract all the shortInt-length integers ( like bit-shifting, but with power of tens' instead of twos').
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)
Result :
6789 5678 4567 3456 2345 1234 123 12 1
Upvotes: 0