+ 2

Please Rectify The Error

Question: Change in Case- You will be given a single string and two positive integers denoting indices. You need to change the case of the characters at those indices.i.e change uppercase to lowercase and lowercase to uppercase. It is guaranteed that all characters in the string are alphabets. Input The first line contains N, the length of string. The next line contains a single string. Two integers, x and y, in next line separated by space. Sample Input 6 Dcoder 0 3 Output Print the string after altering the case of characters at those indices. Sample Output dcoDer Constraints- 1 <= string.length <= 40 O <= x, y <= string.length --- My Solution: Help me rectify the error, please... Input: lnstr = int(input()) strn = str(input()) x = int(input()) y = int(input()) if strn[x].isupper(): a = strn[x].lower() else: a = strn[x].upper() if strn[y].isupper(): b = strn[y].lower() else: b = strn[y].upper() print(strn.replace(strn[x],a).replace(strn[y],b)) --- Output: 6 Dcoder 0 3 DcoDer

13th Jun 2020, 10:28 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
25 Respuestas
+ 1
Thank You Everyone for helping me with this question, atlast this code had worked for me... -------------- n,m=int(input()),list(input()) x,y=[int(i) for i in(input().split())] m[x],m[y]=m[x].swapcase(),m[y].swapcase() print("".join(m)) --------------
13th Jun 2020, 5:28 PM
Arun Bhattacharya
Arun Bhattacharya - avatar
+ 5
Arun Bhattacharya ya that's what the code does. It also, says that you'll be given 2 positive integers. 0 isn't considered positive or negative, but neutral. The constraints however, state that 0 is a possible input. Also that the total length of the string is a possible input. Which would cause an error.The replace() won't work for all possible inputs because it will replace all matches. Try this: instr = int(input()) strn = input() x, y = map(int, input().split()) if 0 < x < instr: strn = strn[:x] + strn[x].swapcase() + strn[x+1:] if 0 < y < instr: strn = strn[:y] + strn[y].swapcase() + strn[y+1:] print(strn)
13th Jun 2020, 11:34 AM
ChaoticDawg
ChaoticDawg - avatar
+ 4
instr = int(input()) strn = input() x, y = map(int, input().split()) if x < instr: strn = strn[:x] + strn[x].swapcase() + strn[x+1:] if y < instr: strn = strn[:y] + strn[y].swapcase() + strn[y+1:] print(strn)
13th Jun 2020, 11:04 AM
ChaoticDawg
ChaoticDawg - avatar
+ 4
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 x,y=[int(i) for i in(input().split())] This is the same as using map() And you don't check for any edge cases. The difference in use of converting the input string to a list, then join() and/or using slicing is negligible. I used slicing because I think the time complexity may have a very slight edge on converting and joining the string. I formatted the code for ease of readability.
13th Jun 2020, 12:42 PM
ChaoticDawg
ChaoticDawg - avatar
+ 4
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Please, read the whole thread and make sure you have a full understanding before making a demand that kinda comes across as rude. My code does pass, depending on what you're calling test cases and what is correct. I have 2 versions here. Yours will also depending on what test cases and what is actually correct.
13th Jun 2020, 12:57 PM
ChaoticDawg
ChaoticDawg - avatar
+ 4
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Like I said read the whole thread! There are 2 versions of my code here. If you carefully read what is asked in the challenge (which I would say is misleading, read my previous posts to see why) and then view both versions of the code etc you'll see that the 1st version I posted gives that correct output.
13th Jun 2020, 1:05 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
This assumes input in the format 6 Dcoder 0 3 x, y on same line separated by a space. I forgot to add the rest of the string in my first post. I deleted it in less than a minute, but you must have got to it too quick. Lol
13th Jun 2020, 11:08 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
I get dcoDer as output with no errors when I run my updated code. You don't need the 1 D swapped? That's not what your post says. edit: nvm, I either read your last post wrong or you edited it to say you "actually need" instead of actually don't need.
13th Jun 2020, 11:18 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Arun Bhattacharya re read my last post carefully. If you want the character at the 0 index to swap cases then use my updated first posted code. According to the way the description reads I don't think it should be swapped. Which would mean to use the code from my last post.
13th Jun 2020, 11:43 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
F҉R҉O҉N҉T҉ 🔚& 🔙🔚/ 𝔸𝕣𝕕𝕦𝕚𝕟𝕠 Lover instead of using list comprehension for b=[x for x in input()] Maybe use the more pythonic way of converting a string into a list b=list(input()) And according to the constraints one of the values of c in your code could be equal to the length of the string, which would cause an error. Also, your current if-else block isn't really needed and would be fine with just the code in the if block (but add check for previously mentioned issue). Otherwise, the code is conceptually good depending on whether or not the 0 index should be changed.
13th Jun 2020, 12:01 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Another possible edge case is whether x == y If so, do you swap the case of the character twice so it is back to its original state or just once so the case remains swapped? Of course this only matters if it is one of the test cases provided as input.
13th Jun 2020, 12:13 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
What you want Arun Bhattacharya tell us clearly in first when you are giving value it reversed the case of letter at given position. Then you told I need that I should always get first letter in uppercase. And now i had done same . Tell the whole information what you need
13th Jun 2020, 11:27 AM
Ayush Kumar
Ayush Kumar - avatar
+ 1
a=int(input()) b=[x for x in input()] if len(b) == a: c=input().split() for p in c: b[int(p)]=b[int(p)].swapcase() print("".join(b)) else: pass Finally this is for you . Cheque if it works your output \/ 6 Dcoder 0 3 dcoDer [Program finished] check it Arun Bhattacharya . I think now it is done
13th Jun 2020, 11:41 AM
Ayush Kumar
Ayush Kumar - avatar
+ 1
Thanks ChaoticDawg . Still I am a Lerner as a 10th student. So sorry for my some silly mistakes. And thanks for your information. 😁😁😁
13th Jun 2020, 12:16 PM
Ayush Kumar
Ayush Kumar - avatar
0
ChaoticDawg Your Code Output: 6 Dcoder 0, 3 Traceback (most recent call last): File "ch.py", line 3, in <module> x, y = map(int, input().split()) ValueError: invalid literal for int() with base 10: '0,' Process finished with exit code 1.
13th Jun 2020, 11:04 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
0
hay Arun Bhattacharya this is for you. have a look a=int(input()) b=input() if len(b) == a: c=input().split() for i in c: i=int(i) b=b.replace(b[i],b[i].swapcase()) print(b) else: None This code will be much better. Try it ! sample output \/ 10 AYushKUmaR 1 3 5 8 AyuShkUmAR [Program finished]
13th Jun 2020, 11:07 AM
Ayush Kumar
Ayush Kumar - avatar
0
ChaoticDawg Still throws an error something like this, 6 Dcoder 0 3 Traceback (most recent call last): File "ch.py", line 9, in <module> strn = strn[y].swapcase() IndexError: string index out of range Process finished with exit code 1.
13th Jun 2020, 11:12 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
0
Hey FRONT, Your code outputs the same as mine, but thanks for your contribution... I actually need the 1st 'D' to be lower as well...
13th Jun 2020, 11:15 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
0
a=int(input()) b=input() e="" if len(b) == a: c=input().split() for i in c: i=int(i) b=b.replace(b[i],b[i].swapcase()).replace(b[0],b[0].upper()) print(b) else: pass Hay Arun Bhattacharya Try this . I hadn't noticed Before it's all right now according to your situation
13th Jun 2020, 11:18 AM
Ayush Kumar
Ayush Kumar - avatar
0
Front The result is the same, 6 Dcoder 0 3 DcoDer Process finished.
13th Jun 2020, 11:24 AM
Arun Bhattacharya
Arun Bhattacharya - avatar