/
calc.rb
65 lines (58 loc) · 1.52 KB
/
calc.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
puts 'Please give me some words to sort alphabetically. One word per line.'
set = []
unsortset = []
sortedset = []
words = 0
tosort = 0
word = 0
compword = 1
x = 0
begin
input = gets.chomp
input = input.downcase
set.push input
words = words +1
tosort = tosort + 1
end while input != ''
set.push ''
def try set, sortedset, words, word, compword, tosort, unsortset, x
if tosort == 0
puts'---'
puts 'In alphabetical order: '
sortedset
elsif set[word] < set[compword] || compword == words
if compword == words
sortedset.push set[word]
for x in (word+1)..(words-1)
unsortset.push set[x]
end
for x in 0..(word-1)
unsortset.push set[x]
end
set = unsortset
unsortset = []
set.push ''
x = 0
words = words - 1
tosort = tosort - 1
word = 0
compword = 1
try set, sortedset, words, word, compword, tosort, unsortset, x
else
x = x + 1
compword = compword + 1
try set, sortedset, words, word, compword, tosort, unsortset, x
end
# Add some more comments to describe what you are trying to do
# at each point in the code. I'm not 100% why this elsif is needed
# for example. This will also help yourself after a day or two away
elsif set[word] > set[compword] || set[word] == set[compword]
x = x + 1
compword = compword + 1
word = compword - 1
try set, sortedset, words, word, compword, tosort, unsortset, x
else
'ugh'
end
end
puts try set, sortedset, words, word, compword, tosort, unsortset, x