Canadian Computing Competition: 2005 Stage 1, Junior #3
Jane's family has just moved to a new city and today is her first day of school. She has a list of instructions for walking from her home to the school. Each instruction describes a turn she must make. For example, the list
R
QUEEN
R
FOURTH
R
SCHOOL
means that she must turn right onto Queen Street, then turn right onto Fourth Street, then finally turn right into the school. Your task is to write a computer program which will create instructions for walking in the opposite direction: from her school to her home.
The input and output for your program will be formatted like the samples below. You may assume that Jane's list contains at least two but at most five instructions, and you may assume that each line contains at most 10 characters, all of them capital letters. The last instruction will always be a turn into the SCHOOL
.
Sample Input 1
R
QUEEN
R
FOURTH
R
SCHOOL
Sample Output 1
Turn LEFT onto FOURTH street.
Turn LEFT onto QUEEN street.
Turn LEFT into your HOME.
Sample Input 2
L
MAIN
R
SCHOOL
Sample Output 2
Turn LEFT onto MAIN street.
Turn RIGHT into your HOME.
Comments
there are 2 ways to do this, the first is by getting 2 lists and reversing the turns list and reversing the location list (all except for school, it needs to be last) then you iterate through the turns list to flip R and L(flipped and reversed). The you want to iterate through the reversed locations list and using an f string and indexes, put it all together. the second way is more complicated, you use one big list and by iterating through the big list reverse everything except for school, then you want to flip the L and Rs and then loop through again to print out the answer
What is wrong with my code? I get WA'd on 2nd test case.
Why are you printing
m[i-1]
? If you are trying to print the last element in an index, just typem[-1]
instead.Basically, your code is getting WA in the second test case since
m[i-1]
will always print the direction of the third last element in your listm
. So if it doesn't happen to be the same as the direction to turn home. You get WA.Consider this case:
Output should be:
Your code will fail this case because for the last direction, you are always printing the same direction two directions ago. (In this case, you will print
Turn RIGHT into your Home.
instead ofLEFT
. This is because two directions ago, you had to turnRIGHT
at Fifth Street. So it just prints the same direction as that and gets WA.)HOPE THIS HELPS :)))
this helped a lot, i needed a longer sample to confirm if my strategy was correct, thanks
I forgot to add street onto my output, spent 30 min trying to see if my math was wrong. I somehow still remembered the period at the end though:-(
my program works when I am testing it with the samples, however when I submit it, it does not fully print the answer. instead it does this "Test case #1:
WA [0.030s, 10.16 MB] (0/10)
Your output (clipped) Turn RIGHT onto D street. Turn RIGHT onto C street. Turn RIGHT o
I do not understand what happened.
DMOJ only shows a clipped version of your output on the site (to make sure nothing major is going wrong). The full version of your output is being seen on the server (and is being judges as WA).
how do you make the street names flip over?
Check if they are L or R and switch correspondingly.
Shouldn't
Sample Input 2
be:Since R = L and L = R?
EDIT: I realized my mistake.
This comment is hidden due to too much negative feedback. Show it anyway.
Don't be such a meany
No, sorry, I now realize that the
Sample
is correct.This comment is hidden due to too much negative feedback. Show it anyway.
It will always end with SCHOOL.