Superheroes (Id-3148)

A file named superheroes.txt contains data on superheroes. Each line of the file specifies 4 values separated by a comma. The four values are: the name of the superhero, their alter ego (their secondary identity), the main abilities the superhero is known for, and a power rating which mentions how powerful they are. For example, one line of the input file is the following string (without double quotes): “Spiderman, Peter Parker, wrist web-shooter, 70” because Spiderman's alternate name is Peter Parker, his main ability is that he shoots webs from his wrist, and his power is 70 (the maximum power rating a person can have is 100).
Write a program which reads the file and prints the following output:
(a) the contents of the file
(b) the contents of the file, sorted by increasing order of their power.
(c) the average power of all the superheroes (rounded down to nearest integer)
(d) those lines of the file where the character has “superhuman strength” as one of their abilities
(e) those lines of the file where the superhero (primary name) and their alter ego (secondary name) are identical.
Input format:
Name of the file that contains the details of superheroes.
Output format:
  1. Prints the contents of the file
  2. Prints the contents of the file sorted in the increasing order of their power
  3. Prints the average power of all the heroes
  4. Prints the lines of the file where the character has “superhuman strength” as one of their abilities
9e) Prints those lines of the file where the superhero (primary name) and their alter ego (secondary name) are identical.
suppose the contents of superheroes.txt is the following:
Batman, Bruce Wayne, high tech weapons, 70
Spiderman, Peter Parker, wrist web-shooter, 70
Superman, Clark Kent, superhuman strength, 99
Captain America, Steve Rogers, vibranium-steel shield, 71
Wonder Woman, Princess Diana, indestructible bracelets, 80
Iron Man, Tony Stark, powered armor suit, 85
Donald Knuth, Donald Knuth, computer science and mathematics, 100
Hulk, Robert Banner, superhuman strength, 90
He-Man, Prince Adam, superhuman strength, 92
Wolverine, Logan, retractable bone claws, 70
Then your program should print the following output:
Batman, Bruce Wayne, high tech weapons, 70
Spiderman, Peter Parker, wrist web-shooter, 70
Superman, Clark Kent, superhuman strength, 99
Captain America, Steve Rogers, vibranium-steel shield, 71
Wonder Woman, Princess Diana, indestructible bracelets, 80
Iron Man, Tony Stark, powered armor suit, 85
Donald Knuth, Donald Knuth, computer science and mathematics, 100
Hulk, Robert Banner, superhuman strength, 90
He-Man, Prince Adam, superhuman strength, 92
Wolverine, Logan, retractable bone claws, 70
Batman, Bruce Wayne, high tech weapons, 70
Spiderman, Peter Parker, wrist web-shooter, 70
Wolverine, Logan, retractable bone claws, 70
Captain America, Steve Rogers, vibranium-steel shield, 71
Wonder Woman, Princess Diana, indestructible bracelets, 80
Iron Man, Tony Stark, powered armor suit, 85
Hulk, Robert Banner, superhuman strength, 90
He-Man, Prince Adam, superhuman strength, 92
Superman, Clark Kent, superhuman strength, 99
Donald Knuth, Donald Knuth, computer science and mathematics, 100
82
Hulk, Robert Banner, superhuman strength, 90
He-Man, Prince Adam, superhuman strength, 92
Superman, Clark Kent, superhuman strength, 99
Donald Knuth, Donald Knuth, computer science and mathematics, 100

Input:
Name of the file that contains the details of superheroes.
Processing:
name = input()
initial_list = []
def read_data():
    super_file=open(name)
    super_list=super_file.readlines()
    for i in range(10):
        initial_list.append(super_list[i].rstrip().split(', '))
        print(super_list[i].rstrip())

def sort_file():
    initial_list.sort(key = lambda x:eval(x[-1]))
    for i in initial_list:
        print(', '.join(i))

def avg():
    sum = 0
    for i in initial_list:
        sum += eval(i[-1])
    print(int(sum//len(initial_list)))

def sup_str():
    for i in initial_list:
        if i[-2].rstrip() == 'superhuman strength':
            print(', '.join(i))
    for i in initial_list:
        if i[0] == i[1]:
            print(', '.join(i))

read_data()
sort_file()
avg()
sup_str()

Output:
Prints the contents of the file
Prints the contents of the file sorted in the increasing order of their power
Prints the average power of all the heroes
Prints the lines of the file where the character has “superhuman strength” as one of their abilities
Prints those lines of the file where the superhero (primary name) and their alter ego (secondary name) are identical.

Program:

name = input()
initial_list = []
def read_data():
    super_file=open(name)
    super_list=super_file.readlines()
    for i in range(10):
        initial_list.append(super_list[i].rstrip().split(', '))
        print(super_list[i].rstrip())

def sort_file():
    initial_list.sort(key = lambda x:eval(x[-1]))
    for i in initial_list:
        print(', '.join(i))

def avg():
    sum = 0
    for i in initial_list:
        sum += eval(i[-1])
    print(int(sum//len(initial_list)))

def sup_str():
    for i in initial_list:
        if i[-2].rstrip() == 'superhuman strength':
            print(', '.join(i))
    for i in initial_list:
        if i[0] == i[1]:
            print(', '.join(i))

read_data()
sort_file()
avg()
sup_str()

Pseudocode:
Step1. define a function read_data to print the contents of the file
Step2. define a function sort_file to print the contents of the file sorted in the increasing order of their power
Step3. define a function avg to print the average power of all the heroes
Step4. define a function sup_str to print the lines of the file where the character has “superhuman strength” as one of their abilities and to print those lines of the file where the superhero (primary name) and their alter ego (secondary name) are identical.
Step5.End

6 comments:

  1. Sir, how do we do part e) here?

    ReplyDelete
    Replies
    1. for i in initial_list:
      if i[0] == i[1]:
      print(', '.join(i))
      This code does the work for part (e)

      Delete
  2. function def sup_str is not working ?? output is only showing upto avg() !!

    ReplyDelete
    Replies
    1. It is working bro...it's this exact code that i used to run my program

      Delete
  3. full output is not coming how we can get full output.

    ReplyDelete
  4. code does not display the full output. what we can do for require output

    ReplyDelete

Bonus Practice Problems(GMT To IST)

IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find...