Algorithms Flashcards
(127 cards)
Reverse and Integer
x = 12345
x = 12345 y = 0 while x > 0 do y = y*10 y = y + (x%10) x = x/10 end puts y
Make a nested array
[‘Dave’, 7, ‘Miranda’, 3, ‘Jason’, 11]
p [‘Dave’, 7, ‘Miranda’, 3, ‘Jason’, 11].each_slice(2).to_a
[[“Dave”, 7], [“Miranda”, 3], [“Jason”, 11]]
array = [“Jason”, “Jason”, “Teresa”, “Judah”, “Michelle”, “Judah”, “Judah”, “Allison”]
=> {“Jason”=>2, “Teresa”=>1, “Judah”=>3, “Michelle”=>1, “Allison”=>1}
count = Hash.new(0)
array.each {|v| count[v] += 1 }
or
array.inject(Hash.new(0)) { |total, e| total[e] += 1 ;total}
p count => {“Jason”=>2, “Teresa”=>1, “Judah”=>3, “Michelle”=>1, “Allison”=>1}
dividing an even number with biggest equal odd numbers as;
36 => [9,9,9,9]
54 => [27, 27]
56 => [7,7,7,7,7,7,7,7]
def fun(num) odd = num odd /= 2 while odd.even? [odd] * (num / odd) end
ruby prime number or not
- prime? => false
- prime? => true
class Fixnum def prime? ('1' * self) !~ /^1?$|^(11+?)\1+$/ end end
19283945 => (5,4,9,3,8,2,9,1)
n.to_s.chars.reverse!.map(&:to_i)
n.to_s.split(“”).reverse!.map(&:to_i)
19283945 => (5,4,9,3,8,2,9,1)
filter_list([1,’a’,’b’,0,15]) => [1,0,15]
l. reject {|i| i.is_a? String }
l. delete_if {|i| i.is_a? String }
l. select {|i| i.is_a? Integer }
l. delete_if { |x| x.class == String }
l. select{ |w| w.is_a? Numeric }
filter_list([1,’a’,’b’,0,15]) => [1,0,15]
add_binary(a,b)
(51,12) => 111111
(1,1) => 10
def add_binary(a,b) (a+b).to_s(2) end (51,12) => 111111 (1,1) => 10
arr(0,1) => 0+1 = 1
arr(-3,2) => -3 + -2 + -1 + 0 + 1 + 2 = -3
if b > a (a..b).to_a.inject(0, &:+) else (b..a).to_a.inject(0, &:+) end
or
b > a ? (a..b).reduce(:+) : (b..a).reduce(:+)
arr(0,1) => 0+1 = 1
arr(-3,2) => -3 + -2 + -1 + 0 + 1 + 2 = -3
find the smallest word in an array
s.split.map(&:size).min l = s.split(" ").min_by {|w| w.size } return l.length # l: length of the shortest word
Increment by set, then sum all numbers
sequence_sum(2, 6, 2), 12)
sequence_sum(1, 5, 1), 15)
sequence_sum(1, 5, 3), 5)
def sequence_sum(begin_number, end_number, step)
return 0 if begin_number > end_number
sum = 0
(begin_number..end_number).step(step) {|v| sum += v}
sum
end
or
(begin_number..end_number).step(step).reduce(0, :+)
sum of the numbers in a range (enumerable method)
(5. .10).reduce(:+)
(5. .10).inject(:+)
(5. .10).inject {|sum, n| sum + n }
Find the binaries 225 76 10011001 101001011
225 = 128 + 64 + 32 + 0 + 0 + 0 + 0 + 1 –> 11100001
76 = 64 + 0 + 0 + 8 + 4 + 0 + 0 –> 1001100
10011001 –> 128 + 0 + 0 + 16 + 8 + 0 + 0 + 1 = 143
101001011 –> 256 + 0 + 128 + 0 + 0 + 16 + 0 + 2 + 1 = 403
Binary to Hex conversion
1 0 1 0 0 1 1 0
----1 0 1 0 0 1 1 0 = 166 8 4 2 1 - 8 4 2 1 1 0 1 0 - 0 1 1 0 0xA 0x6 ----> 0xA6
Hex to Binary conversion
0xF2
---0xF2 0xF 0x2 8 4 2 1 - 8 4 2 1 1 1 1 1 - 0 0 1 0 ------> 11110010 = 242
Decimal to Hex conversion
137
137 / 16 –> 8 whole(128) + 0x9 (remainder)
8 / 16 –> 0 + 8 (remainder)
——> 0x89
Decimal to Hex conversion
243
243 / 16 –> 15 whole(240) + 0x3 (remainder)
15 –> F
——> 0xF3
Hex to Decimal conversion
0x9F
—-0x9F
9 x 16 = 144
F –> 15
—–> 144 + 15 = 159
Hex to Decimal conversion
0xA59C
----0xA59C 10 x (16x16x16) = 40960 5 x (16x16) = 1280 9 x 16 = 144 C --> 12 --------> 42.396
Regex expressions for “a b”
/s
/S
[^a]
“a b”
/s –> whitespace between a and b
/S –> match only a and b
[^a] –> match whitespace and b (not a)
Regex expressions \d \D \h \H \w \W
\d – Any decimal digit (0-9)
\D – Any character but a decimal digit (not decimal)
\w – matches “word characters”,
\W – matches “non-word characters”.
\h – Any hexadecimal digit (0-9, A-F, a-f) (ruby only)
\H – Any character but a hexadecimal digit (ruby only)
Write a regex that matches any sequence of 3 characters delimited by whitespace characters.
“reds and blues
the lazy cat sleeps”
“reds and blues
the lazy cat sleeps”
/\s…\s/ – matches \whitespace(and/the/cat)whitespace\
%w(1 2 3 4)
=> [“1”, “2”, “3”, “4”]
check if the number is integer?
def integer?(num)
num.to_i.to_s == num
end