+ 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))
22 Réponses
+ 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))
+ 5
Russ thank you so much, your advice was very helpful:
Correct code:
import re
print(re.sub(r"^00", "+", input()))
+ 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.
+ 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)
+ 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!).
+ 2
ash ryan rodwynnejones 's code works, I've just tested it. Try it again.
+ 2
ash ryan It is indeed strange. Did my suggested alteration work for you?
+ 1
rodwynnejones this code doesn't work either 🥺. Same hidden test failed
+ 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
+ 1
Russ it works but hidden test #4 in exercise failed ( maybe it's develores' bug in app....
+ 1
Russ thank you for testing... it's so very strange , confusing me.🥺
+ 1
Russ I reseted my code, then paste rodwynnejones' code. One test failed again. Made newline and.... Nothing 🤷🏼♀️ failed again (
+ 1
Russ no( I wrote to developers about this problem. Maybe this make sense
+ 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)
+ 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)
0
Copy and paste the task description onto here as I don't have access it.
Is it code coach task?
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)
0
num = input()
pattern = r"^00"
by = "+"
newstr = re.sub(pattern, by, num)
print(newstr)
0
import re
num = input()
pattern = r"^00"
by = "+"
number_converted = re.sub(pattern, by, num)
print(number_converted)
0
import re
str = input()
pattern = "^00"
print (re.sub(pattern, "+", str))