www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Possible difference in compilers?

reply "Charles" <csmith.ku2013 gmail.com> writes:
Hi everyone

So I've been working on the problems over at HackerRank.com
trying to gain some familiarity with D. I use a Windows computer
with VisualD, but the server used to test the program uses Ubuntu
(I can't tell which compiler they're actually using).

The problem I'm stuck on now is the Utopian Tree
(https://www.hackerrank.com/challenges/utopian-tree).

My solution I came up with (and works locally) is:

import std.stdio;
void main() {
      int height=1,t=1,oldN=0,n;
      readf(" %d\n", &t);
      foreach (i;0 .. t) {
          readf(" %d\n", &n);
          foreach (j; oldN .. n)
              height = (j % 2) ? height + 1 : height * 2;
          writeln(height);
          oldN = n;
      }
}

For the test cases this only produces the first output
(correctly), but then hits a compiler error with format.d before
the next one. Any ideas what might be going on?

Thanks,
Charles
Aug 31 2014
next sibling parent "Charles" <csmith.ku2013 gmail.com> writes:
 For the test cases this only produces the first output
 (correctly), but then hits a compiler error with format.d before
 the next one. Any ideas what might be going on?
Figured it out. The issue was the \n character at the end of the readf statements.
Aug 31 2014
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Charles:

 My solution I came up with (and works locally) is:

 import std.stdio;
 void main() {
      int height=1,t=1,oldN=0,n;
      readf(" %d\n", &t);
      foreach (i;0 .. t) {
          readf(" %d\n", &n);
          foreach (j; oldN .. n)
I suggest to add a blank line after the import, to move the import inside the main, to add a space after the commas, and to add an immutable to foreach variable. Bye, bearophile
Sep 01 2014