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

  • Home
  • SEARCH
  • 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 8880053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:05:23+00:00 2026-06-14T20:05:23+00:00

Given that in recursion you’re using a divide-and-conquer method to break down problems into

  • 0

Given that in recursion you’re using a divide-and-conquer method to break down problems into smaller pieces, how would you be able to read data from beginning of the problem, at the end of problem?

Example: I have a an encrypt() function that replaces characters in a string with characters 3 indices to the right:

A in string "ABCDEF" becomes D for example

So far I did it recursively up to the point where it stops when 3 indices to the right are undefined, but this leaves the last bit of the string the same as the original.

Example: "ABCDEF" becomes "DEFDEF"

Is there a way that I can pass the beginning of the string to the innermost functions during the recursive calls in an efficient way?

This is my code currently:

def shift_cipher_noloop(plain):

    encrypted = ""

    if(plain == ""):
        encrypted = ""

    else:
        if(len(plain) > 3):            
            temp_sub = plain[3]          
            encrypted = encrypted + temp_sub
            encrypted = encrypted + shift_cipher_noloop(plain[1:])

        else:
            temp_sub = plain[0] 
            encrypted = encrypted + temp_sub
            encrypted = encrypted + shift_cipher_noloop(plain[1:])

    return encrypted

x = "ABCDEFGHIJK"

y = shift_cipher_noloop(x)

print(y)
  • 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-14T20:05:24+00:00Added an answer on June 14, 2026 at 8:05 pm

    May be this does not exactly solves your problem, but you may have to mould it a little bit to make it fit. As I see somewhere you want Recursion. I have just shown how you can move to the beginning, when you reach the end of the string.

    Take the modulus of i + 3 with the length of string to move to the beginning automatically: –

    >>> my_str = "ABCDEF"
    >>> length = len(my_str)
    
    >>> for i in range(length):
            print my_str[(i + 3) % length],
    
    
    D E F A B C
    

    So, when i = 3, i + 3 = 6, and 6 % 6 = 0 -> back to the first character


    If you want to use Recursion, here’s your modified program: –

    def shift_cipher_noloop(plain, i):
    
        if(plain == ""):
            return ""
    
        else:
            # Iterate with your string, moving first character to last on each iteration
            if len(plain) > 3 and i > 0: 
                return shift_cipher_noloop(plain[1:] + plain[0], i - 1)
    
            else:
                # else Will be executed when either len <= 3, or `i <= 0`.
                # when (i <= 0) is true, you have created a new string, 
                # with each character shifted by `original i` indices. 
                # So, just return it.
    
                return plain
    
    
    x = "ABCDEF"
    index_to_shift = 3
    y = shift_cipher_noloop(x, len(x) - index_to_shift)
    
    print(y)
    

    OUTPUT : –

    DEFABC
    

    I added one more parameter to your method, which I use to take the appropriate index. And also, I’m passing the complete string to the method each time moving the first character to the end.

    Also, if the len(plain) <= 3, you can simply return the string.

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

Sidebar

Related Questions

Given that it's impossible to see into the future, what factors related to Clojure,
I'm doing some generic object comparison, using reflection and recursion. The recursive method needs
Am trying to solve the given recursion, using recursion tree, T(n) = 3T(n/3) +
I have been looking into recursion and TCO. It seems that TCO can make
Given that the web application doesn't have su privileges, I'd like to execute a
Given that an input String may be specified as follows: read(xpath(‘...’)) or xpath(‘...’) or
Given that I have a Foo model w/ the standard Rails timestamp columns what
Given that sorl isn't an app directory wide and the template tag definition lives
Given that the Rails Way seems to be not to use foreign key constraints,
Given that EVAL is Evil how do I create an Array name dynamically: I

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.