Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE DO NOT COPY THE CODE FROM ANOTHER EXPERT. PLEASE WRITE A NEW ONE! You will write a function to access the file of majors

PLEASE DO NOT COPY THE CODE FROM ANOTHER EXPERT. PLEASE WRITE A NEW ONE!

You will write a function to access the file of majors and colleges represented in CIS 210, and create a list of the majors of students in CIS 210, then another function to analyze the data in the list of majors. Finally, you will write a function to report the number of different majors, and the mode and frequency occurrences of the majors in the file.

Write three new functions: majors_readf, majors_analysis, and majors_report.

Function majors_readf will have one parameter, fname, a string, which is the name of the majors file. majors_readf should open file fname and create and return a list of the majors in this file.

Function majors_analysis will have one parameter, majorsli, a list of majors (the list returned by majors_readf). majors_analysis will determine the most frequently occurring major(s) (mode) in the argument list, and also the count of the number of distinct majors in the list. majors_analysis will return these results.

Function majors_report will have three parameters, majors_mode (the first item returned by majors_analysis), majors_ct (the second item returned by majors_analysis, and majorsli (the list returned by read_majorsf), and return None. It will report the mode of majorsli and the total number of majors represented in the majors list. majors_report will also call frequencyTable (from p62_data_analysis.py) to report the number of occurrences of each item in majorsli. When you have written and tested these, write function main to call functions majors_readf, majors_analysis, and majors_report. main returns None. main should look similar to this:

def main():

'''()->

None Calls: majors_readf, majors_analysis, majors_report Top level function for analysis of CIS 210 majors data. Returns None.

> majors_main() '''

#fname = 'majors_short.txt'

fname = 'p71_majors_cis210f18.txt'

majorsli = majors_readf(fname) #read

majors_mode, majors_ct = majors_analysis(majorsli) #analyze

majors_report(majors_mode, majors_ct, majorsli) #report

return None

Include code in your .py file to call the main function

given function from another file called"p62_data_analysis", the file:

import doctest import statistics #from turtle import * def mean(alist): '''(list of numbers) -> number Return mean of alist (len(alist) > 0). >>> mean([3, 1, 2]) 2.0 >>> mean([0, 0]) 0.0 >>> mean([99]) 99.0 ''' mean = sum(alist) / len(alist) return mean def isEven(n): '''(num) -> boolean Return True if n is even, else return False >>> isEven(4) True >>> isEven(1) False >>> isEven(0) True ''' return (n % 2) == 0 def median(alist): '''(list of numbers) -> number Return median of alist (of len > 0). >>> median([5, 7, 1, 3]) 4.0 >>> median([1, 2, 2, 3, 99]) 2 >>> median([99]) 99 >>> median([0, 0, 0, 0]) 0.0 ''' copyl = alist[:] copyl.sort() copylen = len(copyl) if isEven(copylen): rmid = copylen // 2 lmid = rmid - 1 medi = (copyl[lmid] + copyl[rmid]) / 2 else: mid = copylen // 2 medi = copyl[mid] return medi def mode(alist): '''(list of numbers) -> list Return mode(s) of alist. Calls: genFreqTable >>> mode([5, 7, 1, 3]) [1, 3, 5, 7] >>> mode([1, 2, 2, 3, 99]) [2] >>> mode([99]) [99] >>> mode([0, 0, 1, 1]) [0, 1] ''' countd = genFrequencyTable(alist) countli = countd.values() maxct = max(countli) #there may be more than one mode modeli = [] for k in countd: if countd[k] == maxct: modeli.append(k) # better - use a list comprehension #modeli = [k for k in countd if countd[k] == maxct] return modeli def genFrequencyTable(alist): '''(list) -> dictionary Generate a frequency dictionary with number of occurrences of each item in alist. Called by: frequencyTable, freqChart, mode > genFrequencyTable([1, 2, 3, 3, 1, 4, 5]) {1:2, 2:1, 3:2, 4:1, 5:1} ''' freqD = {} ''' for item in alist: if item in freqD: freqD[item] += 1 else: freqD[item] = 1 ''' # better - use dict set default method for item in alist: freqD.setdefault(item, 0) freqD[item] += 1 return freqD def frequencyTable(alist): '''(list of numbers) -> None Print frequency table of count of each number in alist. None value is returned. Calls: genFrequencyTable, drawTable > frequencyTable([1, 3, 3, 2]) ITEM FREQUENCY 1 1 2 1 3 2 ''' freqD = genFrequencyTable(alist) drawTable(freqD) return None def drawTable(freqD): '''(dict) -> None Display each key-value pair from freqD in a frequency table. None value is returned. Called by: frequencyTable >>> drawTable({1:2, 2:1, 3:2, 4:1, 5:1}) ITEM FREQUENCY 1 2 2 1 3 2 4 1 5 1 ''' iteml = list(freqD) iteml.sort() #iteml.sort(reverse=True) #could use sorted function instead #iteml = sorted(freqD) #or, to sort by value #iteml = sorted(freqD, key=freqD.__getitem__, reverse=True) #print('{:<6} {:<9}'.format('ITEM', 'FREQUENCY')) print(f"{'ITEM':<6} {'FREQUENCY':<9}") for item in iteml: #print('{:<6} {:<9}'.format(item, freqD[item])) print(f'{item:<6} {freqD[item]:<9}') return None def freqChart(alist): '''(list of numbers) -> None Draw frequency chart of count of each number in alist. None value is returned. Calls: genFreqTable, drawChart > freqChart([1, 2, 3, 3, 1, 4, 5]) [frequency occurrences chart] ''' freqD = genFrequencyTable(alist) drawChart(freqD) return None def drawChart(freqD): '''(dictionary of items/number occurrences) -> None Draw frequency chart of items and frequency count in freqD. None value is returned. Called by: freqChart > drawChart(6, 10) [frequency chart] ''' # find min and max items # for drawing graph outline iteml = list(freqD.keys()) iteml.sort() minitem = 0 maxitem = len(iteml) - 1 # do the same for occurrence values countl = freqD.values() maxcount = max(countl) # use these numbers to scale # the canvas appropriately wn = Screen() setworldcoordinates(-1, -1, maxitem+1, maxcount+1) # set up the turtle hideturtle() speed('fastest') # draw the chart outlines penup() goto(0,0) pendown() goto(maxitem, 0) penup() goto(-1, 0) write('0', font=('Helvetica', 16, 'bold')) goto(-1, maxcount) write(str(maxcount), font=('Helvetica', 16, 'bold')) #label the x-axis for i in range(len(iteml)): goto(i, -1) write(str(iteml[i]), font=('Helvetica', 16, 'bold')) #graph the number of occurrences goto(i, 0) pendown() goto(i, freqD[iteml[i]]) penup() #wn.exitonclick() return None shortlist = [1, 2, 3, 1, 2, 2, 2, 4, 1] equakes = [5.3, 3.0, 2.6, 4.4, 2.9, 4.8, 4.3, 2.6, 2.9, 4.9, 2.5, 4.8, 4.2, 2.6, 4.8, 2.7, 5.0, 2.7, 2.8, 4.3, 3.1, 4.1, 2.8, 5.8, 2.5, 3.9, 4.8, 2.9, 2.5, 4.9, 5.0, 2.5, 3.2, 2.6, 2.7, 4.8, 4.1, 5.1, 4.7, 2.6, 2.9, 2.7, 3.3, 3.0, 4.4, 2.7, 5.7, 2.5, 5.1, 2.5, 4.4, 4.6, 5.7, 4.5, 4.7, 5.1, 2.9, 3.3, 2.7, 2.8, 2.9, 2.6, 5.3, 6.0, 3.0, 5.3, 2.7, 4.3, 5.4, 4.4, 2.6, 2.8, 4.4, 4.3, 4.7, 3.3, 4.0, 2.5, 4.9, 4.9, 2.5, 4.8, 3.1, 4.9, 4.4, 6.6, 3.3, 2.5, 5.0, 4.8, 2.5, 4.2, 4.5, 2.6, 4.0, 3.3, 3.1, 2.6, 2.7, 2.9, 2.7, 2.9, 3.3, 2.8, 3.1, 2.5, 4.3, 3.2, 4.6, 2.8, 4.8, 5.1, 2.7, 2.6, 3.1, 2.9, 4.2, 4.8, 2.5, 4.5, 4.5, 2.8, 4.7, 4.6, 4.6, 5.1, 4.2, 2.8, 2.5, 4.5, 4.6, 2.6, 5.0, 2.8, 2.9, 2.7, 3.1, 2.6, 2.5, 3.2, 3.2, 5.2, 2.8, 3.2, 2.6, 5.3, 5.5, 2.7, 5.2, 6.4, 4.2, 3.1, 2.8, 4.5, 2.9, 3.1, 4.3, 4.9, 5.2, 2.6, 6.7, 2.7, 4.9, 3.0, 4.9, 4.7, 2.6, 4.6, 2.5, 3.2, 2.7, 6.2, 4.0, 4.6, 4.9, 2.5, 5.1, 3.3, 2.5, 4.7, 2.5, 4.1, 3.1, 4.6, 2.8, 3.1, 6.3] def main(): '''controller for earthquake data funcs ''' frequencyTable(equakes) #freqChart(equakes) print('Mean value is:', mean(equakes)) print('Python mean is:', statistics.mean(equakes)) print('Median value is:', median(equakes)) print('Python median is:', statistics.median(equakes)) print('Mode is:', mode(equakes)) #print('Python mode is:', statistics.mode(equakes)) - see note below return None if __name__ == '__main__': main() #print(doctest.testmod()) 

given file named"p71_majors_cis210f18.txt":

CIS 210 FALL 2018 Major CIS BADM MATH CIS SDSC CIS EC CIS PBA CIS CIS BADM CIS CIS EC CIS PHYS CIS GS CIS CIS MATH JPN MATH GS BADM CIS CIS CIS CIS CEP PHYS CIS CIS PHYS CIS CEP CIS GS MATH HPHY CIS CIS CIS CIS CIS EXPL CIS MDST CIS CIS CIS EXPL PBA EXPL CIS CIS CIS CIS CIS CIS GB CIS GS GS CIS PJ CIS CIS EXPL CIS CIS CIS ARCH CIS PDS PHYS EC EXPL PBA CIS MATH EC CIS CIS CIS CIS CIS CIS CIS CIS CIS BADM CIS CIS CIS CIS CIS PEN MACS CIS ATCH MATH EXPL EXPL CIS CIS CIS CIS CIS CIS CIS MACS PHYS EC PS CIS CIS LING PHIL PHYS EXPL CIS PDSG CIS CIS CIS CIS EXPL PHYS CIS EXPL BI PBA CIS CIS CIS LING CIS CIS CIS EXPL CIS CINE PBA PSY CIS CIS EXPL MATH GS CIS CIS CIS CIS CIS CIS CIS CIS CIS EXPL MACS EC

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions

Question

l Develop an organization chart using job families.

Answered: 1 week ago

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago