2017-04-24 Pair Programming

posted Apr 24, 2017, 5:35 AM by Samuel Konstantinovich   [ updated Apr 24, 2017, 10:34 AM ]
Classwork: This is meant to be started in class and finished at home, you should have it working by Wednesday morning. (You have 2 dojo days and 2 nights to make it work)

Homework: Submit these 3 questions on CodingBat.


Refresh your memory on python lists here:

PAIR PROGRAMMING! One keyboard/computer, one of you drives the problem solving, the other is typing. Every 8 minutes swap! 


note: that these functions do not return copies! They change the list. If you print the functions the output will be 'None'.

1. Write a function:   removeValuesFromXtoYList(L,x,y)  that will modify the list such that any values n in the list will, such that x < n < y, will be removed. Assume x<=y. (You may use the built in methods to take out the value, as that is a small part of this problem. Read the list method page to help out.)

bigHint: if you remove the first element, does the list have a first element now? What is it? Where did it come from?

Examples:
L=[ 1, 3, -5, 10, -2, 0, -6.0, 0 , 1]
removeValuesFromXtoYList(L,-3,3)  
print L # would print [ 3, -5, 10, -6.0]

L=[ 1, 3, -5, 10, -2, 0, 2, 2, 3, -6.0]
removeValuesFromXtoYList(L,0,10)  
print L # would print [ -5, 10, -2, 0, -6.0]

2. Write a function moveNegativeToEnd(L) that takes a list as a parameter. It moves all negative values to the end of the list. All non-negative values stay in the same relative order, and all negative values stay in the same relative order. The precondition is that all values in L are real numbers. You can use any list methods you find, as well as slices. I strongly urge you to look at the methods! 

BIG hint: If you get an infinite loop, what happens when you test it on [-1, -2] ? Trace through your code and see.

Examples:
x=[ 1, 3, -5, 10, -2, 0, -6.0]
moveNegativeToEnd(x)  
print x # would print [ 1, 3, 10, 0, -5, -2, -6.0]

y=[0, 1 , -3, 4.5]
moveNegativeToEnd(y) 
print y #would print out:  [ 0, 1, 4.5, -3]

3. Write a function reverseWordsWithCapitals(L) that takes a list of strings. If any of the letters in a string are capital letters, that string should be replaced with the reversed string. Before you write this function, think about a good way to break it up. Strings with no capital letters are left alone. The order of the strings should not change. 
Example:
x = ['fish','Hey!','oOps','this is Sparta!']
reverseWordsWithCapitals( x ) 
print x # would print ['fish','!yeH','spOo','!atrapS si siht']
Comments