-
-
Save jporcelli/fa797f547b029bbe803c to your computer and use it in GitHub Desktop.
#hello han | |
Problem 2: Write a function - sub count - that takes two arguments, a | |
string (s) and a substring (sub) , and returns the number of times sub | |
occurs in s. | |
For example: | |
x=‘you say hello and I say hello and they all say hello’ | |
print(subcount(x,’hello’)) should print 3 | |
print(subcount(x,’goodbye’)) should print 0 | |
(Note: There is a python string function count that does the same | |
thing. But please don’t use it for the purpose of this exercise!) | |
def sub_count(s, sub): | |
count = 0 | |
location = 0 | |
while location < len(s): | |
if (s[location:].find(sub) >= 0): | |
count += 1 | |
location += s[location:].find(sub) + len(sub) | |
else: | |
break | |
return count | |
x = 'you say kkhellojj and I say hello and they all say hello' | |
print(sub_count(x, 'hello')) | |
def ss(str, substr): | |
words = str.split() | |
occurences = 0 | |
for word in words: | |
if word == substr: | |
occurences = occurences + 1 | |
return occurences | |
#Alternative solution | |
def sub_count2(s,sub): | |
return len(s.split(sub))-1 | |
x='you say hello and I say hello and they all say hello' | |
#x.split('hello') will return {"you say ", " and I say ", " and they all say "} | |
print(sub_count(x,'hello')) | |
def sub_count(s,sub): | |
count=0 | |
location = 0 | |
while location < len(s): | |
if (s[location:].find(sub) > 0): | |
count+=1 | |
location+= s[location:].find(sub) + len(sub) | |
else: | |
break | |
return count | |
x='you say kkhellojj and I say hello and they all say hello' | |
print(sub_count(x,'hello')) | |
def sub_count2(s,sub): | |
return len(s.split(sub))-1 | |
x='you say hello and I say hello and they all say hello' | |
print(x.split('hello')) | |
print(sub_count2(x,'hello')) | |
def straighten_name(name_string): | |
ln,fn=name_string.split(', ') | |
return fn+' '+ln | |
def straighten_name(name_string): | |
if name_string.find(',') < 0 : | |
return name_string | |
ln,fn=name_string.split(', ') | |
return fn+' '+ln | |
def straighten_name(name_string): | |
if name_string.find(',') < 0 : | |
return name_string | |
names=name_string.split(', ') | |
return names[1]+' '+names[0] | |
name1="Who, Doctor" | |
print(straighten_name(name1)) | |
name2="mary jane" | |
print(straighten_name(name2)) # this produces an error but ur professor said to handle it if you cant do it so i dont agree with his solutuon | |
haha hannys no github
remeber i was just telling you about how chinese try to ddos github
yeah
There's one problem I don't understand:
Problem 3: Write a function - straighten_name - that takes one
argument, a string containing a name in the Last_Name, First_Name
format. The function should parse the string and return a string in
the form First_Name Last_Name. If it can’t do this, then it should
merely return the original string.
For example:
name1=“Who, Doctor”
name2=“William Dalrymple”
print(straighten_name(name1)) should print Doctor Who
print(straighten_name(name2)) should print William Dalrymple
Solution from professor:
def straighten_name(name_string):
ln,fn=name_string.split(', ')
return fn+' '+ln
name1="Who, Doctor"
print(straighten_name(name1))
hello jimmy