Will it help in this problem to convert the string into a list? - python-3.x

I have a question. I am not looking for the answer to this exercise, just a pointer. My question is: will this be easier to solve if the two-word string is converted to a List?
ANIMAL CRACKERS: Write a function takes a two-word string and returns True if both words begin with same letter
animal_crackers('Levelheaded Llama') --> True
animal_crackers('Crazy Kangaroo') --> False

Yes, in this problem it would help to use the .split() method to split the string into a list of the two words.
Generally, the same data can be represented in different ways, and you want to use a representation which aligns with what you need to do with the data. Since this problem is about words, then "a list of words" aligns closer with what you need to do than "a string with two words" does.
Having a list of words allows you to write code that refers to "the first word" (i.e. words[0]) and "the second word" (i.e. words[1]). There is no comparably-simple way to refer to individual words in the original string.

Thanks everyone. I realised quite soon after i posted that the .split() function was what i needed to use. This was my solution.
def animal_crackers(string):
twoWords = (string.split(' '))
if twoWords[0][0].upper() == twoWords[1][0].upper():
return True

Do a split on the string input myListOfTwoSriting = stringInput.split(" "), next split the 2 string link that : firstletter = myListOfTwoSriting [0].split("")[0] then firstletterOfSeconde = myListOfTwoSriting [1].split("")[0] for the seconde.
next:
if firstletter == firstletterOfSeconde :
return True
else:
return False

Related

The two strings as inputs. Check whether one string is a substring of another. Ignore the uppercase/lowercase differences

The two strings as inputs. Check whether one string is a substring of
another. Ignore the uppercase/lowercase differences.
It is an easy question,
largerString = "This is the larger string not the smaller string"
smallerString = "Smaller String"
if largerString.lower().find(smallerString.lower()) != -1:
print("true")
else:
print("false")
You can take the largerString and the smallerString as inputs.
It basically converted them both into lower case and checks if
the lower string is contained within the largerString by trying
to find it, if it finds it then it is contained else it is not.

Looking through a string but avoiding substring

I need to check if a string appear in a text. I want to avoid substring.
string = "one"
text = "bees make honey
if string in text
This return True of course. How do i avoid this issue?
Well, "one" does appear in "bees make honey", as a substring.
But if you want to see if "one" is a word or not, you can use the split() function.
By words, I mean bees, make, and honey.
Sample python implementation:
l = parentString.split(' ') # Returns ['bees','make','honey']
if childString in l:
print('Word found in parent text')
else:
print('Word not found in parent text')

Remove part of string (regular expressions)

I am a beginner in programming. I have a string for example "test:1" and "test:2". And I want to remove ":1" and ":2" (including :). How can I do it using regular expression?
Hi andrew it's pretty easy. Think of a string as if it is an array of chars (letters) cause it actually IS. If the part of the string you want to delete is allways at the end of the string and allways the same length it goes like this:
var exampleString = 'test:1';
exampleString.length -= 2;
Thats it you just deleted the last two values(letters) of the string(charArray)
If you cant be shure it's allways at the end or the amount of chars to delete you'd to use the version of szymon
There are at least a few ways to do it with Groovy. If you want to stick to regular expression, you can apply expression ^([^:]+) (which means all characters from the beginning of the string until reaching :) to a StringGroovyMethods.find(regexp) method, e.g.
def str = "test:1".find(/^([^:]+)/)
assert str == 'test'
Alternatively you can use good old String.split(String delimiter) method:
def str = "test:1".split(':')[0]
assert str == 'test'

How to check if two strings can be made equal by using recursion?

I am trying to practice recursion, but at the moment I don't quite understand it well...
I want to write a recursive Boolean function which takes 2 strings as arguments, and returns true if the second string can be made equal to the first by replacing some letters with a certain special character.
I'll demonstrate what I mean:
Let s1 = "hello", s2 = "h%lo", where '%' is the special character.
The function will return true since '%' can replace "el", causing the two strings to be equal.
Another example:
Let s1 = "hello", s2 = "h%l".
The function will return false since an 'o' is lacking in the second string, and there is no special character that can replace the 'o' (h%l% would return true).
Now the problem isn't so much with writing the code, but with understanding how to solve the problem in general, I don't even know where to begin.
If someone could guide me in the right direction I would be very grateful, even by just using English words, I'll try to translate it to code (Java)...
Thank you.
So this is relatively easy to do in Python. The method I chose was to put the first string ("hello") into an array then iterate over the second string ("h%lo") comparing the elements to those in the array. If the element was in the array i.e. 'h', 'l', 'o' then I would pop it from the array. The resulting array is then ['e','l']. The special character can be found as it is the element which does not exist in the initial array.
One can then substitute the special character for the joined array "el" in the string and compare with the first string.
In the first case this will give "hello" == "hello" -> True
In the second case this will give "hello" == "helol" -> False
I hope this helps and makes sense.

Trying to understand a Python line code

I am new to python, and when I search for a way to get a string length without using "len()", I found this answer:
sum([1 for _ in "your string goes here"])
Can someone help me understand this line,what's the '1' doing there for example?
This is basically equivalent to this:
lst = []
for dontCareAboutTheName in "your string goes here":
lst.append(1)
print(sum(lst))
The list comprehension basically collects the number 1 for each character it finds while looping through the string. So the list will contain exactly as many elements as the length of the string. And since all those list elements are 1, when calculating the sum of all those elements, you end up with the length of the string.

Resources