• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Anagram

 
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to do an anagram. For instance: "and" is an anagram of "Dan" but  "nan" is neither an anagram of "naan" nor "nan".



Also, I have the test such as. And I can not pass these tests.

Where i have done error.



 
Marshal
Posts: 72994
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What algorithm are you using?
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was thinking to check first my hashmap where I am storing the words. convert them and store unto the hash set and then convert a string into a character array.
After all this check if it anagram or not but how I can understand it does not work.

 
Campbell Ritchie
Marshal
Posts: 72994
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That isn't an algorithm. It is an implementation. Look at these FAQs: 1 2.
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry maybe I am not understanding.
I am not using any specific algorithm.
As we a student.
But I have another method which adds the words to the hash map. and then. I am taking from that.
 
Campbell Ritchie
Marshal
Posts: 72994
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tomy Alexsander wrote:. . . I am not using any specific algorithm. . . .

Of course you are; every program more complicated than “Hello, World” uses an algorithm. It appears your algorithm is counting all the letters, and then confirming whether two words have the same letters and the same counts.
What is the Map for?
If you are failing test‍s, have you investigated why? Have you printed the words failing the test‍s?
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using a map to store the words as I have a couple more method which I had to add for the requirement.
Unfortunately, we are using blueJ and it not showing what exactly error it just saying that this test does not pass
 
Saloon Keeper
Posts: 8234
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your test you are testing for a returned count of one. If the word is an anagram it will always be more than one per your code. Why does your code return a count and not a boolean?
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to return the number of case-insensitive anagrams of the word.
 
Campbell Ritchie
Marshal
Posts: 72994
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have just noticed a serious error in your line 19. Avoid the same object and different object operators (== and !=); see Winston's FAQ. When arrays were designed originally, they were created as very basic objects, so they only override one method from Object: see this Java┬« Language Specification (=JS) section. So a few years later (about 1997/1998) the Arrays utility class was introduced to make up those deficiencies; it contains methods to supply all the functionality we always wished arrays had.

edit: I have realised I was mistaken in this post. Sorry
 
Carey Brown
Saloon Keeper
Posts: 8234
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tomy Alexsander wrote:I need to return the number of case-insensitive anagrams of the word.

You are not counting words. You are counting number of histogram characters that match.
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How I can count words which have the anagram of another word?
I thought is i am going to split them in alphabetic and then compare it will work. but is not
 
Carey Brown
Saloon Keeper
Posts: 8234
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to move your "count++" outside of the inner loop which only deals with characters. The outer loop deals with words.
 
Campbell Ritchie
Marshal
Posts: 72994
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Damn! I have got it wrong; I misread the code. Sorry, the use of != is correct in line 19.
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I put counter outside. but now it conflicts with when I am adding and and and it thinks that it is an anagram.


 
Carey Brown
Saloon Keeper
Posts: 8234
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I have of your code based on what you've posted. If you've changed it since then, please post your updated code. In the mean time this has one compile error in addition to some logical errors. Hmmm, it seems that the compile error is due to the fact that I've only got a code snippet to work with that doesn't define "words". Are you familiar with SSCCE (<-link)? I guess that "words" was defined as a Map.
"wordFreq" that you use on line 23 has never been populated. And, as I've mentioned, incrementing "count" on line 30 counts matching frequencies not words.

I might suggest at this point that you refactor your code a bit. I think you could gain some clarity by isolating some of your logic into their own methods.


 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not allowed to split it is must be in one method.
Thank you for explanation.
 
Carey Brown
Saloon Keeper
Posts: 8234
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tomy Alexsander wrote:I put counter outside. but now it conflicts with when I am adding and and and it thinks that it is an anagram.

You put the count outside BOTH loops now. You needed to put it outside the inner loop.

 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I copy the wrong code. but it does not work.

 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
words are the name of the hashMap. because i have another method which I am using to add words and then the anagram method is to find if i have any in my hashmap anagram words.
 
Carey Brown
Saloon Keeper
Posts: 8234
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You created a boolean "isAnagram" before the inner loop. You only want to increment "count" IF  "isAnagram" is still "true".
 
Carey Brown
Saloon Keeper
Posts: 8234
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your comparison of two frequency arrays can also be done by calling Arrays.equals( int[] a1, int[] a2 ). Are you allowed to use Java library methods?
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes i am
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tomy Alexsander wrote:Yes i am

but not all. we have massive limitation
 
Campbell Ritchie
Marshal
Posts: 72994
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tomy Alexsander wrote:. . . we have massive limitation

And you have to write one method. Both would be regarded as poor practice in real life, though part of the exercise might be to write your own algorithms as would be implemented by a library method.
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a task which I have to do.
Unfortunately, we such stupid requirement.
 
Carey Brown
Saloon Keeper
Posts: 8234
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could be written as
Remember that your code is not calculating wordFrequency yet.
 
Campbell Ritchie
Marshal
Posts: 72994
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For having a question mentioned in the January 2021 CodeRanch Journal, congratulations: this question earns you a cow
 
reply
    Bookmark Topic Watch Topic
  • New Topic