Skip to content

Instantly share code, notes, and snippets.

View barbchoy's full-sized avatar

Barbara Choy barbchoy

View GitHub Profile
@barbchoy
barbchoy / most-common-letter.rb
Created October 3, 2017 21:02
most-common-letter created by barbchoy - https://repl.it/Br7Y/2889
# Write a method that takes in a string. Your method should return the
# most common letter in the array, and a count of how many times it
# appears.
#
# Difficulty: medium.
def most_common_letter(string)
i=0
j=0
frequency=0
@barbchoy
barbchoy / third_greatest_alt.rb
Created October 3, 2017 19:18
third_greatest_alt created by barbchoy - https://repl.it/Br7X/2749
# Write a method that takes an array of numbers in. Your method should
# return the third greatest number in the array. You may assume that
# the array has at least three numbers in it.
#
# Difficulty: medium.
def third_greatest(nums)
sorted_nums=[]
new_nums=[]
i=0
# Write a method that takes an array of numbers in. Your method should
# return the third greatest number in the array. You may assume that
# the array has at least three numbers in it.
#
# Difficulty: medium.
def third_greatest(nums)
sorted_nums=nums.sort
return sorted_nums[nums.length-3]
@barbchoy
barbchoy / power-of-2.rb
Created October 2, 2017 05:44
power-of-2 created by barbchoy - https://repl.it/Br7W/2659
# Write a method that takes in a number and returns true if it is a
# power of 2. Otherwise, return false.
#
# You may want to use the `%` modulo operation. `5 % 2` returns the
# remainder when dividing 5 by 2; therefore, `5 % 2 == 1`. In the case
# of `6 % 2`, since 2 evenly divides 6 with no remainder, `6 % 2 == 0`.
#
# Difficulty: medium.
def is_power_of_two?(num)
@barbchoy
barbchoy / two_sum.rb
Created October 2, 2017 05:30
two_sum created by barbchoy - https://repl.it/Br7V/3198
# Write a method that takes an array of numbers. If a pair of numbers
# in the array sums to zero, return the positions of those two numbers.
# If no pair of numbers sums to zero, return `nil`.
#
# Difficulty: medium.
def two_sum(nums)
pos=[]
i=0
@barbchoy
barbchoy / nearby-AZ.rb
Created September 29, 2017 20:23
nearby-AZ created by barbchoy - https://repl.it/Br7R/3300
# Write a method that takes a string in and returns true if the letter
# "z" appears within three letters **after** an "a". You may assume
# that the string contains only lowercase letters.
#
# Difficulty: medium.
def nearby_az(string)
i=0
a_pos=0
while i<string.length
@barbchoy
barbchoy / palindrome.rb
Last active September 29, 2017 19:53
null created by barbchoy - https://repl.it/Br7Q/2500
# Write a method that takes a string and returns true if it is a
# palindrome. A palindrome is a string that is the same whether written
# backward or forward. Assume that there are no spaces; only lowercase
# letters will be given.
#
# Difficulty: easy.
def palindrome?(string)
i=0
palindrome_or_no=true
@barbchoy
barbchoy / count_vowels.rb
Last active September 29, 2017 05:45
null created by barbchoy - https://repl.it/Br7N/2770
# Write a method that takes a string and returns the number of vowels
# in the string. You may assume that all the letters are lower cased.
# You can treat "y" as a consonant.
#
# Difficulty: easy.
def count_vowels(string)
i=0
num_vowels=0
@barbchoy
barbchoy / time_conversion.rb
Last active September 29, 2017 05:25
null created by barbchoy - https://repl.it/Br7M/3332
# Write a method that will take in a number of minutes, and returns a
# string that formats the number into `hours:minutes`.
#
# Difficulty: easy.
def time_conversion(minutes)
hour=0
mins=0
string_hr_mins=""
hour=minutes/60
@barbchoy
barbchoy / sum_nums.rb
Last active September 29, 2017 05:14
null created by barbchoy - https://repl.it/Br7J/2382
# Write a method that takes in an integer `num` and returns the sum of
# all integers between zero and num, up to and including `num`.
#
# Difficulty: easy.
def sum_nums(num)
sum=0
i=0
while i<num+1
sum=sum+i