-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-datastructures.py
More file actions
207 lines (130 loc) · 4.16 KB
/
basic-datastructures.py
File metadata and controls
207 lines (130 loc) · 4.16 KB
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
204
205
206
207
# Covering Sets, Tuples, and more
# Mar-2024
#create and empty list in python
firstList = []
firstList = [23,23,23,23,23,42,342,342,34,23,42, 2334]
#lists in python can have all data types, interesting?? I wonder how this works behind the scenes
secondlist = ["abba", 4, 0.3]
#append second list contents to the first list!
range
for i in range(len(firstList)):
secondlist.append(firstList[i])
print(f"the contents of the second list are the following:\n { secondlist }")
# Programming Problem 1:
#
#Given list1 as a list and list2 as another list created by replicating list1 a
#number of times, write a statement to compute and assign it to the variable n.
list1 = [1, 2, 3]
list2 = []
REPLICATION = 23
for i in range(REPLICATION):
list2.extend(list1)
# Solution:
length = len(list1)
n = 0
for i in range(len(list2)):
#filter out non-matches
if list1[i % length] != list2[i]:
break
if list1[-1] == list2[i]: # if match last element
n += 1 # incriment
print(n)
# OR YOU CAN DO THIS LOl
n = len(list2) // len(list1)
print(min(list2))
## retrieve minimum of first 4 values in list
minimum = list2[0]
for i in range(1, 4):
minimum = min(minimum, list2[i])
print(f" minimum value of first 4 elements in list2 is: {minimum}! ")
# Swap first and last element
last = list1.pop()
first = list1[0]
list1.append(first)
list1.remove(first)
list1.insert(0, last)
print(list1[:])
#assign true if list has less than 10 elements
value = True if len(list1) < 10 else False
#python looks very similar to english, cpp would write it as
#value = (list1.length() < 10) ? True : False
#lists only support two operators, concatenation + and relication *
print(f"concatonating lists are easy { list1 + list2}")
print(f"replacating lists via multiplication operator { list1 * 4 }\n")
print(list1.__contains__(3))
for i in list1:
print(i)
# just like java, lists are compared lexiographically
print (list1 >= list2)
###### Python Only!!!!!!!!!!!!!!! ######
print(x for x in range(19) if x < 10) ## will print 0 thru 9
#assign it too!
newlist = [x * 3 for x in range(19) if x < 10]
## The way this works is
#
# python returns a sequence of x's based off of
# the conditions and iterations specified!
#
# Programming Problem 2:
#
#Given list1 as a list and list2 as another list created by replicating list1 a
#number of times, write a statement to compute and assign it to the variable n.
interleaved = []
index1 = 0
index2 = 0
for i in range(max(len(list1), len(list2))): # total elements
if index1 < len(list1):
interleaved.append(list1[index1])
if index2 < len(list2):
interleaved.append(list2[index2])
# will compare lexiographically
#print(max(list1, list2))
"sdflkj".split
#array shifting trick, shifting left
print( list1[1:] + list1[:1])
#array shifting trick, shifting right
print( [list1[-1]] + list1[:len(list1) - 1])
import statistics
print(f"average of list 1 is { statistics.mean(list1) }")
print(f"average of list 2 is { statistics.mean(list2) }")
def function(ls):
print("id INSIDE",id(ls))
return ls
ls = [234,23,2]
print("id ORIGINAL:", id(ls))
ls23 = function(ls)
print("id :", id(ls23))
scores = list(input())
####
# Write a function that returns the sum of all the elements
# in a specified column in a matrix using the following header:
####
def sumColumn(m, columnIndex):
sum = 0
for row in m:
sum += float(row[columnIndex])
return sum
matrix = []
for i in range(0,3):
row = input(f"Enter a 3-by-4 matrix row {i}:") \
.split()
matrix.append(row)
for i in range(len(matrix[0])):
print(f"Sum of the elements at column {i} is {sumColumn(matrix, i)}")
## just an example
class A:
def __init__(self):
self.x = 1
self.__y = 1
def getY(self):
return self.__y
def __mabyePrivateMethod(self):
return self.__y + self.x
a = A()
len(list)
print(a.x)
# These two wont be interpreted because they are acessing private variables
# and private methods from the class. Remember: __underscore
# indicated the private scope
#print(a.__y)
#print(a.__mabyePrivateMethod())