Question

Python, given a code in class i just need help with the third bullet point ; using a and n (defined in the second picture of python code) find the first digit for k! for k =1,...,n. you dont have to type in all this code just help me write the code in the first picture where it says: def benford(a):

b = [0 for d in range(1,10)]

#Do everthything in here

return b

2.2 Generating data In this assignment you will generate data using your factorial function. In particular, you will: Let the1 from math import log16e 2 from math import log10, floor 3 import numpy as np 4import matplotlib.pyplot as plt 6# ECE 105: P# PLEASE DO NOT CHANGE THE CODE BELOW ! n = 1000 # sequence Length def main) import sys import re try: student-id = sys . arg

2.2 Generating data In this assignment you will generate data using your factorial function. In particular, you will: Let the number n denote the number of data points to generate. Conorate k' for k = 1, , n. Call this sequence (ai, , an), where ak- . Using (ai, , an), find the first digit in k! for k-1, , n. Call this sequence (bi, ,bn), where bk įs the first digit of k!. . Using (bi, . . . ,h), tally the data to get the counts sequence (c, . . . ,c), where cd İs the number of occurrences of the digit d in the sequence b. Naturally, c9 Using (c. ..,co), normalize the tally by the number of data points to get the empirical fraction (fi. where fa-ca/n is the fraction of integers k for which the first digit of k! is d. ,fo) » Use matplotlib to create a pair of bar plots comparing the empirical data (fi,... , /o) with the prediction from Benford's Law (p,... ,Po)
1 from math import log16e 2 from math import log10, floor 3 import numpy as np 4import matplotlib.pyplot as plt 6# ECE 105: Programming for Engineers II (Spring, 2019) 7# Lab 2) Problem 2) Part 1 8# April 11-12, 2019 9# PLEASE rename this solution .py file as your "username-2.ру" (e.g. spW26-2.py 11# generate the sequenc k k! 12 def factSeq (n): return [np.math.factorial(k) for k in range(1,n+1)] 13 14 # given any array (a1, ,an), return (f1, ,f9) 15% where fd is the fraction of numbers in array with first digit d 16 def benford (a): 17 # TODO Return the correct array (fl, ,f9) 18 b [e for d in range(1,10 19 #do everything in here 20 21 return b e of factorials (al,...,an) where a 23 24# aggregate relative error between f and p 25 def error(f,p): 26 return sum(Cabs (f[d] -p[d])/p[d] for d in range(9)])
# PLEASE DO NOT CHANGE THE CODE BELOW ! n = 1000 # sequence Length def main) import sys import re try: student-id = sys . argv [0]. split('/') [-1] student_id = re . match(' ([a-z]+\d+)*-2", if not student_id: student-id) print("PLEASE CHANGE YOUR FILE NAME !") student id "None" except: student id "None" a = factSeq(n) # factorials (a1, ,an) with ak = k! f = benford(a) # empirical data (f1, f9), fd is fraction starting with d p = [log10(1+1/d) for d in range(1,10)] # Benford's Law prediction (p1, ,p e benTable(f,p) # print the results in a text table
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the complete code for your benford() function, it returns the normalised data.import numpy as np def fact (n): return [np.math.factorial(k) for k in range (1,n+1)] def benford (a): b-[e for d in range (1

Add a comment
Know the answer?
Add Answer to:
Python, given a code in class i just need help with the third bullet point ; using a and n (defin...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am having trouble with my Python code in this dictionary and pickle program. Here is...

    I am having trouble with my Python code in this dictionary and pickle program. Here is my code but it is not running as i am receiving synthax error on the second line. class Student: def__init__(self,id,name,midterm,final): self.id=id self.name=name self.midterm=midterm self.final=final def calculate_grade(self): self.avg=(self.midterm+self.final)/2 if self.avg>=60 and self.avg<=80: self.g='A' elif self.avg>80 and self.avg<=100: self.g='A+' elif self.avg<60 and self.avg>=40: self.g='B' else: self.g='C' def getdata(self): return self.id,self.name.self.midterm,self.final,self.g CIT101 = {} CIT101["123"] = Student("123", "smith, john", 78, 86) CIT101["124"] = Student("124", "tom, alter", 50,...

  • PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment...

    PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment the lines please so I can understand. There are short and med files lengths for each the list of names/ids and then search id file. These are the input files: https://codeshare.io/aVQd46 https://codeshare.io/5M3XnR https://codeshare.io/2W684E https://codeshare.io/5RJwZ4 LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below) Imports the Student class...

  • 9p This is for Python I need help. Pet #pet.py mName mAge class Pet: + __init__(name,...

    9p This is for Python I need help. Pet #pet.py mName mAge class Pet: + __init__(name, age) + getName + getAge0 def init (self, name, age): self.mName = name self.mAge = age Dog Cat def getName(self): return self.mName mSize mColor + __init__(name, age,size) + getSize() + momCommento + getDog Years() +_init__(name, age,color) + getColor + pretty Factor + getCatYears def getAge(self): return self.mAge #dog.py import pet #cat.py import pet class Dog (pet. Pet): class Cat (pet. Pet): def init (self,...

  • PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please...

    PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList:     def __init__(self):         self.__head = None         self.__tail = None         self.__size = 0     def insert(self, i, data):         if self.isEmpty():             self.__head = listNode(data)             self.__tail = self.__head         elif i <= 0:             self.__head = listNode(data, self.__head)         elif i >= self.__size:             self.__tail.setNext(listNode(data))             self.__tail = self.__tail.getNext()         else:             current = self.__getIthNode(i - 1)             current.setNext(listNode(data,...

  • Attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i nee...

    attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i need python one!!!!!!!!! the part below is interface for the range search tree which don’t need to modify it. Week 3: Working with a BST TODO: Implement a Binary Search Tree (Class Name: RangesizeTree) Choose one language fromJava or Python. Iin both languages,there is an empty main function in the Range Size Tree file. The main function is not tested, however, it is provided for you...

  • Hello I need help with python programming here is my code # Trivia Challenge # Trivia...

    Hello I need help with python programming here is my code # Trivia Challenge # Trivia game that reads a plain text file import sys def open_file(file_name, mode): """Open a file.""" try: the_file = open(file_name, mode) except IOError as e: print("Unable to open the file", file_name, "Ending program.\n", e) input("\n\nPress the enter key to exit.") sys.exit() else: return the_file def next_line(the_file): """Return next line from the trivia file, formatted.""" line = the_file.readline() line = line.replace("/", "\n") return line def next_block(the_file):...

  • Python Programming (Just need the Code) Index.py #Python 3.0 import re import os import collections import...

    Python Programming (Just need the Code) Index.py #Python 3.0 import re import os import collections import time #import other modules as needed class index:    def __init__(self,path):    def buildIndex(self):        #function to read documents from collection, tokenize and build the index with tokens        # implement additional functionality to support methods 1 - 4        #use unique document integer IDs    def exact_query(self, query_terms, k):    #function for exact top K retrieval (method 1)    #Returns...

  • I need help with this question. I need a supporting paragraph. Following are results from three...

    I need help with this question. I need a supporting paragraph. Following are results from three hypothetical corporate tax returns. Each purports to be a list of expenditures, in dollars, that the corporation is claiming as deductions. Two of the three are genuine, and one is a fraud. Which one is the fraud? Write at least 6 complete sentences explaining why you think your chosen return is a fraud. 17 ORE 80.535 3.037 132.056 59.727 38.154 137.648 79.386 203.374 11.967...

  • Using python Here is the code import turtle def draw_triangle(vertex, length, k): ''' Draw a ...

    Using python Here is the code import turtle def draw_triangle(vertex, length, k): ''' Draw a triangle at depth k given the bottom vertex.    vertex: a tuple (x,y) that gives the coordinates of the bottom vertex. When k=0, vertex is the left bottom vertex for the outside triangle. length: the length of the original outside triangle (the biggest one). k: the depth of the input triangle. As k increases by 1, the triangles shrink to a smaller size. When k=0, the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT