Posts Tagged Python

Converting Numbers to Ranges: Python

I have decided to start doing little programming puzzle problems in various languages, since my duplicate int finding problem is getting old and repetitive. My first puzzle comes from a site called Code Golf and it is titled Home on the Range. I decided to start with Python since it is something I have been playing with.

Basically the puzzle boils down to taking a series of numbers (in sequence) as input and producing a result where the numbers that are reduced to ranges where applicable, as below:

[1 2 3 6 9 10 11 15] becomes "1-3, 6, 9-11, 15."

After a couple iterations and a handful of language/api reference searches I came up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import sys
   
def convert_to_ranges(nums):
    ranges, holder = '',''
    num_count = len(nums)
    cap = endcap(num_count)

    for i in range(num_count):
        if i+1 < num_count and int(nums[i])+1 == int(nums[i+1]):
            if not holder: holder = nums[i]
        else:
            if holder:
                ranges += holder + '-' + nums[i] + cap(i)
                holder = ''
            else:
                ranges += nums[i] + cap(i)
   
    return ranges

def endcap(ln):
    return lambda idx: '.' if idx == ln-1 else ', '
   
print(convert_to_ranges(sys.argv[1:]))

I had originally had the holder variable as a list to hold each grouped number, but I realized that you really only need to store the first one so that you can use it to create the start of the grouping once you find the end of it. The lambda function (endcap()) was originally just a normal function, but I wanted to play with some interesting built-in features, and it actually worked out nicely. The python ternary operator is also interesting ( VAL if CONDITION else OTHERVAL ); it reads better than other ternary operators.

The int() calls I make in the function are simply there because the input comes from the standard in, where they are strings.

Popularity: 1% [?]

  • Share/Bookmark

Tags: ,

Finding Duplicate Ints: Python

I decided to start playing around with Python a bit and figured I should do what is becoming my personal tradition of implementing the Duplicate Int Finding Problem with Python.

It’s actually a very interesting language and I am surprised that I have not really looked into it sooner. It has a rich set of built-in libraries and tons of extension modules.

The code I came up with is pretty straight-forward:

1
2
3
4
5
6
def find_dup_int(items):
    items.sort()
    for i in range(len(items)-1):
        if items[i] == items[i+1]:
            return items[i]
    raise RuntimeError('No duplicate values found!')

I need to find a handful of other more interesting problems to try when playing with other languages, since this one seems to look basically the same in each language I have tried.

I will have to spend a little more time with Python, it feels very useful, and as you can see, not very verbose (though not to a fault).

Popularity: 1% [?]

  • Share/Bookmark

Tags: , ,

Switch to our mobile site