-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings_arrays.rb
executable file
·162 lines (127 loc) · 2.68 KB
/
strings_arrays.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env ruby
# unique with bit vector
arr1 = ['a','b','c','d','e']
arr2 = ['a','b','c','d','e','e']
arr3 = ['a', 'a']
def unique(arr)
vector = nil
arr.each do |item|
val = item.ord - ?a.ord
if vector.nil?
vector = (1 << val)
elsif (vector & (1 << val)) != 0
puts 'false'
return
else
vector |= (1 << val)
end
end
puts 'true'
end
puts 'testbing bit vector unique'
unique(arr1)
unique(arr2)
unique(arr3)
# check permutations
def check_permutations(str1, str2)
str1.chars.sort(&:casecmp).join == str2.chars.sort(&:casecmp).join
end
puts 'check permutations'
str1 = 'abcd'
str2 = 'dcba'
puts check_permutations(str1, str2)
str2 = 'qwerty'
puts check_permutations(str1, str2)
# permutation of a palendrome
def perm_pal(str)
vector = nil
str.chars.each do |chr|
val = chr.ord - ?a.ord
if vector.nil?
vector = (1 << val)
else
vector ^= (1 << val)
end
end
return false if vector == 0
return false if vector & (vector - 1) != 0
true
end
puts 'Perm Pal'
str = 'tactcoapapa'
puts perm_pal(str)
str = 'tactcoapapaa'
puts perm_pal(str)
# one away
def lengths_off_by_1(longer, shorter)
index2 = 0
for i in 0..(shorter.length)
if shorter[i] != longer[i]
return false if index2 != i
index2 += 1
end
index2 += 1
end
true
end
def equal_lengths(str1, str2)
found_dif = false
for i in 0..(str1.length-1)
if str1[i] != str2[i]
return false if found_dif
found_dif = true
end
end
true
end
def one_away(str1, str2)
if str1.length == str2.length
equal_lengths(str1, str2)
elsif str1.length == (str2.length - 1)
lengths_off_by_1(str1, str2)
elsif str2.length == (str1.length - 1)
lengths_off_by_1(str2, str1)
else
false
end
end
puts 'One away'
str1 = 'pales'
str2 = 'paless'
puts one_away(str1, str2)
str1 = 'pales'
str2 = 'paxes'
puts one_away(str1, str2)
str1 = 'pales'
str2 = 'boats'
puts one_away(str1, str2)
# Rotate matrix
matrix = [
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4]
]
def rotate_matrix(matrix)
n = matrix.size
for layer in 0...(n / 2)
first = layer
last = n - layer - 1
for i in first...last
offset = i - first
tmp = matrix[first][i] # top
matrix[first][i] = matrix[last - offset][first] # left -> top
matrix[last - offset][first] = matrix[last][last - offset] # bottom -> left
matrix[last][last - offset] = matrix[i][last] # right -> bottom
matrix[i][last] = tmp # move old top to top right
end
end
print_matrix(matrix)
end
def print_matrix(matrix)
matrix.each do |arr|
puts arr.join(' ')
end
end
puts 'Rotate Matrix'
rotate_matrix(matrix)