#πŸ”’ Im a bit dumb - it should be easy hopefully?

35 messages Β· Page 1 of 1 (latest)

tacit pivot
#

I've just started leetcode and am struggling because I've got code and its verified for 264/266 and idk whats wrong with it.

A sentence is circular if:

The last character of a word is equal to the first character of the next word.
The last character of the last word is equal to the first character of the first word.
For example, "leetcode exercises sound delightful", "eetcode", "leetcode eats soul" are all circular sentences. However, "Leetcode is cool", "happy Leetcode", "Leetcode" and "I like Leetcode" are not circular sentences.

Given a string sentence, return true if it is circular. Otherwise, return false.

Heres the code I wrote:
class Solution(object):
def isCircularSentence(self, sentence):
length = len(sentence) -1
if sentence[0] == sentence[length]:
breaks = []
for i in range(length):
if sentence[i] == ' ':
breaks.append(i)
for x in breaks:
if sentence[x - 1] != sentence[x + 1]:
return False
if breaks == [] and sentence[0] == sentence[length]:
return True
else:
return False

And on the sentence, 'a a ba' it returns true instead of false

dawn spireBOT
#

@tacit pivot

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

last warren
#

you understand what return does?

tacit pivot
#

yeah

#

it returns a value from the function

tacit pivot
last warren
#

return exits from the function immediately

tacit pivot
#

yeha

#

yeah

last warren
#

now, you have return in a loop

#

do you see the problem?

tacit pivot
#

oh shitt

#

thanks but why does it work for 264 problems then

#

btu on the 265th it doesnt work

last warren
#

you won't ever check more than one iteration

last warren
tacit pivot
#

yeah idk

#

thanks though I appreciate it

#

nevermind its still not working 😭

fair ermine
#

!e

def is_circular(string):
    words = iter(string.split())
    first = next(words)
    acc = first[-1]
    first = first[0]
    last = acc
    for word in words:
        last = word[-1]
        if word[0] != acc:
            return False
        acc = word[-1]
    if last == first:
        return True
    return False

print(is_circular("leetcode exercises sound delightful"))
print(is_circular("a a ba"))
dawn spireBOT
tacit pivot
#

class Solution(object):
def isCircularSentence(self, sentence):
length = len(sentence) -1
if sentence[0] == sentence[length]:
breaks = []
for i in range(length):
if sentence[i] == ' ':
breaks.append(i)
for x in breaks:
if sentence[x - 1] != sentence[x + 1]:
return False
if breaks == [] and sentence[0] == sentence[length]:
return True
else:
return False

fair ermine
tacit pivot
#

oh

#

well this is the error its returning from ur code

last warren
fair ermine
#

and the method name they want you to

tacit pivot
#

the first leetcode problem (hopefully of many)

fair ermine
#

!e

class Solution:
    def isCircularSentence(self, string):
        words = iter(string.split())
        first = next(words)
        acc = first[-1]
        first = first[0]
        last = acc
        for word in words:
            last = word[-1]
            if word[0] != acc:
                return False
            acc = word[-1]
        if last == first:
            return True
        return False

s = Solution()
print(s.isCircularSentence("leetcode exercises sound delightful"))  # true
print(s.isCircularSentence("eetcode"))  #true
print(s.isCircularSentence("leetcode eats soul"))  # true
print(s.isCircularSentence("Leetcode is cool"))  # false
print(s.isCircularSentence("happy Leetcode"))  # false
print(s.isCircularSentence("Leetcode"))  # false
print(s.isCircularSentence("I like Leetcode"))  # false
dawn spireBOT
dawn spireBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.