-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathless3
170 lines (128 loc) · 3.8 KB
/
less3
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
# Given the variable,
# define a procedure, how_many_days,
# that takes as input a number
# representing a month, and returns
# the number of days in that month.
days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31]
def how_many_days(month_number):
return days_in_month[month_number-1]
print how_many_days(1)
#>>> 31
print how_many_days(2)
print how_many_days(9)
#>>> 30
#################################Nested Lists
# Given the variable countries defined as:
# Name Capital Populations (millions)
countries = [['China','Beijing',1350],
['India','Delhi',1210],
['Romania','Bucharest',21],
['United States','Washington',307]]
# What multiple of Romania's population is the population
# of China? Calculate this by accessing the list and
# dividing the population of China (1350)
# by the population of Romania (21).
# Please print your result.
#print countries[1][1] == Delhi
print countries[0][2]/countries[2][2]
#### Nest Mutation ##
# We defined:
stooges = ['Moe','Larry','Curly']
# but in some Stooges films, Curly was
# replaced by Shemp.
stooges [2]='Shemp'
# Write one line of code that changes
# the value of stooges to be:
['Moe','Larry','Shemp']
# but does not create a new List
# object.
####################### Aliasing
# Define a procedure, replace_spy,
# that takes as its input a list of
# three numbers, and modifies the
# value of the third element in the
# input list to be one more than its
# previous value.
spy = [0,0,7]
def replace_spy(spy):
spy[2]=spy[2]+1
# In the test below, the first line calls your
# procedure which will change spy, and the
# second checks you have changed it.
# Uncomment the top two lines below.
replace_spy(spy)
print spy
#>>> [0,0,8]
####################Memory Notes
#1 byte = 8 bits
# bit is binary value - 0 or 1
# kilobyte = 2**10 #2 to power 10 = 1024 just over thousand bytes
# megabyte = 2**20 #2 to power 20 = 1,048,576 just over million bytes
# gigabyte = 2**30 #1,073,741,824 just over billion bytes
# terabyte = 2**40 # just over trillion bytes
# DRAM is a capacitor, storing bits temporarily until power goes off
# Registers is fastest type of memory built into CPU itself
#Distinguish types of memory by latency
#DRAM latency is 12nanoseconds
# nanosecond is a billionth of a second
#memory cost varies by latency and amount you can store
##speed of light#######################################
# Given the variables defined here, write Python
# code that prints out the distance, in meters,
# that light travels in one processor cycle.
# speed_of_light in meters per second
# cycles_per_second is 2.7 GHz
speed_of_light = 299792458.0
cycles_per_second = 2700000000.0
print speed_of_light / cycles_per_second
################
def printallelements(p):
i=0
while len(p)>i:
print p[i]
i=i+1
print printallelements([1,2,14,4,1,6,7])
#######################
# Define a procedure, sum_list,
# that takes as its input a
# list of numbers, and returns
# the sum of all the elements in
# the input list.
def sum_list (num_list):
p=0
for i in num_list:
p=p+i
return p
print sum_list([1, 7, 4])
#>>> 12
print sum_list([9, 4, 10])
#>>> 23
print sum_list([44, 14, 76])
#>>> 134
########################
# Define a procedure, union,
# that takes as inputs two lists.
# It should modify the first input
# list to be the set union of the two
# lists. You may assume the first list
# is a set, that is, it contains no
# repeated elements.
a=[1,2]
b=[3,4]
def union(a,b):
for i in b:
if i in a:
a=a
else:
a.append(i)
return a,b
# To test, uncomment all lines
# below except those beginning with >>>.
a = [1,2,3]
b = [2,4,6]
union(a,b)
print a
#>>> [1,2,3,4,6]
print b
#>>> [2,4,6]
########################################