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
| # 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="" |
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
| # 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 |
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
| # 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(' ') |
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
| # 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 |
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
| # 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 |
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
| # 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 |
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
| # 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 |
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
| # 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 |
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
| # 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 |
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
| # 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) |
OlderNewer