How to loop through an integer and extract another shorter integer (from
left to right)?
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 ):
# 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 offset in range(longLength):
subInt = longInt // 10**(offset) % 10**(shortLength)
print (subInt)
if (subInt == shortInt):
print ("Found a match at position ",offset)
So the result I get is:
Input a positive longer integer: 123456
Input a positive shorter integer: 12
56
45
34
23
12
Found a match at position 4
1
But how do I get it to loop through the longInt from left to right instead
of right to left? Like this:
Input a positive longer integer: 123456
Input a positive shorter integer: 12
1
12
Found a match at position 1
23
34
45
56
Please do help! Thanks!
No comments:
Post a Comment