-
Notifications
You must be signed in to change notification settings - Fork 0
/
2016-ransom-note.rb
72 lines (61 loc) · 1.84 KB
/
2016-ransom-note.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# This is the an anwer to the first question in programming praxis exercice:
#
# https://programmingpraxis.com/2016/11/25/three-amazon-interview-questions/
#
# Three Amazon Interview Questions
#
# 1) A kidnapper wants to write a ransom note by cutting characters from the text of
# a magazine. Given two strings containing the characters of the ransom note and
# the characters of the magazine, write a program to determine if the ransom note
# can be formed from the magazine.
@ransom_note = "whatever happens happens"
@magazine1 = "whatever happens happens"
@magazine2 = "whatever happens doesnt"
@magazine3 = "Lorem happens what ipsum dolor happ sit ever ametens"
def can_write?(ransom_note, magazine)
note = @ransom_note.gsub(/\s/, '').chars
mag = magazine.gsub(/\s/, '').chars
note.each do |char|
found_at = mag.index(char)
if found_at
mag.delete_at(found_at) # MUTATION!
else
return false
end
end
true
end
def can_write_recursive?(ransom_note, magazine)
_can_write_recursive(
@ransom_note.gsub(/\s/, '').chars,
magazine.gsub(/\s/, '').chars)
end
def _can_write_recursive(note, mag)
if note.empty?
true
else
note_head, *note_rest = note
found_at = mag.index(note_head)
if found_at
mag.delete_at(found_at) # MUTATION!
_can_write_recursive(note_rest, mag)
else
false
end
end
end
def test(code)
"#{code} -> #{eval(code)}"
end
puts <<-TEST
@ransom_note = #{@ransom_note}
@magazine1 = #{@magazine1}
@magazine2 = #{@magazine2}
@magazine3 = #{@magazine3}
#{test 'can_write?(@ransom_note, @magazine1)'}
#{test 'can_write?(@ransom_note, @magazine2)'}
#{test 'can_write?(@ransom_note, @magazine3)'}
#{test 'can_write_recursive?(@ransom_note, @magazine1)'}
#{test 'can_write_recursive?(@ransom_note, @magazine2)'}
#{test 'can_write_recursive?(@ransom_note, @magazine3)'}
TEST