CheckEmoji Community · the emoji forum
🏠 Home 🆕 What's new ❓ Unanswered 🔥 Popular 📡 RSS Members 👥 0 online log in · register
Home › IT › IT Support › Word Comparison

Word Comparison

Started by coastaltiger39 · · 👁 6 views · 33 replies

📡 Subscribe to replies

Participants coastaltiger39bluecyclist64Michelle Bennett5Benjamin Brooks2Michael Jackson10
coastaltiger39 coastaltiger39 NewcomerOP
3 messages
joined May 2020
#1 ·
I need some help here!

I'm trying to compare two words to see how many matching letters they share.
Here’s what I mean:
apple, egg = 2 (e, p)
apple, butter = 2 (p, e)
potato, bread = 3 (p, t, o)
bluecyclist64 bluecyclist64 Regular
369 messages
joined Oct 2011
#2 ·
coastaltiger39 said:I need some help here!

I'm trying to compare two words to see how many matching letters they share.
Here’s what I mean:
apple, egg = 2 (e, p)
apple, butter = 2 (p, e)
potato, bread = 3 (p, t, o)

1. Determine which word is shorter.
2. Set up a loop that iterates based on the length of that shorter word.
3. During each iteration, compare the individual characters.
4. If the characters match, increment a variable named equalChars by one.
Michelle Bennett5 Michelle Bennett5 Active Member
89 messages
joined Dec 2007
#3 ·
coastaltiger39 said:I need some help here!

I'm trying to compare two words to see how many matching letters they share.
Here’s what I mean:
apple, egg = 2 (e, p)
apple, butter = 2 (p, e)
potato, bread = 3 (p, t, o)

Basically, you build a boolean matrix for tracking usage, sized to the number of letters in the first word.
Then you run a loop through every letter of the second word.
Inside that, you run another loop through all the letters of the first word.
If the current letter from the second word matches one from the first, and that spot in the first word hasn't been "used" yet...
Mark that position in the first word as used.
Add the matching letter to a list.
Break out of the inner loop.
Once the outer loop finishes, just count up how many—and which ones—were found.
--
This logic should mirror the task requirements and match those example solutions.
bluecyclist64 bluecyclist64 Regular
369 messages
joined Oct 2011
#4 ·
Michelle Bennett5 said:Basically, you build a boolean matrix for tracking usage, sized to the number of letters in the first word.
Then you run a loop through every letter of the second word.
Inside that, you run another loop through all the letters of the first word.
If the current letter from the second word matches one from the first, and that spot in the first word hasn't been "used" yet...
Mark that position in the first word as used.
Add the matching letter to a list.
Break out of the inner loop.
Once the outer loop finishes, just count up how many—and which ones—were found.
--
This logic should mirror the task requirements and match those example solutions.

Menščini, I think you’ve overcomplicated this because you missed the core point of the assignment.
There is absolutely no need for some complex boolean matrix here.
Michelle Bennett5 Michelle Bennett5 Active Member
89 messages
joined Dec 2007
#5 ·
bluecyclist64 said:Menščini, I think you’ve overcomplicated this because you missed the core point of the assignment.
There is absolutely no need for some complex boolean matrix here.

An egg produces one "a," while butter results in two "a"s.
I don't think your little suggestion can actually handle that.
Just whip it up in Python and see for yourself—it’s literally just a few lines of code.
bluecyclist64 bluecyclist64 Regular
369 messages
joined Oct 2011
#6 ·
Michelle Bennett5 said:An egg produces one "a," while butter results in two "a"s.
I don't think your little suggestion can actually handle that.
Just whip it up in Python and see for yourself—it’s literally just a few lines of code.

You're right; I completely misread the requirements. I dropped the ball on this one.
My solution would work perfectly if we were looking for identical letters at the exact same positions, but that isn't what was asked. That's where my logic failed.
Michelle Bennett5 Michelle Bennett5 Active Member
89 messages
joined Dec 2007
#7 ·
bluecyclist64 said:You're right; I completely misread the requirements. I dropped the ball on this one.
My solution would work perfectly if we were looking for identical letters at the exact same positions, but that isn't what was asked. That's where my logic failed.

The actual definition of the problem is buried in those calculation examples... the written instructions themselves are pretty vague.
coastaltiger39 coastaltiger39 NewcomerOP
3 messages
joined May 2020
#8 ·
Michelle Bennett5 said:Basically, you build a boolean matrix for tracking usage, sized to the number of letters in the first word.
Then you run a loop through every letter of the second word.
Inside that, you run another loop through all the letters of the first word.
If the current letter from the second word matches one from the first, and that spot in the first word hasn't been "used" yet...
Mark that position in the first word as used.
Add the matching letter to a list.
Break out of the inner loop.
Once the outer loop finishes, just count up how many—and which ones—were found.
--
This logic should mirror the task requirements and match those example solutions.


Thanks for the help ... any chance you could write out the whole thing using functions? I'm still pretty much a total newbie at this stuff... 🙂
coastaltiger39 coastaltiger39 NewcomerOP
3 messages
joined May 2020
#9 ·
coastaltiger39 said:Thanks for the help ... any chance you could write out the whole thing using functions? I'm still pretty much a total newbie at this stuff... 🙂

And honestly, I just need the count of duplicate letters, not actually knowing which ones they are.
Benjamin Brooks2 Benjamin Brooks2 Member
27 messages
joined Jun 2020
#10 ·
coastaltiger39 said:And honestly, I just need the count of duplicate letters, not actually knowing which ones they are.

int c = 0;
string sentence = "apple";
string sentence2 = "banana man";
char[] charArr = sentence.ToCharArray();
char[] charArr2 = sentence2.ToCharArray();
foreach (char ch in charArr)
{
foreach (char ch2 in charArr2)
{
if (ch == ch2) { ++c; }

}
}
Console.WriteLine(c);

This spits out 10. Figure it out yourself.😁
Benjamin Brooks2 Benjamin Brooks2 Member
27 messages
joined Jun 2020
#11 ·
int c = 0;
string sentence = "Benjamin Brooks2";
string sentence2 = "coastaltiger39";
string istaslova = "";
char[] charArr = sentence.ToCharArray();
char[] charArr2 = sentence2.ToCharArray();
foreach (char ch in charArr)
{
if (sentence2.Contains(ch))
{
if (istaslova.Contains(ch)) { }
else { istaslova = istaslova + ch; ++c; }

}
}
Console.WriteLine(istaslova);
Console.WriteLine(c);

******************************************

// Looks good now. It shows the unique letters and the total count. --> ok,2
Michelle Bennett5 Michelle Bennett5 Active Member
89 messages
joined Dec 2007
#12 ·
Benjamin Brooks2 said:int c = 0;
string sentence = "Benjamin Brooks2";
string sentence2 = "coastaltiger39";
string istaslova = "";
char[] charArr = sentence.ToCharArray();
char[] charArr2 = sentence2.ToCharArray();
foreach (char ch in charArr)
{
if (sentence2.Contains(ch))
{
if (istaslova.Contains(ch)) { }
else { istaslova = istaslova + ch; ++c; }

}
}
Console.WriteLine(istaslova);
Console.WriteLine(c);

******************************************

// Looks good now. It shows the unique letters and the total count. --> ok,2

anyway, can you try breaking this down in Python?
there's an online runner here https://www.w3schools.com/python/try...e=demo_default

makes it pretty easy to check if the logic holds up and see the results.
just run it against the examples from the assignment definition:
apple, egg = 2 (p, e)
apple, butter = 2 (a, e)
potato, bread = 3 (p, o, t)
Benjamin Brooks2 Benjamin Brooks2 Member
27 messages
joined Jun 2020
#13 ·
Michelle Bennett5 said:anyway, can you try breaking this down in Python?
there's an online runner here https://www.w3schools.com/python/try...e=demo_default

makes it pretty easy to check if the logic holds up and see the results.
just run it against the examples from the assignment definition:
apple, egg = 2 (p, e)
apple, butter = 2 (a, e)
potato, bread = 3 (p, o, t)

That online runner is 🙂
Got any other one?

Just type a = 10
print(a)

And it actually works.
Michael Jackson10 Michael Jackson10 Active Member
140 messages
joined Feb 2023
#14 ·
a=10
print(a)

And then it just goes to hell.
Michael Jackson10 Michael Jackson10 Active Member
140 messages
joined Feb 2023
#15 ·
c=0
istaslova=""
sentence=input("Enter the first sentence: ")
sentence2=input("Enter the second sentence: ")
for i in range(len(sentence)):
--if sentence2.find(sentence)>-1:
----if istaslova.find(sentence)==-1:
------istaslova=istaslova+sentence
------c=c+1

print("Duplicate letters found:",istaslova)
print("Total count:",c)

The little catch here? It counts spaces too, treating them just like any other character. You might want to skip those when you're testing it out...
Benjamin Brooks2 Benjamin Brooks2 Member
27 messages
joined Jun 2020
#16 ·
Michael Jackson10 said:a=10
print(a)

And then it just goes to hell.

First time looking at this syntax. I'd find the error if I could actually copy-paste.

So I hopped over to this https://www.onlinegdb.com/online_python_compiler

And now I can't figure out how to exit a for loop. Been searching for half an hour. Probably that break thing, just trying every combo.

I mashed two words together and now I'm stuck looking for the same letters.
Michelle Bennett5 Michelle Bennett5 Active Member
89 messages
joined Dec 2007
#17 ·
Michael Jackson10 says:
Python C = 0
istaslova=""
sentence=input("Enter first sentence: ")
sentence2=input("Enter second sentence: ")
for i in range(len(sentence)):
--if sentence2.find(sentence)>-1:
----if istaslova.find(sentence)==-1:
------istaslova=istaslova+sentence
------c=c+1

print("Repeating letters:",istaslova)
print("Total count:",c)

The catch is this thing counts spaces too, treating them just like any other character. You could probably just skip those when you're testing...

The prompt actually specifies a 'word,' so spaces shouldn't even be in the running.
Check the requirements—if you input something like "apple, butter," the result should be 2, but your code only gives you 1.
Benjamin Brooks2 Benjamin Brooks2 Member
27 messages
joined Jun 2020
#18 ·
Michelle Bennett5 said:
Michael Jackson10 says:
Python C = 0
istaslova=""
sentence=input("Enter first sentence: ")
sentence2=input("Enter second sentence: ")
for i in range(len(sentence)):
--if sentence2.find(sentence)>-1:
----if istaslova.find(sentence)==-1:
------istaslova=istaslova+sentence
------c=c+1

print("Repeating letters:",istaslova)
print("Total count:",c)

The catch is this thing counts spaces too, treating them just like any other character. You could probably just skip those when you're testing...

The prompt actually specifies a 'word,' so spaces shouldn't even be in the running.
Check the requirements—if you input something like "apple, butter," the result should be 2, but your code only gives you 1.

Indices start at 0. len is the letter count.
Michelle Bennett5 Michelle Bennett5 Active Member
89 messages
joined Dec 2007
#19 ·
Benjamin Brooks2 said:Indices start at 0. len is the letter count.

the result ends up in 'c', not in 'len()'... obviously.
Michael Jackson10 Michael Jackson10 Active Member
140 messages
joined Feb 2023
#20 ·
Benjamin Brooks2 said:Indices start at 0. len is the letter count.

Nah, he's actually right. I totally misread the prompt—I thought we weren't supposed to count duplicate letters...

Whatever, just comment out that second if statement

You must log in or register to reply here.

Log in Register

🔗 Similar threads