코드로 우주평화

0126. Over The Road 본문

나는 이렇게 학습한다/Algorithm & SQL

0126. Over The Road

daco2020 2023. 1. 27. 00:12

Task

You've just moved into a perfectly straight street with exactly n identical houses on either side of the road. Naturally, you would like to find out the house number of the people on the other side of the street. The street looks something like this:

Street

1|   |6
3|   |4
5|   |2
  you

Evens increase on the right; odds decrease on the left. House numbers start at 1 and increase without gaps. When n = 3, 1 is opposite 6, 3 opposite 4, and 5 opposite 2.

Example (address, n --> output)

Given your house number address and length of street n, give the house number on the opposite side of the street.

1, 3 --> 6
3, 3 --> 4
2, 3 --> 5
3, 5 --> 8

Note about errors

If you are timing out, running out of memory, or get any kind of "error", read on. Both n and address could get upto 500 billion with over 200 random tests. If you try to store the addresses of 500 billion houses in a list then you will run out of memory and the tests will crash. This is not a kata problem so please don't post an issue. Similarly if the tests don't complete within 12 seconds then you also fail.

To solve this, you need to think of a way to do the kata without making massive lists or huge for loops. Read the discourse for some inspiration :)



Solution:

over_the_road = lambda address, n: (n * 2 + 1) - address


'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

0128. Gauß needs help! (Sums of a lot of numbers).  (0) 2023.01.28
0127. Remove the time  (0) 2023.01.28
0125. Counting sheep...  (0) 2023.01.25
0124. Regular Ball Super Ball  (0) 2023.01.24
0123. Check same case  (0) 2023.01.23