Question: This lab exercise provides practice with lists, functions and namespaces in Python. Benfords Law Benford's Law (sometimes called the First -Digit Law) is an observation
This lab exercise provides practice with lists, functions and namespaces in Python. Benfords 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% of the 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). Develop a program which will allow you to experiment with Benfords 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 simplicit y, the program will assume that the data file contains integer numbers, with one value per line. The programs output will be formatted for readability (see below).
Notes and Suggestions
1. The directory for this lab exercise contains three data fi les which you can use as you develop your program: city_part.txt the first 10 lines of the following file (a small file for resting) city_all.txt the population of every city and town in Michigan (1500+ lines) warblers.txt decades of counts of warbler observations (540,000+ lines)
2. There are three phases to this program. What are they? Which should you do first?
3. Requirement : you need a counter for each digit so use a list of counters and use the digit read from the file as an index into your list of counters. BEWARE: be careful that you do not count values with a zero as the leading digit that can throw off your percentage calculation!
4. Requirement : your program must have a function open_file that continuously prompts for a file name until it opens and returns the file pointer. 3. The string method strip is useful for discard ing leading and trailing whitespace. 4. The string method isdigit is useful to ensure that your program only processes integer numbers.
5. The string me thod format might be useful to align the output in columns.
6. Your program does not need to som ehow compute the Benford values (e.g. 30.1%, 17.6%, ...). Instead, you may use the values from the chart (or sample output).
this is my code:
benford_est = [30.1,17.6,12.5,9.7,7.9,6.7,5.8,5.1,4.6] filename = input("Please enter a filename:") file_open = open(filename, 'r') digit_count = {x: 0 for x in "0123456789"} tot_count = 0 for line in file_open: line = line.strip() if line and line[0].isdigit(): digit_f = line[0] digit_count[digit_f] += 1 tot_count = tot_count + 1 percentages = sorted([(int(digit), count / tot_count) for digit,count in digit_count.items()]) print('{:10s} {:10s} {:6s}'.format("Digit","Percent", "Benford")) for d, percentage in percentages: print("{:d}:{:15.2f}% ({:5.2f}%)".format(d, 100*percentage, benford_est[d-1]))
how would i make it so i use a list of counters rather than using a dictionary?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
