Home » How clean is my snow?

How clean is my snow?

Solutons:


Let’s look at the code.

from random import randrange
import time

Your imports are very minimal! Good.

# Snow animation
# Snow is simply the `#` symbol here. Half of the snow moves at 1 char
# per frame, while the other half moves at 0.5 chars per frame. The
# program ends when all the snow reaches the bottom of the screen.
# The viewing area is 80x25. It puts 100 snow flakes to output, half
# fast, and half slow. Every frame it dispenses 4 flakes, 2 fast and
# 2 slow ones, at random locations at the top of the viewing area.

This looks more like a docstring to me. It would be nice to render it as such. You can do this by dropping the # signs, and surrounding it in """ quotes.

screen = {'x': 80, 'y': 20}
drops = []

Global variables are not that nice. But this is a simple file, so maybe we can leave it like this for now? Let’s.

def createRainDrop(x, y, speed):
    return {'x': x, 'y': y, 'speed': speed}

I think something like a class would be better for this. Let’s try

class RainDrop(object):
    def __init__(self, x, y, speed):
        self.x = x
        self.y = y
        self.speed = speed

Of course, now we need to replace createRainDrop(...) with RainDrop(...), and drop['...'] with drop.....

def createRandomDrops():
    dropCount = 4
    for i in range(dropCount):
        yield RainDrop(randrange(0, screen['x']), 0, min((i % 2) + 0.5, 1))

That’s better.

def moveDrops():
    for drop in drops:
        drop.y = drop.y + drop.speed

We’re modifying drop here, instead of asking it to modify itself. We should be writing something like drop.moveDown() here, or maybe drop.tick() (‘tick’ is what’s commonly used to notify an event about stepping forward in time).

def drawDrops():
    out = [''] * screen['y']
    for y in range(screen['y']):
      for x in range(screen['x']):
        out[y] += '#' if any([drop.x == x and drop.y == y for drop in drops]) else ' '

    return 'n'.join(out)

Here, for all the positions on the screen, you’re looping over all the drops. Ideally we’d turn that around:

def drawDrops():
    out = [[' ' for _ in range(screen['x'])] for _ in range(screen['y'])]
    for drop in drops:
        if int(drop.y) < screen['y']:
            out[int(drop.y)][drop.x] = '#'

Now that’s a bit faster and cleaner.

def dropsOnScreen():
    return any([drop.y < screen['y'] for drop in drops])

Makes sense. Except I’d suggest to not use the [...], which creates a list. Better to use

def dropsOnScreen():
     return any(drop.y < screen['y'] for drop in drops)

This behaves the same, but does not have to create an intermediate list.

drops += createRandomDrops()

while dropsOnScreen():
    if len(drops) < 100:
        drops += createRandomDrops()

    print(drawDrops())
    moveDrops()
    time.sleep(0.100)

You want to get rid of the duplicated call to drops += createRandomDrops().

while True:
    if len(drops) < 100:
        drops += createRandomDrops()

    if not dropsOnScreen():
        break

    print(drawDrops())
    moveDrops()
    time.sleep(0.100)

But in my opinion, the extra createRandomDrops is not that bad.

Cool animation!

Let’s get some linting out of the way. As per PEP 8, you should use 4 spaces of indentation consistently, and function names should be snake_case.

Scalability

The main weakness of your design is scalability. If you extend the loop to run indefinitely, then you will eventually run into performance issues.

One problem is that the drops list grows with each iteration, and is never pruned. The drops don’t disappear after falling to the ground; they keep falling forever, invisibly, off-screen. The solution is to have moveDrops() delete drops when they fall beyond the bottom. (That’s a smarter strategy than having dropsOnScreen() re-examine every drop on every animation frame.)

Furthermore, to place the drops on the grid, you do an O(n) scan for each position on the screen with '#' if any([drop['x'] == x and int(drop['y']) == y for drop in drops]). I would rewrite drawDrops() so that each drop places itself, using a dictionary or a 2-D array. I would also prefer to use comprehensions than repeated append operations, but that is mostly a style preference.

Data types

Your comment says that the screen dimensions are 80×25, but your code says screen = {'x': 80, 'y': 20}. Ideally, the dimensions should be detected at runtime using the curses library. Since screen is used as a global variable, I would like to see it named SCREEN and made immutable. A namedtuple would make it immutable, with the additional benefit of allowing the dot accessor rather than the clumsy [] notation. I think that width and height would be more appropriate names than x and y.

Similarly, defining a class for the raindrops would avoid the drop['x'] notation. Furthermore, the createRainDrop() function cries out to be a constructor.

Creating drops and looping

The rest of the code is an exercise in Pythonic iteration. Everything can be handled with liberal usage of iterators.

In createRandomDrops(), instead of the cryptic formula min((i % 2) + 0.5, 1), use itertools.cycle([0.5, 1]). I would turn createRandomDrops() into an infinite generator.

In the solution below, parameters such as the speed, intensity, and duration are all centrally tweakable by modifying drop_params and precipitation. For example, precipitation = drop_generator(**drop_params) would result in an infinite loop with just one new drop per frame.

Suggested solution

from collections import namedtuple
import curses
from itertools import chain, cycle, islice, repeat
from random import randrange
import sys
import time

SCREEN = namedtuple('Screen', 'height width')(*curses.initscr().getmaxyx())
curses.endwin()

class Raindrop:
    def __init__(self, x, y, speed):
        self.x, self.y, self.speed = x, y, speed

def drop_generator(batch_size=1, **drop_params):
    while True:
        yield [
            Raindrop(**{key: next(gen) for key, gen in drop_params.items()})
            for _ in range(batch_size)
        ]

def move_drops(drops):
    """Move each drop down according to its speed, and remove drops from the
       set that have fallen off."""
    for drop in drops:
        drop.y += drop.speed
    drops.difference_update([drop for drop in drops if drop.y >= SCREEN.height])

def render_drops(drops, char="#"):
    """Return a string representation of the entire screen."""
    scr = {
        int(drop.y) * SCREEN.width + int(drop.x): char for drop in drops
    }
    return 'n'.join(
        ''.join(scr.get(y * SCREEN.width + x, ' ') for x in range(SCREEN.width))
        for y in range(SCREEN.height)
    )


drop_params = {
    'x': (randrange(0, SCREEN.width) for _ in repeat(True)),
    'y': repeat(0),
    'speed': cycle([0.5, 1]),
}
precipitation = chain.from_iterable([
    islice(drop_generator(batch_size=4, **drop_params), 25),
    repeat([])  # ... then generate nothing as existing drops keep falling
])
drops = set(next(precipitation))
while drops:
    drops.update(next(precipitation))
    print(render_drops(drops))
    # Python 2.7 seems to have a curses bug that necessitates flushing
    sys.stdout.flush()
    move_drops(drops)
    time.sleep(0.100)

You don’t get drops of snow! Clearly it should be

  for flake in flurry:

Related Solutions

Calculate the sum with minimum usage of numbers

Here's a hint: 23 : 11 + 11+ 1 ( 3 magic numbers) 120: 110+ 10 (2 magic numbers) The highest digit in the target number is the answer, since you need exactly k magic numbers (all having 1 in the relevant position) in order for the sum to contain the digit k. So...

Why not drop the “auto” keyword? [duplicate]

Your proposal would be rejected on the basis of backward compatibility alone. But let's say for the sake of argument that the standards committee like your idea. You don't take into account the numerous ways you can initialize a variable widget w; // (a) widget...

Recursive to iterative using a systematic method [closed]

So, to restate the question. We have a function f, in our case fac. def fac(n): if n==0: return 1 else: return n*fac(n-1) It is implemented recursively. We want to implement a function facOpt that does the same thing but iteratively. fac is written almost in...

How can I match values in one file to ranges from another?

if the data file sizes are not huge, there is a simpler way $ join input1 input2 | awk '$5<$4 && $3<$5 {print $2, $5-$3+1}' B100002 32 B100043 15 B123465 3 This Perl code seems to solve your problem It is a common idiom: to load the entire...

Javascript difference between “=” and “===” [duplicate]

You need to use == or === for equality checking. = is the assignment operator. You can read about assignment operators here on MDN. As a quick reference as you are learning JS: = assignment operator == equal to === equal value and equal type != not equal !==...

Compiler complains about misplaced else [closed]

Your compiler complains about an misplaced else because, well, there is an else without a preceding if: // ... for (j=1; j<n-i; j++) { if(a[j]<=a[j+1]) { // ... } // END OF IF } // END OF FOR else { continue; } // ... The else in your code does not follow...

Bootstrap – custom alerts with progress bar

/* !important are just used to overide the bootstrap css in the snippet */ .alertContainer { border-radius: 0 !important; border-width: 0 !important; padding: 0 !important; height: auto !important; position: absolute !important; bottom: 15px !important; left:...

How to Garbage Collect an external Javascript load?

Yes, s.onload = null is useful and will garbage collect! As of 2019, it is not possible to explicitly or programmatically trigger garbage collection in JavaScript. That means it collects when it wants. Although there is cases where setting to null may do a GC...

Math programming with python

At first, what you are looking for is the modulo operator and the function math.floor() Modulo from wikipedia: In computing, the modulo operation finds the remainder after division of one number by another (sometimes called modulus). for example: 12%12=0...

Android slide over letters to create a word [closed]

Here some advice you can use: First for each cell you can create an object that represents the state of that cell: class Cell { char mChar; int row,column; boolean isSelected; } then you can create a 2D array of your cells Cell[][] mTable = ... For views you...

Sum two integers in Java

You reused the x and y variable names (hence the variable x is already defined in method main error), and forgot to assign the ints read from the Scanner to the x and y variables. Besides, there's no need to create two Scanner objects. public static void...

Extend three classes that implements an interface in Java

Using this simplified implementation of the library, using method() instead of M(): interface IFC { void method(); } class A implements IFC { public void method() { System.out.println("method in A"); }; } As akuzminykh mentions in their comment You'd write a...

How to set the stream content in PHPExcel? [closed]

Okey, First thing first PHPExcel_Worksheet_MemoryDrawing() can't solve your problem if you insist to use stream content and pass that to your worksheet your PDF will not render your image. But you can use `PHPExcel_Worksheet_Drawing()' if you want to render...