Knowee
Questions
Features
Study Tools

In this assignment you will read through and parse a file with text and numbers. You will extract all the numbers in the file and compute the sum of the numbers.

Question

In this assignment you will read through and parse a file with text and numbers. You will extract all the numbers in the file and compute the sum of the numbers.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here are the steps to complete your assignment:

  1. Open the File: Use the built-in open() function to open the file. This function requires the name of the file you want to open as a string.
file = open('filename.txt', 'r')
  1. Read the File: Once the file is opened, you can read its contents using the read() method.
content = file.read()
  1. Parse the File: You can use the re module in Python to find all the numbers in the file. The findall() function will return all non-overlapping matches of pattern in string, as a list of strings.
import re
numbers = re.findall('[0-9]+', content)
  1. Convert Strings to Integers: The findall() function will return a list of strings. You can use a list comprehension to convert these strings to integers.
numbers = [int(num) for num in numbers]
  1. Compute the Sum: Now that you have a list of integers, you can use the built-in sum() function to compute the sum of the numbers.
total = sum(numbers)
  1. Print the Sum: Finally, print the sum of all numbers.
print(total)
  1. Close the File: It's a good practice to always close the file when you're done with it.
file.close()

Remember to replace 'filename.txt' with the name of your text file.

This problem has been solved

Similar Questions

Read one string, within that string we have numerical values also, then we have to display the sum of that numbers in that string.

Single File Programming QuestionProblem StatementImagine Mary is developing a program to calculate the digital root of a positive integer. The digital root of a positive integer is the number obtained by repeatedly summing the digits until a one-digit sum is obtained. For example, for the number 873597, the digital root is calculated in the following steps:-> 8 + 7 + 3 + 5 + 9 + 7 = 39 -> 3 + 9 = 12 -> 1 + 2 = 3 Write a program that will read in a positive integer and write both its digital root and the number of steps required to obtain it.Note: This question was asked in ACM-ISPC contest.Input format :The input consists of a positive integer n.Output format :The first line displays "Digital Root: " followed by the digital root of n.The second line displays "Number of Steps: " followed by the number of steps required to reach a single-digit digital root.Refer to the sample output for the formatting specificationsCode constraints :In the given scenario, the test cases fall under the following constraints:2 ≤ n ≤ 106Sample test cases :Input 1 :123Output 1 :Digital Root: 6Number of Steps: 1Input 2 :2Output 2 :Digital Root: 2Number of Steps: 0Input 3 :873597Output 3 :Digital Root: 3Number of Steps: 3Input 4 :1000000Output 4 :Digital Root: 1Number of Steps: 1

Create a program to calculate the sum of numbers entered by the user. The program should ask the user "How many numbers do like to get their sum?:". According to user's answers, use for loop to read integer numbers from the user and display their sum. Refer to the examples below.Use the following two lines of code at the beginning of your answer:print("How many numbers do like to get their sum?:")nums=int(input())sum = 0For example:Test Input Result1512345How many numbers do like to get their sum?:Sum of all numbers is 152369-5How many numbers do like to get their sum?:Sum of all numbers is 1031-10How many numbers do like to get their sum?:Sum of all numbers is -10

Single File Programming QuestionProblem StatementJamie is managing a school and is looking to find out the number of students in the class based on the sum of their scores and the average score.He wants to design a program that takes the average score and sum of scores as input and outputs the calculated number of students in the class using pointers.Input format :The first line of input consists of an integer value n, representing the sum of scores.The second line of input consists of a floating-point value f, representing the average score.Output format :The output displays the calculated number of students in the class.Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ n ≤ 1051.0 ≤ f ≤ 100.0Sample test cases :Input 1 :187575.45Output 1 :24Input 2 :20010.25Output 2 :19Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks : 10Negative Marks : 0WhitelistSet 1:*

Single File Programming QuestionProblem StatementYou are given an integer array of N elements. Find out the sum of the first N/2 elements and the sum of the last N/2 elements using pointers.ExampleInput: 102 2 2 2 2 3 3 3 3 3Output: 1015Explanation:N = 10 -> N/2 = 5sum of the first 5 elements = 2+2+2+2+2 = 10sum of the last 5 elements = 3+3+3+3+3 = 15Note: This question helps in clearing conceptual interviews of service-based companies.Input format :The first line of input consists of an integer N, representing the number of array elements.The second line consists of N space-separated integers, representing the array of elements.Output format :The first line of output prints the sum of the first N/2 elements of the given array.The second line prints the sum of the last N/2 elements of the given array.Refer to the sample output for formatting specifications.Code constraints :N is an even number.10 ≤ N ≤ 50-50 ≤ array elements ≤ 50Sample test cases :Input 1 :102 2 2 2 2 3 3 3 3 3Output 1 :1015Input 2 :207 26 17 23 -6 -23 26 -23 -25 -18 -18 -5 3 22 -9 -22 -28 18 -16 21Output 2 :4-34

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.