Stock Exchange Losses puzzle discussion

This task isn’t very hard and 4 numbers (including N and iterator in ‘for’ loop :slight_smile: ) should be enough to solve it. Btw, the default code in C++ (getline(cin, str):wink: is bad, it provokes you to parsing input as string, rather than using simple cin >> number;.

Modification done :slight_smile:

Your solution help me to think in my own solution. Thanks o/.

1 Like

This is brilliant! But how does it work?:confounded:

Was this modified since it was first introducted? I basically solved it in two constant assignments(zero values), 2 if expressions and 2 assignments in a cycle, (and, ofc, 2 arifmetic operations). So it’s basically 4 lines of code. Is it really a medium level puzzle?

2 Likes

If anything, it can serve as a trap, to trick people into thinking it’s really hard. And then they solve it with a nice recursive 40-lines-of-code algorithm, go to see other people’s solutions and burst in laughter.

2 Likes

Hi qua11q7,
I tried to understand your description of the code but I couldn’t. I’m not an English speaker myself. Since we cannot publish code here, I really don’t see how to slove this puzzle (I’m stuck on the test #5).
Could you send me a screenshot of your code please? Or show in an online editor (For exemple https://ideone.com/)

Actually you just have to keep track of max value and max loss (delta, diff… what ever you want to call it).
All other variables are just “clutter” because next value will either be new maximum or something you want to subtract from max.

1 Like

Yes. an obvious naive implementation in O(n^2) is to compare all pairs of values. Next logical step is to only compare a value with all the preceding ones. Then, you are only interested in one preceding value - the maximum one. And as you go from current minimum candidate to the next, there is only one way for the preceding maximum to change - if the previous minimum candidate is the new maximum. As it is the only value added to the set of preceding ones from the previous step.

There seems to be some bug in the default code for Go. In Example 5, with 99999 values, the token is too long and scanner.Scan() doesn’t work. I had to had scanner.Split(bufio.ScanWords), and then getting the values one by one for it to work.

For those coding in Haskell: The provided code for default parsing of stdin is ineffective and may cause Test 5 to fail even for an O(n) algorithm!

Instead of running a forM loop, use fmap in the beginning of the code:

main :: IO ()
main = do
    hSetBuffering stdout NoBuffering -- DO NOT REMOVE

-- Auto-generated code below aims at helping you parse
-- the standard input according to the problem statement.

input_line <- getLine
let n = read input_line :: Int
input_line <- getLine
let input = words input_line

-- NB! The default code given by CodinGame is ineffective! Use fmap instead
--    values <- forM [0..(n-1)] $ \i -> do
--        let v = read (input!!(i)) :: Int
--        return v
let values = fmap read input :: [Int]`

works fine, but test still says that it’s not fast enough.

Hi, i have made solution of Stock exchange losses in PHP. All tests go, but on submitting, there is one, which is not passing. It is number 02 (Maximum Loss between the first and last values (10 6 8 4 6 2) -> -8).

I’ve tried to hardcode that input in and my program made correct output. Maybe, something is bad with STDIN for that case.

This puzzle is so easy that my code worked the first time I submitted.

Hi , the base code for Lua seems to be wrong, the line v = int next_token() returns always nil.
I changed it with v = tonumber(next_token()) and it works ok.

n = tonumber(io.read())
next_token = string.gmatch(io.read(), “[^%s]+”)
for i=0,n-1 do
v = int next_token()
end

Hi Rolf,

Thanks for reporting the issue. This is fixed now.

Hi (again :smile: ) the code for bash is wrong too.

for (( i=0; i<n; i++ )); do
read v
done

There are n numbers in v in the first loop, the other n-1 loops are empty. So only the code “read v” is needed. Or repair the loop.

I’m doing the "15 puzzles in all languages " quest and there are 10 more languages left. I’ll be back :wink:

I can’t seem to pass the fifth test. Can someone help me out? Please and thank you!

Your code probably times out. In your code you surely compare two values multiple times. Think of what happends and what you could do if the value with the highest index is of a higher value than the other value.

Hiya, I have an issue. I have wanted to translate my succesful solution from Ruby into Bash and have correctly translated it because every other test passes except the 5th one… Is there an issue with Bash’s 5th Test in this exercise? Please aware me if I’m wrong. I can, of course, provide further info through private messages.