Skip to content

Instantly share code, notes, and snippets.

@bkwaku

bkwaku/activity_4.py

Created Oct 11, 2016
Embed
What would you like to do?
def ascending_order(my_list):
length_list = len(my_list) - 1
sorted = False
while not sorted:
sorted = True
for i in range(length_list):
if my_list[i][1] > my_list[i+1][1]:
sorted = False
my_list[i], my_list[i+1] = my_list[i+1], my_list[i]
return my_list
list_of_music = [('title1',2),('title2',3),('title5',0),('title3',2),('title4',1)]
#def playlist(list_of_music):
# print list_of_music[0][1]
#sorted_music_list = ascending_order(list_of_music)
def create_playlist(my_playlist, max_time):
sorted_play_list = ascending_order(my_playlist)
#print sorted_play_list
our_play_list = []
time_out = 0
i = 0
while time_out < max_time:
our_play_list.append(sorted_play_list[i])
time_out = time_out + sorted_play_list[i][1]
i+=1
return our_play_list
print(create_playlist(list_of_music,5))
def bring_out_matrix_val(matrix, val)
# a = [[1,2,3,4],[5,6,7,8],[9,11,12,13],[14,15,16,17]]
(0..matrix.length - 1).each do |v|
if matrix[v].last >= val
#puts "we are here"
matrix[v].each do |i|
if i == val
first = v
second = matrix[v].index(i)
return "["+first.to_s+","+second.to_s+"]"
end
end
break
end
end
end
a = [[1,2,3,4],[5,6,7,8],[9,11,12,13],[14,15,16,17]]
puts bring_out_matrix_val(a,11)
def compress_file(filename)
cmp_text = ""
File.open(filename).each do |line|
line.gsub! 'the', 'e'
line.gsub! 'down', 'dwn'
line.gsub! 'going', 'goin'
line.gsub! 'could', 'cud'
line.gsub! 'come', 'cme'
line.gsub! 'just', 'jus'
line.gsub! 'before', 'b4'
line.gsub! 'long', 'lng'
line.gsub! 'become', 'bcme'
line.gsub! 'answer', 'ans'
line.gsub! 'you', 'u'
line.gsub! 'are', 're'
line.gsub! 'have', 've'
line.gsub! 'but', 'bt'
line.gsub! 'has', 'hs'
cmp_text = cmp_text + line
end
File.open('passage.txt.cdn', 'w') { |file| file.write(cmp_text) }
end
def de_press_file(filename)
cmp_text = ""
File.open(filename).each do |line|
line.gsub! 'e', 'the'
line.gsub! 'dwn', 'down'
line.gsub! 'goin', 'going'
line.gsub! 'cud', 'could'
line.gsub! 'come', 'come'
line.gsub! 'jus', 'just'
line.gsub! 'b4', 'before'
line.gsub! 'long', 'long'
line.gsub! 'bcme', 'become'
line.gsub! 'ans', 'answer'
line.gsub! 'u', 'you'
line.gsub! 're', 'are'
line.gsub! 've', 'have'
line.gsub! 'bt', 'but'
line.gsub! 'hs', 'has'
cmp_text = cmp_text + line
end
File.open('passage.txt', 'w') { |file| file.write(cmp_text) }
end
de_press_file("passage.txt.cdn")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment