Main.Lab4 History

Hide minor edits - Show changes to markup

February 14, 2013, at 03:29 PM MST by 129.82.44.223 -
Changed line 1 from:

Lab 3

to:

Lab 4

February 12, 2010, at 12:58 PM MST by 10.84.44.99 -
Added lines 39-53:

String reversal

Write a function called reverse that reverses a string. Here are some examples of the output your function should produce:

(:source lang=python:) >>> reverse('happy') 'yppah' >>> reverse('Python') 'nohtyP' >>> reverse("") '' >>> reverse("P") 'P' (:sourceend:)

February 12, 2010, at 12:54 PM MST by 10.84.44.99 -
Changed line 23 from:

Substring counting

to:

Substring counting

February 12, 2010, at 12:51 PM MST by 10.84.44.99 -
Added lines 1-38:

Lab 3

Objectives:

  • Practice with strings and loops

Palindrome detection

Write a function called is_palindrome that determines if a given string is a palindrome, i.e. is the same as its reverse. Here are some examples:

(:source lang=python:) >>>is_palindrome('abba') True >>> is_palindrome('abab') False >>> is_palindrome('tenet') True >>> is_palindrome('banana') False >>> is_palindrome('straw warts') True (:sourceend:)

Substring counting

Write a function called count(s, substr) that returns how many times the string substr occurs in s. Here are some examples of the output your function should produce:

(:source lang=python:) >>> count('is', 'Mississippi') 2 >>> count('an', 'banana') 2 >>> count('ana', 'banana') 2 >>> count('nana', 'banana') 1 >>> count('nanan', 'banana') 0 (:sourceend:)