Inverted words

  • 24
  • Locked
In the main method, read the name of a file that contains words separated by spaces from the console. In the text of the file, find all pairs of words that are mirror images of each other. Add them to result. Use StringBuilder. The file encoding is UTF-8.
You can't complete this task, because you're not signed in.
Comments (9)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Level 24, Spanish Town, Jamaica
3 March, 17:38
Maybe it will give someone ideas, but what I did was: *read the file contents into words and added it to an arraylist
words.addAll(Arrays.asList(line.split("\\s+"))); //can refactor into more than 1 line if you want to
*reversed each individual word and checked for the reversed word it in the arraylist. * also made a special check for words that are palindromes (like "toto"---> when these words are reversed they look the same. to make sure the algorithm doesn't count these words twice I put a marker on them. * if the word list .contains(reversed word) put both the original word and reversed in results. *remove or edit both words so they are no longer counted.
Level 25, Bexleyheath, United Kingdom
26 October 2020, 11:19
Just a fight against the validator. I made several solutions that were better than the 'answer' they provided and none were validated. Also - why does StringBuilder need to be in main? I pushed all of the pair code into a separate method as is better practice and had to just put a random StringBuilder object in main just to pass. Also - it needs to be much clearer what you want in result at the end. 'All pairs of words that are mirror images of each other' just isn't clear enough. What if the file was 'tot(1) tot(2) tot(3) tot(4)'? Output should be: tot1 tot2 tot1 tot3 tot1 tot4 tot2 tot3 tot2 tot4 tot3 tot4 but that isn't what you want.....
Level 24, Spanish Town, Jamaica
3 March, 17:23
Wait, they provide answers??
Level 25
30 March, 12:49
lmao yes, for a while now, you did not know? Check the image I attached.
Level 24, Spanish Town, Jamaica
3 April, 04:38
oh I have been slaving away hahaha! I had no idea.
Level 25
5 April, 07:24
well, don't feel bad because in a way it's better the way you did it, as you did not have the temptation of a ready solution available.. so working hard you understood more. 💪👍
Agent Smith
Level 38
26 September 2020, 20:54
Make sure to actually print the resulting pairs to console in the end.
6 July 2020, 12:58
CHECK YOUR CODE WITH THIS INPUT: rat tar tart a a tot tot tot rat tot a tot OUTPUT: rat tar a a tot tot tot tot It seems you only match each individual word ONCE but if that word appears again later on and it too has a match you add that as well. Also don't just check if the next word is mirrored, if the next word does not mirror, look at all the rest of the words. If you find a match, you can't use that word again (I think!)
Yiheng Niu
Level 26, Beijing, China
26 March 2020, 04:49
Attention: The mirror images can be the same.