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
When calling a function with arguments that should be modified, the __________ of those arguments are passed.
ad-work [718]

Answer:

Addresses.

Explanation:

An argument can be defined as a value that can be passed to a function.

Simply stated, an argument is a value that must be passed into a function, subroutine or procedure when it is called. This value can be passed to a function either by reference or by value.

This ultimately implies that, an argument which is also a parameter variable stores information which is passed from the location of the method call directly to the method that is called by the program.

Basically, parameters can serve as a model for a function; when used as an input, such as for passing a value to a function and when used as an output, such as for retrieving a value from the same function.

When calling a function with arguments that should be modified, the addresses of those arguments are passed. This is simply because the argument is an address or pointer in itself and as such the function parameter being called must be a pointer.

In object-oriented programming (OOP) language, an object class represents the superclass of every other classes when using a programming language such as Java. The superclass is more or less like a general class in an inheritance hierarchy. Thus, a subclass can inherit the variables or methods of the superclass.

Hence, all instance variables that have been used or declared in any superclass would be present in its subclass object.

6 0
2 years ago
Numeric data is stored in what for direct processing
Ede4ka [16]
A string variables??? I'm not sure does this have any multiple choice 
3 0
3 years ago
your teacher has accidentally put the wrong names of the students on her excel science results sheet. What method can be used to
Shtirlitz [24]

Answer:

Explanation:

Excel has a built-in function that allows you to type in a word, letter, or number and it will automatically find every one of that entry and replace it with whatever you want. To do this you simply click on Home > Find & Select > Replace and fill out the information. Once the info is completed click Replace All. This shows exactly why it is way better to do these result sheets on the computer as opposed to on a piece of paper, it allows you to automate everything and fix multiple mistakes with a simple button click.

6 0
2 years ago
Since the password includes all four types of characters (complex password) and is 8 characters long, what is the real reason th
Liono4ka [1.6K]
It makes the possibility of an unwanted user to break into your account much smaller.
8 0
3 years ago
Which type of network topology is the most common and easiest to use?
Airida [17]
<span>A network layout or topology that is the most common, the most simple and easiest to use out of the four network topologies is the Bus topology, one cable is used for installation so it is also the cheapest network type.</span>
5 0
3 years ago
Other questions:
  • Which of the following variable declarations is a correct assignment?
    14·1 answer
  • A(n) ____ is a client, server, or other device that can communicate over a network and is identified by a unique number, known a
    10·1 answer
  • Can Someone give me a 5 paragraph essay about all of the uses in Microsoft Word.
    9·1 answer
  • Which of the following goals is likely to require funding? Check all that apply.
    6·2 answers
  • Which of the following operating systems is able to join a domain a) Microsoft office pro b) Microsoft surface R.T. c) google an
    9·2 answers
  • Which are examples of non-linear presentations? Choose all that apply.
    11·1 answer
  • Beth would like to run an nmap scan against all of the systems on her organization's private network. These include systems in t
    15·1 answer
  • Match the ernerging technologies to their descriptions.
    14·1 answer
  • ________ take advantage of vulnerabilities in software. Group of answer choices Blended threats Bots Trojan horses Direct-propag
    6·1 answer
  • _____ refers to the programs that a device can run.
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!