Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 214 215 [216] 217 218 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 907830 times)

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3225 on: October 25, 2012, 09:48:24 am »

Is is c++? Then use "break;" to break out of the loop.

Don't know why what you described didn't work, though...
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #3226 on: October 25, 2012, 09:48:58 am »

No, I wrote the example code wrong. Edited it to be correct.

It turns out the error was because of a simple type: I forgot I used unsigned int, which makes negative numbers 0 (or a big number, either way) and always fulfills the condition.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

da_nang

  • Bay Watcher
  • Argonian Overlord
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3227 on: October 25, 2012, 11:11:54 am »


for ( int counter = 1; counter >= 10; counter++ )
 {
  if ( position - counter < 0 )
    counter = 10; //to exit the loop
  else
    /* do something */;
 }

How does this for-loop even start? When counter is initialized it doesn't fulfill the condition, so the for-block isn't reached. Even if it somehow does, it won't end as the counter then would never stop fulfilling the condition.
Logged
"Deliver yesterday, code today, think tomorrow."
Ceterum censeo Unionem Europaeam esse delendam.
Future supplanter of humanity.

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3228 on: October 25, 2012, 01:13:16 pm »

What is the difference between an array and a list in Python, and how do you make an array? Or is an array not the same type of 'thing' a str, list, float, etc is?
Logged

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3229 on: October 25, 2012, 01:16:19 pm »

A list is an array of pointers. Which is honestly the closest thing in python you'll be wanting to use to an actual array, and in many languages is simply called an array.

So list an array, same thing.
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3230 on: October 25, 2012, 01:20:10 pm »

What is the difference between an array and a list in Python, and how do you make an array? Or is an array not the same type of 'thing' a str, list, float, etc is?

I'm not aware that there is a difference. To my knowledge, a list is Python's name for what other languages call arrays, except Python's lists can store objects of different types in the same list.

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3231 on: October 25, 2012, 01:23:47 pm »

Though it should be added that what most languages call "arrays" aren't, strictly speaking, arrays, as in the primitive data structure, and are actually lists.

Terminology can be confusing. This is what we get for trying to mangle synonyms into new shapes instead of coming up with new words. :P

except Python's lists can store objects of different types in the same list.
Strictly speaking, this is the exact opposite of true. Most languages allow arrays to hold different types, while there's only one type that can be stored in a python array, ever. It just so happens that that type is a reference. But it does clarify what the actual difference is - a Python "list" is an array of "reference types" - labels, pointers, whatever you want to call them. Essentially, it's a list of where to find the objects you want. In other languages, you can create arrays of other types, but Python doesn't let you, because there's not really any reason to do so with Python.

Although, actually... most languages might handle arrays that way now. But at some point they use to actually contain the objects instead of just referencing them, I'm pretty sure.

You know what I don't even know.
« Last Edit: October 25, 2012, 01:31:38 pm by GlyphGryph »
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3232 on: October 25, 2012, 02:42:49 pm »

Is there an easy way (without creating another string) to return a backward slice of a string? I tried doing the following but that just returned an empty string.
Code: [Select]
>>> s = 'hello'
>>> s[3:2]
''
Logged

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3233 on: October 25, 2012, 02:49:06 pm »

Slice operations always create another string. So... not under your constraints, no.

However... s[3:1:-1] might point you in the right direction for what you actually want. ;)

(Basically, extended slice takes three arguments - start point, end point, steps for next character)
« Last Edit: October 25, 2012, 02:52:06 pm by GlyphGryph »
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3234 on: October 25, 2012, 02:55:23 pm »

I am fairly sure I should have know that >.>.
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3235 on: October 25, 2012, 02:57:32 pm »

Though it should be added that what most languages call "arrays" aren't, strictly speaking, arrays, as in the primitive data structure, and are actually lists.

Terminology can be confusing. This is what we get for trying to mangle synonyms into new shapes instead of coming up with new words. :P

except Python's lists can store objects of different types in the same list.
Strictly speaking, this is the exact opposite of true. Most languages allow arrays to hold different types, while there's only one type that can be stored in a python array, ever. It just so happens that that type is a reference. But it does clarify what the actual difference is - a Python "list" is an array of "reference types" - labels, pointers, whatever you want to call them. Essentially, it's a list of where to find the objects you want. In other languages, you can create arrays of other types, but Python doesn't let you, because there's not really any reason to do so with Python.

Although, actually... most languages might handle arrays that way now. But at some point they use to actually contain the objects instead of just referencing them, I'm pretty sure.

You know what I don't even know.

In regard to your text that you struck out, it depends on the language, mostly. I'm referring to the big 3 programming (as opposed to scripting) languages: C, C++, and Java. All 3 languages have arrays that use copy constructing for storing primitives. For example, if you created an int with a value of 1, created an int array with that int as the first value, and incremented the first value of the array, the original int will still have a value of 1. So, the arrays do not directly contain the primitive - they instead contain a copy of the primitive. For C and C++, the same thing happens with objects, but not with Java, which does copying by reference for objects. If you modify a Java object in an array, the original object will also be modified. While C and C++ arrays create new objects, Java arrays store references to the objects.

Now for the type thing. C and C++ do not have a type that can act as a universal type (void* does not count), so arrays in those languages can only contain the type they are created with (plus any subtypes). Java arrays act the same way, except Java has the Object class, which is the superclass to every class without an explicit superclass. So, an Object array can only hold Objects by definition, but because every object is an Object, in practice, the array can hold any object.

Python does duck typing, so as long as an object supports being put in a list (and I don't know any way to make an object not support that, since it would require making an object unable to be referenced), it can be put in a list.

Is there an easy way (without creating another string) to return a backward slice of a string? I tried doing the following but that just returned an empty string.
Code: [Select]
>>> s = 'hello'
>>> s[3:2]
''

Code: [Select]
>>> s = 'hello'
>>> s = s[::-1]
>>> print s
'olleh'

I just got sniped.

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3236 on: October 25, 2012, 05:28:23 pm »

Is there an easy way (without creating another string) to return a backward slice of a string? I tried doing the following but that just returned an empty string.
Code: [Select]
>>> s = 'hello'
>>> s[3:2]
''
Strings are immutable in Python, so you either need to read the string backwards by hand, or create a new string.
Logged

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3237 on: October 26, 2012, 12:59:14 am »

Like in Mego's example, you can use the [::-1] slice notation to reverse it, but that's still returning a new string.

In Python 2 there's a UserString.MutableString wrapper that seems equivalent to a basestring, though it is not very efficient and apparently only meant as an educational example:

Spoiler (click to show/hide)
« Last Edit: October 26, 2012, 04:49:32 am by SethCreiyd »
Logged

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3238 on: October 26, 2012, 07:13:02 am »

I have mastered the art of displaying tiles!
Spoiler (click to show/hide)

Now onwards, towards whatever the hell I can do with this!


O wait, I do have a question. Does anyone know how to extract the row and position in the row(and the other way around) from a pyramid thing like this:
Code: [Select]
    1
  2 3 4
5 6 7 8 9
Where the pyramid has n rows. I am passing around the n and the number of the tile, or should I just scrap that plan and make some weird 2 dimensional array?
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3239 on: October 26, 2012, 07:15:33 am »

Like in Mego's example, you can use the [::-1] slice notation to reverse it, but that's still returning a new string.
In Python 2 there's a UserString.MutableString wrapper that seems equivalent to a basestring, though it is not very efficient and apparently only meant as an educational example:

We do people keep offering half the right answer when the actual right answer has already been posted. In this case it's worse, as you provide him the syntax to do exactly what he wants, identical to the one I proposed, and then say it won't do what he wants. T_T

I'm so confused.
Logged
Pages: 1 ... 214 215 [216] 217 218 ... 796