-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintro.py
203 lines (138 loc) · 6.18 KB
/
intro.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
## MAHagadorn
## Intro to python assignment
## November 16, 2016
##Getting into python interpreter via command line
#use > python3
#This will allow you to work in python3 through command line
#Mallorys-MacBook-Pro:python-introduction-mahagadorn Mal$ python3 intro.py
## 1. Write a loop that prints out the numbers from 20 to 10
#range will print from 0 to the maximum numbers
# third position is step(what to go by 1's,2's,3's) "-" means to make it go backwards
# first tried for i in range(20, 10, -1); doesn't work only prints to 11.
#Need to make the range from 20 to 9
numbST20 = []
for i in range(20, 9, -1):
numbST20.append(i)
print(i)
## 2. Write a list comprehension that returns the numbers from 20 to 10
#List Comprehension is a much quicker way of doing things (instead of looping)
numb_faster = [i for i in range(20, 9, -1)]
## 3. Write a loop that prints out only the numbers from 20 to 10 that are even.
#by making it -2 we will make it step backwards by TWO's!!! giving us the evens.
even_numb = []
for i in range(20, 9, -2):
even_numb.append(i)
print(i)
## 4. Write a list comprehension that prints out only the numbers from 20 to 10 that are even
#convert the above loop to list comprehension format
even_faster = [i for i in range(20, 9, -2)]
## 5. Write a function that calculates whether a number is a prime number
## (hint: what does 2 % 3 give you?)
def prime (x):
for i in range(x-1, 2 , -1):
if x < 2: #if x is less than 2 return false: not prime
return False
elif x == 4: #for some reason it kept giving me four as prime, so I added this elif statment
return False
elif x % i == 0: #if the remainder of anything along the range is 0 its false: aka divisible by more than itself and one
return False
return True #the rest would be prime so return true
prime(5)
prime(2)
prime(20)
#Mal, like a moron you spent about a hour trouble shooting stupid mistakes
#You kept getting syntax errors: this is because you kept forgetting the : after loops
#Also, note to self indent returns
#Note to self 2, make sure your final return is at the same indentation as you first loop
## 6. Write a function that loads a text file, loops over the lines in it, and prints out the
#fifth character on the fifth line of that file.
#“Hint” (really, frankly, this is the solution):
#with open("name_of_file") as handle:
#for line in handle:
#Do something
#This does not work it give your call content
# def char_five (file_name):
# with open('file_name', 'rt') as input_file: #open() reads in text file; 'rt'=open file as read text data
# content = input_file.read()
# print(content)
# ###THIS PRINTS ALL CONTENT!
#I have fixed this below
#Working Good
def char_five (file_name):
x = file_name
with open(x) as input_file:
for n, line in enumerate(input_file.readlines()):
if n == 4:
print (line[4])
char_five('Question6_Py1.txt')
###This is working. Let's talk about how you fixed the issue.
#Enumerate gives you the line number and the line that corresponds to that number
# EX. 1) Text
#My mistake is that I was trying to subset line twice
# n==4 & line==4
# SOOOO by simply calling the line by saying n==4 we get the text there too
#Then we subset line[at the appropriate positon]
## 7. Write a loop that prints out the numbers from 1 to 20, printing “Good: NUMBER” if the number is
#divisible by five and “Job: NUMBER” if then number is prime, and nothing otherwise.
#This works great: as per Will suggestion I translated his "perfect R code" to python.
#That's easy when you have good code to convert! Thanks, Will. :)
for each in range(1, 20, 1):
if prime(each):
print("Job: ", each)
if (each % 5)==0:
print("Good: ", each)
##8 A biologist is modelling population growth using a Gompertz curve, which is defined as y(t) = a.e−b.e−c.t
# where y is population size, t is time, a and b are parameters, and e is the exponential function. Write
# them a function that calculates population size at any time for any values of its parameters.
#This works, again used Will's code for inspiration
#Returns populations size based on the values of arguments
def gompertz (a, b, c, t):
return a **(-b**(-c * t))
gompertz(2,3,4,5)
## 9. Write a function that draws boxes of a specified width and height that look like this (height 3, width 5):
def ImABox (height, width):
the_row = "*" * width
spacer = (height - 2)
print(the_row)
##
if(height < 3):
return("Can't make a box of this height")
if(width < 1):
return("Can't make a box of this width")
##
for i in range(spacer): #for i in len(spacer): didn't work, error=
print("*" + " "*(width-2) + "*")
##
print(the_row)
ImABox(3,5)
#This Works AWESOME!
#MAH note to self: initially you have len(spacer)
#this did not work because it said "object of type 'int' has no len()"
#meaning that the integer doesnt have a length
#hindsight, this makes sense! the integer doesnt have a length because it
#is not a list...I think?
#=3-2
#>>> type(x)
#<class 'int'>
#so its an integer not a list or a tuple. Yes, it does make sense len() didn't work
## 10. Implement a point class that holds x and y information for a point in space. Note that I am not asking
# you to plot that line.
class point:
def __init__(self, x, y): #REMINDER MUST USE SELF AS FIRST ARGUMENT
self.x, self.y= x,y
pointA=point(5,6)
pointB=point(1,2)
##11. Write a distance method that calculates the distance between two points in space.
import math
class point:
def __init__(self, x, y): #REMINDER MUST USE SELF AS FIRST ARGUMENT
self.x, self.y = x,y
def dist_points (self, ptOne, ptTwo):
return math.sqrt(((self.x-ptTwo.x)**2)+((self.y-ptTwo.y)**2))
#Need to figure out why this wont define!
##12. Implement a line class that takes two point objects and makes a line between them. Note that I am not
#asking you to plot that line.
class new_line:
def __init__(self, start, end): #REMINDER MUST USE SELF AS FIRST ARGUMENT
self.start, self.end = start,end
line1=new_line(pointA, pointB)