Floor, Ceil and Rint

  • + 2 comments

    I was wondering if anyone could help explain this.

    I answered this question successfully, but the comparison kept failing due to whitespace differences with the 'golden' result.

    I checked this discussion and no-one seems to have had the same problem. However, to get my code to complete I had to use the following

    import numpy
    
    def nformat(x, s):
        return "{:{w}d}.".format(int(x), w=s)
    
    a = numpy.array(input().split(), float)
    for f in [numpy.floor, numpy.ceil, numpy.rint]:
        b = f(a)
        size = len(str(max(b)))-1
        numpy.set_printoptions(formatter={'all':lambda x: nformat(x, size)})        
        print(b)
    

    There's probably many other ways to do it, and I won't claim this is the cleanest way - but essentially I had to muck with 'set_printoptions(formatter=...)' before each output was displayed to adjust the formatting width to accomodate the 'width' of the largest value.

    Alternatively I supposed I could have written my own 'printN' function.

    I was hoping someone might be able to explain why I had this problem and it seems no one else did (or didn't comment on it).

    Thanks in advance,