1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
klemol [59]
2 years ago
8

Premise: Tracy has a file that contains a list of actors and the movies in which they acted. She wants to know the top 3 ranked

actors from her list whom have acted/appeared in the most movies. ACTOR_NAME MOVIE_NAME Leonardo DiCaprio The Revenant Christian Bale Vice Morgan Freeman Shawshank Redemption Leonardo DiCaprio The Great Gatsby Christian Bale American Psycho Morgan Freeman The Dark Knight Christian Bale The Dark Knight Samuel L. Jackson Pulp Fiction Question: Write code in Java/Scala/Python to display the top 3 ranked actors appearing in the most movies based on the count of movies in which they have acted. If there are less than 3 actors in her list, display all of them. Consider all scenarios - such as, if two actors have acted in the same number of movies, they will have the same rank.

Computers and Technology
1 answer:
anzhelika [568]2 years ago
5 0

Answer:

see explaination

Explanation:

Python version : 2.7

Python program to read an input file with specified number of records to read

and output the top 3 ranked actors

The format of input file:

<number of records to read>

<actor_name>,<movie_name>

<actor_name>,<movie_name>

....

'''

#define the file name

filename = 'actors_movies.txt'

# create an empty dictionary to contain the actor name as key and number of movies they have acted as value

actors_movie_count = {}

# open the flie

fp = open(filename)

line = fp.readline() # read the first line

count = int(line)

i = 0

# loop to read count number of lines from file

while i < count:

line = fp.readline() # read a line

data = line.split(",") # split the input data into list of strings using comma as the delimiter

# check if actor name is present in the dictionary, then add 1 to its value

# strip is used to remove any leading or trailing space

if data[0].strip() in actors_movie_count:

actors_movie_count[data[0].strip()] += 1

else: # else insert a new record with actor name as its key and 1 as value

actors_movie_count[data[0].strip()] = 1

i += 1

# close the file

fp.close()

# get the list of names of actors and list of number of movies they have acted

actors = actors_movie_count.keys()

num_movies = actors_movie_count.values()

# loop to sort the actors list and num_movies list in descending order of count of movies

for i in range(len(num_movies)-1):

max = i

for j in range(i+1,len(num_movies)):

if num_movies[j] > num_movies[max]:

max = j

if max != i:

actors[max], actors[i] = actors[i], actors[max]

num_movies[max], num_movies[i] = num_movies[i], num_movies[max]

# rank the actors to add top 3 ranked actors to rank_actors list

rank = 1

rank_actors = []

# add the first actor to list

rank_actors.append(actors[0])

# loop from second to end of actors list

for i in range(1,len(actors)):

# if this actor has same number of movies as the actor before him, then assign same rank

if num_movies[i] == num_movies[i-1]:

# if rank <= 3, add the actor in rank_actors list

if rank <= 3:

rank_actors.append(actors[i])

else: # rank > 3, exit the loop

break

else: # assign 1 more rank that the previous

rank += 1

# if rank <= 3, add the actor in rank_actors list

if rank <= 3:

rank_actors.append(actors[i])

else: # rank > 3, exit the loop

break

# loop to display the top 3 ranked actors

for actor in rank_actors:

print(actor)

#end of program

see attachment for the program screenshot and output

You might be interested in
Sarah is entering weekly sales data for week 37 of the current year; however, when she moves down to thecells where she needs to
Nadya [2.5K]

Answer:

a) freeze the panes

Explanation:

By freezing the panes she can lock the row that has the headers of each column and scroll down as much as she wants without losing sight of the rows locked. This can also be done with columns if the spreadsheet is organized horizontally.

4 0
3 years ago
Read 2 more answers
. Electricians will sometimes call ______ "disconnects" or a "disconnecting means."
arlik [135]

Answer: A: Switches

Explanation:

6 0
3 years ago
Moving your Sprite top to bottom is moving along the X coordinate?
Marianna [84]
The answer is false
6 0
3 years ago
In Python please.
SOVA2 [1]

Answer:

negatives = []

zeros = []

positives = []

while True:

   number = input("Enter a number: ")

   if number == "":

       break

   else:

       number = int(number)

       if number < 0:

           negatives.append(number)

       elif number == 0:

           zeros.append(number)

       else:

           positives.append(number)

for n in negatives:

   print(n)

for z in zeros:

   print(z)

for p in positives:

   print(p)

Explanation:

Initialize three lists to hold the numbers

Create a while loop that iterates until the user enters a blank line

Inside the loop:

If the number is smaller than 0, put it in the negatives list

If the number is 0, put it in the zeros list

Otherwise, put the number in the negatives list

When the while loop is done, create three for loops to print the numbers inside the lists

5 0
2 years ago
Wich command converts a number to a string​
AfilCa [17]

Answer:

okay yes i agree

Explanation:

8 0
2 years ago
Other questions:
  • What is the output after the following code executes?
    14·1 answer
  • The addElement operation for the linked implementation must determine the parent of the next node to be inserted. Why?
    5·1 answer
  • Need this!!
    11·2 answers
  • 5. Which one of the following statements is true for spell checkers? A. Most spell checkers flag inappropriate word usage. B. A
    5·1 answer
  • Write a program to complete the task given below: Ask the user to enter any 2 numbers in between 1-10 and add both of them to an
    8·1 answer
  • Select two netiquette guidelines. In three to five sentences, explain why these guidelines make professional online communicatio
    15·2 answers
  • Which shortcut key combination will move the cursor to the beginning of the line?
    13·1 answer
  • On his website, Mario has a video that visitors must click to play. He wants the video to play automatically when the page loads
    9·1 answer
  • What will the declaration below do to its target?
    9·1 answer
  • Easy<br> What is your favorite Anime<br> Mine is Attack On Titan at the moment
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!