Created
May 5, 2015 03:13
-
-
Save data-doge/dcee50f18cd28aca61fd to your computer and use it in GitHub Desktop.
enumerables and roman numerals examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| array = ["9","8","7","6","5","4","3","2"] | |
| array.length.times { |i| p array[i] } | |
| array.each { |char| p char } | |
| array.each_with_index { |char,i| p "#{char} || #{i}" } | |
| matrix = Array.new(10) { Array.new(10) {"a"}} | |
| matrix.each { |row| p row } | |
| matrix.each_with_index do |row,row_num| | |
| row.each_with_index do |col, col_num| | |
| puts "row #{row_num}: #{row}" | |
| puts "col #{col_num}: #{col}" | |
| puts # just puts'ing a new line here | |
| end | |
| end | |
| example_array = [0,1,2,3,4,5,6] | |
| # # take that array, and perform an operation on each element of the array, and put all of that in a new array | |
| def doubler(array) | |
| bucket = [] | |
| array.each { |number| bucket << number * 2 } | |
| bucket | |
| end | |
| # or .. | |
| def doubler(array) | |
| array.map { |number| number * 2 } | |
| end | |
| p doubler(example_array) | |
| @cipher = { | |
| 1000 => "M", | |
| 500 => "D", | |
| 100 => "C", | |
| 50 => "L", | |
| 10 => "X", | |
| 5 => "V", | |
| 1 => "I" | |
| } | |
| # MXXXVII | |
| # number | |
| # 1037 1x how many, add that many, subtract that many | |
| # 37 3x .. | |
| # 7 1x .. | |
| # 2 2x .. | |
| # 0 done | |
| def num_to_roman(number) | |
| roman_numeral = "" | |
| @cipher.each do |unit,symbol| | |
| count = number / unit | |
| roman_numeral += symbol * count | |
| number -= count * unit | |
| end | |
| return roman_numeral | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment