-
Notifications
You must be signed in to change notification settings - Fork 9
/
problems.rb
146 lines (90 loc) · 4.74 KB
/
problems.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# ***********************Iteration*******************************
# Write a method that adds out all the numbers up to N
# Ex: sum_to(5) => 15
def sum_to(n)
end
# Write a method that prints out all the numbers between small_num and big_num
# Ex: sum_between(3, 6) => 18
def sum_between(small_num, big_num)
end
# Write a method which collects all the numbers between small_num and big_num into an array
# Ex: range(2, 5) => [2, 3, 4, 5]
def range(small_num, big_num)
end
# FizzBuzz, a classic problem, returns all the numbers up to N with a slight twist. If a number is divisible by 3, it is replaced with "fizz". If it's divisible by 5, it's replaced with "buzz". If it's divisible by both, it's replaced with "fizzbuzz"
# Ex: fizz_buzz(12) => [1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz']
def fizz_buzz(n)
end
# Write a method that will check whether a number is prime. Remember, a prime number has no factors except for 1 and itself.
# Ex: is_prime?(7) => true
def is_prime?(num)
end
# Write a method n_primes which will return the first N prime numbers. Use the is_prime? method you just wrote as a helper.
def n_primes(n)
end
# ****************Iteration through arrays****************
# Write a method that given an array, returns another array with each of the numbers multiplied by two. Don't change the original array, make sure you construct a copy!
def array_times_two(array)
end
# Write a method that given an array, now CHANGES each of the numbers to be twice as big. This should mutate the original array!
def array_times_two!(array)
end
# Write a method that takes an array of numbers as input returns an array only of even numbers
def get_evens(array)
end
# Write a method that given an array, returns another array of only the unique elements. I.e., return a version without duplicates.
def uniq(array)
end
# Write a method that will reverse a string. Do not use ruby's #reverse method
def reverse(string)
end
# Write a method that takes an array of numbers as input and returns the sum of each element multiplied by its index.
# For example, [2, 9, 7] would return 23, since (2 * 0) + (9 * 1) + (7 * 2) = 0 + 9 + 14 = 23
def array_sum_with_index(array)
end
# Write a method which find the max value in an array. DO NOT use ruby's max method. Instead, iterate through the array and keep track of the max value at all times.
def my_max(array)
end
# Write a method that takes an array of pairs and returns the pair with the greatest sum
def largest_pair(pairs_array)
end
# Write a boolean method that returns true if an array of numbers has any multiples of 3.
def has_multiple_of_three?(numbers)
end
# ***************HASHES******************************
# Write a method called word_lengths(string) that given a sentence, returns a hash of each of the words and their lengths.
# E.g., word_lengths("hello my good good pal") should return { 'hello' => 5, 'my' => 2, 'good' => 4, 'pal' => 3 }
def word_lengths(sentence)
end
# Write a method called uniq(array) that returns an array with only the unique values in the array. Use a hash!
def uniques(array)
end
# Write a method that take an array of numbers and returns a hash of the number of even numbers and of odd numbers.
def evens_and_odds(numbers)
end
# Write a method that will take a list of names and return a hash of first initials and the number of times they appear.
def count_initials(names)
end
# Write a method called greatest_val_key(hash) that takes in a hash with any keys but only integers for values, and returns the key with the largest value.
# Ex: greatest_val_key({ "a" => 5, "b" => 6, "c" => 3 }) => "b"
# Hint: Can you iterate through Hash#keys?
def greatest_val_key(hash)
end
# Write a method that takes a string of words separated by spaces and returns the most common vowel. If more than one vowel has that count, return the one that occurs earliest in the alphabet. For our purposes, count "y" as a vowel.
# Assume all letters are lower case.
def most_common_vowel(string)
end
# *******************Bonus**********************************
# Write a method that takes a string of words separated by spaces and returns a string the same as the original, but
# with five-letter words replaced with "#####"
def redact_five_letter_words(string)
end
# Write a function that finds whether any two elements in the array sum to 0. If it does, return true; else, return false. Hint: you'll need two loops for this one
def two_sum_to_zero?(array)
end
# A magic number is a number whose digits, when added together, sum to 7. For example, the number 34 would be a magic number, because 3 + 4 = 7. Write a method that finds the first N many magic numbers.
# You'll want to write a helper method that checks whether a given number is a magic number.
def magic_numbers(count)
end
def is_magic_number?(number)
end