15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

6-12. Strings.<br />

a.<br />

b.<br />

c.<br />

Create a function called findchr(), with the following declaration:<br />

def findchr(string, char)<br />

findchr() will look for character char in string and return the index of the first<br />

occurrence of char, or -1 if that char is not part of string. You cannot use<br />

string.*find() or string.*index() functions or methods.<br />

Create another function called rfindchr() that will find the last occurrence of a<br />

character in a string. Naturally this works similarly to findchr(), but it starts its<br />

search from the end of the input string.<br />

Create a third function called subchr() with the following declaration:<br />

def subchr(string, origchar, newchar)<br />

subchr() is similar to findchr() except that whenever origchar is found, it is<br />

replaced by newchar. The modified string is the return value.<br />

6-13. Strings. The string module contains three functions, atoi(), atol(), and atof(), that<br />

convert strings to integers, long integers, and floating point numbers, respectively. As<br />

of <strong>Python</strong> 1.5, the <strong>Python</strong> built-in functions int(), long(), and float() can also<br />

perform the same tasks, in addition to complex(), which can turn a string into a<br />

complex number. (Prior to 1.5, however, those built-in functions converted only<br />

between numeric types.)<br />

An atoc() was never implemented in the string module, so that is your task here. atoc<br />

() takes a single string as input, a string representation of a complex number, e.g., '-<br />

1.23e+4-5.67j', and returns the equivalent complex number object with the given<br />

value. You cannot use eval(), but complex() is available. However, you can only use<br />

complex() with the following restricted syntax: complex(real, imag) where real and<br />

imag are floating point values.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!