Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 759413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:35:36+00:00 2026-05-14T15:35:36+00:00

I try to break down the decoding algorithm wanted question into smaller questions. This

  • 0

I try to break down the decoding algorithm wanted question into smaller questions. This is Part I.

Question:

  • two strings: s1 and s2
  • part of s1 is identical to part of s2
  • space is separator
  • how to extract the identical part(s)?

example 1:

s1 = "12 November 2010 - 1 visitor"
s2 = "6 July 2010 - 100 visitors"

the identical parts are "2010", "-", "1" and "visitor"

example 2:

s1 = "Welcome, John!"
s2 = "Welcome, Peter!"

the identical parts are "Welcome," and "!"

example 3: (to clarify the “!” example)

s1 = "Welcome, Sam!"
s2 = "Welcome, Tom!"

the identical parts are "Welcome," and "m!"

Python and Ruby preferred. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:35:37+00:00Added an answer on May 14, 2026 at 3:35 pm

    EDIT: Updated this example to work with all the examples, including #1:

    def scan(s1, s2):
        # Find the longest match where s1 starts with s2
        # Returns None if no matches
        l = len(s1)
        while 1:
            if not l:
                return None
            elif s1[:l] == s2[:l]:
                return s1[:l]
            else:
                l -= 1
    
    def contains(s1, s2):
        D = {} # Remove duplicates using a dict
        L1 = s1.split(' ')
        L2 = s2.split(' ')
    
        # Don't add results which have already 
        # been processed to satisfy example #1!
        DProcessed = {}
    
        for x in L1:
            yy = 0
            for y in L2:
                if yy in DProcessed:
                    yy += 1
                    continue
    
                # Scan from the start to the end of the words
                a = scan(x, y)
                if a: 
                    DProcessed[yy] = None
                    D[a] = None
                    break
    
                # Scan from the end to the start of the words
                a = scan(x[::-1], y[::-1])
                if a: 
                    DProcessed[yy] = None
                    D[a[::-1]] = None
                    break
                yy += 1
    
        return list(D.keys())
    
    print contains("12 November 2010 - 1 visitor",
                   "6 July 2010 - 100 visitors")
    print contains("Welcome, John!",
                   "Welcome, Peter!")
    print contains("Welcome, Sam!",
                   "Welcome, Tom!")
    

    Which outputs:

    ['1', 'visitor', '-', '2010']
    ['Welcome,', '!']
    ['Welcome,', 'm!']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: What I really wanted all along were greenlets . Note: This question mutated
I have a two part problem that needs fixing. I'll try my best to
To keep my code cleaner I often try to break down parts of my
after 6 month of break I try to finish my first app. After I
I have somehow managed to break my infopath form :( When I try to
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
Try this piece of code - public class WhitespaceTest { public static void main(String[]
try: recursive_function() except RuntimeError e: # is this a max. recursion depth exceeded exception?
This is not the clearest of question titles, sorry about that, but I will
My files are a mess, I try to structure them into folders with names

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.