1

I am a total newbie to programming and have been researching/reading as much as I can but have yet to find a specific answer to help me out.

My question is as follows, if I have an array with multiple words as follows: myArray= "this","makes","bats","look","funny"

And I have a long string of jumbled words (that include a few instances of the words listed in the array such as: string = "jkmakespohbatsllfunnythisqwemakes"

String contains two instances of the word "makes", one instance of "bats", one instance of "funny", one instance of "this" and zero instances of "look".

Can someone please help point me in the right direction with some pseudo-code on how I would search the string for any substring words from my array and print the count of each instance found (as noted above) or not found.

I am in different on programming language right now but would probably want to code this later when I have a better grasp of the structure.

Thanks!

3
  • The two elements you'll need are a for loop, and to know that strings have a count method, so that myvar.count("makes") will return a number. Commented Sep 2, 2015 at 0:08
  • can you give me an example of how you'd use a for loop and the count method you listed? thanks! Commented Sep 2, 2015 at 0:21
  • wiki.python.org/moin/ForLoop has a good explanation of for loops. for word in myArray: print(longstring.count(word)) maybe? Commented Sep 2, 2015 at 0:25

1 Answer 1

1
# define a list of your words (call it 'words')

# define the string you want to search through (call it 'jumble')

# Define an empty dictionary that will hold your results. You can use a regular
# dictionary, or a cool thing in Python called "DefaultDict", in the collections module.
# Call it 'count'.

# Now loop through each of your words

   # Start searching at the beginning of your jumble, by
   # specifying a start position of 0. (This will make sense later.)

   # Add another loop here, that keeps looping until we're sure there's no
   # more words to be found.

       # Try to find (hint: check out the 'string' module) the word inside of jumble.

       # If we did NOT find a word
           # We just 'break' out of the inner loop

       # Else If we DID find a word, let's save the position of that word.
       # Call that 'index'.
           # If this is the first time, we put the word in our dictionary
           # with a count of 1
           # Else If this is not the first time, add 1 to the dictionary entry
           # for that word.

       # Now we search AGAIN through the jumble, but start AFTER the
       # position where we saw the first time.


# Now we've exited the entire thing, let's just print out our 'count' dictionary.

That's the pseudocode.

Here's some example of the various functions and features you might want to use:

colors = ['red', 'blue', 'green']  # Lists

for color in colors:  # For loops
    print(color)

person = {}  # Empty dictionary
person['name'] = 'Symmitchry'  # Add a key / value pair to a dictionary

if 'name' in person:  # Looking for a key in a dictionary
    print('Person has a name:')  # How to print
    print(person['name'])  # How to access a value from a dictionary
else:  # how to write an else statement
    print('Person has no name! Weird.')

import random  # importing a module
x = random.randint()  # Using a function from a module
Sign up to request clarification or add additional context in comments.

1 Comment

This is awesome. Thank you so much! I've got a lot more reading to do but this definitely puts my thinking in the right direction :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.