[270] | 1 | # Porting from
|
---|
| 2 | # https://github.com/jeanlazarou/blockly2ruby
|
---|
| 3 | # Copyright (c) 2014 Jean Lazarou
|
---|
| 4 | # MIT Lisence
|
---|
| 5 |
|
---|
| 6 | def blockly_puts x
|
---|
| 7 | if x.is_a?(Array)
|
---|
| 8 | puts x.join(',')
|
---|
| 9 | else
|
---|
| 10 | puts x
|
---|
| 11 | end
|
---|
| 12 | end
|
---|
| 13 |
|
---|
| 14 | class Array
|
---|
| 15 | def find_first v
|
---|
| 16 | i = self.index(v)
|
---|
| 17 | i ? i + 1 : 0
|
---|
| 18 | end
|
---|
| 19 |
|
---|
| 20 | def find_last v
|
---|
| 21 | i = self.rindex(v)
|
---|
| 22 | i ? i + 1 : 0
|
---|
| 23 | end
|
---|
| 24 |
|
---|
| 25 | def numbers
|
---|
| 26 | self.delete_if {|v| !v.is_a?(Numeric)}
|
---|
| 27 | end
|
---|
| 28 |
|
---|
| 29 | def sum
|
---|
| 30 | self.numbers.inject(0) {|sum, v| sum + v}
|
---|
| 31 | end
|
---|
| 32 |
|
---|
| 33 | def average
|
---|
| 34 | x = self.numbers
|
---|
| 35 | x.sum / x.size.to_f
|
---|
| 36 | end
|
---|
| 37 |
|
---|
| 38 | def standard_deviation
|
---|
| 39 | x = self.numbers
|
---|
| 40 | return 0 if x.empty?
|
---|
| 41 | mean = x.average
|
---|
| 42 | variance = x.map {|v| (v - mean) ** 2}.sum / x.size
|
---|
| 43 | Math.sqrt(variance)
|
---|
| 44 | end
|
---|
| 45 |
|
---|
| 46 | def median
|
---|
| 47 | x = self.numbers
|
---|
| 48 | x.sort!
|
---|
| 49 | index = x.size / 2
|
---|
| 50 | x.size.odd? ? x[index] : ((x[index - 1] + x[index]) / 2.0)
|
---|
| 51 | end
|
---|
| 52 | end
|
---|
| 53 |
|
---|
| 54 | class String
|
---|
| 55 | def find_first v
|
---|
| 56 | i = self.index(v)
|
---|
| 57 | i ? i + 1 : 0
|
---|
| 58 | end
|
---|
| 59 |
|
---|
| 60 | def find_last v
|
---|
| 61 | i = self.rindex(v)
|
---|
| 62 | i ? i + 1 : 0
|
---|
| 63 | end
|
---|
| 64 | end
|
---|
| 65 |
|
---|
| 66 | class Float
|
---|
| 67 | def even?
|
---|
| 68 | false
|
---|
| 69 | end
|
---|
| 70 |
|
---|
| 71 | def odd?
|
---|
| 72 | false
|
---|
| 73 | end
|
---|
| 74 | end
|
---|
| 75 |
|
---|
| 76 | def colour_random()
|
---|
| 77 | '#%06x' % rand(2**24 - 1)
|
---|
| 78 | end
|
---|
| 79 |
|
---|
| 80 | def colour_rgb(r, g, b)
|
---|
| 81 | r = (2.55 * [100, [0, r].max].min).round
|
---|
| 82 | g = (2.55 * [100, [0, g].max].min).round
|
---|
| 83 | b = (2.55 * [100, [0, b].max].min).round
|
---|
| 84 | '#%02x%02x%02x' % [r, g, b]
|
---|
| 85 | end
|
---|
| 86 |
|
---|
| 87 | def colour_blend(colour1, colour2, ratio)
|
---|
| 88 | _, r1, g1, b1 = colour1.unpack('A1A2A2A2').map {|x| x.to_i(16)}
|
---|
| 89 | _, r2, g2, b2 = colour2.unpack('A1A2A2A2').map {|x| x.to_i(16)}
|
---|
| 90 | ratio = [1, [0, ratio].max].min
|
---|
| 91 | r = (r1 * (1 - ratio) + r2 * ratio).round
|
---|
| 92 | g = (g1 * (1 - ratio) + g2 * ratio).round
|
---|
| 93 | b = (b1 * (1 - ratio) + b2 * ratio).round
|
---|
| 94 | '#%02x%02x%02x' % [r, g, b]
|
---|
| 95 | end
|
---|
| 96 |
|
---|
| 97 | def lists_random_item(myList)
|
---|
| 98 | myList[rand(myList.size)]
|
---|
| 99 | end
|
---|
| 100 |
|
---|
| 101 | def lists_remove_random_item(myList)
|
---|
| 102 | myList.delete_at(rand(myList.size))
|
---|
| 103 | end
|
---|
| 104 |
|
---|
| 105 | def lists_sublist(myList, range)
|
---|
| 106 | myList[range] || []
|
---|
| 107 | end
|
---|
| 108 |
|
---|
| 109 | def lists_insert_random_item(myList, value)
|
---|
| 110 | myList.insert(rand(myList.size), value)
|
---|
| 111 | end
|
---|
| 112 |
|
---|
| 113 | def lists_set_random_item(myList, value)
|
---|
| 114 | myList[rand(myList.size)] = value
|
---|
| 115 | end
|
---|
| 116 |
|
---|
| 117 | # loops though all numbers from +params[:from]+ to +params[:to]+ by the step
|
---|
| 118 | # value +params[:by]+ and calls the given block passing the numbers
|
---|
| 119 | def for_loop params
|
---|
| 120 | from = params[:from] #.to_f
|
---|
| 121 | to = params[:to] #.to_f
|
---|
| 122 | by = params[:by].abs #.to_f
|
---|
| 123 |
|
---|
| 124 | from.step(to, (from > to) ? -1 * by : by) do |value|
|
---|
| 125 | yield value
|
---|
| 126 | end
|
---|
| 127 | end
|
---|
| 128 |
|
---|
| 129 | def is_prime n
|
---|
| 130 | return false if n < 0
|
---|
| 131 | (2..Math.sqrt(n)).each { |i| return false if n % i == 0}
|
---|
| 132 | true
|
---|
| 133 | end
|
---|
| 134 |
|
---|
| 135 | def math_modes(some_list)
|
---|
| 136 | groups = some_list.group_by{|v| v}
|
---|
| 137 | groups = groups.sort {|a,b| b[1].size <=> a[1].size}
|
---|
| 138 | max_size = groups[0][1].size
|
---|
| 139 | modes = []
|
---|
| 140 |
|
---|
| 141 | groups.each do |group|
|
---|
| 142 | break if group[1].size != max_size
|
---|
| 143 | modes << group[0]
|
---|
| 144 | end
|
---|
| 145 |
|
---|
| 146 | modes
|
---|
| 147 | end
|
---|
| 148 |
|
---|
| 149 | def text_get_from_start(text, index)
|
---|
| 150 | return "" if index < 0
|
---|
| 151 | text[index] || ""
|
---|
| 152 | end
|
---|
| 153 |
|
---|
| 154 | def text_get_from_end(text, index)
|
---|
| 155 | return "" if index < 0
|
---|
| 156 | text[-index-1] || ""
|
---|
| 157 | end
|
---|
| 158 |
|
---|
| 159 | def text_random_letter(text)
|
---|
| 160 | text[rand(text.size)]
|
---|
| 161 | end
|
---|
| 162 |
|
---|
| 163 | def text_to_title_case(str)
|
---|
| 164 | str.gsub(/\S+/) {|txt| txt.capitalize}
|
---|
| 165 | end
|
---|
| 166 |
|
---|
| 167 | def text_prompt(msg)
|
---|
| 168 | print(msg)
|
---|
| 169 | $stdin.gets
|
---|
| 170 | end
|
---|