Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
No Safe Word
Feb 26, 2005

The phrase "something awful" is not exclusively licensed by these forums or anything

Adbot
ADBOT LOVES YOU

No Safe Word
Feb 26, 2005

Gothmog1065 posted:

Thanks. I really need to get into writing quick for loops like that.

comprehensions, not for loops :eng101:

No Safe Word
Feb 26, 2005

Roargasm posted:

Recursively you could do:
code:
str = "racecar"
str = str.lower()

def isPal(s):
    if len(s) <= 1:
        return True

    else:
        answer = s[0] == s[-1] and isPal(s[1:-1])
        return answer

print isPal(str)

Pro tip: don't name variables str (or list or int, etc.)

No Safe Word
Feb 26, 2005

vodkat posted:

Whats the neatest way to sort a list of integers into other lists? At the moment I've got something and its working fine, but I'm sure its probably a really inefficient way to solve this problem.

code:
for x in pricesort:
    if x >= 50000000:
        fiftymil.append(x)
    if x >=45000000 and x <= 50000000:
        fourtyfivemil.append(x)
and so on all the way down to zero. Obviously this takes up a ridiculous amount of lines for a relatively simply operation so I wondering how else it could be done.

My python is super rusty so this may not be very pythonic or even that efficient but
code:
>>> def segment(nums, segment_size):
...     results = []
...     sorted_nums = sorted(nums)
...     for upper in range(segment_size, max(nums), segment_size):
...             results.append([])
...             x = sorted_nums[0]
...             while x < upper:
...                     results[-1].append(x)
...                     sorted_nums.remove(x)
...                     x = sorted_nums[0]
...     results.append(sorted_nums)
...     return results
>>> nums = [random.randint(0, 1000) for _ in range(100)]
>>> nums
[154, 457, 937, 456, 947, 278, 763, 151, 266, 165, 379, 783, 261, 795, 723, 312, 464, 139, 215, 793, 560, 914, 816, 677, 57,
84, 977, 856, 217, 991, 762, 328, 545, 860, 520, 767, 697, 750, 294, 232, 134, 190, 48, 111, 337, 751, 243, 353, 738, 951, 370,
116, 815, 401, 48, 688, 614, 824, 894, 643, 591, 756, 358, 772, 134, 797, 522, 856, 612, 413, 323, 231, 104, 425, 153, 352,
347, 690, 573, 223, 74, 706, 379, 484, 588, 935, 389, 67, 228, 397, 43, 559, 227, 86, 737, 738, 832, 819, 898, 495]
>>> segment(nums, 10)
[[], [], [], [], [43, 48, 48], [57], [67], [74], [84, 86], [], [104], [111, 116], [], [134, 134, 139], [], [151, 153, 154], [165], [], [], [190], [], [215,
217], [223, 227, 228], [231, 232], [243], [], [261, 266], [278], [], [294], [], [312], [323, 328], [337], [347], [352, 353, 358], [], [370, 379,
379], [389], [397], [401], [413], [425], [], [], [456, 457], [464], [], [484], [495], [], [], [520, 522], [], [545], [559], [560], [573], [588], [591], [],
[612, 614], [], [], [643], [], [], [677], [688], [690, 697], [706], [], [723], [737, 738, 738], [], [750, 751, 756], [762, 763, 767], [772], [783],
[793, 795, 797], [], [815, 816, 819], [824], [832], [], [856, 856], [860], [], [], [894, 898], [], [914], [], [935, 937], [947], [951], [], [977], [], [991]]
>>> segment(nums, 100)
[[43, 48, 48, 57, 67, 74, 84, 86], [104, 111, 116, 134, 134, 139, 151, 153, 154, 165, 190], [215, 217, 223, 227, 228, 231, 232, 243, 261, 266, 278, 294],
[312, 323, 328, 337, 347, 352, 353, 358, 370, 379, 379, 389, 397], [401, 413, 425, 456, 457, 464, 484, 495], [520, 522, 545, 559, 560, 573, 588, 591],
[612, 614, 643, 677, 688, 690, 697], [706, 723, 737, 738, 738, 750, 751, 756, 762, 763, 767, 772, 783, 793, 795, 797], [815, 816, 819, 824, 832, 856, 856,
860, 894, 898], [914, 935, 937, 947, 951, 977, 991]]

This will obviously create a shitload of empty lists if your resultset is sparse, in which case you could cull the empty lists or use a dict if you wanted (would make the -1 go away). Also it doesn't preserve ordering obviously.

e: forgot to append the leftovers, fixed

No Safe Word fucked around with this message at 16:23 on Oct 15, 2015

No Safe Word
Feb 26, 2005

Python comes with a csv module which will simplify some of that

Your more general questions is that pinging by computer name will only work if you get name resolution via NetBIOS/WINS, which if you're in a domain should "just" work (or really I think even if you're just on the same subnet, I think Windows runs network discovery services for those kind of name announcements, though I can't remember for sure). The name resolution stuff is more of a network sys admin question. Hopefully they're aware of what you're doing if you're going to be randomly pinging machines on the network :v:

No Safe Word
Feb 26, 2005

Python using the Go runtime? Why not: https://opensource.googleblog.com/2017/01/grumpy-go-running-python.html

No Safe Word
Feb 26, 2005

Hammerite posted:

The use of _ to mean "this is an unused variable" is bad. Don't tell people to do that. If you need a variable that's unused, use the word "unused".

Good lord no, _ as "ignore me" is convention. Naming a variable "unused" is worse than just naming it the intentionally-awkward-and-already-established-as-conventional name.

No Safe Word
Feb 26, 2005

Hammerite posted:

It's a bad convention. It's unnecessarily cryptic, and fails to be self-explanatory. On the other hand if you name a variable "unused", that's self-explanatory.

code:
>>> year, month, day, _, _, _, _, _, _ = tuple(datetime.datetime.now().utctimetuple())
>>> year, month, day, unused, unused, unused, unused, unused, unused = tuple(datetime.datetime.now().utctimetuple())
Contrived but you pick which one is better. You generally shouldn't have unused stuff at all, and the rare cases where it is used is because you need a terse way to skip it. A single, obviously-intended-to-not-be-used character, does that job more effectively than a word. And it's already internationalized to boot!

No Safe Word
Feb 26, 2005

Dominoes posted:

This is the way to handle it:
code:
>>> year, month, day, *rest = tuple(datetime.datetime.now().utctimetuple())
*what Ela said.

You whippersnappers with your expanded tuple unpacking. *waves python 1.5.2 stick*

No Safe Word
Feb 26, 2005

Hammerite posted:

I've dragged this argument (in which I am clearly in the minority) out for too many posts already, but... I don't see that it does make it harder to read for anybody, irrespective of their level of experience.

If I see an underscore I know without any thought "oh, that's stuff that's not used". If I see anything else, I have to parse it. I also have to wonder "hmm, is it really unused? if this were just an underscore it would stand out elsewhere if somebody tried to use it, but somebody got cute and tried to use a name with semantic meaning for a throwaway variable"

So yeah, something you can immediately and without any conscious thought ignore is simpler

Adbot
ADBOT LOVES YOU

No Safe Word
Feb 26, 2005

lifg posted:

I do like this convention, but I don't see it much.
The most common other use I see in other languages is in pattern matching constructs like:

Haskell
O'Caml
F#

They all have the convention of "anytime we need a placeholder for bindings we will discard, use an underscore"

  • Locked thread