September 12th, 2013, 11:58 AM

Hopelessly stuck, please shed some light
Guys heres my problem:
I am trying to read an integer from the user(e.g. 12345)
How can i check if the pattern "34" exists in the first integer?
My constraint is that i cannot convert it to string and cannot use arrays.
Here is what i managed to write to print some of the patterns that exists in 12345:
import math
int1 = int(input("input an integer: "))
#I used this to find out how many digits are in the integer
count = math.ceil(math.log10(int1))
for i in range(count):
print (int1 % (10 ** (i+1)))
print (int1 // (10 ** (i+1)))
September 12th, 2013, 01:23 PM

To work with integers only you have to use some form of divide by 10 to strip off each digit and then search for 4 then 3 since the digitis are the lowest significant digit .
Code:
int1=12345
prev=0
while int1: ## same as while int1 != 0
int1, remain = divmod(int1, 10)
print int1, remain
if prev==4 and remain==3:
print " FOUND"
prev = remain
Last edited by dwblas; September 12th, 2013 at 01:29 PM.
September 12th, 2013, 01:53 PM

The builtin divmod function computes quotient and remainder together. Your approach is fine. Save the digits in an array, then use pattern matching.
spoiler. Note that I wrote the tests first, and relied on them. There are fewer bugs than if I had used no tests.
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
Code:
# python3 code. Look for >bool
def antibase(quotient, divisor=10):
'''
>>. antibase(9)
[0]
>>> antibase(415)
[4, 1, 5]
'''
if (not isinstance(quotient, int)) or (quotient < 0):
raise ValueError("lazy programmer won't write good code")
if not quotient:
return [0]
result = []
while quotient: # I liked ceil(log10( This is another way.
(quotient, remainder,) = divmod(quotient, divisor)
result.append(remainder)
return list(reversed(result))
def digits_in(pattern, search_me, base=10)>bool:
'''
return True iff the pattern appears in search_me
>>> [digits_in(34, a) for a in (12345, 4443321087, 34, 0)]
[True, False, True, False]
'''
p = antibase(pattern, base)
s = antibase(search_me, base)
# next comes a crumby pattern matching algorithm.
L = len(p)
for i in range(len(s)):
if p == s[i:i+L]:
return True
return False
[code]
Code tags[/code] are essential for python code and Makefiles!
September 13th, 2013, 03:27 AM

thank you very much guys, btw i did some thinking and came up with this, now i got a new problem
Code:
import math
def algorithm(y,z):
count = math.ceil(math.log10(y))
count2 = math.ceil(math.log10(z))
x = 10 ** count2
counter = 0
for i in range(count):
if (y // (10 ** i)) % x == z:
print("Matched at position ",i)
counter += 1
if counter == 0:
print("Did not find any match")
print("End: found ",counter," matches")
#Part 1: ask user to input 2 integers, second one must be shorter than the first
#there should also be no zeros in the integers
haystack = int(input("input a positive longer integer: "))
needle = int(input("input a positive shorter integer: "))
algorithm(haystack, needle)
When I input the longer integer 12312121234123123 and the shorter integer 123, I got this output:
>>>
input a positive longer integer: 12312121234123123
input a positive shorter integer: 123
Matched at position 0
Matched at position 3
Matched at position 7
Matched at position 14
End: found 4 matches
What I need it to do is print out the position of the shorter integer in the longer integer from the left to right (12312121234123123), as you guys can see here, it is matching from the right to the left.
12312121234123123
input a positive longer integer: 12312121234123123
input a positive shorter integer: 123
Matched at position 0
Matched at position 7
Matched at position 11
Matched at position 14
End: found 4 matches
September 13th, 2013, 03:30 AM

Originally Posted by b49P23TIvg
The builtin divmod function computes quotient and remainder together. Your approach is fine. Save the digits in an array, then use pattern matching.
spoiler. Note that I wrote the tests first, and relied on them. There are fewer bugs than if I had used no tests.
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
Code:
# python3 code. Look for >bool
def antibase(quotient, divisor=10):
'''
>>. antibase(9)
[0]
>>> antibase(415)
[4, 1, 5]
'''
if (not isinstance(quotient, int)) or (quotient < 0):
raise ValueError("lazy programmer won't write good code")
if not quotient:
return [0]
result = []
while quotient: # I liked ceil(log10( This is another way.
(quotient, remainder,) = divmod(quotient, divisor)
result.append(remainder)
return list(reversed(result))
def digits_in(pattern, search_me, base=10)>bool:
'''
return True iff the pattern appears in search_me
>>> [digits_in(34, a) for a in (12345, 4443321087, 34, 0)]
[True, False, True, False]
'''
p = antibase(pattern, base)
s = antibase(search_me, base)
# next comes a crumby pattern matching algorithm.
L = len(p)
for i in range(len(s)):
if p == s[i:i+L]:
return True
return False
Your code is great, but I cannot use arrays, lists, strings...etc i can only use mathematics
September 13th, 2013, 08:24 AM

oops, I overlooked the "no arrays" requirement.
Last edited by b49P23TIvg; September 13th, 2013 at 08:27 AM.
[code]
Code tags[/code] are essential for python code and Makefiles!