In: Computer Science
note: Input-output screen-shot if needed #No-copy paste codes, please.
Q) Create a python functions that rotates the elements of a list by one position. Consider a function rotate(a_list, direction) that takes as arguments the list to rotate, and the direction (left or right) of rotation. Rotate the list in place.
Q) Choice of appropriate data structure is an important step in
designing algorithms. Suppose a text file contains student records
on each line and each record is of the format: Name of Student,
Student ID, GPA. Now consider reading that le to create a list of
lists, a list of tuples or a tuple of tuples.
(a) If you want to create a report using the data, what data
structure would you use?
(b) If you want to modify the name, id or GPA, what data structure would you use?
Solution to Question 1:
Code in python:
------------------------------------------------------------CODE------------------------------------------------------
'''
if direction True it rotates the list to right
if it is False then it rotates the list to left
by default it will be True i.e the functctio
rotates
the list to right if 2nd argument is not
supplied
'''
def rotate(a_list,direction=True):
if direction:
last=a_list[-1]
for i in
range(len(a_list)-2,-1,-1):
a_list[i+1]=a_list[i]
a_list[0]=last
return a_list
else:
first=a_list[0]
for i in
range(len(a_list)-1):
a_list[i]=a_list[i+1]
a_list[-1]=first
return a_list
a_list=[1,2,3,4,5,6]
print("list :",a_list)
print("Right Rotate :",rotate(a_list,True))
a_list=[1,2,3,4,5,6]
print("Left Rotate :",rotate(a_list,False))
--------------------------------------------------------------END------------------------------------------------------------
Code in sublime text:

OUTPUT:

Solution to question 2:
a ) we can use list of tuples because, tuples are immutable in nature and provide faster access than lists.
for creating we already know the size of each tuple i.e 3 (Student_name,student_id,GPA) and we append it to the list.
Example Structure would be:
[('Alice','001',7.5),('Bob','002',8.6).........................]
b)Since we need to modify the data using tuples inside a list is not considered as a good idea. Why because tuples are immutable in nature and we cannot modify the data inside a tuple.
The better Idea would be to use a List inside a List.Why because a list is mutable in nature and we can modify the data inside a List.
Example Structure would be:
L=[['Alice','001',7.5],['Bob','002',8.6]........................]
if you want to update Alice GPA to 9.5 you can do something like
L[0][2]=9.5
Updated list would be:
L=[['Alice','001',9.5],['Bob','002',8.6]........................]
Have a Nice day........:D