Skip to content

Instantly share code, notes, and snippets.

View barbchoy's full-sized avatar

Barbara Choy barbchoy

View GitHub Profile
@barbchoy
barbchoy / reverse-string.rb
Last active September 27, 2017 19:36
null created by barbchoy - https://repl.it/Br5n/8302
# Write a method that will take a string as input, and return a new
# string with the same letters in reverse order.
#
# Don't use String's reverse method; that would be too simple.
#
# Difficulty: easy.
def reverse(string)
i=0
reversed_string=""
@barbchoy
barbchoy / factorial.rb
Created September 27, 2017 19:49
null created by barbchoy - https://repl.it/Br5o/4391
# Write a method that takes an integer `n` in; it should return
# n*(n-1)*(n-2)*...*2*1. Assume n >= 0.
#
# As a special case, `factorial(0) == 1`.
#
# Difficulty: easy.
def factorial(n)
factorialn=1
i=n
@barbchoy
barbchoy / longest_word.rb
Last active September 28, 2017 19:50
null created by barbchoy - https://repl.it/Br7E/4153
# Write a method that takes in a string. Return the longest word in
# the string. You may assume that the string contains only letters and
# spaces.
#
# You may use the String `split` method to aid you in your quest.
#
# Difficulty: easy.
def longest_word(sentence)
words=sentence.split(' ')
@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
@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 / 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 / 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 / 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 / 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 / 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)