একটি ব্যবহারকারীর ইনপুট তালিকা দেওয়া এবং ঘূর্ণন নম্বর দেওয়া. আমাদের কাজ হল প্রদত্ত ঘূর্ণন নম্বর থেকে তালিকাটি ঘোরানো।
উদাহরণ
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
এখানে আমরা len().
ব্যবহার করে স্লাইসিং কৌশল প্রয়োগ করি
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]