-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex26 - more examples.rb
111 lines (87 loc) · 2.14 KB
/
ex26 - more examples.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
def sum_even_numbers(*nums)
sum = 0
nums.each do |i|
if i % 2 == 0
sum += i
end
end
return sum
end
puts sum_even_numbers(2, 4, 5, 6, 7, 8, 9)
# ######################## Question ########################
# Write a function that takes a string as input and returns the same string with all vowels removed.
# ##########################################################
# ############
# Way 1
# ############
def remove_vowels(string)
vowels_letters = ["a", "u", "i", "o", "e"]
new_string = ""
string.each_char do |char|
new_string += vowels_letters.include?(char) ? "" : char
end
return new_string
end
puts remove_vowels("ahmed")
# ############
# Way 2
# ############
def remove_vowels(string)
vowels_letters = ["a", "u", "i", "o", "e"]
new_string = ""
string.each_char do |char|
if vowels_letters.include?(char)
# Skip vowels
else
new_string += char
end
end
return new_string
end
puts remove_vowels("ahmed")
# ######################## Question ########################
# Write a function that takes a list of integers as input and returns the second largest integer in the list.
# If the list has fewer than two elements, return None.
# # #######################################################
# ############
# Way 1
# ############
def second_largest_number(*nums) # O(n log n)
if nums.length < 2
puts "None"
else
list = []
for i in nums
list << i
sorted = list.sort
end
return sorted[sorted.length-2]
end
end
puts second_largest_number(500,2, 4, 10, 50, 60)
# ############
# Way 2
# ############
def second_largest_number(*nums)
return nil if nums.length < 2
max = nums[0]
second_max = nil
nums.each do |num|
if num > max
second_max = max
max = num
elsif second_max.nil? || num > second_max
second_max = num
end
end
return second_max != max ? second_max : nil
end
puts second_largest_number(50, 70, 20, 10, 1, 90)
# ############
# Way 3
# ############
def second_largest_number(*nums)
sorted_nums = nums.sort.reverse
return sorted_nums[1] if sorted_nums.length >= 2
end
puts second_largest_number(50, 70, 20, 10, 1, 90)