-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathruby_problems.rb
168 lines (121 loc) · 2.23 KB
/
ruby_problems.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
163
164
165
166
# Create a script that takes in a string input from a user and prints:
# a) all the unique characters in the string
# b) a distribution of the characters in the string
# c) order the output of b) by letter
# d) order the output of b) by frequency
# e) order the output of by reverse frequency then letter
# a)
# example input)
# Hello world
# example output)
# H
# e
# l
# o
# w
# r
# d
# b)
# example input)
# Hello world
# example output)
# H - 1
# e - 1
# l - 3
# o - 2
# - 1
# w - 1
# r - 1
# d - 1
# c)
# example input)
# Hello world
# example output)
# - 1
# H - 1
# d - 1
# e - 1
# l - 3
# o - 2
# r - 1
# w - 1
# d)
# example input)
# Hello world
# example output)
# H - 1
# e - 1
# d - 1
# r - 1
# w - 1
# - 1
# o - 2
# l - 3
# example output)
# l - 3
# o - 2
# - 1
# H - 1
# d - 1
# e - 1
# r - 1
# w - 1
# possible sol a)
puts "Enter a sentence:"
input = gets.chomp
char_array = input.chars
puts char_array.uniq
# possible sol b)
# puts "Enter a sentence:"
# input = gets.chomp
# dist_hash = Hash.new(0)
# char_array = input.chars
# char_array.each do |char|
# dist_hash[char] += 1
# end
# dist_hash.each do |key, value|
# puts "#{key} - #{value}"
# end
# possible sol c)
# puts "Enter a sentence:"
# input = gets.chomp
# dist_hash = Hash.new(0)
# char_array = input.chars
# char_array.each do |char|
# dist_hash[char] += 1
# end
# sorted_keys = dist_hash.keys.sort
# sorted_keys.each do |hash_key|
# puts "#{hash_key} - #{dist_hash[hash_key]}"
# end
# possible sol d)
# puts "Enter a sentence:"
# input = gets.chomp
# dist_hash = Hash.new(0)
# char_array = input.chars
# char_array.each do |char|
# dist_hash[char] += 1
# end
# sorted_char_num_array = dist_hash.sort_by do |key, value|
# value
# end
# sorted_char_num_array.each do |char_num|
# puts "#{char_num[0]} - #{char_num[1]}"
# end
# possible sol e)
# puts "Enter a sentence:"
# input = gets.chomp
# dist_hash = Hash.new(0)
# char_array = input.chars
# char_array.each do |char|
# dist_hash[char] += 1
# end
# dist_array = dist_hash.map do |char_num|
# char_num
# end
# dist_array.sort! do |first,second|
# [second[1],first[0]] <=> [first[1], second[0]]
# end
# dist_array.each do |char_num|
# puts "#{char_num[0]} - #{char_num[1]}"
# end