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.

The point of the tutorial he is following is probably to solve the problem imperatively and step by step. It sounds like OP is not at the point where he would use or understand regex.

Yeah, but he said over here:
That he'd like to see how it would actually look.
I mean, depending on the language, the libraries...
But that was the joke too. If anything it'll make him want to study regex, which is really the basis of all computing (state machines).

Also, I fucked up the regex. It's

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

and in the code
r"*(?:(blue|green|brown|gray|red|black|purple)*|.*)*.*"

> If anything it'll make him want to study regex, which is really the basis of all computing (state machines).
No it isnt. This is just a simple exercise to help him understand strings.

you guys are the best, and yea i'm learning this stuff as i go. I'm not in a school or anything just basically teaching myself. I'm curious to know what regex is though

>people in this thread actually doing someones homework

Regular expressions.
They are used for string manipulation.
The "downside" is it can be harder to spot what the flaw is in your regex and thus people tend to not use it universally.
If you want to make it more clear, you can write a function that builds the regex and then use a more verbose language just for you. That certainly helped me out in the beginning.

Or expand it. Like an auto comment system

Is the the most simple way to count the colors in a string? I like to start with baby steps

Yes and no. Regex looks like "magic" and works like it too, theres a whole regex engine underneath that handles the actual manipulation.

You should, I think, write your own method where you break up the string into separate words and compare those words with the ones in your list (colours).

So, like this:
//split string into words.
string[] opisanigger = { "orange", "yellow", "blue", "purple" };
string sentence = "colour abc xd op is a faggot yellow orange etc";
string[] words = sentence.Split(' ');
int OPshomework = 0;

//find the colours
foreach (string s in words)
{
if(s is a colour...)
{
increment OPshomework
}
}

Forgot to mention that:
sentence.Split(' ')

is an extension method in C# (you can call it on types) that will split a string by a character (in this case a whitespace or ' ')

Baby steps, since you can't think for yourself:
1. store color names in array of strings: this is your dictionary
2. turn the input string into an array of words: for example, by replacing every white space by a sentinel character (look up for strtok in C, which does exactly that)
3. compare each word of the input sentence with the words of the dictionary to determine if it's a color or not

On a side, the dictionary could be implemented in a far more efficient way, for example with a trie, so that checking if a word from the input is a color takes at worst as many steps as the length of the word, and not the length of the whole dictionary. That should be the next step if you want another exercise.

test

OP still here?

yea i was watching some videos on if statements

What language are you doing it in?

Make it into a list of words, iterate through those words and every time you find a color then add one to the counter.

I'm trying to learn Java

There is a great book on regex called "mastering regular expressions" with owls on the cover. And you will have more use for it than you think even if you don't do programming professionally. But better to learn one thing at a time.

Great. Java is good for a beginner language as its very verbose.
I think thats far too much for OP, too early.

is this bait or are you just really fucking stupid?

nope, just have 0 experience working with this stuff

Here user, I made a recursive method for you.

public static int countColors(String s, String[] colors) {
int count = 0;
for (String color : colors) {
count += countSubstringOccurrences(s, color);
}
return count;
}

public static int countSubstringOccurrences(String s, String color) {
return countSubstringOccurrences(s, color, 0);
}

public static int countSubstringOccurrences(String s, String color, int startIndex) {
int index = s.indexOf(color, startIndex);
if (-1 == index) {
return 0;
}
return 1 + countSubstringOccurrences(s, color, index+1);
}

i think i should watch more videos on this so i understand exactly what i'm looking at here and what everything does

Are you using an IDE?

I'm using Repl.it

What language?

I would imagine the resource that asked you this would have literally just taught you about a way to solve this, most textbooks introduce problems that you can solve with everything you've been taught up to that point.

tl;dr: actually read the fucking chapter

bleh
int count = 0;
//see System.Drawing.KnownColor enum
var colors = new HashSet(Enum.GetNames(typeof(KnownColor)), StringComparer.OrdinalIgnoreCase);
string sentence = "big fat black rod of salty liquorice and thick white milk";
sentence.Split(' ').AsParallel().ForAll(x => count += colors.Contains(x) ? 1 : 0);