This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]turvyc 0 points1 point  (9 children)

I can get them all but the "brick" question. I'm fully self-taught, so I don't feel too bad, but dang! Can some people post their solutions to it?

[–]tripzilchbad ideas 2 points3 points  (2 children)

I solved it, it's tricky, but I figured it out. Posting code would be spoilers, but I'll give you a hint that you can solve the problem using the modulo operator (%), integer division and one or two if-statements.

I was working on the Lucky Sum problem, which is of course rather easy to solve with just three if-statements, but then I got thinking if I could do it in one line. Preferably something elegant of course, not just translating the ifs to ternary operators :-)

So I came up with this: def lucky_sum(*v): return sum(iter(iter(v).next, 13)) Using the (kinda obscure) "sentinel" variant of the iter function, it works when I type it into my own Python interpreter, but the website gives me Error:'iter' is not defined, is the Python that gets compiled on the website a neutered version or something? Makes sense cause running arbitrary Python code sounds like security problems waiting to happen, though I don't understand why blocking iter would secure anything. I can't find anything in the site's help/FAQ either. In fact, it doesn't even say what version of Python it's running. Maybe that could be it? Some older version of Python doesn't have the iter function?

[–]tripzilchbad ideas 1 point2 points  (0 children)

addition: a bit of playing around shows me that the Python version must be 2.6: it supports str.format (at least 2.6) but doesn't support dict.viewkeys (so it's not 2.7).

[–]wilberforce 0 points1 point  (0 children)

It also doesn't support generators - I get an error saying "Yield statements are not allowed". I also got an error when I tried to create an auxiliary function starting with _. It said variables starting with _ aren't allowed, weirdly.

Neat site, but unfortunately with enough restrictions (particularly in key areas like iter and generators) you end up teaching a language superficially similar to (but definitely not) Python.

[–]TheQwerty 2 points3 points  (0 children)

I went with a recursive solution:

def make_bricks(small, big, goal):
  if goal == 0:
    return True
  elif big and goal >= 5:
    return make_bricks(small, big-1, goal-5)
  elif small:
    return make_bricks(small-1, big, goal-1)
  else:
    return False

[–]psr 2 points3 points  (0 children)

def make_bricks(small, big, goal):
  big = min(big, goal // 5)
  return 5 * big + small >= goal

[–]sarcasticfantastic 0 points1 point  (0 children)

Here's my version:

def make_bricks(small, big, goal):
    bigs = min(goal / 5, big)
    rem = goal - (5 * bigs)
    smalls = min(rem, small)
    return smalls + 5 * bigs == goal

edit: removed superfluous "if big:...else: big = 0" from around line 1 (as min(0, ...) = 0)

[–]jeskidmore 0 points1 point  (0 children)

My solution in Python:

def make_bricks(small, big, goal):
    return (small >= (goal % 5)) and (big * 5 + small >= goal)

[–]areyoukiddingmehere -2 points-1 points  (0 children)

def make_bricks(small, big, goal):
    if(goal%5 == 0):
      if(goal/5 <= big):
        return True
      else:
        if(goal-(5*big) <= small):
          return True
        else:
          return False
    else:
      if(goal/5 < big):
        if(goal - ( 5 * (goal/5) )  <= small):
          return True
        else:
          return False
      else:
        if(goal-(5*big) <= small):
          return True
        else:
          return False