Algorithms Flashcards

(127 cards)

1
Q

Reverse and Integer

x = 12345

A
x = 12345
y = 0
while x > 0 do
    y = y*10
    y = y + (x%10)
    x = x/10 
end
puts y
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Make a nested array

[‘Dave’, 7, ‘Miranda’, 3, ‘Jason’, 11]

A

p [‘Dave’, 7, ‘Miranda’, 3, ‘Jason’, 11].each_slice(2).to_a

[[“Dave”, 7], [“Miranda”, 3], [“Jason”, 11]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

array = [“Jason”, “Jason”, “Teresa”, “Judah”, “Michelle”, “Judah”, “Judah”, “Allison”]
=> {“Jason”=>2, “Teresa”=>1, “Judah”=>3, “Michelle”=>1, “Allison”=>1}

A

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}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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]

A
def fun(num)
  odd = num
  odd /= 2 while odd.even?
  [odd] * (num / odd)
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

ruby prime number or not

  1. prime? => false
  2. prime? => true
A
class Fixnum
  def prime?
    ('1' * self) !~ /^1?$|^(11+?)\1+$/
  end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

19283945 => (5,4,9,3,8,2,9,1)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

filter_list([1,’a’,’b’,0,15]) => [1,0,15]

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

add_binary(a,b)
(51,12) => 111111
(1,1) => 10

A
def add_binary(a,b)
(a+b).to_s(2)
end
(51,12) => 111111
(1,1) =>  10
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

arr(0,1) => 0+1 = 1

arr(-3,2) => -3 + -2 + -1 + 0 + 1 + 2 = -3

A
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

find the smallest word in an array

A
s.split.map(&:size).min
l = s.split(" ").min_by {|w| w.size }
return l.length
# l: length of the shortest word
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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)

A

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, :+)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

sum of the numbers in a range (enumerable method)

A

(5. .10).reduce(:+)
(5. .10).inject(:+)
(5. .10).inject {|sum, n| sum + n }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
Find the binaries
225
76
10011001
101001011
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Binary to Hex conversion

1 0 1 0 0 1 1 0

A
----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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Hex to Binary conversion

0xF2

A
---0xF2   
  0xF       0x2
8 4 2 1 - 8 4 2 1
1 1 1 1    - 0 0 1 0
------> 11110010 = 242
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Decimal to Hex conversion

137

A

137 / 16 –> 8 whole(128) + 0x9 (remainder)
8 / 16 –> 0 + 8 (remainder)
——> 0x89

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Decimal to Hex conversion

243

A

243 / 16 –> 15 whole(240) + 0x3 (remainder)
15 –> F
——> 0xF3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Hex to Decimal conversion

0x9F

A

—-0x9F
9 x 16 = 144
F –> 15
—–> 144 + 15 = 159

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Hex to Decimal conversion

0xA59C

A
----0xA59C
10 x (16x16x16) = 40960
5 x (16x16) = 1280
9 x 16 = 144
C --> 12
--------> 42.396
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Regex expressions for “a b”
/s
/S
[^a]

A

“a b”
/s –> whitespace between a and b
/S –> match only a and b
[^a] –> match whitespace and b (not a)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
Regex expressions
\d 
\D 
\h  
\H
\w  
\W
A

\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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Write a regex that matches any sequence of 3 characters delimited by whitespace characters.

“reds and blues
the lazy cat sleeps”

A

“reds and blues
the lazy cat sleeps”

/\s…\s/ – matches \whitespace(and/the/cat)whitespace\

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

%w(1 2 3 4)

A

=> [“1”, “2”, “3”, “4”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

check if the number is integer?

A

def integer?(num)
num.to_i.to_s == num
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How to load a file like .yml?
require 'yaml' | MESSAGES = YAML.load_file('calculator_messages.yml')
26
what does .yml file return? | welcome: 'welcome to programming'
Hash => def messages(message) MESSAGES[message] end
27
print float m with only 2 digits
{format('%02.2f', m)}
28
process => Another game! Y/N
answer = gets.chomp unless answer.downcase.start_with?('y') if answer !~ /y/i
29
famous_words = 'seven years ago..' | add 'Four score and' in front of the famous words string
"Four score and " + famous_words famous_words.prepend("Four score and ") "Four score and " << famous_words
30
flintstones = { "Fred" => 0, "Wilma" => 1, "Barney" => 2, "Betty" => 3} get the array ["Barney", 2] ?
flintstones.assoc("Barney")
31
``` # check that "Spot" is present ages = { "Herman" => 32, "Lily" => 30, "Grandpa" => 402 } ```
ages. has_key?("Spot") ages. include?("Spot") ages. member?("Spot") ages. key?("Lily")
32
monsters = "The Monsters are creepy in a good way." | => "tHE mUNSTERS ARE CREEPY IN A GOOD WAY."
monsters.swapcase
33
``` ages = { "Herman" => 32, "Lily" => 30 } additional_ages = { "Marilyn" => 22, "Spot" => 237 } ```
ages.merge!(additional_ages)
34
flintstones = %w(Fred Barney Wilma) | add "Dino" and "Hoppy" to array
flintstones.concat(%w(Dino Hoppy))
35
statement = "The Flintstones Rock!" | count number of "t" in string
=> statement.scan('t').count | => statement.chars.count{|char| char =~ /t/}
36
clear screen in command line?
def clear_screen system("clear") || system("cls") end
37
``` produce = { 'apple' => 'Fruit', 'carrot' => 'Vegetable', 'pear' => 'Fruit', 'broccoli' => 'Vegetable' } ``` select_fruit(produce) # => {"apple"=>"Fruit", "pear"=>"Fruit"}
``` def select_fruit(str) h = str.delete_if {|key,value| str[key] != 'Fruit' } end ``` select_fruit(produce) # => {"apple"=>"Fruit", "pear"=>"Fruit"}
38
{ a: "ant", b: "bear", c: "cat" }.each_with_index {|pair, index| puts "The index of #{pair} is #{index}." } [1, 2, 3].each_with_object([]) {|num, array| array << num if num.odd? } { a: "ant", b: "bear", c: "cat" }.first(2)
each_with_index => # The index of [:a, "ant"] is 0. each_with_object => # [1, 3] # array is initialized in method! firtst(2) => # [[:a, "ant"], [:b, "bear"]]
39
odd, even = [1, 2, 3].partition do |num| num.odd? end long, short = { a: "ant", b: "bear", c: "cat" }.partition do |key, value| value.size > 3 end
odd # => [1, 3] even # => [2] partition # => [[[:b, "bear"]], [[:a, "ant"], [:c, "cat"]]] long. to_h # => { :b => "bear" } short. to_h # => { :a => "ant", :c => "cat" }
40
[1, 2, 3].reject do |num| num > 3 end
reject => [1, 2, 3] (return values if false)
41
hash = { a: 'ant', b: 'bear' } | hash.shift
=> [:a, "ant"]
42
arr = [1, 2, 3, 4, 5] arr. take(2) arr. fetch(2) arr. slice(2)
arr = [1, 2, 3, 4, 5] arr. take(2) => # [1, 2] arr. fetch(2) => # 3 arr. slice(2) => # 3
43
ages = { "Herman" => 32, "Lily" => 30, "Grandpa" => 5843} sum all values
total_ages = 0 ages.each { |key, value| total_ages += value } total_ages ages.values.inject(:+)
44
ages = { "Herman" => 32, "Lily" => 30, "Grandpa" => 402 } remove if the age is over 100
ages. keep_if { |_, age| age < 100 } ages. select {|k,v| v < 100 } ages. delete_if {|k,v| v > 100 }
45
flintstones = %w(Fred Barney Wilma Betty BamBam Pebbles) get index of element starting with 'Be'
flintstones. index {|v| v.start_with?('Be') } flintstones. index { |name| name[0, 2] == "Be" } flintstones. each_with_index {|v, index| puts index if v.chars.first(2).join == 'Be'}
46
flintstones = %w(Fred Barney Wilma Betty BamBam Pebbles) => ["Fre", "Bar", "Wil", "Bet", "Bam", "Peb"]
flintstones. map {|v| v.split('').take(3).join } | flintstones. map! { |name| name[0, 3] }
47
statement = "The Flintstones Rock" #{ "F"=>1, "R"=>1, "T"=>1, "c"=>1, "e"=>2, ... }
statement.chars.each_with_object({}) {|letter, hash| hash[letter] = statement.scan(letter).count unless letter == ' ' } #{ "F"=>1, "R"=>1, "T"=>1, "c"=>1, "e"=>2, ... }
48
munsters = { "Lily" => { "age" => 30, "gender" => "female" }, "Grandpa" => { "age" => 402, "gender" => "male" }, "Eddie" => { "age" => 10, "gender" => "male" }, } add an age_group acc. to the age { "Lily" => {"age" => 30, "gender" => "female", "age_group" => "adult" }, "Grandpa" => { "age" => 402, "gender" => "male", "age_group" => "senior" }, "Eddie" => { "age" => 10, "gender" => "male", "age_group" => "kid" } }
``` munsters.values.each do |hash| case hash['age'] when (0..18) hash['age_group'] = 'kid' when (18...65) hash['age_group'] = 'adult' else hash['age_group'] = 'senior' end end ```
49
``` 2 <=> 1 1 <=> 2 2 <=> 2 'b' <=> 'a' 'a' <=> 'b' 'b' <=> 'b' 1 <=> 'a' ```
``` 2 <=> 1 # => 1 1 <=> 2 # => -1 2 <=> 2 # => 0 'b' <=> 'a' # => 1 'a' <=> 'b' # => -1 'b' <=> 'b' # => 0 1 <=> 'a' # => nil ```
50
'!' <=> 'A' 'b' <=> '}' 'A' <=> 'a' How do you determine a string's ASCII position?
'!' <=> 'A' # => -1 'b' <=> '}' # => -1 'A' <=> 'a' # => -1 You can determine a string's ASCII position by calling ord on the string. 'b'.ord # => 98 '}'.ord # => 125
51
['cot', 'bed', 'mat'] sort acc. to second character of strings people = { Kate: 27, john: 25, Mike: 18 } sort acc. to age
``` ['cot', 'bed', 'mat'].sort_by { |word| word[1] } # => ["mat", "bed", "cot"] ``` ``` people.sort_by { |name, age| age } # => [[:Mike, 18], [:john, 25], [:Kate, 27]] ```
52
arr1 = ["a", "b", "c"] arr2 = arr1.dup or arr2 = arr1.clone arr2[1].upcase!
arr2 # => ["a", "B", "c"] | arr1 # => ["a", "B", "c"]
53
arr1 = ["a", "b", "c"].freeze arr2 = arr1.clone arr2 << "d" => ?
=> RuntimeError: can't modify frozen Array
54
arr1 = ["a", "b", "c"].freeze arr2 = arr1.dup arr2 << "d" =>
arr2 # => ["a", "b", "c", "d"] | arr1 # => ["a", "b", "c"]
55
str = "abc".freeze str << "d" str = >
=> RuntimeError: can't modify frozen String Only mutable objects can be frozen because immutable objects, like integers, are already frozen. We can check if an object is frozen with the frozen? method. 5.frozen? # => true
56
arr = ['10', '11', '9', '7', '8'] sort in descending order # => ["11", "10", "9", "8", "7"]
arr = ['10', '11', '9', '7', '8'] arr. map! {|v| v.to_i }.sort.reverse arr. sort {|a,b| b.to_i <=> a.to_i }
57
munsters = { "Herman" => { "age" => 32, "gender" => "male" }, "Grandpa" => { "age" => 402, "gender" => "male" }, "Marilyn" => { "age" => 23, "gender" => "female"} } get the sum of the ages of males
``` sum = 0 #munsters.values.each {|spec| sum += spec['age'] if spec['gender'] == 'male'} #munsters.each_value {|spec| sum += spec['age'] if spec['gender'] == 'male' } sum => # 434 ```
58
hsh = {first: ['the', 'quick'], second: ['brown', 'fox'], third: ['jumped'], fourth: ['over', 'the', 'lazy', 'dog']} get the vowels as string
hsh = {first: ['the', 'quick'], second: ['brown', 'fox'], third: ['jumped'], fourth: ['over', 'the', 'lazy', 'dog']} hsh.values.each {|arr| arr.map! {|str| str.scan(/[aeoui]/) } }.flatten.join => "euiooueoeeao"
59
arr = [{a: [1, 2, 3]}, {b: [2, 4, 6], c: [3, 6], d: [4]}, {e: [8], f: [6, 10]}] get the keys contain only the even values => [{:e=>[8], :f=>[6, 10]}]
arr = [{a: [1, 2, 3]}, {b: [2, 4, 6], c: [3, 6], d: [4]}, {e: [8], f: [6, 10]}] arr.select {|hash| hash.all? {|k,v| v.all? {|val| val.even? } } }
60
get a random hexadecimal number
num = Random.rand(16).to_s(16)
61
!!nil => false def winner?(brd) !!detect_winner(brd) end
while detect_winner method returns nothing yet (nil) | !!nil returns false if it returns a value (which is true) then returns true for the winner method to be used.
62
number = 12345 number, remainder = number.divmod(10) number? remainder?
number => [1234, 5] number => 1234 remainder => 5
63
arr = [1,2,3] new_arr = arr[0], arr[1], arr[2] new_arr => ?
arr = [1,2,3] new_arr = *arr new_arr => [1,2,3]
64
vehicles = ['car', 'car', 'truck', 'car', 'SUV', 'truck', 'motorcycle', 'motorcycle', 'car', 'truck'] how to count the quantity of 'car'
puts vehicles.count('car') => 4
65
``` puts "==> Enter the first number:" first = gets.chomp # 3 puts "==> Enter the second number:" second = gets.chomp # 2 %w(+ - * / % **).each do |op| equation = "#{first} #{op} #{second}" puts "==> #{equation} = #{eval(equation)}" end ``` eval => Evaluates the Ruby expression(s) in string.
``` ==> Enter the first number: 3 ==> Enter the second number: 2 ==> 3 + 2 = 5 ==> 3 - 2 = 1 ==> 3 * 2 = 6 ==> 3 / 2 = 1 ==> 3 % 2 = 1 ==> 3 ** 2 = 9 ```
66
array = [3, 2] %w(+ - * / % **). perform calculations for the array elements
``` array = [3, 2] %w(+ - * / % **).each { |op| array.inject(op) } ==> 3 + 2 = 5 ==> 3 - 2 = 1 ==> 3 * 2 = 6 ==> 3 / 2 = 1 ==> 3 % 2 = 1 ==> 3 ** 2 = 9 ```
67
oddities([2, 3, 4, 5, 6]) == [2, 4, 6] oddities(['abc', 'def']) == ['abc'] oddities([123]) == [123] oddities([]) == []
oddities([2, 3, 4, 5, 6]) == [2, 4, 6] ``` def oddities(arr) arr.select { |val| arr.index(val).even? } end ```
68
group([3, 2, 6, 2, 2, 2]) => [[3], [2, 2, 2, 2], [6]] how to group an array by occurence of its values?
``` def group(arr) arr.group_by {|v| v}.values end ``` arr.group_by {|v| v} => returns a hash like {3=>[3], 2=>[2, 2, 2, 2], 6=>[6]}
69
delete_nth([20,37,20,21], 1) # [20,37,21], "From array [20,37,20,21],1 you get") delete_nth([1,1,3,3,7,2,2,2,2], 3) # [1, 1, 3, 3, 7, 2, 2, 2], "From array [1,1,3,3,7,2,2,2,2],3 you get")
delete_nth([20,37,20,21], 1) # [20,37,21], "From array [20,37,20,21],1 you get") ``` def delete_nth(array, max_e) hash = Hash.new(0) arr.reject {|val| (hash[val] += 1) > max_e } end ```
70
make a password generator which is: * 6 - 20 characters long * contains at least one lowercase letter * contains at least one uppercase letter * contains at least one number * contains only alphanumeric characters (no special characters)
ALPHA_NUM = [*('a'..'z'), *('A'..'Z'), *('0'..'9')] ``` def password_gen Array.new(20) { ALPHA_NUM.sample }.take(rand(6..20)).join end ```
71
reverse a string without using String.reverse method
``` def reversed(str) reversed_str = '' str.chars.each {|val| reversed_str.prepend(val) } reversed_str end ``` reversed('hello') => 'olleh'
72
create a hash from the hexadecimal strings to integer values
numbers = '0123456789ABCDEF'.split('').zip(0..16).to_h
73
convert an Integer to a String integer_to_string(4321) == '4321' integer_to_string(0) == '0' integer_to_string(5000) == '5000'
integer_to_string(4321) == '4321' ``` def integer_to_string(num) return [num].join if num == 0 arr = [] while num > 0 arr.unshift(num%10) num /= 10 end arr.join end ```
74
convert an Integer to a String (Alternative way) integer_to_string(4321) == '4321' integer_to_string(0) == '0' integer_to_string(5000) == '5000'
integer_to_string(4321) == '4321' ``` def integer_to_string(num) arr = [*'0'..'9'] str = '' loop do num, rem = num.divmod(10) str.prepend(arr[rem]) break if num == 0 end str end ```
75
ascii_value('Four score') #== 984 ascii_value('Launch School') #== 1251 ascii_value('a') # == 97 ascii_value('') #== 0
ascii_value('Four score') #== 984 ``` def ascii_value(str) str.inject(0) {|sum, v| sum + v.ord } end or str.sum => return direclty sum of ascii numbers alternatively => str.map(&:ord).reduce(0, :+) ``` char.ord.chr == char => .chr is to convert number to ascii character
76
``` time_of_day(0) == "00:00" time_of_day(-3) == "23:57" time_of_day(35) == "00:35" time_of_day(-1437) == "00:03" time_of_day(3000) == "02:00" ```
time_of_day(35) == "00:35" ``` def time_of_day(minutes) hours, mins = minutes.divmod(60) hours = hours % 24 format('%02d:%02d', hours, mins) end ```
77
time_of_week(3000) == "Tuesday 02:00" time_of_week(800) == "Sunday 13:20" time_of_week(-4231) == "Thursday 01:29"
time_of_week(-4231) == "Thursday 01:29" def time_of_week(mins) t = Time.new(2017, 1, 1) # start from a sunday 00:00 (t + (mins * 60)).strftime('%A %H:%M') # convert minutes to seconds before adding end
78
after_midnight('00:00') == 0 | after_midnight('12:34') == 754
after_midnight('12:34') == 754 def after_midnight(str) hour, min = str.split(':').map(&:to_i) (hour * 60 + min) % (24 * 60) end hour, min = str.scan(/\d\d/).map(&:to_i)
79
Write the method to find primes number between a range. find_primes(3, 10) => 3, 5, 7 find_primes(1, 2) => 1, 2
find_primes(3, 10) => 3, 5, 7 ``` def is_prime?(number) (2..(number-1)).each do |div| return false if number % div == 0 end end ``` ``` def find_primes(num1, num2) (num1..num2).select do |num| is_prime?(num) end end ```
80
swap the first and last character of each word of string. ``` p swap('Oh what a wonderful day it is') == 'hO thaw a londerfuw yad ti si' p swap('Abcde') == 'ebcdA' p swap('a') == 'a' ```
p swap('Oh what a wonderful day it is') == 'hO thaw a londerfuw yad ti si' ``` def swap(str) str.split.each {|word| word[0], word[-1] = word[-1], word[0] }.join(', ') end ``` alternative way => str.gsub(/(\w)(\w*)(\w)/, '\3\2\1')
81
Exchange the values of a and b
a, b = b, a
82
cleanup("---what's my +*& line?") == ' what s my line '
cleanup("---what's my +*& line?") == ' what s my line ' def cleanup(str) str.gsub(/[^a-z]+/i, ' ') end str. tr_s('^a-zA-Z', ' ') str. gsub(/[^a-z]/i, ' ').squeeze(' ')
83
alphabetic_number_sort((0..19).to_a) == [ 8, 18, 11, 15, 5, 4, 14, 9, 19, 1, 7, 17, 6, 16, 10, 13, 3, 12, 2, 0]
NUMBERS = %w(zero one two three fou, five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen) ``` def alphabetic_number_sort(numbers) numbers.sort_by {|number| NUMBERS[number] } end ```
84
inter_leave([1,2,3], ['a', 'b', 'c']) => [1, "a", 2, "b", 3, "c"]
inter_leave([1,2,3], ['a', 'b', 'c']) => [1, "a", 2, "b", 3, "c"] ``` def inter_leave(arr1, arr2) # [arr1, arr2].transpose.flatten # arr1.zip(arr2).flatten end ```
85
``` crunch('ddaaiillyy ddoouubbllee') == 'daily double' crunch('4444abcabccba') == '4abcabcba' crunch('ggggggggggggggg') == 'g' crunch('a') == 'a' crunch('') == '' ```
crunch('ddaaiillyy ddoouubbllee') == 'daily double' ``` def crunch(str) arr = str.chars arr.map.with_index {|v, idx| v unless v == arr[idx+1] }.compact.join end ``` ``` def crunch(str) new_arr = [] arr = str.chars arr.each_with_index {|v, idx| new_arr << v unless v == arr[idx+1] } new_arr.join end ``` str. squeeze str. scan(/(.)(?!\1+)/).join str. tr_s(' -z', ' -z') str. chars.select.with_index(1) { |char, idx| char != str[idx] }.join
86
remove the fist character of a string str = 'hello' => 'ello'
str = 'hello' => 'ello' str[0] = '' str.slice!(0) str[1..-1] => 'ello' # no mutation
87
Write a method to calculate n elements of a fibonacci serie. fibonacci(7) => [1, 1, 2, 3, 5, 8, 13]
fibonacci(7) => [1, 1, 2, 3, 5, 8, 13] ``` def fibonacci(quantity) fibonacci = [1, 1] (quantity-2).times { fibonacci << fibonacci.last(2).inject(:+) } fibonacci end ```
88
reverse an array without using reverse method. (with mutation the array) list = [1,2,4,3] reverse!(list) # => [3,4,2,1]
list = [1,2,4,3] reverse!(list) # => [3,4,2,1] ``` def reverse!(arr) result = arr.clone n = arr.size count = 0 while n > 0 arr[count] = result[n-1] n -= 1 count += 1 end arr end ``` list.sort_by!.with_index { |_, idx| -idx } list.each_index { |idx| list.insert(idx, list.pop) }
89
reverse an array (not mutating) list = [1,2,4,3] reverse(list) # => [3,4,2,1]
list = [1,2,4,3] reverse(list) # => [3,4,2,1] ``` def reverse(array) result_array = [] array.reverse_each { |element| result_array << element } result_array end ``` ``` def reverse(arr) arr.map.with_index {|_, idx| arr[-idx-1] } end ``` ``` def reverse(arr) list = [] arr.each_index {|idx| list << arr[-idx-1] } list end ```
90
merge([1, 3, 5], [3, 6, 9]) # == [1, 3, 5, 6, 9]
merge([1, 3, 5], [3, 6, 9]) # == [1, 3, 5, 6, 9] ``` def merge(arr1, arr2) #arr1.concat(arr2).uniq # arr1 | arr2 # (arr1 + arr2).uniq end ```
91
halvsies([1, 2, 3, 4]) == [[1, 2], [3, 4]] halvsies([1, 5, 2, 4, 3]) == [[1, 5, 2], [4, 3]] halvsies([5]) == [[5], []] halvsies([]) == [[], []]
halvsies([1, 2, 3, 4]) == [[1, 2], [3, 4]] halvsies([1, 5, 2, 4, 3]) == [[1, 5, 2], [4, 3]] def halvsies(arr) first, second = arr.partition.with_index do |_, idx| arr.size.even? ? idx < (arr.size/2) : idx < (arr.size/2)+1 end end def halvsies(array) first_half = array.slice(0, (array.size / 2.0).ceil) second_half = array.slice(first_half.size, array.size - first_half.size) [first_half, second_half] end
92
find the repeating number find_dup([1, 5, 3, 1]) #== 1
find_dup([1, 5, 3, 1]) #== 1 ``` def find_dup(arr) arr.find {|val| arr.count(val) == 2 } end ``` ``` def find_dup(arr) arr.group_by {|v| v}.find {|key, value| value.size > 1 }.first end ``` ``` def find_dup(arr) arr.group_by(&:itself).each {|key, value| return key if value.size > 1 } end ```
93
Write an include? method ``` include?([1,2,3,4,5], 3) == true include?([1,2,3,4,5], 6) == false include?([], 3) == false include?([nil], nil) == true include?([], nil) == false ```
include?([1,2,3,4,5], 3) == true ``` def include?(arr, val) arr.any? {|v| v == val } end ``` arr.member?(val) !!arr.find_index(val)
94
triangle(5, 4) ``` # * # ** # *** # **** #***** ```
triangle(5, 4) ``` def triangle(number, coord=1) case coord when 1 #top-left number.downto(1) do |count| puts ('*' * count).ljust(number) end when 2 #top-right number.downto(1) do |count| puts ('*' * count).rjust(number) end when 3 #bottom-left 1.upto(number) do |count| puts ('*' * count).ljust(number) end when 4 #bottom-right 1.upto(number) do |count| puts ('*' * count).rjust(number) end end end ```
95
letter_case_count('abCdef 123') == { lowercase: 5, uppercase: 1, neither: 4 }
letter_case_count('abCdef 123') == { lowercase: 5, uppercase: 1, neither: 4 } def letter_case_count(str) hash = {} hash[:lowercase] = str.chars.count {|char| char.match(/[a-z]/) } hash[:uppercase] = str.chars.count {|char| char.match(/[A-Z]/) } hash[:neither] = str.chars.count {|char| char.match(/[^a-z]/i) } hash end def letter_case_count(str) counts = {} counts[:lowercase] = str.scan(/[a-z]/).length counts[:uppercase] = str.scan(/[A-Z]/).length counts[:neither] = str.scan(/[^A-Za-z]/).length counts end
96
word_cap('four score and seven') == 'Four Score And Seven'
word_cap('four score and seven') == 'Four Score And Seven' ``` def word_cap(str) str.split.each {|char| char[0] = char.chr.upcase }.join(' ') end ``` def word_cap(str) str.gsub(/\S+/, &:capitalize) end ``` # without using String#capitalize: def word_cap(str) str.downcase.gsub(/(?<=\A|\s)\S/, &:upcase) end ``` ``` def word_cap(str) str.downcase.gsub(/(\A\w|\s\S)/) { |match| match.upcase } end ```
97
regexp only the block with quotes. "this is a string in quotes". but this one is not.
"this is a string in quotes". but this one is not. /(['"]).+?\1/ "this is a string in quotes" will be selected
98
text = %(We read "War of the Worlds".) ==> We read "The Time Machine".
text = %(We read "War of the Worlds".) puts text.sub(/(['"]).+\1/, '\1The Time Machine\1') prints: We read "The Time Machine".
99
``` p fields("Pete,201,Student") # -> ['Pete', '201', 'Student'] ``` ``` p fields("Pete \t 201 , TA") # -> ['Pete', '201', 'TA'] ``` ``` p fields("Pete \t 201") # -> ['Pete', '201'] ```
``` p fields("Pete \t 201 , TA") # -> ['Pete', '201', 'TA'] ``` def fields(str) str.split(/[ \t,]+/) end
100
change the FIRST operator to a ? ``` mystery_math('4 + 3 - 5 = 2') # -> '4 ? 3 - 5 = 2' ``` ``` mystery_math('(4 * 3 + 2) / 7 - 1 = 1') # -> '(4 ? 3 + 2) / 7 - 1 = 1' ```
def mystery_math(str) str.sub(/[+\-*\/]/, '?') end sub => replace the FIRST occurence gsub => replace ALL occurences tr => just writing string not regex tr_s => removes all found duplicates while replacing
101
format_date('2016-06-17') # -> '17.06.2016' hint => think about capture groupes defined by (...)
format_date('2016-06-17') # -> '17.06.2016' ``` def format_date(str) #str.split('-').reverse.join('.') ``` str.sub(/\A(\d\d\d\d)-(\d\d)-(\d\d)\z/, '\3.\2.\1') end
102
([a-c])x\1x\1 matches?
([a-c])x\1x\1 matches axaxa, bxbxb and cxcxc.
103
q(?!u) q(?=u) (?
q(?!u) => match a q NOT followed by u q(?=u) => match a q followed by u (? match b NOT preceded by an a (?<=a)b => match b preceded by a
104
swapcase the every second character of the words staggered_case('I Love Launch School!') #== 'I LoVe lAuNcH ScHoOl!'
staggered_case('I Love Launch School!') #== 'I LoVe lAuNcH ScHoOl!' def staggered_case(str) str.gsub(/..?/, &:capitalize) end ``` def staggered_case(str) str.chars.each_slice(2).map { |a, b| [a.upcase, b.to_s.downcase] }.join end ``` str.scan(/..?/).map(&:capitalize).join
105
multiply each element of first arry with second arry and sort the numbers. multiply_all_pairs([2, 4], [4, 3, 1, 2]) == [2, 4, 4, 6, 8, 8, 12, 16]
multiply_all_pairs([2, 4], [4, 3, 1, 2]) == [2, 4, 4, 6, 8, 8, 12, 16] ``` def multiply_all_pairs(arr1, arr2) arr1.product(arr2).map {|a, b| a * b }.sort end ```
106
enultimate('last word') == 'last' | penultimate('Launch School is great!') == 'is'
enultimate('last word') == 'last' penultimate('Launch School is great!') == 'is' def enultimate(str) str[/\S+(?=\s\S+$)/] end str.split[-2]
107
sum_of_sums([3, 5, 2]) == (3) + (3 + 5) + (3 + 5 + 2) # -> (21) sum_of_sums([1, 5, 7, 3]) == (1) + (1 + 5) + (1 + 5 + 7) + (1 + 5 + 7 + 3) # -> (36) sum_of_sums([4]) == 4 sum_of_sums([1, 2, 3, 4, 5]) == 35
sum_of_sums([3, 5, 2]) == (3) + (3 + 5) + (3 + 5 + 2) # -> (21) ``` def sum_of_sums(arr) sum = 0 arr.each.with_index(1) {|_, idx| sum += arr.first (dx).inject(:+) } sum end ``` ``` def sum_of_sums(arr) arr.size.downto(1).reduce(0) { |sum, n| sum + arr[-n] * n } end ```
108
write a madlib game "Do you walk your blue dog quickly? That's hilarious!"
def madlib noun, verb, adj, adv = %w[noun verb adjective adverb].map do |word_type| puts "Please enter a#{'n' if word_type[/\Aa/]} #{word_type}:" gets.chomp end puts format("Do you %s your %s %s %s? That's hilarious!", verb, adj, noun, adv) end
109
substrings_at_start('abc') #== ['a', 'ab', 'abc'] substrings_at_start('a') #== ['a'] substrings_at_start('xyzzy') #== ['x', 'xy', 'xyz', 'xyzz', 'xyzzy']
substrings_at_start('abc') #== ['a', 'ab', 'abc'] ``` def substrings_at_start(str, arr=[]) (0..str.size-1).each_with_object([]) {|idx, result| result << str[0..idx] } end ``` ``` # recursive def substrings_at_start(str, arr=[]) return arr if arr.size == str.size arr << str[0..arr.size] substrings_at_start(str, arr) end ```
110
remove first and last characters str = 'hello' arr = [1, 2, 3, 4, 5]
str = 'hello' unmutated methods str.chr => 'h' str.chop => 'o' mutated methods str.prepend(a) => prepends a str[0] = '' str[-1] ='' arr = [1, 2, 3, 4, 5] unmutated methods arr.first => 1 arr.last => 5 mutated methods arr. shift => 1 arr. pop => 5 arr. unshift(a) => prepends a
111
Regex cheat sheet /a/ character 'a' /\// character '/' (/\?*+{[.|()^$ need to be escaped with \) /./ any character (including newline for /.../m) ``` /a?/ 0..1 'a' /a*/ 0..n 'a' /a+/ 1..n 'a' /a{2,7}/ 2..7 'a' /a{2,}/ 2..n 'a' /a{,7}/ 0..7 'a' ``` /a?bc?/ 'b' or 'ab' or 'bc' or 'abc' /a|bc/ 'a' or 'bc' /(a|b)c/ 'ac' or 'bc' /[abc]/ a or b or c /[^abc]/ any character except a or b or c /[a-cF-H]/ a or b or c or F or G or H /\d/ any digit [0-9] /\w/ any letters, numbers or underscores [a-zA-Z0-9_] /\s/ any whitespace character (including newline for /.../m) /\D/ any character except digits /\W/ any character except letters, numbers or underscores /\S/ any character except whitespace /^abc/ abc after line start /abc$/ abc before line end
Regex cheat sheet /a/ character 'a' /\// character '/' (/\?*+{[.|()^$ need to be escaped with \) /./ any character (including newline for /.../m) ``` /a?/ 0..1 'a' /a*/ 0..n 'a' /a+/ 1..n 'a' /a{2,7}/ 2..7 'a' /a{2,}/ 2..n 'a' /a{,7}/ 0..7 'a' ``` /a?bc?/ 'b' or 'ab' or 'bc' or 'abc' /a|bc/ 'a' or 'bc' /(a|b)c/ 'ac' or 'bc' /[abc]/ a or b or c /[^abc]/ any character except a or b or c /[a-cF-H]/ a or b or c or F or G or H /\d/ any digit [0-9] /\w/ any letters, numbers or underscores [a-zA-Z0-9_] /\s/ any whitespace character (including newline for /.../m) /\D/ any character except digits /\W/ any character except letters, numbers or underscores /\S/ any character except whitespace /^abc/ abc after line start /abc$/ abc before line end
112
``` substrings('abcde') # == ['a', 'ab', 'abc', 'abcd', 'abcde', 'b', 'bc', 'bcd', #'bcde', # 'c', 'cd', 'cde', # 'd', 'de', # 'e' ] ```
``` p substrings('abcde') # == ['a', 'ab', 'abc', 'abcd', 'abcde', 'b', 'bc', 'bcd', #'bcde', # 'c', 'cd', 'cde', # 'd', 'de', # 'e' ] ``` ``` def substrings(str, arr = []) return arr if str.empty? str.size.times {|idx| arr << str[0..idx] } str[0] = '' substrings(str, arr) end ```
113
``` twice(37) == 74 twice(44) == 44 twice(334433) == 668866 twice(444) == 888 twice(107) == 214 twice(103103) == 103103 twice(3333) == 3333 twice(7676) == 7676 twice(123_456_789_123_456_789) == 123_456_789_123_456_789 twice(5) == 10 ```
twice(103103) == 103103 twice(3333) == 3333 def twice(num) num.to_s[/\A(\d+)\1\z/] ? num : num * 2 end alternative way def twice(number) return number * 2 if number.to_s.size.odd? arr = number.to_s.chars arr.first(arr.size/2) == arr.last(arr.size/2) ? number : number * 2 end
114
``` uppercase?('t') == false uppercase?('T') == true uppercase?('Four Score') == false uppercase?('FOUR SCORE') == true uppercase?('4SCORE!') == true uppercase?('') == true ```
def uppercase?(str) !str[/[a-z]/] end ``` def uppercase?(str) str == str.upcase end ```
115
``` p sequence(5, 1) == [1, 2, 3, 4, 5] p sequence(4, -7) == [-7, -14, -21, -28] p sequence(3, 0) == [0, 0, 0] p sequence(0, 1000000) == [] ```
``` p sequence(5, 1) == [1, 2, 3, 4, 5] p sequence(4, -7) == [-7, -14, -21, -28] ``` def sequence(count, start) start.step(by: start).take(count) end ``` def sequence(count, start) (1..count).map { |idx| idx * start } end ```
116
Grade ``` 90 <= score <= 100 'A' 80 <= score < 90 'B' 70 <= score < 80 'C' 60 <= score < 70 'D' 0 <= score < 60 'F' ``` get_grade(95, 90, 93) == "A" get_grade(50, 50, 95) == "D"
``` GRADES = { A: 90..1000, B: 80...90, C: 70...80, D: 60...70, F: 0...60 } def get_grade(*scores) GRADES.find { |k, v| v.cover?(scores.reduce(:+).fdiv(scores.size)) }.first.to_s end ```
117
buy_fruit([["apples", 3], ["orange", 1], ["bananas", 2]]) ``` def buy_fruit(arr) arr.map {|a, b| b} end ```
buy_fruit([["apples", 3], ["orange", 1], ["bananas", 2]]) ``` def buy_fruit(arr) arr.map {|a, b| b} end ``` a => ["apples", "orange", "bananas"] b => [3, 1, 2]
118
diamond(9) ``` * *** ***** ******* ********* ******* ***** *** * ```
``` def diamond(n) [*(1...n), *n.downto(1)].each { |i| puts ('*' * i).center(n) if i.odd? } end ``` [*(1...n), *n.downto(1)] => [1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1]
119
word_to_digit('Please call me at five five five one two three four. Thanks.') == 'Please call me at 5 5 5 1 2 3 4. Thanks. Tip for creating the hash: DIGITS = %w(one two three four five six seven eight nine).zip([1,2,3,4,5,6,7,8,9]).to_h
word_to_digit('Please call me at five five five one two three four. Thanks.') == 'Please call me at 5 5 5 1 2 3 4. Thanks. DIGITS = %w(zero one two three four five six seven eight nine) def word_to_digit(str) str.gsub(/\w+/) { |word| DIGITS.index(word) || word } end DIGIT_HASH = { 'zero' => '0', 'one' => '1', 'two' => '2', 'three' => '3', 'four' => '4', 'five' => '5', 'six' => '6', 'seven' => '7', 'eight' => '8', 'nine' => '9' }.freeze ``` def word_to_digit(words) DIGIT_HASH.keys.each do |word| words.gsub!(/\b#{word}\b/, DIGIT_HASH[word]) end words end ``` ``` def word_to_digit(str) NUMERIC_WORDS.each do |number| str = str.gsub(number, NUMERIC_WORDS.index(number).to_s) end str end ```
120
``` p fibonacci(20) #== 6765 p fibonacci(100) #== 354224848179261915075 p fibonacci(100_001) ```
``` p fibonacci(20) #== 6765 p fibonacci(100) #== 354224848179261915075 ``` ``` def fibonacci(n) (3..n).reduce([1, 1]) { |(a, b), _| [b, a + b] }.last end ```
121
Count how many 13th friday in a year. ``` p friday_13th(2015) #== 3 p friday_13th(1986) #== 1 p friday_13th(2019) #== 2 ```
friday_13th(2015) #== 3 friday_13th(1986) #== 1 friday_13th(2019) #== 2 require 'date' ``` def friday_13th(year) (1..12).count do |month| Date.new(year, month, 13).friday? end end ```
122
``` p find_fibonacci_index_by_length(2) #== 7 p find_fibonacci_index_by_length(10) #== 45 p find_fibonacci_index_by_length(100) #== 476 p find_fibonacci_index_by_length(1000) #== 4782 p find_fibonacci_index_by_length(10000) #== 47847 ```
``` def find_fibonacci_index_by_length(digit) fib = [1, 1] idx = 2 loop do fib << fib.last(2).reduce(:+) return idx+1 if fib[idx].to_s.size == digit idx += 1 end end ```
123
The method should determine if any two numbers in the array sum to the target number. ``` p sum([1, 2, 3, 4], 5) #== [1, 4] p sum([9, 3, 11, 5, 7], 18) #== [11, 7] p sum([1, 2], 15) #== "No match was found" ```
``` p sum([1, 2, 3, 4], 5) #== [1, 4] p sum([9, 3, 11, 5, 7], 18) #== [11, 7] p sum([1, 2], 15) #== "No match was found" ``` ``` def sum(arr, sum) arr.each_index do |idx| count = 1 loop do return [arr[idx], arr[count]] if arr[idx] + arr[count] == sum count += 1 break if count == arr.size end end 'No match was found' end ``` def sum(arr, sum) result = arr.combination(2).to_a.select {|val| val.inject(:+) == sum } result.empty? ? 'No match was found' : result.min end *** alternative method for the consecutive array elements => each_cons(param)
124
list = ['x','cat', 'dog', 'x', 'dolphin', 'cougar', 'whale'] slice the array where x is. [["cat", "dog"], ["dolphin", "cougar", "whale"]]
list = ['x','cat', 'dog', 'x', 'dolphin', 'cougar', 'whale'] ``` def solution(list) list.slice_when {|prev, curr| curr == 'x' }.to_a.map {|v| v.drop(1) } end ```
125
slice the array if there are at least 3 incremented elements list = [-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 11, 12, 15] ==> "-6,-3-1,3-5,7-9,11,12,15"
def solution(list) list.slice_when {|prev, curr| curr != prev.next }.to_a.map do |arr| arr.size < 3 ? arr.first(2) : "#{arr[0]}-#{arr[-1]}" end.join(',') end
126
Find the shortest way by eliminating inefficient directions. ``` a = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"] p dirReduc(a) #, ["WEST"]) ``` ``` u=["NORTH", "WEST", "SOUTH", "EAST"] p dirReduc(u) #, ["NORTH", "WEST", "SOUTH", "EAST"]) ```
a = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"] ``` OPPOSITE = { "NORTH" => "SOUTH", "SOUTH" => "NORTH", "EAST" => "WEST", "WEST" => "EAST" } ``` ``` def dirReduc(arr) stack = [] arr.each do |dir| OPPOSITE[dir] == stack.last ? stack.pop : stack << dir end stack end ```
127
transpose any matrix ``` p transpose([[1, 2, 3, 4]]) == [[1], [2], [3], [4]] p transpose([[1], [2], [3], [4]]) == [[1, 2, 3, 4]] p transpose([[1, 2, 3, 4, 5], [4, 3, 2, 1, 0], [3, 7, 8, 6, 2]]) == [[1, 4, 3], [2, 3, 7], [3, 2, 8], [4, 1, 6], [5, 0, 2]] p transpose([[1]]) == [[1]] ```
``` def transpose(matrix) count = 0 result = [] while count < matrix[0].size result << matrix.each_index.map do |idx| matrix[idx][count] end count += 1 end result end ``` Alternative way: def transpose(matrix) matrix.first.zip(*matrix.drop(1)) end [1, 2, 3].zip([4, 5, 6], [7, 8, 9]) # => [[1, 4, 7], [2, 5, 8], [3, 6, 9]] matrix = [[1, 2, 3, 4, 5], [4, 3, 2, 1, 0], [3, 7, 8, 6, 2]] matrix.drop(1) # => [[4, 3, 2, 1, 0], [3, 7, 8, 6, 2]] matrix.first # => [1, 2, 3, 4, 5] .zip(*matrix.drop(1)) # => [[1, 4, 3], [2, 3, 7], [3, 2, 8], [4, 1, 6], [5, 0, 2]]