Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Code Advise: counting how many times something appears in a string

string

  • Please log in to reply
5 replies to this topic

#1 Lucifer_Alpha

Lucifer_Alpha

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 03 February 2012 - 01:21 PM

Please, could someone help with this code. I want to write a function which takes a string and check how many times some other string appears in it.

Below is my totally incompetent attempt. I have written like only 3 codes -- ever. If someone could explain what is wrong with python that it gives me an error, that would be helpful.

from string import *

def countMatches(target,key):
    l = []
    def checkandmakelist(target,key): 
        if find(target,key) != -1:
            global l
            l = l + [str(find(target,key))]
            target = target[find(target,key):]
            checkandmakelist(target,key)
            print len(l)
        else:
            print "not there"
    checkandmakelist(target,key)
    
countMatches("sfsddfzwefdffe","df")

python 2.7 btw
  • 0

#2 Sysop_fb

Sysop_fb

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 151 posts
  • Location:Missouri

Posted 03 February 2012 - 05:25 PM

Is there a reason you can't use the standard string count method?

>>> s = "hello there hello how are you hello"
>>> s.count("hello")
3
>>> "hello there hello how are you hello".count("hello")
3
>>> "hello there hello how are you hello".count("hellow")
0
>>> 

  • 0
"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

#3 Lucifer_Alpha

Lucifer_Alpha

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 04 February 2012 - 12:14 AM

That is not allowed.

My problem set says: "Write two functions, calledcountSubStringMatch and countSubStringMatchRecursive that take two arguments, a key string and a target string. These functions iteratively and recursively count the number of instances of the key in the target string." I have not learned count and it is not in any of the readings. So, it's safe to assume I can't use it.

This is for the ocw intro to cs course, btw.

---------- Post added at 12:14 PM ---------- Previous post was at 12:00 PM ----------

How do I view the source code for your count function? That would help.
Oh, is there a source code encyclopedia or something somewhere?
  • 0

#4 Sysop_fb

Sysop_fb

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 151 posts
  • Location:Missouri

Posted 05 February 2012 - 03:19 PM

They taught you how to do subscripting and for loops right? So if I wanted to print out every 5 subsequent letters from the string
"hellotherehitherehello"

string = "hellotherehitherehello"
key = "hello"
for i in range(0,len(string)-1):
     print string[i:i+len(key)]

Unsure if that code will work in 3 though because it goes out of bounds but you can fix that pretty easily if you think about it.
  • 0
"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

#5 JackomoLight

JackomoLight

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 97 posts
  • Programming Language:PHP, Python, JavaScript, PL/SQL
  • Learning:C, Java, C++, Perl, Ruby, Lisp, Bash

Posted 12 April 2012 - 03:57 AM

Is there a reason you can't use the standard string count method?

>>> s = "hello there hello how are you hello"
>>> s.count("hello")
3
>>> "hello there hello how are you hello".count("hello")
3
>>> "hello there hello how are you hello".count("hellow")
0
>>>


Is there a way to get the indices of all of the occurrences of the string?
  • 0

#6 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 12 April 2012 - 08:50 AM

You can with regular expressions or with find method. find returns the index of the match and you can also specify offset.
  • 1

The roots of education are bitter, but the fruit is sweet.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download