首页 > > 详细

辅导 Project #2 – Big Practice讲解 Python编程


Project #2 – Big Practice

Making many files to solve many different problems

Your Task

Your task is to solve all the provided problems using the python skills we've learned so far. 

There are 6 problems to solve. Each one should be done in their own .py file, and they must be named exactly what is asked for. Note the points for each problem

All problems should be no more than a couple of lines. Occasionally you will be asked to include certain lines at the start of the file. 

You will be penalized if the statements in the program do not work in the same general way as the problem describes, even if the program works correctly otherwise. 

After the project is due, your program will also be graded by hand. The autograder is not necessarily the final grade you will receive on the project.

You must include a comment at the top of all your program files containing your name, “Project #2”, and the date you turn it in.

 Problems

1. A starting example repeat.py  (20 point)

The first program is provided to you as a starting example. It’s already implemented. All you need to do is to download the file, open it in VSCode, add your name/date. You do NOT need to change the program code. This example reads in a user input, stores it in a variable, and prints it back to the terminal. After you have edited the comments, save it. You can then run it, try some different inputs, and verify the outputs are correct.

File download: repeat.py      (click download button at the top by the printer icon)

This first problem should now serve as your template for the rest of the problems

2. Implement temp.py (20 points)

Create a new program, name it temp.py

This program will first ask the user to enter a floating point number representing a temperature in Fahrenheit. It should then convert the input number to Celsius and Kelvin respectively, and print them out on separate lines. The formulas for temperature conversions are:

C = (F - 32) * 5 / 9

K = C + 273.15

Where F is Fahrenheit, C is Celsius and K is Kelvin.

Running a correct implementation of this program could look like:

Enter temperature in Fahrenheit: 31.41

31.41 degrees Fahrenheit is equivalent to:

-0.3277777777777777 degrees Celsius

272.8222222222222 Kelvin

Formatting details:

● The last number on the second-to-last line must be the Celsius value

● The last number on the last line must be the Kelvin value

● No other lines of text or additional formatting is necessary. Do NOT print extra lines.

Do not worry about the number of decimal places, just include all you get from the conversion process (ie don't round or cast the result)

3. Implement cube.py (20 points)

Create a new program, name it cube.py

This program will first ask the user to enter an integer representing the side length of a cube. It should then compute the volume and surface area of the cube, and print them out on separate lines.

For a given side length:

The volume is the side length cubed

The surface area is 6 times the side length squared (as a cube has 6 sides)

Running a correct implementation of this program could look like:

Enter the length of the cube: 5

A cube with length 5 has:

Volume of 125

Surface area of 150

Formatting details:

● The last number on the second-to-last line must be the volume as an integer number.

● The last number on the last line must be the surface area as an integer number.

● No other lines of text or additional formatting is necessary

4. Implement add_or_find.py (15 points)

Download this file: add_or_find.py

After the creation of the list names on line 2, your program should:

● Ask the user what they want to add to the list

● If the item is already in the list, print the index where it is found

● If the item isn't in the list, add it to the end, and print out the list.

Running a correct implementation of this program could look like:

Add a name? whalers

['blues', 'canucks', 'ducks', 'jets', 'kings', 'kraken', 'panthers', 'penguins', 'rangers', 'wild','bulls', 'cavaliers', 'celtics', 'kings', 'knicks', 'lakers', 'raptors', 'rockets', 'suns', 'warriors', 'whalers']

Add a name? ducks

Already in the list, index: 2

The prints described above must be the last thing your program prints out before it ends. Do not print anything else after it or it will fail the autograder.

HINTS:

● To test if a value is in a list, you can use the membership operator (in) (link)

● To find the index of a value you know to be in a list, you can use the index() method (link)

● I made this one sports themed, for all you sports-heads

5. Implement triangle_id.py (10 points)

Create a new program, name it triangle_id.py

This program will first ask the user to enter three different integers. We will call them a, b and c. Each represents the length of a side of a triangle (a and b are the legs, c is the hypotenuse). Your program should print out invalid, equilateral, isosceles, or scalene depending on the values of a,b,c.

A triangle is invalid if the length a+b is less than or equal to the length of c. A triangle is equilateral if a, b, and c are all the same length. A triangle is isosceles if two of a, b, and c are equal (for example if a=b but != c). If a triangle isn't any of the previous types, we will say it's scalene.

YOU DO NOT NEED TO UNDERSTAND THE UNDERLYING MATH TO SOLVE THIS PROBLEM. You just need to know how to take the above rules and turn them into code.

This is a simple control flow diagram describing the solution to the problem. Use the tools we talk about in class to convert this diagram into code. Remember triangles represent branches, the code should start at start, and only be able to follow along the arrows.:

Running a correct implementation of this program could look like:

Enter a: 5

Enter b: 5

Enter c: 5

equilateral

Enter a: 5

Enter b: 5

Enter c: 9

isosceles

Enter a: 5

Enter b: 5

Enter c: 12

invalid

The type of triangle must be in the last line your program prints out.

HINTS:

● To find if a triangle is equilateral, you need to do three different Boolean expressions. Those Boolean expressions need to be combined in some way, it will be worth your time to review the reading on logical operations (also this link: here)

● To find if a triangle is isosceles, you need to do three different Boolean expressions. The expressions themselves will look like your solution to finding equilateral triangles, but you will combine them differently. Review the same materials as above, but pick a different way to combine them.

6. Implement even_division.py (10 points)

Create a new program, name it even_division.py

Division is made up of two parts, the dividend and the divisor. The number that is being divided is the dividend, the number which we divide by is the divisor.

This program will first ask the user to enter the dividend, and then ask them to enter the divisor. Both will be integers and the user will never enter anything invalid (like giving 0 as the divisor).

The program should then check to see if the dividend is divisible by the divisor. A number is divisible by another if the division results in no decimal values. For instance, 12 is divisible by 3 (the result is 4) but it is not divisible by 5 (the result is 2.4)

*The program should output "True, {dividend} is divisible by {divisor}" or "False, {dividend} is not divisible by {divisor}" depending on the circumstance. Note, the print out should put the numbers in place of {dividend} and {divisor}. So for 12 and 3 this should be printed out "True, 12 is divisible by 3"

Running a correct implementation of this program could look like:

Enter dividend: 12

Enter divisor: 5

False, 12 is not divisible by 5

The formatting of this line needs to match the described* exactly, and it needs to be the last thing your program prints out

7. Implement anagram.py (5 points)

Create a program, name it anagram.py

A string is said to be an anagram of another string if it can be created by rearranging the letters of the other string. For example, fried is an anagram of fired and vice versa. Another example is listen and silent.

Your program should accept two strings, and should print out True if they are anagrams. It should print out False otherwise. YOU CANNOT USE LOOPS TO SOLVE THIS PROBLEM.

Running a correct implementation of this program could look like:

Enter first: santa

Enter second: stana

True

HINTS:

● You do not need loops, or branching to solve this problem. In fact, using them could make the problem much harder than it would be otherwise.

● Strings are hard to use, is there a way to turn them into a list of characters?

● Lists have many built-in methods that could be useful. What do all anagrams have in common? Could you reorder both lists to make identifying anagrams easier?

● Often solving CS problems is like solving a problem. You have to think about anagrams in a new way in order to teach them to a computer.

● BTW lists have a built in sort method, I wonder how that could be useful here???(https://www.w3schools.com/python/ref_list_sort.asp)

Testing

Test your programs completely. Run it several times: verify that for each section outputs the desired values, and that the prints match exactly to the desired. Once you have confirmed to yourself your program is functioning, you should submit it to the appropriate place on gradescope.com. If you fail any tests, gradescope will tell you which test, a small amount about the test, but it is up to you to figure out what went wrong. This is a very important part of the programming process.

Extra Credit

The extra credit are three additional problems you can attempt. These problems are meant to really test your understanding up to this point.

Extra Credit #1

Create a new program, name it perfect_weave.py

This program requests 3 inputs in order:

● A string, referred to as A here

● Another string, referred to as B here

● An integer, referred to as n here

It will then print out n copies of A with n copies of B interweaved / interleaved. So the output should look like ABABAB… in other words: A followed by B, repeated n times.

Running a correct implementation of this program could look like:

Enter first string: on

Enter second string: i

Enter a number: 2

onioni

Enter first string: flip

Enter second string: flop

Enter a number: 4

flipflopflipflopflipflopflipflop

Hints: Try to do a few examples by hand first, looking for a pattern in how the output relates to the input. The solution to this problem is deceptively simple, remember that we have already learned about an operator that lets us repeat strings!

Extra Credit #2

Create a new program, name it fraction_division.py

Division is made up of two parts, the dividend and the divisor. The number that is being divided is the dividend, the number which we divide by is the divisor.

The goal of this program is to show integer division results with fractions. The user inputs the dividend first, and then the divisor. You can assume both are positive integers. Your program should then print out a string showing the integer result of the division, including the quotient and remainder. Follow examples below for more insight.

Hint: the arithmetic operations you need to solve this problem already exist in Python.

This is also meant to test your knowledge of strings to get a perfect recreation of the formatting shown below. Note that after the equals: there is a newline and a tab.
Note: unlike the previous questions, for this question the formatting must exactly match the examples given below.

What is the dividend: 7

What is the divisor: 2

7/2 equals:

       3 and 1/2

What is the dividend: 50

What is the divisor: 10

50/10 equals:

       5 and 0/10

What is the dividend: 147

What is the divisor: 6

147/6 equals:

       24 and 3/6

Extra Credit #3

Create a new program, name it seconds.py, and implement it to complete the following task:

1. ask the user to enter a positive integer representing the number of seconds.

2. convert that to a total number of days, hours, minutes, and seconds such that hours must be between 0 and 23, minutes between 0 and 59, and seconds between 0 and 59. For example: 3599 seconds converts to 0 days, 0 hours, 59 minutes and 59 seconds;
90061 seconds converts to 1 day, 1 hour, 1 minute, and 1 second. 

3. output the result in 5 lines: the first line is the original number the user entered, followed by a space character, and the text seconds is equal to: then lines 2 to 5 are the number of days, hours, minutes, and seconds that you calculated.

Hint: for this program you will need to use the floor division operator // and the modulo % operator. First think about how to solve this problem by hand; next, write down code to implement your solution. Then test your program by running it with a few example inputs like those listed above.

Running a correct implementation of this program may look like:

Enter a number of seconds: 314159

314159 seconds is equal to:

3 days,

15 hours,

15 minutes, and

59 seconds.

Submission

When you are done, turn in the assignment via Gradescope, you should submit only your .py file. Make sure that your file names match exactly. 

After you submit, gradescope will automatically test your program, and you should shortly see the results from the tests. If you have failed any, a small amount of feedback might be given, and you will be allowed to resubmit your code. It is up to you to figure out what went wrong. This is a very important part of the programming process. We may offer assistance in finding the bug, but we will not bug fix for you.

After the project is due, your program will also be graded by hand. The autograder is not necessarily the final grade you will receive on the project.

Grading

After the due date, your project will also be hand graded. We are looking to see if your project was completed authentically, and if there are parts of your code that are partially correct and deserve partial credit.

In the report of your grade, you will see a score and a set of letter codes explaining what you did wrong. If you get 10 points, there will be no associated letter codes.

The grading codes A-E are defined and will be the same for all programs that you turn in. A summary of those codes is as follows:

A:   -10 (once) Student’s name is missing from the top of the program.

B:   -100 a Program cannot run due to syntax errors.

C:   -100 a Program crashes before finishing.

D:   -10 (once) Program uses overly advanced methods not yet discussed in class

E:   -25 (once) Program works correctly, but changes the assignment in order to do it.

In addition, penalties for this assignment only will be incurred for the following infractions (which may supersede some of the generic codes listed above):

F:  -10 (each) Required line of code is omitted (like omitting a Print statement, for example, either in part or in its entirety).

G: -15 (once) Project file is not named correctly, causing autograder to fail.

Each of the three extra credit items are worth +3.33 points added to the score only if implemented correctly. Incorrect implementation will not be penalized.

 


联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!