liveBook · Manning

Name mashup

As explained in Get Programming: Learn to code with Python

A name mashup is a constructed name formed by combining parts of two or more existing names.

A name mashup combines parts of two existing names to create a new first name and last name. A typical task asks the user for two names in FIRST LAST format and displays two possible results. section 12.0

Build the solution by turning the open-ended idea into smaller steps: identify the inputs and output, sketch the process, create test examples, and write pseudocode before coding. section 12.1

One simple mashup method is:

  • Separate each full name into its first and last name. section 12.1
  • Find the midpoint of each name by measuring its length and dividing by two. section 12.3
  • Take the first half of one name and the second half of the other, then repeat for the last names. section 12.3
  • Use integer conversion when a name has an odd number of characters, rounding the midpoint down. section 12.3
  • Test short, long, even-length, odd-length, repeated-letter, and duplicate names. section 12.1

Alice Cat and Bob Dog

Combining the first part of Alice with the second part of Bob, and the first part of Cat with the second part of Dog, can produce the mashup “Bolice Dot.” The exact result depends on the chosen splitting method. section 12.0

Figure 12.1. Drawing the inputs to the program, a sample mashup of the input names, and the output shown to the user

Trace the input names, the half-name combinations, and the output in one visual overview. section 12.1

Figure 12.3. Two ways to mash up Aqua Man and Cat Woman. You take half of each of the first names and combine them. Then you take half of each of the last names and combine them.

Compare two ways to combine halves from Aqua Man and Cat Woman, including the treatment of odd-length names. section 12.3

Listing 12.3. Storing halves of each name
len_name1_first = len(name1_first)                                       #1
len_name2_first = len(name2_first)                                       #1
len_name1_last = len(name1_last)                                         #1
len_name2_last = len(name2_last)                                         #1
index_name1_first = int(len_name1_first/2)                               #2
index_name2_first = int(len_name2_first/2)                               #2
index_name1_last = int(len_name1_last/2)                                 #2
index_name2_last = int(len_name2_last/2)                                 #2

lefthalf_name1_first = name1_first[0:index_name1_first]                  #3
righthalf_name1_first = name1_first[index_name1_first:len_name1_first]   #4
lefthalf_name2_first = name2_first[0:index_name2_first]
righthalf_name2_first = name2_first[index_name2_first:len_name2_first]

lefthalf_name1_last = name1_last[0:index_name1_last]
righthalf_name1_last = name1_last[index_name1_last:len_name1_last]
lefthalf_name2_last = name2_last[0:index_name2_last]
righthalf_name2_last = name2_last[index_name2_last:len_name2_last]

Inspect the Python variables and slices used to calculate and store each name half. section 12.3

How Get Programming: Learn to code with Python covers it

How deep it goes
Explains planning, test design, midpoint calculation, integer casting, and string slicing, but stops short of advanced letter detection.
Examples
Uses Alice Cat and Bob Dog, Aqua Man and Cat Woman, and varied-length or repeated names as examples.
What it assumes
Assumes basic Python knowledge of strings, lengths, indices, slicing, integer casting, and user input.
What you can do after
Build it from scratch by gathering two names, splitting them, combining their halves, and testing the resulting output.
How it connects
Connects name mashups with user input, strings, indexing, integer casting, pseudocode, and testing.

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.