Exercise 1. create a function that takes a character as an argument, if it is 'a' then return 'A' otherwise return the original character. f('a') = 'A' and f(x) = x for all other x.
def f(x):
if x == 'a':
up = 'A'
return up
else:
return x
list = []
for i in range(0, len(word)):
letter = list.append(f(word[i]))
print list
map(f, list)
"".join(list)
Exercise 2. Sum the first 100 numbers in a for loop.
num = 0
for i in range(1, 101):
num = num + i
print num
Sum the first 100 numbers, but using a generator expression.
sum(x for x in range(1,101))
Using a generator expression sum the square of the first 100 numbers.
sum(x**2 for x in range(1,101))
Exercise 3. Let's create the Fibonacci sequence. Start a list $Fib$ of where the first elements are 1,1, and follow the relation that $$Fib[i] = Fib[i-1] + Fib[i-2]$$ Generate the Fibonacci numbers less than 1000 and store it in a list.
list = [1,1]
def fib(x):
if x == 0:
return 0
elif x == 1:
return 1
else:
x = fib(x - 1) + fib(x - 2)
return x
i = 3
while fib(i) < 1000:
y = fib(i)
letter = list.append(y)
i = i + 1
Fib = list
print Fib
Exercise 4. Create a function called addone that does the following depending on the type of the input.
def addone(x):
if type(x) == int:
x = x + 1
return x
elif type(x) == float:
x = x + 1.0
return x
else:
list = []
for i in range(0, len(x)):
letter = list.append(x[i])
y = ord(x[len(x) - 1])
y = y + 1
list.remove(x[len(x) - 1])
list.append(chr(y))
return list
print addone('bonfire'), addone(1), addone(3.2)
Exercise 5. Create a function called commarem that removes all commas from a string. Try to make it so that it will run faster if there is no comma in it. Below is a timing of these two different inputs.
def commarem(x):
list = []
for i in range(0, len(x)):
letter = list.append(x[i])
for i in range(0, len(x)):
if x[i] == ',':
list.remove(x[i])
new = "".join(list)
return new
%timeit commarem('1300')
%timeit commarem('1,300')
Exercise 6. Create a set of the 5 lower case vowels and the set of 21 lower case consonants by removing the set of vowels from the alphabet.
vowels = ['a', 'e', 'i', 'o', 'u']
consanants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']
Create a function called newletters that takes a string, s, and returns a list of strings formed by appending a new character to s, with the provision that that character must be a vowel if the last letter of s is a consonant and must be a consonant if the last letter of s is a vowel.
def newletters(s):
list = []
for i in range(0, len(s)):
letter = list.append(s[i])
new = "".join(list)
if s[len(s) - 1] == 'a' or s[len(s) - 1] == 'e' or s[len(s) - 1] == 'i' or s[len(s) - 1] == 'o' or s[len(s) - 1] == 'u':
list2 = []
for i in range(0, len(consanants)):
letter2 = list2.append(new)
letter2 = list2.extend(consanants[i])
list3 = []
for i in range(0, len(list2), 2):
letter3 = list3.append(list2[i] + list2[i+1])
else:
list2 = []
for i in range(0, len(vowels)):
letter2 = list2.append(new)
letter2 = list2.extend(vowels[i])
list3 = []
for i in range(0, len(list2), 2):
letter3 = list3.append(list2[i] + list2[i+1])
return list3
newletters('banan')
words = ['hamburge','chicke','hotdog','rubarb','tahini']
For the words list above, using a generator expression, create a dictionary with the keys being the elements of words and the items being newletters applied to the key.
newwords = dict([('hamburge', newletters('hamburge')), ('chicke', newletters('chicke')), ('hotdog', newletters('hotdog')), ('rubarb', newletters('rubarb')),, ('tahini', newletters('tahini')),])
newwords['hotdog']