Any codefags here...

any codefags here? does anyone know how to write a method/function that takes a string and counts how many colors are used in that string?

Attached: 20151228_103739000_iOS.jpg (749x741, 96K)

A string is a just a bunch of characters.

Why would they have colour?

I just started studying this stuff because i want to learn to develop software. the study guid asked me this. "Write a method/function that takes a string and counts how many colors are used in that string. For the purposes of our challenge, let's say that the only available colors are: blue, green, brown, gray, red, black, and purple.

Example:

color_counter('the quick brown fox jumped over the purple dog')
# returns 2 because the string has two of the colors in it". i want to know what the code would look like

It's a word count where, it's only counting words that are colours.

Also, I'm not doing your fucking homework.

Tokenize the string, and for each token, see if it's in the list of your color dictionary. If it's the case, increase your color word count.

In laymans terms (not that guy), split up the string into words (look at regex) and then iterate through that list counting matches (colours)

Break down the problem into logical components. If any of those components is unclear on how to do it, break it down further. Welcome to programming.

For example, you need to find the count of color words in an input string.

This means you need:
- A function that takes a string and returns a mapping of color word to number (or any structure that lets you deduce the same thing).

That function itself can be broken down into parts. You need to
- pick out words from a string
- track the count of each word
- (optimally) only pick out the words you care about (colors)

Each of those parts can be broken down.

At some point you reach a level where you can use the building blocks of your programming language to do the task. E.g. "reading words from a string" may be "my strong.split(',');" and so on.

Instead of giving you the answer, it is better to ask you: what part of this do you not know how to do?

Simple.
/.*(blue|green|brown|gray|red|black|purple).*/

There's your regular expression, now you just need to count the groups.
So it's look something like this:
import re

def countColors(s):
regex = r".*(blue|green|brown|gray|red|black|purple).*"
return re.search(s, regex).groups.length


This is definitely what your guide wants.