Write a program to take in 10 integers from the input, and display the 10 integers in increasing order

Here, we want to build up a list, and then sort it. We'll use the sorted function

In [1]:
def get_10_ints():
    '''Take in 10 integers from input, store them in a list, and return the list
    '''
    ints = []  #empty initially
    for i in range(10):
        new_int = int(input("Give me an integer: "))
        ints.append(new_int)
    return ints


ints = get_10_ints()
print(sorted(ints))
Give me an integer: 4
Give me an integer: 5
Give me an integer: 10
Give me an integer: 8
Give me an integer: 3
Give me an integer: 2
Give me an integer: 10
Give me an integer: 2
Give me an integer: -5
Give me an integer: 3
[-5, 2, 2, 3, 3, 4, 5, 8, 10, 10]

Write a program to first take in 3 integers from the input, store them in a list, then double every integer in the list, then print the list

In [3]:
def get_N_ints(N):
    '''Take in N integers from input, and then sort them
    '''
    ints = []
    for i in range(3):
        new_int = int(input("Give me an integer: "))
        ints.append(new_int)

    return ints

ints = get_N_ints(3)
for i in range(len(ints)):
    ints[i] *= 2  #the same as ints[i] = ints[i] * 2
print(ints)
Give me an integer: 3
Give me an integer: 4
Give me an integer: 10
[6, 8, 20]

Suppose we have a list of measurements. Write a program to find the two largest measurements. First, used sort() or sorted(). Then, write the program while avoiding sort() and sorted()

In [5]:
M = [10, 2, 20, 3]
print(sorted(M)[-2:])  #Done! 

M = [10, 2, 20, 3]
M.sort()
print(M[-2:])
[10, 20]
[10, 20]

Now, let's try to not use sort/sorted. The idea is to keep track of the largest and the second-largest measurement that we saw so far.

In [6]:
M = [10, 2, 20, 3]


#First, let's set up the initial values. The idea is to use the first two.
if M[0] > M[1]:
    largest, second_largest = M[0], M[1]
else:
    largest, second_largest = M[1], M[0]
    
#Now, we just go through the entire list

for i in range(2, len(M)):
    #Three cases: 
    #1. M[i] is the new largest number
    #2. M[i] is the new second-largest number
    #3. M[i] is neither
    
    if M[i] > largest:
        largest, second_largest = M[i], largest
    elif M[i] > second_largest:
        second_largest = M[i]
        
print(second_largest, largest)
            
        
10 20

Write a function that takes in a string containing the patient's description of their symptoms, and outputs "your nose is runny" if the string contains "runny nose", "your eye is runny" if the string contains "runny eye" etc. That is, the function finds the word that comes after runny, and uses that for the output.

OPTIONAL The easiest thing to do is to use str.split together with list.index. That requires knowing about index. Let's use a bit of acrobatics to do this in one line

In [9]:
def what_is_runny(sympt):
    words = sympt.lower().split()
    return "Your " + words[words.index("runny") + 1] + " is runny"
In [10]:
what_is_runny("Doctor, I have a runny foot")
Out[10]:
'Your foot is runny'

Now, let's do this in a way that's helpful for the next exercise. We'll find the index where "runny" starts using str.find, and then we'll find the the index where the next word ends. In order to facilitate that, we'll replace all punctuation with spaces.

In [12]:
def what_is_runny2(sympt):
    sympt = sympt.lower()
    for punc in "!.,;:?":
        sympy = sympy.replace(punc, " ")  #replace every puncutation sign with a space
        
    ind_runny = sympy.find("runny")
    
    #Now, we know that whatever is runny will start at index sympt[ind_runny+len("runny")+1] (the + 1 is for the space)
    ind_bodypart = ind_runny + len("runny") + 1
    
    
    ind_bodypart_end = sympy.find(" ", ind_bodypart)  #find the space that follows the name of the body part. There's
                                                      #definitely a space, since we got rid of all 
                                                      #punctuation
            
    return "Your " + sympt[ind_bodypart:int_bodypart_end] + " is runny"
In [13]:
what_is_runny("Doctor, I have a runny foot")
Out[13]:
'Your foot is runny'
In [ ]: