Pytorch Check Yourself


To check your Python skills, try writing the following two programs.

First Program

sentence = "In the loveliest town of all, where the houses were white and high and the elms trees were green and higher than the houses, where the front yards were wide and pleasant and the back yards were bushy and worth finding out about, where the streets sloped down to the stream and the stream flowed quietly under the bridge, where the lawns ended in orchards and the orchards ended in fields and the fields ended in pastures and the pastures climbed the hill and disappeared over the top toward the wonderful wide sky, in this loveliest of all towns Stuart stopped to get a drink of sarsaparilla."
Copy the above stub code into a new file called "words.py". This code sets a variable called "sentence" to a sentence from E.B. White's story "Stuart Little". Your task is to finish writing this program so that it prints the average length of words in the sentence, to two decimal places (be aware not to count commas and full stops). To run your program, enter the following command into the terminal:
python3 words.py
The correct output should be 4.49

Second Program

sales = "2543,4,4.34;5463,7,8.31;7765,10,1.23;9833,9,34.12;5056,4,5.67;7657,10,4.23;3343,7,2.98;3778,9,9.27;1118,5,8.23;3873,3,4.45;6588,2,5.67;5778,6,3.41;7765,11,2.23;9343,8,4.12;5057,5,4.67;7657,5,4.23;3356,7,4.98;3776,8,8.27;1228,5,7.23;3873,2,4.50"
Copy the above stub code into a program called "sales.py". It has a variable called "sales" whose value is a fictitious set of sales data. Sale items are separated by semicolons (;). Within a sale item there are three pieces of data, separated by commas (,): product id, quantity sold, and sale price in dollars. Your task is to finish writing this program so that it prints the total sales amount in dollars. To run your program, enter the following command into the terminal:
python3 sales.py
The correct answer should be $898.64

Hint: You can split sales into its items using the split() method, and then loop through those items. And you can split each item into its three pieces of data also using the split() method.