Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =begin
- Largest 5 digit number in a series
- link: http://www.codewars.com/kata/largest-5-digit-number-in-a-series/
- description:
- In the following 6 digit number:
- 283910
- 91 is the greatest sequence of 2 digits.
- Complete the solution so that it returns the largest five digit number found within the number given.. The number will be passed in as a string of only digits. It should return a five digit integer. The number passed may be as large as 1000 digits.
- =end
- #my version
- def solution(digits)
- str = digits.to_s
- maxsum_str = ""
- for index in 0...str.length-4
- sum = "#{str[index]}#{str[index+1]}#{str[index+2]}#{str[index+3]}#{str[index+4]}"
- if( sum.to_i > maxsum_str.to_i )
- maxsum_str = sum
- end
- if( sum.to_i==99999 )
- return maxsum_str.to_i
- end
- end
- return maxsum_str.to_i
- end
- #best practices #1
- def solution(digits)
- digits.split('').each_cons(5).max.join.to_i
- end
- #best practices #2
- def solution(digits)
- digits.scan(/\d{5}/).max.to_i
- end
Advertisement
Add Comment
Please, Sign In to add comment