Question

In: Computer Science

File has a format Name and number, the number represents power. The name and the (integer)...

File has a format Name and number, the number represents power. The name and the (integer) power are separated by some amount of space. Importantly, any line that begins with a hash '#' are comments and they need to be ingored. Write a program that reads from that file, and prints out only the name of the hero with the strongest power. That name should be capitalized (not uppercase, but capitalized, as in 'Galadriel')

Here is the heroes.txt

# DC heroes
# format: "name" "power"
# 57
# 83
hal 12
batman 48
grayson 14
cyclone 24
superman 38
luthor 15
joker 18
drake 33
wayne 42
rayner 18
# below heroes are additional heroes
arrow 22
kord 48
batwoman 37
supergirl 49
stargirl 24
darkseid 41
gardner 28
pennyworth 27
west 12
aquaman 47
kallor 45
arisia 36

What i have so far:

fn = open('heroes.txt')
count = 0

for z in fn:
line = z.strip()
  
if not '#' in line:
print(line.capitalize())
continue
po1 = line.find()
print(po1)
  
fn.close
  

Solutions

Expert Solution

# do comment if any problem arises

# Code

fn = open('heroes.txt')

# list of names of heroes

heroes = []

# list of powers of heroes

powers = []

count = 0

# this variable stores index containing max power

max_power = 0

for z in fn:

    line = z.strip()

    if not '#' in line:

        # split name and power

        name, power = line.split()

        name=name.capitalize()

        # add to heroes

        heroes.append(name)

        power = int(power)

        # add to powers

        powers.append(power)

        # if current max power is less than current power

        if powers[max_power] < power:

            max_power = count

        # increment count

        count += 1

        continue

print(heroes[max_power])

fn.close

Output:


Related Solutions

Define a function average_entries_for(integer) that takes in an integer than represents the number of times the...
Define a function average_entries_for(integer) that takes in an integer than represents the number of times the function will ask the user to input a number. The function will return the average of all the numbers entered. You must use a for loop to implement this function. (Hint: use range)
(8 marks) Write a program to ask user to enter an integer that represents the number...
Write a program to ask user to enter an integer that represents the number of elements, then generate an ArrayList containing elements which are all random integers in range [75, 144] , and finally display index and value of each element. REQUIREMENTS The user input is always correct (input verification is not required). Your code must use ArrayList. Your program must use only printf(…) statements to adjust the alignment of your output. Your code must display the index in descending...
In JAVA Write a brief program that writes your name to a file in text format...
In JAVA Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
To read in a number from a file as an integer is very simple. (Assuming our...
To read in a number from a file as an integer is very simple. (Assuming our file is defined as myInput; int mynum; myInput >> mynum; The file name should be myNum.txt A sample run of the program: The min is: 40 The max is: 90 The total is: 180 The average is: 60 The number of records read in was: 3 This run used 90 40 50 as its test input. A couple of things to keep in mind,...
Create & name a file with the following format: LastNameFirstNameUnit5.java. Example: The instructor would create a...
Create & name a file with the following format: LastNameFirstNameUnit5.java. Example: The instructor would create a file with the following name: TonsmannGuillermoUnit5.java Proper coding conventions required the first letter of the class start with a capital letter and the first letter of each additional word start with a capital letter. Only submit the .java file needed to make the program run. Do not submit the .class file or any other file. Comments REQUIRED; flow charts & pseudocode NOT REQUIRED. 5%...
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into...
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into a vector and loop through that vector and find the max number.
In Python: read in 'marks.txt' . The file format is one number per line. Your program...
In Python: read in 'marks.txt' . The file format is one number per line. Your program will run through all these numbers, and make sure that they are valid input. you have to Create two lists: one for valid numbers and one for invalid numbers. For a number to be valid the number has to: Must be less than or equal to 100 Must be greater than or equal to 0 Must not have any letters inside of it (it...
Using the given file, ask the user for a name, phone number, and email. Display the...
Using the given file, ask the user for a name, phone number, and email. Display the required information. These are the Files that I made: import java.util.Scanner; public class Demo5 { public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); System.out.println("New number creation tool"); System.out.println("Enter name"); String name = keyboard.nextLine(); System.out.println("Enter phone number"); String phoneNumber = keyboard.nextLine(); System.out.println("Enter email"); String email = keyboard.nextLine(); Phone test1 = new SmartPhone(name, phoneNumber, email); System.out.print(test1); System.out.println("Telephone neighbor: " + ((SmartPhone) test1).getTeleponeNeighbor()); }...
The program shall take two integer arrays as input. Each array represents a non-negative number. Each...
The program shall take two integer arrays as input. Each array represents a non-negative number. Each element in the array is one digit however when all digits in the array are combined a number is formed. See example below: int Array1 = {4,5,6,7} represents number 7654 int Array2 = {2,3,4,5} represents number 5432 You will add the two numbers i.e., 7654 + 5432 = 13086 and store it in a new array similar to how the numbers were stored earlier...
Write a bash shell script that takes exactly one argument, a file name. If the number...
Write a bash shell script that takes exactly one argument, a file name. If the number of arguments is more or less than one, print a usage message. If the argument is not a file name, print another message. For the given file, print on the screen its content.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT