Wish Yourself a Happy New Year

Well, before you do that, I will do it from my side: Happy new year to you!

So, I was just working on somethings, and found this nice little code snippet that was totally appropriate for this event.

Ruby

If you are a fan of the ruby (like me), define a function with the following definition (try, using irb for this purpose):

1
def encode(str); str.chars.map(&:ord).inject(:+); end

The above function does the following:

  • accept a string as an input
  • break it into its constituent characters
  • convert these characters to their ascii integer value
  • sum up all the integer values and return that sum

Nothin’ great about that function, right?
Now, try this in your irb session:

1
encode "Happy new year to you!"

You will see that the above function is quite intelligent in itself.

That little function is empowered with an artificial intelligence by its code, without any complex mechanisms :)

Oh, and here is an alternative:

1
2
Happy = Time
Happy.new.year

Python

If you are a python fan, instead, try this:

1
2
encode = lambda str: sum(ord(c) for c in str)
encode("Happy new year to you!")
- by Nikhil Gupta

Comments

Copyright © 2015 - Nikhil Gupta - Powered by Octopress