티스토리 뷰

3.1 (Geometry: area of a pentagon) Write a program that prompts the user to enter the length from the center of a pentagon to a vertex and computes the area of the pentagon, as shown in the following figure.

The formula for computing the area of a pentagon is

 

 

, where s is the length of a side. The side can be computed using the formula where r is

 

the length from the center of a pentagon to a vertex.

 

1
2
3
4
5
6
7
8
9
import math
 
= eval(input("Enter the length from the center to a vertex: "))
PI = 3.14159
 
= 2*r*math.sin(PI/5)
area = 3*(3**0.5)/2*(s**2)
 
print("The area of the pentagon is ",round(area,2))
cs

*3.2 (Geometry: great circle distance) The great circle distance is the distance between two points on the surface of a sphere. Let (x1, y1) and (x2, y2) be the geographical latitude and longitude of two points. The great circle distance between the two points can be computed using the following formula:

 

 

 

Write a program that prompts the user to enter the latitude and longitude of two points on the earth in degrees and displays its great circle distance. The average earth radius is 6,371.01 km. Note that you need to convert the degrees into radians using the math.radians function since the Python trigonometric functions use radians. The latitude and longitude degrees in the formula are for north and west. Use negative to indicate south and east degrees.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import math
 
x1,y1 = eval(input("Enter Point 1 (latitude and longitude) in degrees: "))
x2,y2 = eval(input("Enter Point 2 (latitude and longitude) in degrees: "))
 
= 6371.01
 
x1 = math.radians(x1)
x2 = math.radians(x2)
y1 = math.radians(y1)
y2 = math.radians(y2)
 
= R * math.acos(math.sin(x1) * math.sin(x2) + math.cos(x1) * math.cos(x2) * math.cos(y1 - y2))
 
print("The distance between the two points is ",d,"km")
cs

*3.3 (Geography: estimate areas) Find the GPS locations for Atlanta, Georgia; Orlando, Florida; Savannah, Georgia; and Charlotte, North Carolina from www.gps-data-team.com/map/ and compute the estimated area enclosed by these four cities. (Hint: Use the formula in Programming Exercise 3.2 to compute the distance between two cities. Divide the polygon into two triangles and use the formula in Programming Exercise 2.14 to compute the area of a triangle.) 

 

생략


3.4 (Geometry: area of a pentagon) The area of a pentagon can be computed using the following formula (s is the length of a side):

 

Write a program that prompts the user to enter the side of a pentagon and displays the area.

1
2
3
4
5
6
7
8
import math
side = eval(input("Enter the side: "))
 
PI = 3.14159
 
area = (* side**2)/(4*math.tan(PI/5))
 
print("The area of the pentagon is ",area)
cs

*3.5 (Geometry: area of a regular polygon) A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). The formula for computing the area of a regular polygon is

 

Here, s is the length of a side. Write a program that prompts the user to enter the number of sides and their length of a regular polygon and displays its area.

1
2
3
4
5
6
7
8
9
10
import math
 
numOfside = eval(input("Enter the number of sides: "))
side = eval(input("Enter the side: "))
 
PI = 3.141592
 
area = (numOfside * side**2)/(* math.tan(PI/numOfside))
 
print("The area of the polygon is ",area)
cs

*3.6 (Find the character of an ASCII code) Write a program that receives an ASCII code (an integer between 0 and 127) and displays its character. For example, if the user enters 97, the program displays the character

1
2
3
code = eval(input("Enter the ASCII code: "))
 
print("The character is " + chr(code))
cs

3.7 (Random character) Write a program that displays a random uppercase letter using the time.time() function. 

1
2
3
4
5
6
import time
 
time = time.time()
 
value = ord('A'+ time % (ord('Z'- ord('A'+ 1);
print(chr(int(value)))
cs

*3.8 (Financial application: monetary units) Rewrite Listing 3.4, ComputeChange.py, to fix the possible loss of accuracy when converting a float value to an int value. Enter the input as an integer whose last two digits represent the cents. For example, the input 1156 represents 11 dollars and 56 cents. 

 

생략


*3.9 (Financial application: payroll) Write a program that reads the following information and prints a payroll statement:

 

Employee’s name (e.g., Smith)

Number of hours worked in a week (e.g., 10)

Hourly pay rate (e.g., 9.75)

Federal tax withholding rate (e.g., 20%)

State tax withholding rate (e.g., 9%)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
name = (input("Enter employee's name: "))
numOfhour = eval(input("Enter number of hours worked in a week: "))
payrate = eval(input("Enter hourly pay rate: "))
federalRate = eval(input("Enter federal tax withholding rate: "))
stateRate = eval(input("Enter state tax withholding rate: "))
 
gross = payrate * numOfhour
 
print()
print("Employee Name: ",name)
print("Hours Worked: ",format(numOfhour,".1f"))
print("pay Rate: $",payrate)
print("Gross Pay: $",format(gross,".1f"))
print("Deduections: ")
federalWith = gross*federalRate 
stateWith = gross*stateRate 
print("Federal Withholging (",format(federalRate,".1%"),"): $",format(federalWith,".1f"))
print("State Withholging (",format(stateRate,".2%"),") $",format(stateWith,".2f"))
print("Total Deduction: $",format(federalWith + stateWith,".2f"))
print("Net pay: $",format(gross - (federalWith + stateWith),".2f"))
cs

*3.10 (Turtle: display Unicodes) Write a program to display Greek letters The Unicode of these characters are \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7 \u03b8. 

1
2
3
4
5
import turtle as t
 
 
t.write("\u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7 \u03b8")
t.done()
cs

**3.12 (Turtle: draw a star) Write a program that prompts the user to enter the length of the star and draw a star, as shown in Figure 3.5a. (Hint: The inner angle of each point in the star is 36 degrees.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import turtle as t
 
length = eval(input("Enter the length of star: "))
 
t.forward(length)
t.right(144)
t.forward(length)
t.right(144)
t.forward(length)
t.right(144)
t.forward(length)
t.right(144)
t.forward(length)
t.right(144)
 
t.done()
cs

*3.13 (Turtle: display a STOP sign) Write a program that displays a STOP sign, as shown in Figure 3.5b. The hexagon is in red and the text is in white. 

 

생략


3.14 (Turtle: draw the Olympic symbol) Write a program that prompts the user to enter the radius of the rings and draws an Olympic symbol of five rings of the same size with the colors blue, black, red, yellow, and green, as shown in Figure 3.5c.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import turtle
 
turtle.pensize(10)
turtle.color("blue"#파란색 원 그리기
turtle.penup()
turtle.goto(-110-25)
turtle.pendown()
turtle.circle(45)
 
turtle.color("black"#검정색 원 그리기
turtle.penup()
turtle.goto(0-25)
turtle.pendown()
turtle.circle(45)
 
turtle.color("red"#빨간색 원 그리기
turtle.penup()
turtle.goto(110-25)
turtle.pendown()
turtle.circle(45)
 
turtle.color("yellow"#노란색 원 그리기
turtle.penup()
turtle.goto(-55-75)
turtle.pendown()
turtle.circle(45)
 
turtle.color("green")# 초록색 원 그리기
turtle.penup()
turtle.goto(55-75)
turtle.pendown()
turtle.circle(45)
 
turtle.done() 
 
cs

 

*3.17 (Turtle: triangle area) Write a program that prompts the user to enter the three points p1, p2, and p3 for a triangle and display its area below the triangle, as shown in Figure 3.7a. The formula for computing the area of a triangle is given in Exercise 2.14.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import turtle as t
 
x1,y1,x2,y2,x3,y3 = eval(input("Enter three points: "))
 
t.penup()
t.goto(x1,y1)
t.pendown()
t.write("p1 ("+ str(x1) +"," + str(y1) + ")")
t.goto(x2,y2)
t.write("p2 ("+ str(x2) +"," + str(y2) + ")")
t.goto(x3,y3)
t.write("p3 ("+ str(x3) +"," + str(y3) + ")")
t.goto(x1,y1)
t.hideturtle()
 
side1 = ((x1-x2)*(x1-x2) + (y1 - y2)*(y1 - y2))**0.5
side2 = ((x2-x3)*(x2-x3) + (y2 - y3)*(y2 - y3))**0.5
side3 = ((x3-x1)*(x3-x1) + (y3 - y1)*(y3 - y1))**0.5
= (side1 + side2 + side3)/2
 
area = (s*(s-side1)*(s-side2)*(s-side3))**0.
 
t.penup()
t.goto(x2,y2-30)
t.pendown()
t.write("The area of triangle is " + str(format(area,".1f")))
t.done()
cs

 **3.19 (Turtle: draw a line) Write a program that prompts the user to enter two points and draw a line to connect the points and displays the coordinates of the points, as shown in Figure 3.7c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import turtle as t
 
x1,y1,x2,y2 = eval(input("Enter two points: "))
 
t.penup()
t.goto(x1,y1)
t.pendown()
t.write("p1 ("+ str(x1) +"," + str(y1) + ")")
t.goto(x2,y2)
t.write("p2 ("+ str(x2) +"," + str(y2) + ")")
 
t.hideturtle()
 
t.done()
cs

참고 문헌 : Introduction to Programming Using Python / Y.DANIEL LIANG



본 게시물은 개인적인 용도로 작성된 게시물입니다. 이후 포트폴리오로 사용될 정리 자료이니 불펌과 무단도용은 하지 말아주시고 개인 공부 목적으로만 이용해주시기 바랍니다.

  

'파이썬 > 예제' 카테고리의 다른 글

[파이썬]Chapter 6 예제 part 1 (Q.1 ~ Q.25)  (0) 2017.08.30
[파이썬]Chapter 5 예제  (0) 2017.08.16
[파이썬]Chater 4 예제  (0) 2017.07.26
파이썬 Chapter 2 예제  (0) 2017.07.14
댓글