Problem 134 Flying text errata?

Back to General discussions forum

Kremor Part     2015-01-06 20:45:03

I just finished the problem using the following code:

[width, height, size] = [int(n) for n in input().split(' ')]

x = y = 0
x_step = y_step = 1

for step in range(101):
    print(x, y, '')
    x += x_step
    y += y_step
    if x == 0 or x+size == width:
        x_step *= -1
    if y == 0 or y == height-1:
        y_step *= -1

As you can see in my code, when the answer is checked, instead of using the given height, it uses the height-1.

Is this an error?

Rodion (admin)     2015-01-06 22:38:19
User avatar

Hi! Thanks for your message!

I think your code is pretty correct. See, we can rewrite the expression x+size == width like this:

if x == 0 or x == width-size:
    x_step *= -1
if y == 0 or y == height-1:
    y_step *= -1

Now you see that size is the width of the flying text and 1 is its height so both horizontal and vertical conditions look similarly. Really, if we have the screen of height lines, the first of them has index 0 and the last has index height-1 - so we want to change direction in any of these two... :)

For example if height is 3 than lines have indices 0, 1 and 2. There is no line with index 3...

Matthew Cole     2015-01-07 05:01:15

@Kremor:

On a slightly unrelated note: I noticed that you used a list comprehension to capture input, which is how I do it too (except lately I've been ingesting it all at once using sys.stdin then breaking it apart as needed). But since you're using what looks like Python 3.x, you might want to consider changing your comprehension from

[width, height, size] = [int(n) for n in input().split(' ')] to [width, height, size] = [int(n) for n in input().split()].

The reason is because the default behavior of str.split() is to split the string on consecutive whitespace characters - including \t (tab) and \n (linebreak). Not saying that our Admin is mischievous (...is he?...) but some of the later problems have characters from the string.whitespace constant in the input file. If you were to use str.split(' ') on such an input, the result would be that the comprehension will throw an unhandled exception with the following error message: ValueError: invalid literal for int() with base 10: '\n'

Rodion (admin)     2015-01-07 09:03:27
User avatar

> but some of the later problems have characters from the string.whitespace constant in the input file

Oh! If you'll be able to recollect some of the problems with such behaviour please report about them. I think it would be better if I fix them to avoid misguiding people :(

abhijeetgurjar273     2018-10-22 13:41:48

Thanx for posting the solution. In my opinion this feature of not being able to see solution untill you don't solve the question is not helpful. I tried my best to solve it, and If I'm still not able to solve it, what should I do?

Please login and solve 5 problems to be able to post at forum