-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz.rb
74 lines (61 loc) · 1.29 KB
/
quiz.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
# take next 2nd letter
str = 'abc dez'.downcase
result = ''
letter = ('a'..'z').to_a
range = (1..26).to_a
str.chars.each_with_index do |c, i|
if c == ' '
result << ' '
else
result << letter[(letter.index(c) + 2) % 26]
end
end
p result
# conver each char with 2 digit number
str = 'abc q'.downcase
result = ''
letter = ('a'..'z').to_a
range = (1..26).to_a
str.chars.each_with_index do |c, i|
if c == ' '
result << ' '
else
result << "%02d" % letter.index(c).next
end
end
p result
# first letter capitalize, even position take next char, odd position prev number
str = 'a bb'.downcase
result = ''
letter = ('a'..'z').to_a
range = (1..26).to_a
str.chars.each_with_index do |c, i|
if i.zero?
result << c.upcase
elsif c == ' '
result << ' '
elsif i % 2 == 1
next_index = letter.index(c).next % 26
result << letter[next_index]
else
prev_index = letter.index(c) - 1
result << letter[prev_index]
end
end
p result
# ----------------------------
str = 'sumon a'.downcase
result = ''
letter = ('a'..'z').to_a
range = (1...26).to_a
str.chars.each_with_index do |c, i|
if c == ' '
result << ' '
else
rand_n = range.sample
f_index = (rand_n - i) % 26
p f_index
result << "#{letter[f_index]}#{"%02d" % f_index}"
end
end
print result