उपयोगकर्ता इनपुट सूची को देखते हुए और रोटेशन नंबर दिया गया। हमारा काम दिए गए रोटेशन नंबर से सूची को घुमाना है।
उदाहरण
Input A= [2, 4, 5, 12, 90] rotation number=3 Output [ 90,12,2, 4, 5]
विधि1
यहां हम सूची में प्रत्येक तत्व को पार करते हैं और दूसरी सूची में आवश्यक स्थानों पर तत्व सम्मिलित करते हैं।
उदाहरण
def right_rotation(my_list, num):
output_list = []
for item in range(len(my_list) - num, len(my_list)):
output_list.append(my_list[item])
for item in range(0, len(my_list) - num):
output_list.append(my_list[item])
return output_list
# Driver Code
A=list()
n=int(input("Enter the size of the List"))
print("Enter the number")
for i in range(int(n)):
p=int(input("n="))
A.append(int(p))
print (A)
rot_num=int(input("Enter rotate number"))
print("After rotation",right_rotation(A, rot_num))
python54.py
आउटपुट
Enter the size of the List 6 Enter the number n= 11 [11] n= 22 [11, 22] n= 33 [11, 22, 33] n= 44 [11, 22, 33, 44] n= 55 [11, 22, 33, 44, 55] n= 66 [11, 22, 33, 44, 55, 66] Enter rotate number 3 After rotation [44, 55, 66, 11, 22, 33]
विधि2
यहां हम लेन () का उपयोग करके स्लाइसिंग तकनीक लागू करते हैं।
A=list()
ni=int(input("Enter the size of the List"))
print("Enter the number")
for i in range(int(ni)):
p=int(input("ni="))
A.append(int(p))
print (A)
n = 3
A = (A[len(A) - n:len(A)] + A[0:len(A) - n])
print("After Rotation",A)
आउटपुट
Enter the size of the List 6 Enter the number ni= 11 [11] ni= 22 [11, 22] ni= 33 [11, 22, 33] ni= 44 [11, 22, 33, 44] ni= 55 [11, 22, 33, 44, 55] ni= 66 [11, 22, 33, 44, 55, 66] After Rotation [44, 55, 66, 11, 22, 33]
विधि3
इस पद्धति में, सूची A के अंतिम n तत्वों को लिया गया और फिर सूची A के शेष तत्वों को लिया गया।
उदाहरण
A=list()
ni=int(input("Enter the size of the List"))
print("Enter the number")
for i in range(int(ni)):
p=int(input("ni="))
A.append(int(p))
print (A)
n = 3
A = (A[-n:] + A[:-n])
print("After Rotation",A)
आउटपुट
Enter the size of the List 6 Enter the number ni= 11 [11] ni= 22 [11, 22] ni= 33 [11, 22, 33] ni= 44 [11, 22, 33, 44] ni= 55 [11, 22, 33, 44, 55] ni= 66 [11, 22, 33, 44, 55, 66] After Rotation [44, 55, 66, 11, 22, 33]