All Articles

python-practice2

1. The formataddress function separates out parts of the address string into new strings: housenumber and street_name, and returns: “house number X on street named Y”. The format of the input string is: numeric house number, followed by the street name which may contain numbers,but never by themselves, and could be several words long. For example, “123 Main Street”, “1001 1st Ave”, or “55 North Center Drive”. Fill in the gaps to complete this function.

  # Declare variables
  house_number =''
  street_name =''
  # Separate the address string into parts
  spi = address_string.split()
  # Traverse through the address parts
  for ele in spi:
    # Determine if the address part is the
    # house number or part of the street name
    if ele.isdigit():
      house_number = ele
    else:
      street_name += ele
      street_name += ' '
  # Does anything else need to be done 
  # before returning the result?
  
  # Return the formatted string  
  return "house number {} on street named {}".format(house_number, street_name)

2. Question 2

A professor with two assistants, Jamie and Drew, wants an attendance list of the students, in the order that they arrived in the classroom. Drew was the first one to note which students arrived, and then Jamie took over. After the class, they each entered their lists into the computer and emailed them to the professor, who needs to combine them into one, in the order of each student’s arrival. Jamie emailed a follow-up, saying that her list is in reverse order. Complete the steps to combine them into one list as follows: the contents of Drew’s list, followed by Jamie’s list in reverse order, to get an accurate list of the students as they arrived.

def combine_lists(list1, list2):
  # Generate a new list containing the elements of list2
  # Followed by the elements of list1 in reverse order
  new_list = list2
  for i in reversed(range(len(list1))):
    new_list.append(list1[i])
  return new_list

Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]

3. Use a list comprehension to create a list of squared numbers (n*n). The function receives the variables start and end, and returns a list of squares of consecutive numbers between start and end inclusively. For example, squares(2, 3) should return [4, 9].

def squares(start, end):
	return [ (i*i) for i in range(start,end+1) ]

print(squares(2, 3)) # Should be [4, 9]
print(squares(1, 5)) # Should be [1, 4, 9, 16, 25]
print(squares(0, 10)) # Should be [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]