Skip to content

Instantly share code, notes, and snippets.

@johnotu
Created October 12, 2016 08:53
Show Gist options
  • Save johnotu/98d234a6b3eb3423fe6db51d4c56085f to your computer and use it in GitHub Desktop.
Save johnotu/98d234a6b3eb3423fe6db51d4c56085f to your computer and use it in GitHub Desktop.
def earliest_letter word
#word = word.downcase
earliest = word[0]
word.split('').each do |letter|
if letter < earliest
earliest = letter
end
end
earliest
end
def sorted_in_asc word
tmp_word = word.dup
sorted_letters = ''
len = tmp_word.length
count = 0
while count < len
letter = earliest_letter tmp_word
sorted_letters += letter
tmp_word.slice! letter
count += 1
end
[sorted_letters]
end
def group_anagrams word_list
anagrams = []
word_list.each do |word|
word_list.each do |word_2|
anagrams<<[word, word_2] if sorted_in_asc(word) == sorted_in_asc(word_2)
end
end
anagrams
end
def find_in_matrix matrix, e
matrix.each_with_index do |each_matrix, _index|
if each_matrix[0] > e
outer_index = _index - 1
inner_index = matrix[_index - 1].index(e)
return '[' + outer_index.to_s + '][' + inner_index.to_s + ']'
elsif each_matrix[-1] >= e
outer_index = _index
inner_index = matrix[_index].index(e)
return '[' + outer_index.to_s + '][' + inner_index.to_s + ']'
end
end
-1
end
def smallest_item(list_):
smallest = list_[0]
for item in list_:
if item < smallest:
smallest = item
return smallest
def sorted_in_asc(list_):
sorted_list = []
tmp_list = list_[:]
list_length = len(tmp_list)
count = 0
while count < list_length:
current_smallest = smallest_item(tmp_list)
sorted_list.append(current_smallest)
tmp_list.remove(current_smallest)
count += 1
return sorted_list
def sum_of_list(list_):
sum = 0
for item in list_:
sum += item
return sum
# Activity 1: Suggest as many songs from playlist as possible to fit set time
def select_songs(playlist, time):
tmp_playlist = playlist[:]
song_duration = []
suggested_duration = []
suggested_playlist = []
for song in tmp_playlist:
song_duration.append(song[1])
song_duration = sorted_in_asc(song_duration)
for duration in song_duration:
if duration + sum_of_list(suggested_duration) <= time:
suggested_duration.append(duration)
#return suggested_duration
for duration in suggested_duration:
for song in tmp_playlist:
if song[1] == duration:
suggested_playlist.append(song)
tmp_playlist.remove(song)
return suggested_playlist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment