Created
October 12, 2016 08:55
-
-
Save ednasawe/ebf040b6e01f71a7c540284852fb83ce to your computer and use it in GitHub Desktop.
Activity 1,2,3
This file contains 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
Activity 1 (sort_playlist.py) | |
def lowest(song): | |
sorted_playlist = [] | |
min = song[0] | |
for i in song: | |
if i[1] < min[1]: | |
min = i | |
return min | |
def sort_playlist(playlist): | |
songs = [] | |
for i in range(len(playlist)): | |
small_time = lowest(playlist) | |
songs.append(small_time) | |
playlist.remove(small_time) | |
i+= 1 | |
return songs | |
def select_song(playlist, time): | |
sorted_playlist = sort_playlist(playlist) | |
total_time = 0 | |
new_playlist = [] | |
for song in sorted_playlist: | |
total_time += song[1] | |
if total_time > time: | |
break | |
new_playlist.append(song) | |
return new_playlist | |
print(select_song([('song1', 3),('song2',2),('song3', 7), ('song4', 4)], 10)) | |
Question Number 2 | |
def check_numbers(matrix, number) | |
matrix.each do |row| | |
if row[-1] >= number | |
x= matrix.index(row) | |
y = matrix[x].index(number) | |
return x,y | |
end | |
end | |
end | |
print(check_numbers([[1,2,3,4],[6,7,8,9],[10,11,12,13,14]], 11)) | |
Activity 3(group_anagrams.rb) | |
def group_anagrams(anagrams) | |
new_anagrams = [] | |
anagrams.each do |word1| | |
anagrams.each do |word2| | |
if word1 != word2 | |
new_anagrams << [word1, word2] if is_anagram?(word1, word2) | |
end | |
end | |
anagrams.delete(word1) | |
end | |
return new_anagrams | |
end | |
def is_anagram?(word1, word2) | |
return false if word1.length != word2.length | |
word1.each_char do |character| | |
if word1.count(character) == word2.count(character) | |
return true | |
else | |
return false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment