1

I'm trying to figure out the solution to this particular challenge and so far I'm stumped.

Basically, what I'm looking to do is:

  1. Check if substring false exists in string s
  2. If false exists in s, return the boolean True

However, I am not allowed to use any conditional statements at all.

Maybe there is a way to do this with objects?

4
  • What if false doesn't exist in s? Commented Feb 24, 2017 at 16:41
  • Then return blank Commented Feb 24, 2017 at 16:42
  • what do you consider "conditional statements" to be exactly? Commented Feb 24, 2017 at 16:42
  • Any kind of if statement Commented Feb 24, 2017 at 16:43

2 Answers 2

1

There is always str.__contains__ if it's needed as a function somewhere:

In [69]: str.__contains__('**foo**', 'foo')
Out[69]: True

This could be used for things like filter or map:

sorted(some_list, key=partial(str.__contains__,'**foo**'))

The much more common usecase is to assign a truth value for each element in a list using a comprehension. Then we can make use of the in keyword in Python:

In[70]: ['foo' in x for x in ['**foo**','abc']]
Out[70]: [True, False]

The latter should always be preferred. There are edge cases where only a function might be possible.

But even then you could pass a lambda and use the statement:

sorted(some_list, key=lambda x: 'foo' in x)
Sign up to request clarification or add additional context in comments.

Comments

1

Evaluating condition without using if statement:

True:

>>> s = 'abcdefalse'
>>> 'false' in s
True

False:

>>> s = 'abcdefals'
>>> 'false' in s
False

Return blank if False:

>>> s = 'abcdefals'
>>> 'false' in s or ''
''

7 Comments

OP wants blank if False.
'false' in s or None (if 'blank' is None)
Also, your last method is what I suggested in my answer 13 minutes ago! ;)
Your input/output formatting is the wrong way around. I might be the only one bothered about this, but check out meta.stackoverflow.com/questions/310839/… :)
I like this answer a lot by the way. I upvoted, and I think it's the better answer. I just added the __contains__ for completeness because sometimes you cant use a statement somewhere...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.