Skip to content

Commit 816e696

Browse files
committed
Completed so far
1 parent bea7dc4 commit 816e696

File tree

6 files changed

+51
-14
lines changed

6 files changed

+51
-14
lines changed

exercises/count_in_list.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,22 @@ def count_in_list(list, item_to_count)
1919
# 1. A running total of the number of times you've seen the item
2020
# 2. A way to loop/iterate through the list
2121
# 3. A way to add to the running total as you see the item
22-
list = list.to_s
23-
words = list.split
24-
25-
num = words.count(item_to_count)
22+
mycount = [] # Sets the initial count of item_to_count to empty array
2623

24+
list.each do |str| # For each str in list
25+
if str == item_to_count # If str is equal to item_to_count
26+
mycount.push(str) # Push str to the mycount array
27+
end
2728

29+
end
30+
31+
return mycount.count # Return the count of str in mycount
2832
end
2933

3034
if __FILE__ == $PROGRAM_NAME
3135
# I'd advise putting some sanity checks here.
3236
# How else will you be sure your code does what you think it does?
33-
p count_in_list("A way to add to the running total", "to") == 2
37+
p count_in_list(["A", "way", "to", "add", "to", "the"], "to") == 2
3438
p count_in_list([1,1,1], 1) == 3
3539
p count_in_list([1,2,3], -1) == 0
3640
p count_in_list([1,2,3], 1) == 1

exercises/count_max.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,20 @@ def count_max(list)
2222
#
2323
# But remember: inelegant, working code is better than elegant,
2424
# unfinished code.
25+
item = max(list) # Sets item to the maximum number in the list
26+
27+
list.each do |num| # For each num in the list do
28+
maxcount = count_in_list(list, item) # Sets citem equal to the count of item
29+
return maxcount # returns the count
30+
end
31+
2532
end
2633

2734
if __FILE__ == $PROGRAM_NAME
2835
# I'd advise putting some sanity checks here.
2936
# How else will you be sure your code does what you think it does?
37+
p count_max([1, 2, 3]) == 1
38+
p count_max([50, -100, 50, -200]) == 2
39+
p count_max([-200, -400, -100, -300]) == 1
40+
p count_max([10, 1,2,10,10]) == 3
3041
end

exercises/mean.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@
2424
def mean(list)
2525
total = sum(list) # This is the "sum" method from our sum.rb file
2626
# result = ____ # Given the list's sum, how can we calculate the average?
27+
28+
result = (total)/list.count # Divides total by list count and set value to result
29+
30+
return result # return result
2731
end
2832

2933
if __FILE__ == $PROGRAM_NAME
3034
# I'd advise putting some sanity checks here.
3135
# How else will you be sure your code does what you think it does?
36+
p mean([64, 10, 21, -69, 33]) == 11
37+
p mean([44, -59, 98, 82, 13]) == 35
38+
p mean([84, 55, -62, 3, 99]) == 35
39+
p mean([12, 68, 21, -86, 80]) == 19
3240
end

exercises/print_square.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ def print_line(count)
1919
print "*" # This prints a single "*"
2020
end
2121

22-
print "\n" # This forces the output to the next line, like hitting "return"
22+
#print "\n" # This forces the output to the next line, like hitting "return"
2323
end
2424

2525
# We can call methods we've defined ourselves. In this case, we want
2626
# to call the print_line method we've defined to help us print out a square.
2727
def print_square(dimension)
2828
(1..dimension).each do |i| # or, equivalently, for i in (1..dimension)
29-
print_line(____) # Fill in the blank, here.
29+
print_line(2) # Fill in the blank, here.
3030
end
3131
end
3232

exercises/sum.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121

2222
def sum(list)
2323
# This is your job. :)
24+
25+
sum = 0 # Set initial value to zero
26+
27+
list.each {|x| sum += x } # Iterate through list, get an index, add to sum and set it equal to sum. Repeat until last array
28+
29+
return sum # Return final value of sum
2430
end
2531

2632
if __FILE__ == $PROGRAM_NAME

exercises/testrunfile.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@ def count_in_list(list, item_to_count)
33
# 1. A running total of the number of times you've seen the item
44
# 2. A way to loop/iterate through the list
55
# 3. A way to add to the running total as you see the item
6-
list = list.to_s
7-
puts list
6+
mycount = []
87

9-
words = list.split(" ")
8+
list = list.map(&:to_s)
109

11-
puts words
10+
print list
1211

13-
num = words.count(item_to_count)
12+
list.each do |str|
13+
if str == item_to_count
14+
mycount.push(str)
15+
puts
16+
print mycount
17+
18+
end
19+
puts
20+
end
21+
22+
puts mycount.count
1423

1524

16-
puts num
1725

1826
end
1927

20-
count_in_list([1,1,1], "to")
28+
count_in_list([1,1,1, "to", "to kill", 2562], "1")

0 commit comments

Comments
 (0)