Last active
August 29, 2015 14:18
-
-
Save jporcelli/fa797f547b029bbe803c to your computer and use it in GitHub Desktop.
han yin python midterm practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 | |
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))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yeah