Question

In: Computer Science

note: Input-output screen-shot if needed #No-copy paste codes, please. Q) Create a python functions that rotates...

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?

Solutions

Expert Solution

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


Related Solutions

Please note that I tried a screen shot and scanned, and I could not paste this...
Please note that I tried a screen shot and scanned, and I could not paste this on this site because of the browser will not allow, and I called, and was told to type this. The problem and questio Analysis of Variance by hand. The average number of purchases in three different stores are compared to determine if they are significantly different. The following summary statistics is given for each store. State the null and alternative hypothesis, calculate the F-statistics,...
Please attach the output screenshots, narrative descriptions, or paste the Python codes. Please write code to...
Please attach the output screenshots, narrative descriptions, or paste the Python codes. Please write code to print the type of the following variables. Please write down the codes and the output as well. x = 3.5 y = '3.5' z = {1:'John', 2:'Wick', 3:'Barry', 4:'Allen'}
Please attach the output screenshots, narrative descriptions, or paste the Python codes when requested. Task 2...
Please attach the output screenshots, narrative descriptions, or paste the Python codes when requested. Task 2 We are producing two types of products, product A and product B. Product A is defective if the weight is greater than 10 lbs. Product B is defective if the weight is greater than 15 lbs. For a given product, we know its product_type and its weight. Design a program to screen out defective products. Starting the program with variable definition: product_type = 'xxx'...
Please attach the output screenshots, narrative descriptions, or paste the Python codes when requested. Given two...
Please attach the output screenshots, narrative descriptions, or paste the Python codes when requested. Given two exam scores, exam_1 and exam_2, write a program to determine if the student makes progress (exam_2 is greater than exam_1). Return “Yes” or “No”. You can use any exam_1 score and exam_2 score for this exercise.
Please attach the output screenshots, narrative descriptions, or paste the Python codes when requested. Task 2...
Please attach the output screenshots, narrative descriptions, or paste the Python codes when requested. Task 2 Please define a function that extracts the even numbers in a given list. For example, given a list x = [2,3,5,6,7], the function will return a sublist [2,6] because 2 and 6 are even numbers in the list x. The input of this function should be a list, and the output of the function should be a list as well (the sublist). You can...
USE Python 2.7(screen shot program with output) the task is: takes in a list of protein...
USE Python 2.7(screen shot program with output) the task is: takes in a list of protein sequences as input and finds all the transmembrane domains and returns them in a list for each sequence in the list with given nonpolar regions and returns the lists for those. 1. This code should call two other functions that you write: regionProteinFind takes in a protein sequence and should return a list of 10 amino acid windows, if the sequence is less than...
Fundamentals of Programming USING JAVA Please copy here your source codes, including your input and output...
Fundamentals of Programming USING JAVA Please copy here your source codes, including your input and output screenshots. Please upload this document along with your source files (i.e., the .java files) on Blackboard by the due date. 1. (Display three messages) Write a program that displays Welcome to Java, Welcome to Computer Science, and Programming is fun. 2. (Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the result. One foot...
No Explanation Needed. Just the answers and please copy paste the question from above and then...
No Explanation Needed. Just the answers and please copy paste the question from above and then the answer 2. Which industries fall under Canadian federal labour law? Select one: a. education and communications b. media broadcasting and banking c. mining and logging d. agriculture and performing arts 3.Employment law is generally silent on which subject? Select one: a. hours of work b. overtime c. pay performance systems d. health and safety 4. What are teleworking and flextime examples of? Select...
Note: Strictly no copy paste, write in your language. Q. What are the best case studies...
Note: Strictly no copy paste, write in your language. Q. What are the best case studies and examples of Lean practice?
Note: Strictly no copy paste, write in your language. Q. What are the best Lea resources?...
Note: Strictly no copy paste, write in your language. Q. What are the best Lea resources? Q. What are the best lean resources?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT