কম্পিউটার টিউটোরিয়াল

পাইথনে দুটি স্ট্রিং থেকে লেক্সিকোগ্রাফিক্যালি মিনিমাল স্ট্রিং বানানোর প্রোগ্রাম

দুটি স্ট্রিং দেওয়া আছে। লেক্সিকোগ্রাফিক্যালি মিনিমাল স্ট্রিং বানাতে হবে: প্রতিবার দুটি স্ট্রিং-এর প্রথম ক্যারেক্টার কম্পেয়ার করে ছোটটি নিন। টাই হলে প্রথম স্ট্রিং-এর ক্যারেক্টার নিন। দুটি স্ট্রিং খালি না হওয়া পর্যন্ত চালিয়ে যান।

উদাহরণ

input_1 = 'TUTORIALS', input_2 = 'POINT' → আউটপুট: POINTTUTORIALS

স্টেপ-বাই-স্টেপ: TUTORIALS POINT → P TUTORIALS OINT → PO TUTORIALS INT → POI TUTORIALS NT → POIN TUTORIALS T → POINT TUTORIALS (খালি) → POINTTUTORIALS

এলগরিদম

  1. দুটি স্ট্রিংয়ের শেষে 'z' অ্যাপেন্ড করুন (সেন্টিনেল)।
  2. temp_1 := 0, temp_2 := 0, res_str := ""
  3. while temp_1 < len(input_1) and temp_2 < len(input_2):
    • যদি input_1[temp_1:] < input_2[temp_2:]: res_str += input_1[temp_1], temp_1++
    • অন্যথায়: res_str += input_2[temp_2], temp_2++
  4. সেন্টিনেল 'z' রিমুভ করুন।
  5. বাকি স্ট্রিং অপেন্ড করুন।

পাইথন কোড

def solve(input_1, input_2):
    input_1 += "z"
    input_2 += "z"
    temp_1 = 0
    temp_2 = 0
    res_str = ""
    while temp_1 < len(input_1) and temp_2 < len(input_2):
        if input_1[temp_1:] < input_2[temp_2:]:
            res_str += input_1[temp_1]
            temp_1 += 1
        else:
            res_str += input_2[temp_2]
            temp_2 += 1
    res_str = res_str[:-1]
    if temp_1 < len(input_1):
        res_str += input_1[temp_1:-1]
    if temp_2 < len(input_2):
        res_str += input_2[temp_2:-1]
    return res_str

print(solve('TUTORIALS', 'POINT'))  # আউটপুট: POINTTUTORIALS

ইনপুট / আউটপুট

'TUTORIALS', 'POINT'
POINTTUTORIALS
  1. দুই স্ট্রিং থেকে অনকমন ওয়ার্ড বের করার পাইথন প্রোগ্রাম

  2. পাইথনে স্ট্রিং থেকে nth ক্যারেক্টার রিমুভ করা

  3. পাইথনে স্ট্রিং এবং স্ট্রিং লিস্ট থেকে টাপল কীভাবে তৈরি করবেন?

  4. পাইথনে লিস্ট থেকে স্ট্রিং তৈরি করার পদ্ধতি