This problem is solving \"most recent search\" with Ruby programming. Your task
ID: 3746789 • Letter: T
Question
This problem is solving "most recent search" with Ruby programming. Your task is to store and maintain a list of movies looked-up by a user similar to how Google stores our searches (see the image below) and shows most recent search on top. Google How to how to make slime how to screenshot on mac how to get rid of flies how to tie a tie how to get rid of fleas how to get rid of gnats how to boil eggs how to get rid of bed bugs how to train your dragon how to make french toast Google Search m Feeling Lucky A simple example: Current list: ['Captain America, Iron Man] New lookup: The Post Updated list: [The Post, 'Captain America', 'Iron Man1 the most recent item gets appended to the front/top. You can start by downloading partially implemented Ruby code (main.rb & seachcontroller.rb) from the Canvas. And when user exit, save updated list to "data.txt fileExplanation / Answer
def updateList(moviename,list)
a = list.find_index(moviename)
#find index of moviename which is entered by user
# It returns nil if it is not there in the list
if(a==nil)
#If the movie is not there in list we can insert it at beginning of list
list.insert(0,moviename)
#puts("Updated list is #{list}")
#Uncomment to see the output
else
#If it is already present then we have to delete and insert it at beginning
list.delete_at(a)
#puts("Updated list is #{list}")
#Uncomment to see the output
end
end
def saveListToFile(list)
#opening file to f and for each element in list we have write it to the file
#data.txt is file name and 'w' means mode that is write mode
File.open('data.txt', 'w') do |f|
list.each do |ch|
f.write("#{ch} ")
end
end
end
list = ['captian america','iron man']
#Printing default list
puts("Default movies list is #{list}")
#We have to take input from user until he enters 'exit'
while true do
#getting user input for movie name .chop returns string without newline character at the end
#While gets returns newline character at the end
moviename = gets.chop
#converting movie name to downcase as we have a condition i.e. movie names are case insensitive
moviename = moviename.downcase
#If user enters exit as his input we are saving the list to file and exiting from loop
if(moviename=='exit')
saveListToFile(list)
#Exit from the loop without any errors so we use exit(0)
exit(0)
end
#If user entered otherthan exit this funcion will called up and list is updated
updateList(moviename,list)
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.