Question

1. Expense Pie Chart Create a text file that contains your expenses for last month in...

1. Expense Pie Chart
Create a text file that contains your expenses for last month in the following categories:
-Rent
-Gas
-Food
-Clothing
-Car Payment
-Misc

Write a Python program that reads the data from the file and uses matplotlib to plot a pie chart showing you how you spend your money.

2. 1994 weekly gas graph

The text file named 1994_Weekly_Gas_Averages.txt contains the average gas price for each week in the year 1994. (There are 52 lines in the file.) Using matplotlib, write a Python program that reads the contents of the file then plots the data as either a line graph or a bar chart. Be sure to display meaningful labels along the X and Y axes, as well as the tick marks.

1994_Weekly_Gas_averages.txt:

0.992
0.995
1.001
0.999
1.005
1.007
1.016
1.009
1.004
1.007
1.005
1.007
1.012
1.011
1.028
1.033
1.037
1.04
1.045
1.046
1.05
1.056
1.065
1.073
1.079
1.095
1.097
1.103
1.109
1.114
1.13
1.157
1.161
1.165
1.161
1.156
1.15
1.14
1.129
1.12
1.114
1.106
1.107
1.121
1.123
1.122
1.113
1.117
1.127
1.131
1.134
1.125

3. Average Number of Words

For the attached file named average_Number.txt, the text that is in the file is stored as one sentence per line. Write a program that reads the file’s contents and calculates the

average number of words per sentence.

average_Number.txt:

No one is unaware of the name of that famous English shipowner, Cunard.
In 1840 this shrewd industrialist founded a postal service between Liverpool and Halifax, featuring three wooden ships with 400-horsepower paddle wheels and a burden of 1,162 metric tons.
Eight years later, the company's assets were increased by four 650-horsepower ships at 1,820 metric tons, and in two more years, by two other vessels of still greater power and tonnage.
In 1853 the Cunard Co., whose mail-carrying charter had just been renewed, successively added to its assets the Arabia, the Persia, the China, the Scotia, the Java, and the Russia, all ships of top speed and, after the Great Eastern, the biggest ever to plow the seas.
So in 1867 this company owned twelve ships, eight with paddle wheels and four with propellers.
If I give these highly condensed details, it is so everyone can fully understand the importance of this maritime transportation company, known the world over for its shrewd management.
No transoceanic navigational undertaking has been conducted with more ability, no business dealings have been crowned with greater success.
In twenty-six years Cunard ships have made 2,000 Atlantic crossings without so much as a voyage canceled, a delay recorded, a man, a craft, or even a letter lost.
Accordingly, despite strong competition from France, passengers still choose the Cunard line in preference to all others, as can be seen in a recent survey of official documents.
Given this, no one will be astonished at the uproar provoked by this accident involving one of its finest steamers.

4. Character Analysis

Write a program that reads the attached file named character_Analysis.txt ‘s contents and determines the following:

• The number of uppercase letters in the file

• The number of lowercase letters in the file

• The number of digits in the file

• The number of whitespace characters in the file

character_Analysis :

No one is unaware of the name of that famous English shipowner, Cunard.
In 1840 this shrewd industrialist founded a postal service between Liverpool and Halifax, featuring three wooden ships with 400-horsepower paddle wheels and a burden of 1,162 metric tons.
Eight years later, the company's assets were increased by four 650-horsepower ships at 1,820 metric tons, and in two more years, by two other vessels of still greater power and tonnage.
In 1853 the Cunard Co., whose mail-carrying charter had just been renewed, successively added to its assets the Arabia, the Persia, the China, the Scotia, the Java, and the Russia, all ships of top speed and, after the Great Eastern, the biggest ever to plow the seas.
So in 1867 this company owned twelve ships, eight with paddle wheels and four with propellers.
If I give these highly condensed details, it is so everyone can fully understand the importance of this maritime transportation company, known the world over for its shrewd management.
No transoceanic navigational undertaking has been conducted with more ability, no business dealings have been crowned with greater success.
In twenty-six years Cunard ships have made 2,000 Atlantic crossings without so much as a voyage canceled, a delay recorded, a man, a craft, or even a letter lost.
Accordingly, despite strong competition from France, passengers still choose the Cunard line in preference to all others, as can be seen in a recent survey of official documents.
Given this, no one will be astonished at the uproar provoked by this accident involving one of its finest steamers.

5. Word Separator

Write a program that accepts as input a sentence in which all of the words are run together

but the first character of each word is uppercase. Convert the sentence to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example the string “StopAndSmellTheRoses.” would be converted to “Stop and smell the roses.”

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Here is the Python code for all your requirements

1. Expense Pie Chart

  • Text file which contains montly expenses of each category

weekly_expenses.txt 10,20,30,40,80,20

Raw pyhon code:

#importing library

from matplotlib import pyplot as plt

#opening the file

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

#reading all lines

data=file.readlines()

#close the file

file.close()

#Split the data based on ,

data=[i.split(',') for i in data]

#get the data

data=data[0]

#convert to integer

data=[int(i) for i in data]

#given labels

labels=["Rent","Gas","Food","Clothing","Car Payment","Misc"]

#creat objects

fig1, ax1 = plt.subplots()

#draw the pie

ax1.pie(data, labels=labels, autopct='%1.1f%%')

ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.

#show the plotting

plt.show()

Editor:

4 5 weekly_expenses.py > ... 1 #importing library 2 from matplotlib import pyplot as plt 3 #opening the file file=open( week

output:

Figure 1 Food Clothing 15.0% Gas 20.0% 10.0% Rent 5.0% 10.0% Misc 40.0% Car Payment

2. 1994 weekly gas graph

Text file:

الما 1994_Weekly Gas_averages.txt 1 0.992 2 0.995 1.001 4 0.999 5 1.005 6 1.007 7 1.016 8 1.009 9 1.004 10 1.007 11 1.005 12

Raw code:

#importing libraries

from matplotlib import pyplot as plt

#opening the file

file=open('1994_Weekly_Gas_averages.txt','r')

#read lines

data=file.readlines()

#close the data file

file.close()

#remove new line character

data=[i.strip()for i in data]

#convert the data to float

data=[float(i) for i in data]

#get the week numbers

week=[i for i in range(1,53)]

#print(week)

#plot the bar

plt.bar(week,data,color='green')

#title

plt.title("1994 weekly gas graph")

#labels

plt.xlabel("Week")

plt.ylabel("Gas Average")

#week numbers as ticks

plt.xticks(week,week)

#show the plot

plt.show()


Editor:

1 e linegraphy.py > ... #importing libraries 2 from matplotlib import pyplot as plt 3 #opening the file 4 fileopen(1994_Week

output:

1994 weekly gas graph 1.2 1.0 0.8 Gas Average 0.6 0.4 0.4 0.2 0.0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

3).

Text file:

average_Number.txt No one is unaware of the name of that famous English shipowner, Cunard. 2 In 1840 this shrewd industrialis

Raw code:

#opening the file in read mode

with open('average_Number.txt','r') as file:

#reading all lines

data=file.readlines()

#declare sum variable and set it to 0

sum=0

#removing the new line character

data=[i.strip() for i in data]

#for loop over the data

for i in data:

#increment the sum with length of each sentence words

sum=sum+len(i)

#get the number of total lines

total_lines=len(data)

#print the average words of each sentence

print("\nAverage number of words per each sentence: {}".format(sum/total_lines))


Editor:

2 3 6 7 numb.py > ... 1 #opening the file in read mode with open(average_Number.txt, r) as file: #reading all lines 4 dat

output:

Average number of words per each sentence: 158.3 Ajay Kumars-MacBook-Air:Desktop alpha$ |

4. Character Analysis

العا 4 character_Analysis.txt 1 No one is unaware of the name of that famous English shipowner, Cunard. 2 In 1840 this shrewd

Raw code:

with open('character_Analysis.txt','r') as file:

data=(file.read())

upperCaseCount=0

lowerCaseCount=0

digitsCount=0

whitespaceCount=0

for i in data:

if i.isupper():

upperCaseCount+=1

if i.islower():

lowerCaseCount+=1

if i.isdigit():

digitsCount+=1

if i.isspace():

whitespaceCount+=1

print(f"The number of uppercase letters in the file :{upperCaseCount}\nThe number of lowercase letters in the file: {lowerCaseCount}\nThe number of digits in the file: {digitsCount}\nThe number of whitespace characters in the file: {whitespaceCount}")

Editor:

with open(character_Analysis.txt,r) as file: data=(file.read() upperCaseCount=0 lowerCaseCount=0 digitsCount=0 whitespace

output:

ру S/alpha/ top hun ktop Lpna. alpha/anato The number of uppercase letters in the file :29 The number of lowercase letters in

5. Word Separator

Raw code:

#getting the input from user

word=input("Enter Sentence: ")

#declared a null string

empty=""

#Checking the first character

#if its upper add it to the empty string

if word[0].isupper():

empty=empty+word[0]

#else make it upper and add it

else:

word[0]=word[0].upper()

empty=empty+word[0]

#iterating over the word from index one to the end

for i in range(1,len(word)):

#if there is any upper case

if word[i].isupper():

#add a space before and add it to empyt string and convert the character to lower case

empty=empty+" "+word[i].lower()

#or else

else:

#add the charcter to empty string

empty=empty+word[i]

#print the string

print(empty)


Editor:

#getting the input from user word=input(Enter Sentence: ) #declared a null string empty= #Checking the first character #i

output:

Enter Sentence: StopAndSmellTheRoses. Stop and smell the roses. Ajay Kumars-MacBook-Air:Desktop alpha$

Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.

"Please refer to the screenshot of the code to understand the indentation of the code".

Thank you! Do upvote.

Add a comment
Know the answer?
Add Answer to:
1. Expense Pie Chart Create a text file that contains your expenses for last month in...
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
  • 1. Please provide code based on Chapter 12 only from the text. Also include output to...

    1. Please provide code based on Chapter 12 only from the text. Also include output to console picture after the program is compiled. 2. Text is Tony Gaddis 9th edition C++. Chapter 12, page 714, Problem 5 'Line Numbers' 3. Please use 'forChapt12.txt' file for the problem. The text is pasted below after description of the problem. 5. LINE NUMBERS (page 714) Write a program that asks the user for the name of the file. The program should display 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