Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSimbiso Tatenda Mataire
6,258 Pointswhats wrong with this code help pliz
Challenge Task 2 of 2
Create a method named WordsWithCountGreaterThan that takes an integer as a parameter. The method should return a dictionary of all of the words that have a count greater than the parameter passed in. The dictionary should contain the word and the word's count.
using System.Collections.Generic;
namespace Treehouse.CodeChallenges
{
public class LexicalAnalysis
{
public Dictionary<string, int> WordCount = new Dictionary<string, int>();
public void AddWord(string word)
{
if (WordCount.ContainsKey(word))
{
WordCount[word]++;
}
else
{
WordCount.Add(word, 1);
}
public void WordsWithCountGreaterThan (int num){
if (WordCount > num) {
WordCount.Add(num);
}
return WordCount;
}
}
}
2 Answers
Brody Ricketts
15,612 PointsTo build on what Steven Parker has already stated, here is a small walkthrough of the code challenge.
The following will have a mix of code from the challenge and pseudocode.
public class LexicalAnalysis
{
public Dictionary<string, int> WordCount = new Dictionary<string, int>();
public void AddWord(string word)
{
//TODO Check if word is in dictionary, if word exists add 1 to word count.
// if WordCount contains word
//WordCount[Key]++ This will increment the value of the key because the value is an integer
// If word does not exist, add word to dictionary with a count of 1
// WordCount Add(word, 1) This will add the key and value to the WordCount Dictionary
}
// Create a new method called WordsWithCountGreaterThan with a return type of a Dictionary!
public Dictionary<string, int> WordsWithCountGreaterThan(int count)
{
// TODO. Create a new dictionary to hold the NEW values after determining if a word count is
// greater than a specified number.
Dictionary<string, int> HigherWordCount = new Dictionary<string, int>();
// For each Key Value Pair of type string and int named word in WordCount
// If word.value is greater than count (Remember count was passed into the method above)
// Add the word key and the word value into the new Dictionary created above
//return the new dictionary, i.e. HigherWordCount
}
}
If you still have issues please let me know.
Thanks!!!
Steven Parker
231,248 PointsIt looks like you asked this same question already.
But here's the hints I gave there about what your WordsWithCountGreaterThan method should have:
- it should create a new Dictionary variable
- it should have a loop to iterate the Wordcount
- it should compare the count of each word to the passed-in argument
- it should add both the word and the count to the new dictionary when the criterion is met
- it should return the newly-created dictionary