Back to Problem Solutions forum
Hi
I get the correct result but not in the correct order, was wrong?
Taking the words as index in a dict where I count the words, the correct order would be
deltas, drapes, diapers, posited, rescued, singers, retrains
But if I sort the data:
from: ['rescued', 'drapes', 'retrains', 'deltas', 'diapers', 'singers', 'posited']
I get
['deltas', 'diapers', 'drapes', 'posited', 'rescued', 'retrains', 'singers']
So I get the result in different order, was wrong?
@InXsense:
First, make sure that you have read the problem statement carefully. It's not important to return a list of the words themselves, but rather the size of the list.
Second, it's difficult to understand what's going wrong. Can you provide the test data and the exact output of your program?
Third, do not sort the input data if that's what you're doing. It's important to return the number of anagrams in the same order as the test data as given.
I'll wait for your reply, then take another look at the question then.
Thanks Matthew.
Instead of:
4 5 3 3 3 3 4
I get
3 5 4 4 3 3 3
If I don't sort the data to get the results
data = ["rescued","drapes","retrains","deltas","diapers","singers","posited"]
# My algorithm
print data
for i in data:
print i,resultado[i]-1,
['rescued', 'drapes', 'retrains', 'deltas', 'diapers', 'singers', 'posited'] rescued 3 drapes 5 retrains 4 deltas 4 diapers 3 singers 3 posited 3
I know I dont have to print the data, is to check what is going on.
This are the results of my algorithm if I print what I found:
found: > aspired diapers
found: > deltas deltas
found: > deposit posited
found: > despair diapers
found: > diapers diapers
found: > dopiest posited
found: > drapes drapes
found: > ingress singers
found: > lasted deltas
found: > padres drapes
found: > parsed drapes
found: > posited posited
found: > praised diapers
found: > rasped drapes
found: > reduces rescued
found: > rescued rescued
found: > resigns singers
found: > restrain retrains
found: > retrains retrains
found: > salted deltas
found: > secured rescued
found: > seducer rescued
found: > signers singers
found: > singers singers
found: > slated deltas
found: > spared drapes
found: > spread drapes
found: > staled deltas
found: > strainer retrains
found: > terrains retrains
found: > topside posited
found: > trainers retrains
OK, for input: 7 rescued drapes retrains deltas diapers singers posited
I also get output:
3 5 4 4 3 3 3
Admin, would you take a look at this one? InXsense might have found a bug. I'll look at this some more later tonight. I have a class to go teach.
Hi there! I finally managed my finction to work correctly and it gives me almost the right result, except it is increased by ONE, for some reason. I've been inspecting my code for quite a long time but still don't understand as to why it happens.
here's my code:
using namespace std;
bool is_anagram(string source, string interest){ int i, j, c = 0;
for(i=0; i<source.size(); i++){
for(j=0; j<source.size(); j++){
if(source[i] == interest[j]){
interest[j] = 0;
c++;
break;
}
}
}
if(source.size() != interest.size()-1) return 0;//subtracting one only bcz the stream takes the characters and CR
else if(source.size() == c) return 1;
else return 0;
}
int main(int argc, const char * argv[]) { int i, casesNum, counter; string line; string cases[20];
cout << "Enter the number of cases and the cases themselves!" << endl;
cin >> casesNum;
for(i=0; i<casesNum; i++){
cin >> cases[i];
}
fstream Dictionary;//opening the file on the go with constructor
cout << "Answer: " << endl;
for(i=0; i<casesNum; i++){
Dictionary.open("/Users/mikhail/Desktop/MyCtrial/MyCtrial/Anagrams/words.txt");
counter = 0;
while (getline (Dictionary, line) ){
if(is_anagram(cases[i], line)) counter++;
}
Dictionary.close();
cout << counter << " ";
}
cout << "\n";
return 0;
}
My input:
9 adheres ingrates leaps passer teariest medical reveres testers sparely
My output:
4 4 6 5 4 5 4 4 5
The site says my answer is incorrect, but I know for sure, that my counter just overcounts by one. Is it possible that the las "line" could have taken CR from the file stream? And then something weird happened to my cases?
Thank you in advance for your help.
Hi Michael,
As what you posted is mangled, I tried your "accepted" code, but can't get it to output anything like the correct answer. For the first test case "adheres" it only finds one anagram "headrest" but that has an extra letter.
Hi Quandrey! If only you could advise me on how to post my code correctly... Then, I think, you would've been able to try it out.