+ 2

[Solved] Regular expression exercise 1.3

Hello guys! Why this code below isn't solution for python exercise (regular expressions#1.3, making "+636637281" from input "00636637281" ) Code works, all tests except one (hidden) don't work. Why?( Can you help me pls? #Ps.Sorry for my English) Всем привет) может быть вы знаете ответ, почему данный код в упражнении по регулярным выражениям №1.3 не работает. Все тесты проходятся, кроме одного, скрытого теста...задача выводить "+" , если пользователь вводит вначале номера "00". Прошу, помогите мне🙏 import re inp=str(input()) pattern=r"00" mat=re.search(pattern,inp) if mat.start()==0: print(re.sub(pattern,'+',inp,1)) else: print(int(inp))

26th Dec 2020, 10:06 PM
Ash Ryan
Ash Ryan - avatar
22 Antworten
+ 5
It solves all test cases for me. This amendment in your original code also worked for me: import re inp=str(input()) pattern=r"00" mat=re.search(pattern,inp) if mat and mat.start()==0: # <-- change is here! print(re.sub(pattern,'+',inp,1)) else: print(int(inp))
27th Dec 2020, 10:07 AM
Russ
Russ - avatar
+ 5
Russ thank you so much, your advice was very helpful: Correct code: import re print(re.sub(r"^00", "+", input()))
27th Dec 2020, 11:17 AM
Ash Ryan
Ash Ryan - avatar
+ 3
Ok, ignore my previous remark because the problem wasn't what I thought it was. If "00" is not matched in the input string, there is no match object. So calling mat.start() causes an error.
26th Dec 2020, 11:22 PM
Russ
Russ - avatar
+ 3
Because you've made an effort .....try this (I haven't tried the exercise myself), import re inp = input() pattern = r"00" if re.match(pattern, inp): print(re.sub(pattern, '+', inp, 1)) else: print(inp)
27th Dec 2020, 12:31 AM
rodwynnejones
rodwynnejones - avatar
+ 3
I don't know what device you're using but I noticed a very strange bug a while back (on iOS). I noticed that, with code coaches, modifying the code sometimes wasn't registered and retesting would actually run the old code through the cases. I found I had to modify the code again (usually by just adding an extra empty line but it could be anything that would be detected as an edit) in order for the new code to be run through the test cases. Can you try something for me; try pasting rodwynnejones' code into the code challenge again and run. It should (as you've previously said) still fail one test case. Then add another empty line and run it again. Hopefully that will then pass the cases (and hopefully that all made sense!).
27th Dec 2020, 10:34 AM
Russ
Russ - avatar
+ 2
ash ryan rodwynnejones 's code works, I've just tested it. Try it again.
27th Dec 2020, 9:19 AM
Russ
Russ - avatar
+ 2
ash ryan It is indeed strange. Did my suggested alteration work for you?
27th Dec 2020, 10:56 AM
Russ
Russ - avatar
+ 1
rodwynnejones this code doesn't work either 🥺. Same hidden test failed
27th Dec 2020, 6:54 AM
Ash Ryan
Ash Ryan - avatar
+ 1
rodwynnejones "We need to create a number formatting system for a contacts database. Create a program that will take the phone number as input, and if the number starts with "00", replace them with "+". The number should be printed after formatting. Sample Input 0014860098 Sample Output +14860098
27th Dec 2020, 9:01 AM
Ash Ryan
Ash Ryan - avatar
+ 1
Russ it works but hidden test #4 in exercise failed ( maybe it's develores' bug in app....
27th Dec 2020, 9:58 AM
Ash Ryan
Ash Ryan - avatar
+ 1
Russ thank you for testing... it's so very strange , confusing me.🥺
27th Dec 2020, 10:08 AM
Ash Ryan
Ash Ryan - avatar
+ 1
Russ I reseted my code, then paste rodwynnejones' code. One test failed again. Made newline and.... Nothing 🤷🏼‍♀️ failed again (
27th Dec 2020, 10:42 AM
Ash Ryan
Ash Ryan - avatar
+ 1
Russ no( I wrote to developers about this problem. Maybe this make sense
27th Dec 2020, 10:59 AM
Ash Ryan
Ash Ryan - avatar
+ 1
import re #your code goes here number = input() pattern = r"00" if re.match(pattern, number): print(re.sub(pattern, '+', number)) else: print(number)
19th Apr 2021, 9:33 AM
Gunjan Patel
Gunjan Patel - avatar
+ 1
import re input1 = str(input()) x = r"00" match = re.search(x,input1) try: if match.start() == 0: y = re.sub(x,'+',input1) print(y) else: print(input1) except: print(input1)
13th Aug 2021, 5:09 AM
Lâm Dũng
Lâm Dũng - avatar
0
Copy and paste the task description onto here as I don't have access it. Is it code coach task?
27th Dec 2020, 8:58 AM
rodwynnejones
rodwynnejones - avatar
0
Hello! The hidden test fails because you can have several 00s in a number, they can be not only at the beginning, but at its end as well. That's why this code works: import re input1 = str(input()) x = r"00" match = re.search(x,input1) try: if match.start() == 0: y = re.sub(x,'+',input1) print(y) else: print(input1) except: print(input1) and my original code didn't: import re phone_numbers = input() pattern = "00" formatted_numbers = re.sub(pattern, "+", phone_numbers, count = 1) print(formatted_numbers)
6th Sep 2021, 10:30 AM
Anna Kartasheva
Anna Kartasheva - avatar
0
num = input() pattern = r"^00" by = "+" newstr = re.sub(pattern, by, num) print(newstr)
22nd Feb 2022, 3:54 PM
Alcino Castelo
0
import re num = input() pattern = r"^00" by = "+" number_converted = re.sub(pattern, by, num) print(number_converted)
22nd Feb 2022, 4:01 PM
Alcino Castelo
0
import re str = input() pattern = "^00" print (re.sub(pattern, "+", str))
17th Mar 2022, 7:49 PM
Alexander Hazankin
Alexander Hazankin - avatar