Computer Science Programming Task

Watch this thread
username5723140
Badges: 2
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#1
Report Thread starter 1 year ago
#1
Hi, any help would be appreciated ) (C#)

Write a program that gets two words from the user and then displays a message saying if the first word can be created using the letters from the second word or not.

For example:
• The word EAT can be formed from the word ATE as the first word uses one E, one A and one T and the second word also contains one of each of these letters.
• The word EAT can be formed from the word HEART as the second word contains one E, one A and one T which are the letters needed to form the first word.
• The word TO can be formed from the word POSITION as the second word contains one T and (at least) one 0 which are the letters needed to form the first word.
• The word MEET cannot be formed from the word MEAT as the second word only contains one E and two Es are needed to form the first word.

You may assume that the user will only enter words that consist of upper case letters.

Thanks again
Last edited by username5723140; 1 year ago
0
reply
aslem125
Badges: 4
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#2
Report 1 year ago
#2
Yh same I need an answer for this ASAP a levels coming soon
0
reply
Lazmiin
Badges: 2
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#3
Report 1 year ago
#3
same I am very confused right now
0
reply
shadowdweller
Badges: 21
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#4
Report 1 year ago
#4
(Original post by Lazmiin)
same I am very confused right now
Is it the exact same task? How far have you gotten with it currently?
0
reply
Strange5050
Badges: 17
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#5
Report 1 year ago
#5
Code:
public Dictionary<char, int> LetterCount(string word) {
    Dictionary<char, int> letterCount = new Dictionary<char, int>();
    char[] wordLetters = word.ToCharArray();

    for (int i = 0; i < wordLetters.Length; i++) {
        if (letterCount.ContainsKey(wordLetters[i])) {
            letterCount[wordLetters[i]]++;
        }
        else { 
            letterCount.Add(wordLetters[i], 1);
        }
    }

    return letterCount;
}
Here’s a start for you.
Last edited by Strange5050; 1 year ago
0
reply
miser
Badges: 20
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#6
Report 1 year ago
#6
The problem can be simplified as:
Is word 2 a subset of word 1?

(Where word 1 is technically speaking a multiset, since we care about the number of letters involved.)

A simple way to do this is to make a list (multiset) of the allowed letters from word 1, then make sure each letter of word 2 is in that list of allowed letters, removing used letters as we go.

Here is a solution:
https://dotnetfiddle.net/AXnHx8
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest

How did The Student Room help you with your university application?

Talking to current university students (12)
17.65%
Talking to peers going through the same thing (24)
35.29%
Speaking to student ambassadors from the universities (5)
7.35%
Speaking to staff members from universities (1)
1.47%
Using the personal statement builder, library or helper service (7)
10.29%
Reading articles about what steps to take (14)
20.59%
Learning about/speaking to Student Finance England (2)
2.94%
Something else (tell us in the thread) (3)
4.41%

Watched Threads

View All