Skip to content

Comments

To be graded#65

Open
trashcanmonster8 wants to merge 3 commits intopaircolumbus:masterfrom
trashcanmonster8:master
Open

To be graded#65
trashcanmonster8 wants to merge 3 commits intopaircolumbus:masterfrom
trashcanmonster8:master

Conversation

@trashcanmonster8
Copy link

I am interested in your thoughts on this algorithm. I have commented my
thoughts about it. I am wondering if there are any cases that can fool
this algorithm.

I am interested in your thoughts on this algorithm. I have commented my
thoughts about it. I am wondering if there are any cases that can fool
this algorithm.
Copy link
Member

@mikegee mikegee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love that you have a unique solution to this problem. But, I'm not convinced it handles all input correctly. If ASCII codes started at 1, it surely would have bugs, right?

Since an anagram means two words have the same letters, we're expecting canonical to return the input word's letters.

@trashcanmonster8
Copy link
Author

trashcanmonster8 commented Dec 2, 2016 via email

@mikegee
Copy link
Member

mikegee commented Dec 2, 2016

I threw together a tester and found some problem "words":

["sjkip", "sbjxk"]
["wxsby", "wyisp"]
["nnmlf", "mlyfd"]
["jncxw", "iyxjf"]

@mikegee
Copy link
Member

mikegee commented Dec 2, 2016

Here's something that might help:

pry> require 'prime'
pry> primes = Prime.each.to_enum
pry> alphabet = {}
pry> ('a' .. 'z').each do |letter|
pry*   alphabet[letter] = primes.next
pry* end
pry> alphabet
{
    "a" => 2,
    "b" => 3,
    "c" => 5,
    "d" => 7,
    "e" => 11,
    "f" => 13,
    "g" => 17,
    "h" => 19,
    "i" => 23,
    "j" => 29,
    "k" => 31,
    "l" => 37,
    "m" => 41,
    "n" => 43,
    "o" => 47,
    "p" => 53,
    "q" => 59,
    "r" => 61,
    "s" => 67,
    "t" => 71,
    "u" => 73,
    "v" => 79,
    "w" => 83,
    "x" => 89,
    "y" => 97,
    "z" => 101
}

@mikegee
Copy link
Member

mikegee commented Dec 2, 2016

Is there a way to make a function (hash right?) that can make additional unique keys

canonical basically is the hash function in this exercise. Except it isn't a "good" one. We expect collisions on anagrams.

Copy link
Member

@qubbit qubbit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your canonical function is broken. It will think the words abducent and ethician are anagrams of each other. Think of a simpler way to do this.

@mikegee I just saw your comments after posting mine. -_-

While the concept remains the same, I make a hash that maps each ascii
characters to a prime by iterating through numbers 0 to 255. Thus each
set of characters has a unique set of primes with a unique product.
@trashcanmonster8
Copy link
Author

I updated the algorithm. If you got the time, let me know what you think! Thanks.

@sjreich
Copy link

sjreich commented Dec 8, 2016

Check out some of the other pull requests on this problem...

#Furthermore, it is less likely to happen as the strings get longer since the sp
#ecific value of the product would be hard to match with those large numbers.
#each ascii character is assigned a unique prime number
(0..255).each { |asciiNumber| @alphabet[asciiNumber.chr] = primes.next}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By convention variables in ruby are snaked_cased, not camelCased

Copy link
Member

@qubbit qubbit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your solution probably won't work for unicode strings. Oh well

Copy link
Member

@mikegee mikegee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is neat, but it is still overly complicated for the task. 😺

#initialize the variable product
product = 1

for i in 0..(word.length - 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To iterate over the characters in a string, use String#chars.

Looked at string/array methods to develop a simipler algorithm.

#Editting detect_anagram in this manner would also decrease the likelihood of th
#e above exception.
def detect_anagram(word1, word2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplified to one line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants