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 8852047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:18:18+00:00 2026-06-14T13:18:18+00:00

Winkleson here! I am currently learning Python when I got stuck on a problem.

  • 0

Winkleson here! I am currently learning Python when I got stuck on a problem. I’ve gotten to the point where I’m dizzy just thinking about it 😛 Anyways any help would be greatly appreciated! Thanks in advance!

Question:


Interlock

Create a function that takes two strings that are the same
length or within one character of the same length as parameters. It
should then take these two strings and interlock them, taking one
character from each string, interlocking them. If the strings are
different lengths, then the result should always start with the longer
string.

My Coding (sorry I’m a beginner and it’s not very Pythonic :P):


def interlock(s1, s2):
    
    r = 0
    l1 = []
    l2 = []
    inters = ''
    
    for i in range(len(s1)):
        
            l1.append(i)
        
    for i in range(len(s2)):
              
            l2.append(i)
    
    if len(s2) == len(s1):        
        
        for i in range(len(s1)):
            inters += ''.join(s1[i])
            inters += ''.join(s2[i])                
    
    elif len(s1) < len(s2):
        
        for i in range(len(s1)):
            inters += ''.join(s2[i])
            inters += ''.join(s1[i])
            r = i
            
        inters += ''.join(s2[r])
            
    elif len(s2) < len(s1):
        
        for i in range(len(s2)):
            inters += ''.join(s1[i])
            inters += ''.join(s2[i])
            r = i
        
        inters += ''.join(s1[r])
        
    else:
        pass
    
    return inters

Results (what results I recieve):

___________________________________________________________________________________________

 Call                   Expected    Received    Correct

 interlock('shoe','cold')   schooled    schooled    true

 interlock('flat','etry')   feltarty    feltarty    true

 **interlock('ab','siy')    saiby           saibi           false**

 **interlock('abalone','hammer')    ahbaamlmoenre   ahbaamlmoenrn   false**

 interlock('','a')          a           a           true

___________________________________________________________________________________________

The two bolder fields are where I am having the most issues. If I try to add in the last characters I get a mysterious out of range exception. Any ideas/solutions would be greatly appreciated! – Winkleson

P.s This is shorter than my normal posts… Usually I’ll give an (un)accurate idea on what I’m think I’m doing wrong and it drags on and on and on and on…. you get the idea. Anyways I probably broke my loops like an idiot. So… Goodluck!

THANKS

Thank you everyone who suggested ways to become a better programmer! I don’t get much time in a day to program so it’s great when so many people take time out of their day to suggest stuff. I love this website and it’s community 🙂

  • 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-06-14T13:18:19+00:00Added an answer on June 14, 2026 at 1:18 pm

    While others are showing you how to do this using itertools (which is a very useful exercise), this will hopefully demonstrate how to write your function to help you learn some basic programming:

    def interlock(s1, s2):
    
        r = 1
        l1 = list(s1)
        l2 = list(s2)
        inters = ''
        if len(s2) == len(s1):        
            for i in range(len(s1)):
                inters += s1[i]
                inters += s2[i]
    
        elif len(s1) < len(s2):
            for i in range(len(s1)):
                inters += s2[i]
                inters += s1[i]
                r = i+1
            inters += s2[r]
    
        elif len(s2) < len(s1):
            for i in range(len(s2)):
                inters += s1[i]
                inters += s2[i]
                r = i+1
    
            inters += s1[r]
    
        else:
            pass
    
        return inters
    
    
    a = interlock('abalone','hammer')
    print (a)
    print (a[::2])
    print (a[1::2])
    a = interlock('hammer','abalone')
    print (a)
    print (a[::2])
    print (a[1::2])
    a = interlock('ab','siy')
    print (a)
    

    I’ve purposefully kept a lot of the structure from your original code, only removing the pieces which are completely unnecessary (e.g. your excessive use of str.join).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Winkleson here with another question for a http://singpath.appspot.com problemset question. I am a beginner
Winkleson here looking for some help on a very simplistic question... I'm pretty out

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.