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
PLS FAST DONT EXPLAIN
Rzqust [24]

Answer:

</head>

Explanation:

8 0
3 years ago
What is the name of the place where students access all of their course information? Student handbook. Learning management syste
ehidna [41]
The syllabus is where students can find their course information.
3 0
3 years ago
True or False: A private access modifier is normally used with methods
Veseljchak [2.6K]
I’m pretty sure it’s true! Brainliest if correct
4 0
3 years ago
Read 2 more answers
You have decided to use relative links for your Website's local pages instead of specifying absolute URLs. Your supervisor has a
marin [14]

Answer:

C. The site can be moved from one server to another with minimal disruption.

Explanation:

The perfect reason is the option C.The site can be moved from one server to another with minimal disruption. with relative links you don't need to change the host name for the local page because the browser assumes the address that is in the browser and creates a path relative to it as specified by the file.

4 0
3 years ago
Make a ladtract class that has 2 fields, one for the tractor's length and one for the width. The class should have a method that
Virty [35]

Explanation:

Below is the java code for the ladtract class :-

public class ladtract

{

private double length;  //length of tractor

private double width;   //width of tractor

public double calculateArea(){  

    return length*width;    //calculate and return the area of the tractor

}

public boolean equals(Object o) {

    if (o == this) {    //check if it's the same object

        return true;

    }

       if(o.length==this.length && o.width==this.width){   //check if the length and width are same for both objects

           return true;

       }

       return false;  

}

public String toString(){

    return "Area="+calculateArea(); //return the area of the tractor as a string

}

}

7 0
3 years ago
Other questions:
  • In the 21st century organization we will see:
    10·1 answer
  • After a suspected identity fraud case has been resolved, you should: AContinue to monitor your credit reports and accounts. BKee
    12·2 answers
  • To prevent injury when pulling a nail be sure the material holding the nail is
    14·1 answer
  • In Java please:Re-type the code and fix any errors. The code should convert non-positive numbers to 1. if (userNum &gt; 0) Syste
    10·1 answer
  • Karen is learning about searching the Internet and collaborating with other people online. ​ ​ Karen has found and removed all W
    7·1 answer
  • One day you tap your smartphone screen to turn it on, and nothing happens. It appears to be turned off and will not turn on. Wha
    12·2 answers
  • Who likes games https://www.gameszap.com/game/12038/y8-multiplayer-stunt-cars.html
    6·1 answer
  • Name 3 supercomputers along with their cost, purpose and the country it is located.​
    15·1 answer
  • In cell K10, create a formula using the SUM function that calculated the total of the range F17:F20 subtracts it from the value
    13·1 answer
  • What is the size of the rc.conf file in bytes
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!