This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def reverse_words(str) | |
| a = str.split.collect { |word| word.reverse } | |
| a.join(" ") | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def mean(array) | |
| array.reduce(:+).to_f / array.length | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def is_fibonacci?(i) | |
| fib1 = 0 | |
| fib2 = 1 | |
| while fib1 <= i | |
| fib1, fib2 = fib2, fib1 + fib2 | |
| end | |
| if fib1 == i | |
| return true | |
| else | |
| return false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def super_fizzbuzz(array) | |
| new_array = [] #create a new array to store the results | |
| #reminder to make sure that two word variables are seperated by a underscore_k? | |
| array.each do |x| | |
| if x % 15 == 0 | |
| new_array << "FizzBuzz" #remember stephen the << is the same as saying array.push("FizzBuzz") | |
| elsif x % 5 == 0 | |
| new_array << "Buzz" | |
| elsif x % 3 == 0 | |
| new_array << "Fizz" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| solution #1 | |
| array = [1, 1, 1, 2, 3] | |
| #whats going on here?? | |
| freq = array.inject(Hash.new(0)) { |h,v| h[v] += 1; h } | |
| arr.sort_by { |v| freq[v] }.last | |
| solution #2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Array | |
| def pad!(min_size, value = nil) | |
| if self.length >= min_size | |
| return self | |
| else | |
| add_amount = (min_size - self.length) | |
| add_amount.times do self.push(value) | |
| return self |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class RPNCalculator | |
| def evaluate(expression) # expression = '1 2 +' | |
| operators = ["+", "-", "*"] | |
| stack = [] | |
| array = expression.split(" ") # ["1", "2", "+"] | |
| array.each do |i| | |
| if operators.include?(i) | |
| second_operand = stack.pop #is defining a variable that takes the | |
| first_operand = stack.pop | |
| stack.push(first_operand.send(i, second_operand)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # These are just to make the map # easier for me to read. "M" is # visually more dense than "o". M= 'land' | |
| o= 'water' | |
| world = [[o,o,o,o,o,o,o,o,o,o,o], | |
| [o,o,o,o,M,M,o,o,o,o,o], | |
| [o,o,o,o,o,o,o,o,M,M,o], | |
| [o,o,o,M,o,o,o,o,o,M,o], | |
| [o,o,o,M,o,M,M,o,o,o,o], | |
| [o,o,o,o,M,M,M,M,o,o,o], | |
| [o,o,o,M,M,M,M,M,M,M,o], | |
| [o,o,o,M,M,o,M,M,M,o,o], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Determine whether a string contains a Social Security number. | |
| def has_ssn?(string) | |
| if string.scan(/\d\d\d.\d\d.\d\d\d\d/)[0] == nil | |
| false | |
| elsif string.scan(/\d\d\d.\d\d.\d\d\d\d/)[0] != nil | |
| true | |
| end | |
| end | |
| # Return the Social Security number from a string. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def combination(length=5) | |
| return [[]] if length == 0 | |
| combination(length-1).collect {|c| [:h] + c } + | |
| combination(length-1).collect {|c| [:t] + c } | |
| end | |
| combination | |
| print "*There are #{combination.length} ways" | |
| #my output |
OlderNewer