The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
triangle numbers
Posted by: asQuirreL | June 7, 2010 @ 4:00pm
Ruby Code
[
Download
]
def arr_from_words_at_loc word_loc words_file = File.new(word_loc, "r") words_string = words_file.gets words_file.close words_arr = words_string.split "," formatted_words_arr = words_arr.collect{ |word| word[1...-1] } words_arr = nil formatted_words_arr end def is_triangle_num n return true if ((8*n+1)**0.5)%1 == 0 return false end def value_of_char char char-64 end def convert_word_to_num word word.each_char.inject(0) { |wordnum, char| wordnum + value_of_char(char[0])} end def num_of_tri_words_in_arr arr arr.inject(0) { |num, currword| if is_triangle_num(convert_word_to_num(currword)) num += 1 else num end } end words = arr_from_words_at_loc "words.txt" num_of_tri_words = num_of_tri_words_in_arr words p num_of_tri_words
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.49 / 8
35 Votes
http://codebin.yi.org/675
page generated in 0.00 seconds