my ans for this assignment is showing that the ans is wrong | Coursera Community

# my ans for this assignment is showing that the ans is wrong

• 2 replies
• 3666 views +1
• Newcomer
• 0 replies
Finding Numbers in a Haystack
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.
Data FilesWe provide two files for this assignment. One is a sample file where we give you the sum for your testing and the other is the actual data you need to process for the assignment.
These links open in a new window. Make sure to save the file into the same folder as you will be writing your Python program. Note: Each student will have a distinct data file for the assignment - so only use your own data file for analysis.Data FormatThe file contains much of the text from the introduction of the textbook except that random numbers are inserted throughout the text. Here is a sample of the output you might see:
code:
`Why should you learn to write programs? 774612 1929 8827Writing programs (or programming) is a very creative 7 and rewarding activity.  You can write programs for many reasons, ranging from making your living to solving8837 a difficult data analysis problem to having fun to helping 128someone else solve a problem.  This book assumes that everyone needs to know how to program ...`

The sum for the sample text above is 27486. The numbers can appear anywhere in the line. There can be any number of numbers in each line (including none).Handling The DataThe basic outline of this problem is to read the file, look for integers using the re.findall(), looking for a regular expression of '[0-9]+' and then converting the extracted strings to integers and summing up the integers.
Turn in Assignent
and plz check my code below:

import re

file = open('regex_sum_232111.txt', 'r')

sum = 0

for line in file:
numbers = re.findall('[0-9]+', line)
for number in numbers:
sum = sum + int(number)

print(sum)

Moved to the Data Science forum by ﻿@Laura﻿
icon

Best answer by THANGA MANICKAM M 13 June 2019, 10:03

@dharwalamukul Is it mentioned in course assignment that only space separated integers which are not part of text should be added?
Because your code for the Actual data sums the numbers which are part of a word also.
Example:
In the line "http://www.py4e.com/code3/" in the actual data 4 and 3 are also added during the summation. But they are just part of the URL and not space separate random integers.
Another example is in line,
"you consult www.py4e.com where " . Here 4 is considered during summation though it is part of a word and not a separate integer.
Example for a space separate integer is in line,
"Writing 6778 programs 5780 ".
Clarify this with assignment section whether only space separated integers should be considered. If so, then consider changing regex pattern in such a way that only integers which have a space or newline on both the sides are considered for summing .
I hope this helps .
View original

### 2 replies

Userlevel 3 +5
@dharwalamukul Is it mentioned in course assignment that only space separated integers which are not part of text should be added?
Because your code for the Actual data sums the numbers which are part of a word also.
Example:
In the line "http://www.py4e.com/code3/" in the actual data 4 and 3 are also added during the summation. But they are just part of the URL and not space separate random integers.
Another example is in line,
"you consult www.py4e.com where " . Here 4 is considered during summation though it is part of a word and not a separate integer.
Example for a space separate integer is in line,
"Writing 6778 programs 5780 ".
Clarify this with assignment section whether only space separated integers should be considered. If so, then consider changing regex pattern in such a way that only integers which have a space or newline on both the sides are considered for summing .
I hope this helps . Finding Numbers in a Haystack

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.

Data Files

We provide two files for this assignment. One is a sample file where we give you the sum for your testing and the other is the actual data you need to process for the assignment.

These links open in a new window. Make sure to save the file into the same folder as you will be writing your Python program. Note: Each student will have a distinct data file for the assignment - so only use your own data file for analysis.

Data Format

The file contains much of the text from the introduction of the textbook except that random numbers are inserted throughout the text. Here is a sample of the output you might see:

`Why should you learn to write programs? 7746 12 1929 8827 Writing programs (or programming) is a very creative 7 and rewarding activity. You can write programs for many reasons, ranging from making your living to solving 8837 a difficult data analysis problem to having fun to helping 128 someone else solve a problem. This book assumes that everyone needs to know how to program ... `

The sum for the sample text above is 27486. The numbers can appear anywhere in the line. There can be any number of numbers in each line (including none).

Handling The Data

The basic outline of this problem is to read the file, look for integers using the re.findall(), looking for a regular expression of '[0-9]+' and then converting the extracted strings to integers and summing up the integers.

Turn in Assignent

Enter the sum from the actual data and your Python code below:
Sum:  (ends with 783)
Python code:

Optional: Just for Fun

There are a number of different ways to approach this problem. While we don't recommend trying to write the most compact code possible, it can sometimes be a fun exercise. Here is a a redacted version of two-line version of this program using list comprehension:

`Python 2 import re print sum( [ ****** *** * in **********('[0-9]+',**************************.read()) ] ) Python 3: import re print( sum( [ ****** *** * in **********('[0-9]+',**************************.read()) ] ) ) `

Please don't waste a lot of time trying to figure out the shortest solution until you have completed the homework. List comprehension is mentioned in Chapter 10 and the read() method is covered in Chapter 7.