Anishluke92 / RubySolution

Problem solving using Ruby programming language
0 stars 1 forks source link

Is the Sum of Letters Even or Odd? #15

Closed danielpaul closed 3 years ago

danielpaul commented 4 years ago

Create a function that takes a string and returns true if the sum of the position of every letter in the alphabet is even and false if the sum is odd.

Examples

is_alpha("i'am king")  ➞ true
# 9 + 1 + 13 + 11 + 9 + 14 + 7 = 64 (even)

is_alpha("True") ➞ true
# 20 + 18 + 21 + 5= 64 (even)

is_alpha("alexa") ➞ false
# 1 + 12 + 5 + 24 + 1= 43 (odd)

Notes Case insensitive. Ignore non-letter symbols.

Anishluke92 commented 4 years ago

Algorithm

  1. We know the alphabets order. like A is 1, B is 2 and goes on.
  2. Now lets take a sentence and from it lets take the first word. We need to find each alphabet's corresponding order number that of word and sum it. Like "in" 9+14 = 23.
  3. Similarly we do the same for the all words in that sentence.
  4. We add all the sums together.