1

Question: Given are a string and m and n.

input 1: original string

input 2: m (less than length of string): cut m alphabets from end of string and then add to begining of string.

input 3: n (less than length of string) : cut n alphabets from end of string obtained from above step and then add to begining of that string.

This process is continued, need to find out the number turns it takes to get back original string.

def harry(str, m, n):
    le = len(str)
    org = str.upper()
    str = str.upper()
    turn = 0
    for i in str:
        str3 = str[-m:] # last n letters
        str = str.replace(' ', '')[:-m]
        str = str3 + str
        print(str)
        if org != str:
            turn = turn + 1
            str4 = str[-n:]
            str = str.replace(' ', '')[:-n]
            str= str4 + str
            print(str)
            turn = turn + 1
        if org == str:
            break
    print(turn)

str = input("Enter the string")
m=int(input("Enter the value of m"))
n=int(input("Enter the value of n"))
harry(str, m, n)

output obtained:

Enter the stringarya
Enter the value of m1
Enter the value of n2
AARY
RYAA
ARYA
2

original output to be obtained:

3 

(It takes 3 turns to get back original string Arya.)

I am getting output for all words except words like this, especially when same letter occurs consecutively. Please help me out with this.

Robert
  • 7,394
  • 40
  • 45
  • 64
Sree
  • 13
  • 1
  • 1
  • 5
  • can you give where it fails? – Pygirl May 31 '20 at 12:51
  • In the second iteration, where original string arya is obtained in str , it is directly going to second if condition without incrementing turn i.e. turn is only 2 because condition is getting true in second iteration itself .(In 9th step, when str=arya is obtained after 2 iterations , it is not counting the same ) . Without incrementing turn it is jumping to print turn .Hope it is clear for you . I think for loop condition is incorrect . – Sree Jun 02 '20 at 07:46
  • The issue is with assigning and increment turn value. – Pygirl Jun 02 '20 at 11:46
  • Always close your question by accepting the answer if that helps :). – Pygirl Jun 02 '20 at 11:49

5 Answers5

1

Actually you will take atleast one step to see whether they are equal or not. So your turn should start from 1.

See: https://ide.geeksforgeeks.org/Hw4AWx1O95

def harry(str, m, n):
    le = len(str)
    org = str.upper()
    str = str.upper()
    turn = 1 # <--------Here
    for i in str:
        str3 = str[-m:] # last n letters
        str = str.replace(' ', '')[:-m]
        str = str3 + str
        print(str)
        if org != str:
            turn = turn + 1
            str4 = str[-n:]
            str = str.replace(' ', '')[:-n]
            str= str4 + str
            print(str)
        if org == str:

            break
        turn = turn + 1 #< --------------- Here 

    print(turn)

str = input("Enter the string\n")
m=int(input("Enter the value of m\n"))
n=int(input("Enter the value of n\n"))
harry(str, m, n)
Pygirl
  • 12,969
  • 5
  • 30
  • 43
1

Mine approach is different. Suppose s, n and m are input length which we have to cut from the end of string s and add it to the beginning.

Explanation: Suppose string s is 'bcdefghijklmnop' and n and m are 2 and 3. After cutting 2 length string: opbcdefghijklmn after 3 length lmnopbcdefghijk and this process continues until it forms original string

Above part we all know

Conclusion: The length of string must be divisible by n+m (or *), because you can see that it is forming series like this n+m+n+m+n+m+.....+ So it either ends with n or m i.e.

  1. (length_of_string % (n+m)==0) if it ends with m. --> (n+m)*k

    answer --> (length_of_string/(n+m))*2
    2 is multiplying here as it includes n and m both.

    ELSE

  2. *(length_of_string % (n+m)==n) if it ends with n. --> (n+m)*k + n

    answer --> (length_of_string/(n+m))*2 + 1

    1 is added extra for n

This is my code:

ls = len(input())
n=int(input())
m=int(input())
x=n+m
if(ls%x==0):
    print(ls//x<<1)
elif(ls%x==n):
    print((ls//x<<1)+1)

I hope this will work for you. If not then please comment it down.

0
word = input()
m = int(input())
n = int(input())

count = 1
word2 = word[-1*m:] + word[:len(word)-m]

#print("Starting with",word2)

while(word != word2):
    if(count%2 != 0):
        word2 = word2[-1*n:] + word2[:len(word2)-n]
    else:
        word2 = word2[-1*m:] + word2[:len(word2)-m]

    #print("String now", word2)
    count+=1

print(count)
fcdt
  • 2,371
  • 5
  • 14
  • 26
Supriyo Das
  • 111
  • 1
  • 2
0
#include<bits/stdc++.h>

using namespace std;

int findmin(string s ,int m, int n){
    
    int len=s.length();
    int count=1;
    string q=s.substr(len-m,m)+s.substr(0,len-m);
    int cut=n;
    while(q!=s){
        q=q.substr(len-cut,cut)+q.substr(0,len-cut);
        if(cut==m){
            cut=n;
        }
        else{
            cut=m;
        }
        count++;
    }
    return count;
}

int main() {
    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        int m,n;
        cin>>m>>n;
        cout<<findmin(s,m,n)<<"\n";
    }
}
0

package collection_learning;

import java.util.*;

public class Learning {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Pease enter a String");
    String string = sc.next();
    System.out.println("enter first number");
    int n = sc.nextInt();
    System.out.println("enter second number");
    int m = sc.nextInt();
    int count = 0;
    String str = string;
    String cutn, cutm, remaining, newString;

    for (int i = 0; i < str.length(); i++) {

        cutn = str.substring(str.length() - n);
        remaining = str.substring(0, str.length() - n);
        newString = cutn + remaining;
        count++;
        if (newString.equals(string)) {
            break;
        } else {
            cutm = newString.substring(str.length() - m);
            remaining = newString.substring(0, str.length() - m);
            newString = cutm + remaining;
            count++;
        }
        if (newString.equals(string)) {
            break;
        }
        str = newString;
    }
    System.out.println("total  turn need =" + count);

}

}

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 25 '23 at 23:58