Question
I need help with this Python 3+ program. Let me know if you can read the pictures too (they look big on my screen but
I need help with this Python 3+ program. Let me know if you can read the pictures too (they look big on my screen but could look smaller on another)
The link to the text files can be found here: http://www.cse.msu.edu/~cse231/Online/Labs/Lab08/
This is what I have so far if you want to work from this (don't have to):
import math # Find the leading digit def lead_digit(num): while num >= 10: num //= 10 return num # Counter for all digits counter = [0,0,0,0,0,0,0,0,0,0] n = 0 # Prompt file name name=input('Enter a file name: ') with open(name) as file: for line in file: # Remove whitespaces line=line.strip(' ') k=int(line) digit = lead_digit(k) # Increment counter counter[digit] += 1 n += 1 tot = sum(counter) # Find by benford method benford = [((tot*math.log10(1 + 1/k))/float(n)) for k in range(1, 10)]; print() print('{:5} {:>7} {:7} {}'.format("Digit","Percent","Benford",'')) for k in range(1, 10): print('{:>5}:{:7.1%}'.format(k,float(counter[k]) / float(n)),'{}{:5.1%}{}'.format("(".strip(),benford[k-1],")".strip()))
-------
output for test 1:
Digit Percent Benford 1: 32.6% (30.1%) 2: 19.8% (17.6%) 3: 13.2% (12.5%) 4: 9.1% ( 9.7%) 5: 5.8% ( 7.9%) 6: 4.6% ( 6.7%) 7: 5.2% ( 5.8%) 8: 5.0% ( 5.1%) 9: 4.5% ( 4.6%)
my calculations are just a little off and I can't figure out why. For the percent in digit 1, it's supposed to be 32.7% for example.
Benford's Law Benford's Law (sometimes called the First-Digit Law) is an observation about the distribution of first digits in many data sets: the number 1 occurs as the leading digit about 30% of the time, while larger numbers occur in that position less frequently (for example, 9 occurs as the first digit less than 5% ofthe time). As noted on Wikipedia: This result has been found to apply to a wide variety of data sets, including electricity bills, street addresses, stock prices, population numbers, death rates, lengths of rivers, physical and mathematical constants, and processes described by power laws (which are very common in nature). d Pod Relative size of P(d 130.1% 217.6% 3 12.5% 4 9.7% 5 7.9% 6 6.7% 7 5.8% 8 5.1% 9 4.6% Develop a program which will allow you to experiment with Benford's Law. The program will prompt the user for the name of an input file, read the contents of the file and count the leading digits, and then display the results. For simplicity, the program will assume that the data file contains integer numbers, with one value per line. The program's output will be formatted for readability (see below)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started