-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruby_solutions.rb
More file actions
84 lines (74 loc) · 1.74 KB
/
ruby_solutions.rb
File metadata and controls
84 lines (74 loc) · 1.74 KB
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
# original string
string = "This is a string in reverse"
# change to array to manipulate
array = string.split("")
index = 0
# new array to save reverse version
reverse_array = []
#until loop iterating through array starting with first item in array working up and saving it to reverse_array
until index == array.length
reverse_array = reverse_array.prepend(array[index])
index += 1
end
# save reversed characters to string
reverse_string = reverse_array.join
#attempting same thing as lines 1-16 in less code -- success
string = "This is a string in reverse"
string_reverse = ""
index = 0
until index == string.length
string_reverse = string_reverse.prepend(string[index])
index += 1
end
# reverses a string and puts it into a new string -- success
string = "This is a string in reverse"
reverse_string = ""
string.each_char { |c| string_reverse = string_reverse.prepend(c) }
str = r
# reverse string prob using recursion -- success
s = "This is a string"
r = ""
num = 0
def r_str(x, y, z)
return y if (x.length == y.length)
y = y.prepend(x[z])
z += 1
r_str(x, y, z)
end
# reverse string prob using recursion r must be a blank string
string = "This is a string in reverse"
r = ""
def r_str(s, r)
r = r.prepend(s.slice!(s.first))
if s == ""
s = r
return s
end
r_str(s, r)
end
# fizzbuzz problem using for loop -- success
for num in 0..1000
if num % 15 == 0
puts "fizzbuzz"
elsif num % 5 == 0
puts "buzz"
elsif num % 3 == 0
puts "fizz"
else
puts num
end
end
# fizzbuzz problem using until loop -- success
x = 0
until x == 1000
x += 1
if x % 15 == 0
puts "fizzbuzz"
elsif x % 5 == 0
puts "buzz"
elsif x % 3 == 0
puts "fizz"
else
puts x
end
end