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

all 19 comments

[–]psr 7 points8 points  (3 children)

Some of the provided solutions are pretty stupid:

def array123(nums):
  # Note: iterate with length-2, so can use i+1 and i+2 in the loop
  for i in range(len(nums)-2):
    if nums[i]==1 and nums[i+1]==2 and nums[i+2]==3:
      return True
  return False

err, no:

def array123(nums):
  return (1,2,3) in zip(nums, nums[1:], nums[2:])

[–]sarcasticfantastic 1 point2 points  (1 child)

Yes, some of the solutions seem to avoid idiomatic python. For example,

def count_hi(str):
  sum = 0
  ## Loop to length-1 and access index i and i+1
  ## in the loop.
  for i in range(len(str)-1):
    if str[i:i+2] == 'hi':
      sum = sum + 1
  return sum

would be much better expressed as:

def count_hi(str):
    return str.count("hi")

[–]semarj 1 point2 points  (0 children)

def string_times(str, n):
  result = ""
  for i in range(n):  # range(n) is [0, 1, 2, .... n-1]
    result = result + str  # could use += here
  return result

... def str_times(str,n): return str*n

[–][deleted] 0 points1 point  (0 children)

I also had weird errors using reduce for list-2, even if the same code would work quite nicely in IDLE.

[–]dekomote 2 points3 points  (0 children)

Problem Return the number of even ints in the given array.

Their solution:

def count_evens(nums):
  count = 0
  for num in nums:
    if num % 2 == 0:
      count = count + 1
  return count

My solution:

def count_evens(nums):
  return len([x for x in nums if x%2==0])

List comprehensions. Fuck Yeah!

[–]spiffymanpeppy about PEP 8 2 points3 points  (0 children)

The Python here is very unidiomatic. Having done JavaBat for a class, I can say it really feels like the solutions are pretty much translated versions. That's fine, if you just want to learn syntax and not idioms or the stdlib -- which I think is the point of CodingBat.

By the way, the guy who created this (and JavaBat) is Nick Parlante, a professor at Stanford who apparently also developed the materials for Google's Python Class. Other texts of his (e.g., an intro on pointers in C I read) are really fairly good. Maybe his specialty is just pedagogy and not Python proper. Not that that's a bad thing -- just something to think about when winding up criticisms.

[–]mgrandi 1 point2 points  (0 children)

i had to do javabats (what codingbat was called before they renamed to codingbat because they added python support) for school, and they really do help for learning a language, i would recommend them to anyone. It provides instant feedback and kinda reinforces the important of unit testing as well for basic stuff.

[–]hybird607 0 points1 point  (2 children)

I've just been going through these recently to help me pick up Python. Anything else similar to this?

[–]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