Write a program (stored in the file collision.py) that does the following: Reads
ID: 3844057 • Letter: W
Question
Write a program (stored in the file collision.py) that does the following:
Reads in data from the file points1.txt and adds all data into a Pointset. (Please use 10 rows and 10 columns for your pointset.) Each line contains the y, and feature values for one entry. Reads in data from the file points2.txt, with the same format as points1.txt. (How you want to process/store points2.txt, in a Pointset or otherwise, is up to you.) For each point in points2.txt, determines if there is a point in points 1.txt that occupies the same x, y coordinates (i.e., a 'collision). For each collision print a message on a single line, using the following structure: collision x 4 y 18 where 4 would be the x value of the collision, 18 would be the yvalue of the collision, and there is a single space separating each element after the colon. One line is printed per collision; if there are multiple collisions they should printed be in the same order as encountered in the file points2.txt. If there are no collisions in the file, then a single message should be printed: No collisionsExplanation / Answer
Code:
#!/usr/bin/python
def main():
# initializing the arrays
pt11 = []
pt12 = []
# opening the file and storing in arrays
with open('points1.txt') as fp:
for line in fp:
# fetching a line and splitting by space
line_fields = line.split()
pt1 = line_fields[0]
pt2 = line_fields[1]
# adding points from file to arrays
pt11.append(pt1)
pt12.append(pt2)
fp.close()
# initializing arrays
pt21 = []
pt22 = []
# opening the file and storing in arrays
with open('points2.txt') as fp:
for line in fp:
# fetching a line and splitting by space
line_fields = line.split()
pt1 = line_fields[0]
pt2 = line_fields[1]
# adding points from file to arrays
pt21.append(pt1)
pt22.append(pt2)
fp.close()
# flag variable to identify if there are collisions
flag = 0
# iterating over points in points2.txt data file
for i in range(0, len(pt21)):
# iterating over points in points1.txt data file
for j in range(0, len(pt11)):
# if there is a match we will set flag variable to 1 and display collisions
if pt21[i] == pt11[j] and pt22[i] == pt12[j]:
flag = 1
print "Collision: x ", pt21[i], " y ", pt22[i]
if not flag:
print "No collisions"
if __name__=='__main__':
main()
NOTE: I have tested the script on different inputs by changing points2.txt file
Execution and output:
186590cb0725:python bonkv$ cat points1.txt
1 2 first
2 3 second
1 5 verylong
2 4 test
1 6 test2
186590cb0725:python bonkv$ cat points2.txt
3 3 a
1 2 b
6 5 c
2 3 d
1 1 e
1 2 f
186590cb0725:python bonkv$ python collission.py
Collision: x 1 y 2
Collision: x 2 y 3
Collision: x 1 y 2
186590cb0725:python bonkv$ cat points1.txt
1 2 first
2 3 second
1 5 verylong
2 4 test
1 6 test2
186590cb0725:python bonkv$ cat points2.txt
3 3 a
1 2 b
6 5 c
2 3 d
1 6 e
2 4 f
186590cb0725:python bonkv$ python collission.py
Collision: x 1 y 2
Collision: x 2 y 3
Collision: x 1 y 6
Collision: x 2 y 4
186590cb0725:python bonkv$
186590cb0725:python bonkv$ cat points1.txt
1 2 first
2 3 second
1 5 verylong
2 4 test
1 6 test2
186590cb0725:python bonkv$ cat points2.txt
3 3 a
186590cb0725:python bonkv$ python collission.py
No collisions
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.