কম্পিউটার

একটি স্ট্রিং ঘোরানোর জন্য পাইথনে স্ট্রিং স্লাইসিং


একটি স্ট্রিং দেওয়া হয়েছে, আমাদের কাজ হল স্ট্রিংটিকে দুইভাবে স্লাইস করা। একটি ঘড়ির কাঁটার দিকে এবং অন্যটি ঘড়ির কাঁটার বিপরীত দিকে৷

1. বাম দিকে (বা কাঁটার বিপরীত দিকে) প্রদত্ত স্ট্রিংটিকে d উপাদান দ্বারা ঘোরান (যেখানে d <=n)।

2. ডানদিকে (বা ঘড়ির কাঁটার দিকে) প্রদত্ত স্ট্রিংটিকে d উপাদান দ্বারা ঘোরান (যেখানে d <=n)।

উদাহরণ

Input: string = "pythonprogram"
d = 2
Output: Left Rotation: thonprogrampy
Right Rotation: ampythonprogr

অ্যালগরিদম

Step 1: Enter string.
Step 2: Separate string in two parts first & second, for Left rotation Lfirst = str[0 : d] and Lsecond = str[d :]. For Right rotation Rfirst = str[0 : len(str)-d] and Rsecond = str[len(str)-d : ].
Step 3: Now concatenate these two parts second + first accordingly.

উদাহরণ কোড

def rotate(input,d):
   # Slice string in two parts for left and right
   Lfirst = input[0 : d]
   Lsecond = input[d :]
   Rfirst = input[0 : len(input)-d]
   Rsecond = input[len(input)-d : ]
   print ("Left Rotation : ", (Lsecond + Lfirst) )
      print ("Right Rotation : ", (Rsecond + Rfirst) )
      # Driver program
   if __name__ == "__main__":
      str = input("Enter String ::>")
d=2
rotate(str,d)

আউটপুট

Enter String ::> pythonprogram
Left Rotation: thonprogrampy
Right Rotation: ampythonprogr

  1. পাইথনে স্ট্রিং ঘোরান

  2. পাইথনে বিপরীত স্ট্রিং

  3. পাইথনে অ্যারে ঘোরান

  4. পাইথনে casefold() স্ট্রিং