Python is great, I think that the university should consider teaching it to first years in place of Java -- Aimaz
Contents |
ASCII Mapping
Write a program that will display the char associated with a specific int value. As a second part of the problem display the int value associated with a specific char.
Solutions
mapping=lambda x:{int:chr,str:ord}[x.__class__](x)
Compound Interest
Write a program that calculates the amount of interest paid on an investment, given the amount of capital, interest rate and number of years saved input at the keyboard by the user.
Solutions
Aimaz:
compound_interest=lambda p,r,n: p*(1+r)**n
Which One
Write a program that reads in two integer values and determines whether one is a multiple of the other.
Solutions
Aimaz:
is_multiple=lambda a,b:float(a)%b==0
String Class
Write a program where you can enter a string. Subsequently it should change the string to uppercase, lowercase and remove the first and last character from the string. You should only use the methods associated with the Java String class to achieve most aspects. Feel free to investigate additional aspects of the functionality within the String class.
Solutions
Ok, so we'll just use python string functions.
Aimaz:
# Let the user enter a string
input_string = raw_input("Your string: ")
Your string: Hello, World!
# Find out what methods we can use.
>>> dir(input_string)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
# Upper case
input_string.upper()
'HELLO, WORLD!'
# lower case
input_string.lower()
'hello, world!'
# remove first and last chars
input_string[1:-1]
'ello, World'
2 x 2
Write three programs that print out the two times table, first using a while loop, then using a do-while loop and lastly using a for loop. Which program is easiest to understand?
= Solutions
Aimaz:
# For loop
for n in xrange(1,11):
print n, "times 2 is", n*2
# Do while loop
>>> do
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'do' is not defined
# Do while loops are not possible in python for the time being http://mail.python.org/pipermail/python-dev/2006-February/060718.html
# While loop
n=0
while (n<11):
n += 1
print n, "times 2 is", 2*n
# For loop is nicer imo.
Positive, Negative or Zero
Write a program that reads in an integer value and outputs this is a positive integer if the integer is greater than zero, this is a negative integer if the integer is less than zero and this is zero otherwise.
Solutions
Aimaz:
pos_neg=lambda n:" ".join(["this is", {-1:"a negative number",0:"zero",1:"a positive number"}[n.__cmp__(0)]])