vinod's erudition

we learn everything from failure not from success so keep failing

Home Ruby Rails Javascript Python Agentic AI

What I Learnt Today-19-10-2015

Posted on October 19, 2015 by vinod

###Reversing loop

To find the factorial of the number, we need to go from the highest number to lowest so it was bit tricky to find it but ruby made my work so easy using downto method

downto - gets the argument in which order to decrease

5.downto(1) - 5,4,3,2,1

####Find the longest word in a sentence

def LongestWord(sen)

  # code goes here

  alph = 'abcdefghijklmnopqrstuvwxyz'
  longest = ''
  max,count = 0,0
  word = sen.split(' ')
  word.each do |value|
  longest = ''
    value.split('').each do |y|
                  if (alph.include? y)
                   count = count + 1  
                   longest+=y
                  end
    end
    if (count.to_i > max.to_i)
                    max = count
                                sen = longest
                end
    #sen = value if (max < value.size)    

  end
  return sen
         
end
   
# keep this function call here 

# to see how to enter arguments in Ruby scroll down   

puts LongestWord(STDIN.gets)