Create a file lists-practice.py
in your cs100/ch4
folder.
myString = 'abc'
myString[0] = 'd'
print(myString)
myList = ['a', 'b', 'c']
myList[0] = 'd'
print(myList)
k1
, k2
, and k3
? Write in comments.
s = "I like apples"
k1 = s.split()
k2 = s.split('a')
k3 = s.split('e')
Split the string "mississippi"
into a list uing the 'i'
as a split point. Print the result.
numWords(sentence)
which takes a string as a parameter and returns the number of words in the given string. Call your function to test it works. For example,
print( numWords("the quick brown fox") ) # will print 4
makeString(myList)
that takes a list of words as a parameter and returns one big string consisting of all words in the list separated by spaces. Call your function to test it works. For example,
print( makeString(["the", "quick", "brown", "fox"]) ) # will print "the quick brown fox"
checkIn(myList, key)
that works similarly to the containment operator (in
), and call it to check that it works. It should operate by way of iterating through the list, comparing the given key to each item in the list, and if there is a match return true; otherwise, if it iterates over the entire list and does not find it, it returns false. For example,
print( checkIn( ['a','b','c','d','e'], 'd') ) # will print True
print( checkIn( ['a','b','c','d','e'], 'f') ) # will print False
mean(mylist)
that takes a list of numbers and returns the mean (average). Call it and test your function. For example,
grades = [100, 90, 80, 90, 95]
print( mean(grades) ) # will print 91.0
median(mylist)
that takes a list of numbers in any order and returns the median (the middle of the sorted list). If the list length is even, the median is the average of the two middle numbers. Test your function by calling it.For example,
print("Median of first list: ", median( [12, 3, 5, 7, 1, 9] ) ) # prints 6.0
test_even = [12, 3, 5, 7, 1, 9, 10]
print("Median of second list: ", median(test_even)) # prints 7
Write a function shuffle(myList)
that takes a list as a parameter and returns a new list with the elements shuffled in a random order. Hint: create a copy of myList, then repeatedly remove elements at random indicies of the copy list to put in the new list. It will help to use random.randint(start,end)
from the random
module which will give you a random integer between [start, end).
partialEncrypt(myString)
that takes a string as a parameter and returns a new string with all words shuffled in a random order. Hint: use the shuffle
function you wrote earlier. For example, it might look something as follows:
print( partialEncrypt("the quick brown fox")) # might print "quick fox the brown"
anagram(myString)
that takes a string as a parameter and returns a new string with all the words and letters shuffled in a random order. Hint: use the shuffle
function you wrote earlier. For example, it might look as follows:
print( anagram("the_quick_brown_fox")) # might print "o_ectquorhfxwkb_i_n"
Write a function called indexOf(myList, key)
that works similar to (but does not use) index
. It should return the index where key belongs in myList, or -1 if it is not in the list.
Submit your working python file to Moodle.