Jump to content

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

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Lucifer_Alpha

Lucifer_Alpha

    Newbie

  • Members
  • Pip
  • 2 posts
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

#2
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
  • Location:Missouri
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
>>> 

"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

    Newbie

  • Members
  • Pip
  • 2 posts
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?

#4
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
  • Location:Missouri
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.
"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

    Newbie

  • Members
  • PipPipPip
  • 38 posts

View PostSysop_fb, on 03 February 2012 - 05:25 PM, said:

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?

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
  • Location:::1
You can with regular expressions or with find method. find returns the index of the match and you can also specify offset.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users