Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Add This Thread To: Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb
 « Previous Thread | Next Thread » Thread Tools Search this Thread Rate Thread Display Modes
 Dev Shed Forums Sponsor:
#1
September 12th, 2013, 11:58 AM
 mib1413456
Registered User

Join Date: Sep 2013
Posts: 3
Time spent in forums: 41 m 13 sec
Reputation Power: 0
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)))

#2
September 12th, 2013, 01:23 PM
 dwblas
Contributing User

Join Date: May 2009
Posts: 415
Time spent in forums: 5 Days 6 h 8 m 12 sec
Reputation Power: 32
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.

#3
September 12th, 2013, 01:53 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,182
Time spent in forums: 1 Month 3 Weeks 2 Days 12 h 18 m 6 sec
Reputation Power: 455
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!

#4
September 13th, 2013, 03:27 AM
 mib1413456
Registered User

Join Date: Sep 2013
Posts: 3
Time spent in forums: 41 m 13 sec
Reputation Power: 0
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

#5
September 13th, 2013, 03:30 AM
 mib1413456
Registered User

Join Date: Sep 2013
Posts: 3
Time spent in forums: 41 m 13 sec
Reputation Power: 0
Quote:
 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

#6
September 13th, 2013, 08:24 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,182
Time spent in forums: 1 Month 3 Weeks 2 Days 12 h 18 m 6 sec
Reputation Power: 455
oops, I overlooked the "no arrays" requirement.

Last edited by b49P23TIvg : September 13th, 2013 at 08:27 AM.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Hopelessly stuck, please shed some light

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap