Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ninashulman committed Oct 21, 2014
1 parent 66a00e4 commit 59959da
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.c9/
13 changes: 10 additions & 3 deletions series-A/count_in_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@

def count_in_list(list, item_to_count)
# You'll need three things:
# 1. A running total of the number of times you've seen the item
# 2. A way to loop/iterate through the list
# 3. A way to add to the running total as you see the item
count = 0 # 1. A running total of the number of times you've seen the item
list.each do |item| # 2. A way to loop/iterate through the list
if item == item_to_count
count += 1 # 3. A way to add to the running total as you see the item
end
end
return count
end

if __FILE__ == $0
# I'd advise putting some sanity checks here.
# How else will you be sure your code does what you think it does?
p count_in_list([1,2,3], 1) == 1
p count_in_list([1,2,3], -1) == 0
p count_in_list([1,1,1], 1) == 3
end
4 changes: 4 additions & 0 deletions series-A/count_max.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ def count_max(list)
# trying. The "requite_relative" statements above make them available to us, here.
#
# But remember: inelegant, working code is better than elegant, unfinished code.
max_num = max(list)
count = count_in_list(list, max_num)
return count

This comment has been minimized.

Copy link
@jcdavison

jcdavison Oct 23, 2014

Ruby 'implicitly' returns the last line of any method/function call, so you don't literally need return you could write count and you would be fine.

end

if __FILE__ == $0
# I'd advise putting some sanity checks here.
# How else will you be sure your code does what you think it does?
p count_max([10, 1,2,10,10]) == 3
end
8 changes: 8 additions & 0 deletions series-A/longest_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@

def longest_string(list)
# This is your job. :)
saved = list.first

This comment has been minimized.

Copy link
@jcdavison

jcdavison Oct 23, 2014

If you looked at the variable saved all by itself, what would the literal use of the word saved tell another programmer? A well named variables provides other engineers with about the purpose of the variable and/or the nature of the underlying data. In this case 'saved' doesn't tell us much. A better variable name could be longest_so_far or the like.

list.each do |item|
if item.length > saved.length
saved = item
end
end
return saved

This comment has been minimized.

Copy link
@jcdavison

jcdavison Oct 23, 2014

⚡ Think about implicit return as referenced above.

end

if __FILE__ == $0
# I'd advise putting some sanity checks here.
p longest_string(['Nina', 'school', 'outmost']) == 'outmost'
# How else will you be sure your code does what you think it does?
end
6 changes: 5 additions & 1 deletion series-A/mean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@

def mean(list)
total = sum(list) # This is the "sum" method from our sum.rb file
# result = ____ # Given we have the sum of the list, how can we calculate the average?
result = total/list.length # Given we have the sum of the list, how can we calculate the average?
return result
end

if __FILE__ == $0
# I'd advise putting some sanity checks here.
# How else will you be sure your code does what you think it does?
p mean([Float(5), Float(2)]) == 3.5

This comment has been minimized.

Copy link
@jcdavison

jcdavison Oct 23, 2014

You would def want to think about handling the use of a Float inside the method. In this case, if someone passes the mean(3,2,2,3) the method should return 2.5. Enforcing that the method caller explicitly passes a Float is out of bound. Think about how you could use 1.to_f or n.to_f to handle this inside the method.

p mean([5 , 5]) == 5.0
p mean([10, 20, 30]) == 20.0
end
10 changes: 5 additions & 5 deletions series-A/min.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
# This is going to be very similar to max, so don't be afraid if
# these two methods look almost identical
def min(list)
____ = ____
____.each do |____|
if ____
____ = ____
min_so_far = list.first
list.each do |num|
if num < min_so_far
min_so_far = num
end
end

return ____
return min_so_far
end

if __FILE__ == $0
Expand Down
20 changes: 20 additions & 0 deletions series-A/print_horizontal_pyramid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,27 @@
# *****
# *******

def print_line(count)
(1..count).each do |i| # or, equivalently, for i in (1..count)
print "*" # This prints a single "*"
end
end

def print_line_of_spaces(count)
(1..count).each do |i| # or, equivalently, for i in (1..count)
print " " # This prints a single "*"
end
end

def print_horizontal_pyramid(height)
count = height
(1..height).each do |r|

This comment has been minimized.

Copy link
@jcdavison

jcdavison Oct 23, 2014

I don't personally like one letter variable names r doesn't really mean anything to me. The only time i'm ok with one letter variable names is something like 5.times do |n| .. end where n clearly points to an integer. Otherwise, I don't use one letter variable names. 😄

count -= 1
print_line_of_spaces(count)
print_line(r)
print_line(r-1)
print "\n" # This forces the output to the next like, like hitting "return" on the keyboard
end
end

if __FILE__ == $0
Expand Down
18 changes: 18 additions & 0 deletions series-A/print_pyramid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,27 @@ def print_pyramid(height)
# This is your job. :)
# Suggestion: you can call print_triangle to print out the first, "upward" half of the pyramid
# You'll have to write code to print out the second, "downward" half of the pyramid.
print_triangle(height)
while height > 1 do

This comment has been minimized.

Copy link
@jcdavison

jcdavison Oct 23, 2014

The use of the while loop will for certain, but you are forcing yourself to manually keep track of the value of height, you might consider something more like height.times do |n| .... end and then look at the value of n as you execute the loop. How can n be used as an argument input to another method call?

height -=1
print_line(height)
end
end

if __FILE__ == $0
# I'd advise putting some sanity checks here.
# How else will you be sure your code does what you think it does?
print_pyramid(1)

print "\n\n\n" # This is here just to make the separation between triangles clearer

print_pyramid(2)

print "\n\n\n" # This is here just to make the separation between triangles clearer

print_pyramid(3)

print "\n\n\n" # This is here just to make the separation between triangles clearer

print_pyramid(10)
end
2 changes: 1 addition & 1 deletion series-A/print_square.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def print_line(count)
# to call the print_line method we've defined to help us print out a square.
def print_square(dimension)
(1..dimension).each do |i| # or, equivalently, for i in (1..dimension)
print_line(____) # Fill in the blank, here.
print_line(dimension) # Fill in the blank, here.
end
end

Expand Down
3 changes: 3 additions & 0 deletions series-A/print_triangle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def print_line(count)

def print_triangle(height)
# You have to fill in the details here.
(1..height).each do |i|
print_line(i)
end
end

# There are no rumble strips this time. It's up to you to decide whether
Expand Down
9 changes: 9 additions & 0 deletions series-A/shortest_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@

def shortest_string(list)
# This is your job. :)
saved = list.first
list.each do |item|
if item.length < saved.length
saved = item
end
end
return saved
end

if __FILE__ == $0
# I'd advise putting some sanity checks here.
# How else will you be sure your code does what you think it does?
p shortest_string(['Nina', 'school', 'outmost']) == 'Nina'
p shortest_string(['a', 'ab', 'abc']) == 'a'
end
5 changes: 5 additions & 0 deletions series-A/sum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

def sum(list)
# This is your job. :)
total = 0
list.each do |item|
total += item
end
return total
end

if __FILE__ == $0
Expand Down
2 changes: 2 additions & 0 deletions series-A/word_count.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
def word_count(string)
# Hint: You'll want to use String#split
# See: http://www.ruby-doc.org/core-2.1.2/String.html#method-i-split
str = string.split(' ')

This comment has been minimized.

Copy link
@jcdavison

jcdavison Oct 23, 2014

We could 'one line' this method to be

def word_count(string)
  string.split(' ').length
end
return str.length
end

if __FILE__ == $0
Expand Down

1 comment on commit 59959da

@jcdavison
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, all solid work here 👍

Please sign in to comment.