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]
3 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]3 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
1. Write the full forms of the following.
Dmitry_Shevchenko [17]

Answer:

1. A) Language of graphics-oriented.

B) Initial public offering

C)

D) Central processing unit

2A) False

B) True

C) False

D) True

7 0
3 years ago
Read 2 more answers
When forced distribution is used to reduce leniency bias, this can cause __________ if a pfp system is in place?
ziro4ka [17]
When forced distribution is used to reduce leniency bias, this can cause <decreased trust> between employees if a pfp system is in place.
3 0
3 years ago
Describe Relational Query Languages and Operations.
Lelechka [254]

Answer:

Relational query language is the language that is used in the queries of the relational databases that has rows and columns in table. The user or client presents a request for gaining the information from the database. The relationships in the database are defined in numerous ways which creates the query.

This language can be procedural form or non-procedural form.The operations performed by this language are communication with the relational database, analyzing the relationships between the entities of database,splitting the request from client and then execution of that request is done by database management system(DBMS), etc.

7 0
2 years ago
Which of the following is NOT an example of a font style?
erastovalidia [21]
C. Underline because it is not a kind of font. it is an effect.
3 0
3 years ago
After the explosion of the Union Carbide plant the fire brigade began to spray a curtain of water in the air to knock down the c
AysviL [449]

Answer:

d. The system of water spray pipes did not have sufficient water supply

Explanation:

The Union Carbide India Ltd. was chemical factory situated at Bhopal that produces various pesticides, batteries, plastics, welding equipment, etc. The plant in Bhopal produces pesticides. In the year 1984, on the night of 2nd December, a devastating disaster occurred on the Bhopal plant which killed millions of people due to the leakage of the poisonous, methyl isocyanate. This disaster is known as Bhopal Gas tragedy.

Soon after the gas pipe exploded, the fire brigade started spraying water into the air to \text{knock down} the cloud of the gases in the air but there was not sufficient amount of water in the water sprays and so it was not effective.

Thus the correct answer is option (d).

4 0
3 years ago
Other questions:
  • Hello, please help write code in C++. Primary U.S. interstate highways are numbered 1-99. Odd numbers (like the 5 or 95) go nort
    12·1 answer
  • A _____ is a harmful program that resides in the active memory of a computer and duplicates itself. Select one: a. scareware b.
    7·1 answer
  • Which of the following attacks seeks to introduce erroneous or malicious entries into a server's hostname-to-IP address cache or
    5·1 answer
  • Write an if statement that assigns 100 to x when y is equal to 0.
    7·1 answer
  • How is the internet made?
    7·1 answer
  • Anybody know this question???
    6·1 answer
  • ¿porque y como surge la informatica y las computadoras
    13·1 answer
  • What is Japanese tradition?
    8·2 answers
  • ________ helps in determining the cause of a security threat in an incident response plan. Question 7 options: A) Restricting sy
    5·1 answer
  • Most spyware programs are benign in that they do not perform malicious acts or steal data. group of answer choices true false
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!