Home > Software engineering >  How can I manipulate 2 lists to make 2 new lists?
How can I manipulate 2 lists to make 2 new lists?

Time:12-07

I have instructions to: A little kid has been given directions on how to get to school from his house. Unfortunately he lost the paper that tells him how to get home from school. Being that you are such a nice person, you are going to write a program to help him.

Suppose his mother gave him a note that said the following:

R

JOHN

L

KING

L

SCHOOL

this means he turned right on john, left on king, and left to school. To get the new list I need to output:

R

KING

R

JOHN

L

HOME

this means he turned right on king, right on john, and left to home. The input for the program consists of the direction and the street to turn onto.

The direction is entered first as L or R The name of the street is entered next on a separate line of input The input keeps going until SCHOOL is entered as the street name

MY QUESTION: What I understand is that I need 4 lists. I also need to be able to check if R or L is to be printed for the directions home since the directions aren't opposites of each other like R=L or L=R in the new output. But how can I check this? Also, if school can't be an input since the program is going to break, how is the first instruction from the kid going to school going to be entered? I'm really confused. This is all of my code right now..

     while True:
       direction= input("Enter the directions for all three streets (L or R):")
       street= input("Enter all three street names for the L/R directions in order:\n")
       streets= street.split()
       if streets[0] or streets[1] or streets[2] == "school" or streets[0] or streets[1] or streets[2] =="SCHOOL":
          break
  #original two lists
     directions= direction.split()
     print(directions)
     print(streets)
  #new list:        

CodePudding user response:

directions = []
streets = []

while True:
    direction = input("Enter the directions for all three streets (L or R):")
    street = input("Enter all three street names for the L/R directions in order:\n")
    directions.append(direction)
    streets.append(street)
    if street.lower() == "school":
        break
for (street, direction) in list(zip(streets, directions))[::-1]:
    if street.lower() != "school":
        print(street.upper())
    if direction.upper() == "L":
        print("R")
    else:
        print("L")
print("HOME")

CodePudding user response:

Assuming that the inputs are correctly formatted and seperated by spaces:

while True:
    direction= input("Enter the directions for all three streets (L or R):")
    street= input("Enter all three street names for the L/R directions in order:\n")
    directions = directions.split()[::-1]
    streets = street.split()[::-1]
    # above reverses the lists since we are now going in the opposite direction
    # also remove the first entry from streets "SCHOOL" and add "HOME"
    streets = streets[1:]   ["HOME"]
    # then you just need to output the results:
    for a,b in zip(directions, streets):
        if a == "R":
            print("L")
        else:
            print("R")
        print(b)

zip makes it possible to iterate multiple iterables at the same time it would be roughly you used the range function and used the index to access both items like this:

for i in range(len(directions)):
    a = directions[i]
    b = streets[i]
    ...

If you wanted the user to input the data instruction by instruction and breaking once it sees the "SCHOOL" input then it could look something like this:

directions, streets = [], []
while True:
    street = input("Enter street")
    direction = input("Enter Direction")
    directions.append(direction)
    streets.append(street)
    if street == "SCHOOL":
        break

directions = directions[::-1]
streets = streets[::-1][1:]   ['HOME']
for a,b in zip(directions, streets):
    if a == "L":
        print("R")
    else:
        print("L")
    print(b)

CodePudding user response:

This builds a list starting at HOME, adds the directions to get to SCHOOL, then reverses them replacing the L/R turns:

# start at home
directions = ['HOME']

# add turn and street until arrive at school.
# Note that the instructions say: "The direction is entered first as L or R
# The name of the street is entered next on a separate line of input The input
# keeps going until SCHOOL is entered as the street name", so you can't
# ask for all the directions on one line.
while True:
    d = input('Direction (L or R)? ').upper()
    s = input('Street name? ').upper()
    directions.append(d)
    # break if at school and don't add it to the directions
    if s == 'SCHOOL':
        break  # exit the while loop
    directions.append(s)

# Reverse the directions and replace L/R with R/L.
# This construction is called a "list comprehension".
# It builds a new list by calculating new values from the
# original list.
new_directions = ['R' if item == 'L' else 'L' if item == 'R' else item
                  for item in reversed(directions)]

# display new directions
for item in new_directions:
    print(item)

Output:

Direction (L or R)? r
Street name? john
Direction (L or R)? l
Street name? king
Direction (L or R)? l
Street name? school
R
KING
R
JOHN
L
HOME
  • Related