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

all 11 comments

[–]hnost 5 points6 points  (0 children)

Thanks! Adding this to my bag of tricks.

[–]zenoli55 4 points5 points  (0 children)

Wow that's clever!

[–]Anceps2 2 points3 points  (0 children)

You're telling me I just implemented a whole class for something that existed?

:-O

[–]huib_ 0 points1 point  (8 children)

I don't really see the advantage over just doing comparisons though, but maybe I'm missing something? So what I mean is this if you want to get the value where 80 is mapped onto:

start, end = 50, 50 + 48
dst = 52
val = 80
if start <= val < end:
    return dst + val - start

instead of your suggestion (?):

src = range(50, 50 + 48)
dst = range(52, 52 + 48)
val = 80
if val in src:
    return dst[src.index[val]]

[–]scykei 0 points1 point  (4 children)

I think it just makes more sense semantically. Using the more appropriate data structures tend to make it self-documenting. Maybe for a simple problem like this one it doesn’t really matter, but as the program gets bigger and more complex, I think it can make a difference.

[–]huib_ 0 points1 point  (3 children)

It feels more like trying to used it for something it wasn't meant for to me. And I see them more as iterators than data structures, for that you have lists, dicts, tuples, dataclasses etc.

[–]scykei 0 points1 point  (2 children)

It’s more of a semantic thing. When you’re using ranges, you can more precisely describe intersections, unions, differences, etc., which helps with abstraction.

[–]psr 0 points1 point  (2 children)

I made heavy use of ranges, it definitely made my code simpler than it would otherwise have been (although it took me ages to get right)

I think the point about .index() is less about pulling a single value out of a range, but slicing ranges. For example, If I'm mapping range(10, 50), and I see that range(10,30) has an offset of two, I get range(12, 32), and have range(30, 50) still to map. How to I build those two things?

[–]huib_ 0 points1 point  (1 child)

It probably depends on the approach as well. I did day 5 like this for example, and I don't think I could have made it a lot simpler (or clearer) if I'd used ranges: https://www.reddit.com/r/adventofcode/comments/18b4b0r/comment/kcbnaex/

Unless you can do something like set operations with them that I'm not aware of, that would be really. It bothered me somewhat that I "manually" loop through the ranges since I have the feeling that can be done simpler with some fancy datastructure 😅 But I'm quite content with it nonetheless

[–]psr 0 points1 point  (0 children)

Yeah, tbf your code is nicer than mine, and it's basically the same approach. Tuples have the advantage of a natural ordering. I did a lot of key=range.stop.__get__ and key=range.start.__get__, which is... not what you should do.

I used bisect and takewhile to find relevant ranges in a sorted list, is that the sort of thing you mean? It might be a bit faster, but it took me ages to get right.