Help in XYZ problem in python? | SoloLearn: Learn to code for FREE!

+10

Help in XYZ problem in python?

Edit: Without regex Return True if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not. E.g: xyz_there('abcxyz') → True xyz_there('abc.xyz') → False xyz_there('xyz.abc') → True My mindless attempt: It does't pass all conditions https://code.sololearn.com/cu05NPBlYUql/?ref=app

5/30/2020 9:44:57 AM

Tricker

56 Answers

New Answer

+10

Try this one: https://code.sololearn.com/cAzxk02W6fUI/?ref=app

+9

Regex! https://code.sololearn.com/cErhYv2F92Ev/?ref=app

+6

Russ ohh i never thought of making it shorter. Nice!!

+6

Russ, thanks a lot for your hint!!! Now it works.

+5

Russ Good one :) My attempt: https://code.sololearn.com/c4iVsRm45zrl/?ref=app

+5

[Edited / corrected] - Here is my version: test = ['.xyz.', 'abcxyz', 'abc.xyz', 'xyz.abc','.xyzabc', 'abc.xyzxyz', '.xyz.xyz', 'abc.xyzxyz.xyz'] for i in test: print(f'{i} -> OK') if (i.count('xyz') - i.count('.xyz') > 0) else print(f'{i} -> NOT OK') print() https://code.sololearn.com/ca8NngqIpDeg/?ref=app

+5

Bikash Rouniyar, You posted a question here in this thread. The better way would be if you post it as a separate thread. Thanks!

+4

PythonPip Send an email(📧) to them to allow it 😂😂😂

+4

PythonPip I suspect one of the failing tests might be from values like: ".xyzxyz" "x.xyzxyz" "xyz.xyz" "x.xyz.xyz.xyzxyz" Both ".xyz" and "xyz" without a preceeding "." exists. So the solution should also return True for these instances.

+4

✞༒K4l5ik༒✞ to maintain the humour of the statement

+4

How about a little state machine FSM States: -1,0,1,2 Start state 0 Accepted state 3 0->0 for each char except x and. O->1 for x 0->-1 for . 1->2 for y 1->-1for . 1->0 for others 2->-1 for . 2->3 for z 2->0 for others -1 -> -1 for . -1 ->0 for others

+3

PythonPip import subprocess import sys def install(package): subprocess.call([ sys.executable, "-m", "pip", "-q", "install", package ]) install('requests') install('beautifulsoup4') import requests from bs4 import BeautifulSoup help(requests) print() help(BeautifulSoup) use this to install regex ☝️☝️☝️

+3

Lothar I think change "== 1" to "> 0" and your code is good 👍

+3

it has been a looong night, but this is a good exercise... I will look again when I am functioning on less caffiene and a few more brain cells available. 🙄👍

+3

Cassandra If only '.x' is removed then the letters before '.x' will create problem if it's 'x'. Consider this string: "x.xyz" If ".x" would be removed then then string will read as "xyz", which will return True instead of False.

+2

PythonPip Have a look on it 👇👇👇 return '.xyz' not in str and 'xyz' in str

+2

Kuba Siekierzyński It needs to catch "xyzabc" as true 😉

+2

Russ Right, your way is more efficient 👍🏻

+2

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 😅😅 It's an online website test. That won't allow this.

+2

PythonPip I thought ".xyz" should return False? Code Crasher Your code produces this: djd.xyzxyz True djdj.xyzjen True - should be False jdjxyz.xyz True djdjhexyz False - should be True abcxyz False - should be True xyzabc False - should be True I think Valmob101 's solution is the cleanest. def xyz_there(string): return "xyz" in string.replace(".xyz", "")