Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your objective in this assignment is to create a program that uses data within f

ID: 3703881 • Letter: Y

Question

Your objective in this assignment is to create a program that uses data within files to display a list of Major League baseball World Series winners and tell the user how many times a given team has won the World Series over its history. Records exist from 1903 through 2017. The data is stored in two text files that accompany the assignment. The first file, teams.txt, contains an alphabetical list of teams who have won at least once. The second file, winners.txt, contains a chronological list of the winning teams from 1903 through 2017. (The first line shows the team that won in 1903, and the last line shows the team that won in 2017. Note that the World Series was not played in 1904 and 1994.) Your program should bring up a menu with the three choices shown below: 1. Show Major League winners 2. Show number of wins for a team 3. Exit The menu options should operate as follows: 1. The first choice simply shows the list of winning teams in alphabetical order. 2. The second choice asks the user for a team and, if valid, displays the number of times the team has won the World Series. Hint: To get the count, read the contents of the winners.txt file into an array. When the user selects a team, an algorithm should step through the array counting the number of times the selected team appears. 3. The menu should continue to appear until the user selects the third choice, which will terminate the program. Use your knowledge of Ruby and build the program in a file named Asg11.rb. Download the data files from Blackboard and put them in the same folder. Build a list of comments that constitute an algorithm, and then write the code to implement it Make sure you document your program and consistently follow style guidelines, which include blank spaces between functional sections of code, naming variables properly, and adding comments where the purpose of a line of code is not immediately obvious by itself.

Explanation / Answer

Solution:

code:

#reading the lines from winners.txt
wining=File.readlines("winners.txt").map(&:strip)
counts = Hash.new(0)
wining.each { |name| counts[name] += 1 }
while true
#displaying the menu
puts("1. Show Major League winners")
puts("2. Show number of wins for a team")
puts("3. Exit")
puts "Enter your choice:"
#getting the choice
choice=gets.to_i
case choice
when 1
#displaying the teams who wins the league atleast once
begin
File.open("teams.txt").each_line{|line| puts line}
rescue Exception => msg
puts msg
end
when 2
#display the team win count for given team
puts "Enter team name:"
team=gets.chomp
puts counts[team]
when 3
#breaking the loop
break
end
end

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)