Difference between revisions of "CSC111 Exercises on Loops and Strings"
(Created page with '===Exercise 1=== What is output by the following code sections? for i in range( 5 ): for j in range( 3 ): print i+j, print for i in range( 3 ): pri…') |
|||
(7 intermediate revisions by the same user not shown) | |||
Line 27: | Line 27: | ||
====Exercise 3==== | ====Exercise 3==== | ||
+ | Use this [http://rgruet.free.fr/PQR24/PQR2.4.html Python Quick Refence] guide for this Exercise. | ||
+ | |||
+ | Given the list of string '''poem''' shown below, generate the code that prints it as shown. | ||
+ | |||
+ | poem = [ "If the person you are talking to", "doesn't appear to be listening," , \ | ||
+ | "be patient. ", "It may simply be that he has", "a small piece of fluff in his ear." ] | ||
+ | |||
+ | |||
+ | If the person you are talking to | ||
+ | Doesn't appear to be listening, | ||
+ | Be patient. | ||
+ | It may simply be that he has | ||
+ | A small piece of fluff in his ear. | ||
+ | |||
+ | <center> | ||
+ | If The Person You Are Talking To | ||
+ | Doesn't Appear To Be Listening, | ||
+ | Be Patient. | ||
+ | It May Simply Be That He Has | ||
+ | A Small Piece Of Fluff In His Ear. | ||
+ | </center> | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | ===Solutions=== | ||
+ | <code><pre> | ||
+ | def main(): | ||
+ | for i in range( 5 ): | ||
+ | for j in range( 3 ): | ||
+ | print i+j, | ||
+ | print | ||
+ | |||
+ | for i in range( 3): | ||
+ | print '-' * i | ||
+ | |||
+ | for i in range( 4 ): | ||
+ | for j in range( i ): | ||
+ | print j * 'o' | ||
+ | |||
+ | # 1 1 | ||
+ | # 0 5 0 5 | ||
+ | s = "jan31feb28mar31apr30" | ||
+ | for i in range( 0, len( s ), 5 ): | ||
+ | #print s[i] | ||
+ | month = s[i:i+3] | ||
+ | days = s[i+3:i+5] | ||
+ | days = int( days ) | ||
+ | print month, | ||
+ | for d in range( days ): | ||
+ | print d+1, | ||
+ | print | ||
+ | |||
+ | poem = [ "If the person you are talking to", \ | ||
+ | "doesn't appear to be listening," , \ | ||
+ | "be patient. ", "It may simply be that he has", \ | ||
+ | "a small piece of fluff in his ear." ] | ||
+ | for line in poem: | ||
+ | #line = line.capitalize() | ||
+ | words = line.split() | ||
+ | newLine = "" | ||
+ | for word in words: | ||
+ | newLine = newLine + ' ' + word.capitalize() | ||
+ | newLine = newLine[1:] # get rid of first space | ||
+ | newLine = newLine.center( 80 ) | ||
+ | print newLine | ||
+ | |||
+ | main() | ||
+ | </pre></code> | ||
+ | [[Category:CSC111]][[Category:Exercises]] |
Latest revision as of 11:12, 19 February 2010
Exercise 1
What is output by the following code sections?
for i in range( 5 ): for j in range( 3 ): print i+j, print
for i in range( 3 ): print '-' * i
for i in range( 4 ): for j in range( i ): print j * '-'
Exercise 2
Write the code necessary to take the string "jan31feb28mar31apr30" and generate the following output from it:
jan 1 2 3 4 5 6 7 8 9 10 11 ... 27 28 29 30 31 feb 1 2 3 4 5 6 7 8 9 10 11 ... 27 28 mar 1 2 3 4 5 6 7 8 9 10 11 ... 27 28 29 30 31 apr 1 2 3 4 5 6 7 8 9 10 11 ... 27 28 29 30
Exercise 3
Use this Python Quick Refence guide for this Exercise.
Given the list of string poem shown below, generate the code that prints it as shown.
poem = [ "If the person you are talking to", "doesn't appear to be listening," , \ "be patient. ", "It may simply be that he has", "a small piece of fluff in his ear." ]
If the person you are talking to Doesn't appear to be listening, Be patient. It may simply be that he has A small piece of fluff in his ear.
If The Person You Are Talking To Doesn't Appear To Be Listening, Be Patient. It May Simply Be That He Has A Small Piece Of Fluff In His Ear.
Solutions
def main():
for i in range( 5 ):
for j in range( 3 ):
print i+j,
print
for i in range( 3):
print '-' * i
for i in range( 4 ):
for j in range( i ):
print j * 'o'
# 1 1
# 0 5 0 5
s = "jan31feb28mar31apr30"
for i in range( 0, len( s ), 5 ):
#print s[i]
month = s[i:i+3]
days = s[i+3:i+5]
days = int( days )
print month,
for d in range( days ):
print d+1,
print
poem = [ "If the person you are talking to", \
"doesn't appear to be listening," , \
"be patient. ", "It may simply be that he has", \
"a small piece of fluff in his ear." ]
for line in poem:
#line = line.capitalize()
words = line.split()
newLine = ""
for word in words:
newLine = newLine + ' ' + word.capitalize()
newLine = newLine[1:] # get rid of first space
newLine = newLine.center( 80 )
print newLine
main()