All solutions were successfully tested on 18 April 2013.
def double_char(str): result = '' for char in str: result += char * 2 return result
def count_hi(str): count = 0 for i in range(len(str)-1): if str[i:i+2] == 'hi': count += 1 return count
def cat_dog(str): count_cat = 0 count_dog = 0 for i in range(len(str)-2): if str[i:i+3] == 'dog': count_dog += 1 if str[i:i+3] == 'cat': count_cat += 1 return count_cat == count_dog
def count_code(str): count = 0 for i in range(0, len(str)-3): if str[i:i+2] == 'co' and str[i+3] == 'e': count += 1 return count
def end_other(a, b): a = a.lower() b = b.lower() #return (b.endswith(a) or a.endswith(b)) return a[-(len(b)):] == b or a == b[-(len(a)):]
Either way is fine.
def xyz_there(str): for i in range(len(str)): if str[i] != '.' and str[i+1:i+4] == 'xyz': return True if str[0:3] == 'xyz': return True return False
For ‘cat_dog’, would you criticize this solution:
if str.count('cat') == str.count('dog'):
The Java String class does not have a method named count(), so your code wouldn’t work. However, assuming such a method exists, and does what you intend it to do, you could simply write “return str.count(‘cat’) == str.count(‘dog’)”.
Hmm, well I replied to your Python solutions–not the Java ones. Rather, my question centers around the usefulness of using Python’s built-in list methods, as opposed to writing something more ‘verbose’. After perusing your blog articles it seems that you have opinions about most things computer science-related, so I thought I would ask what you thought.
Please excuse the oversight. Yes, the String class in Python does have a method named count. In the real world you would normally use that method instead of rolling your own. However, the point of the Coding Bat exercises is to teach you basic programming skills, i.e. programmatic thinking, which is why Nick Parlante didn’t include the count() method in his overview of strings in Python.
count = 0
for i in range(len(str)): #I didn’t use “len(str)-1” here.
if str[i:i+2] == ‘hi’:
count += 1
Hi Gregor, I just had a quick question the code i just posted above also works for the count “hi” function. However, I noticed that you used len(str) -1. Can you please explain why the -1 would matter? I just want to have a clear understanding.
That’s because strings are zero-indexed in Java.
For cat_dog, why do we iterate till len(str) – 2 and not len(str) – 1. Please explain.
Have a look at the subsequent if-statements, and think about how they relate to the variable i being iterated in the for-loop.
I know it’s an old topic, but last exercise could be shortened to that:
return str.count(‘xyz’) > str.count(‘.xyz’)
Can you please explain in case value of both xyz and .xyz is 1, as in string “.xyzxyz” then why does it still return True?
Hey guys, I sometimes understand why this is the case, but in other situations..I don’t. When it comes to seeing if a string starts with a certain sequence of letters, I understand why it is done. But for the example count_hi and cat dog, I would appreciate some help!
why is it that you do
for i in range(len(str) – 2): or (-1) in the other case?
I know we are looking for the string ‘hi’, so why subtract one from the string? Is it because the last index is omitted in the range(len), so we only need to subtract 1 to get to -2?
Same with the -2 for cat dog, is this the same reason? -2 off of a string with the last character already omitted is -3 (the len of string cat or dog)?
if ‘.xyz’ in str:
str = str.replace(‘.xyz’, “”)
if ‘xyz’ in str:
This is a better solution than the one you gave for `xyz_there`
You’re not supposed to use the method ‘replace’.
I’ve got a shorter one
return str.count(“xyz”) > str.count(‘.xyz’)
That works, but it won’t teach you the skills CodingBat is intended to teach.
Very nice – but you don’t need to test for .xyz before replacing.
return ‘xyz’ in s.replace(‘.xyz’, ”)
I think, this will solve it quickly.
return True if str.count(“xyz”) – str.count(“.xyz”) else False
# Affix character ‘a’ in front of str
str = “a” + str
# The algorithm below (enclosed in the for loop) handles all cases
# except when “xyz” is the beginning pf str.
# So my thinking is, if I add another valid character in front so that this
# leading “xyz” is counted, then I avoid writing another if statement
for i in range(len(str) – 3):
if str[i] != ‘.’ and str[i + 1 : i + 4] == ‘xyz’:
Easier way for count_hi()
You’re using an in-built method, which defeats the purpose of this exercise.
return str.count(‘xyz’) > str.count(‘.xyz’)
The point of this exercise is not to use in-built methods.
Please can someone elaborate as to why the solution for xyz_there: does not give the out of range error?
Look up how string slicing in Python works.
For the xyz_there, why doesn’t it work when the second if statement is indented under the for statement?
This changes the program logic. Try tracing the execution.