Re: [Tutor] List of ints

2015-03-04 Thread Alan Gauld

On 03/03/15 23:44, Mark Lawrence wrote:


Having never heard of QPython I've just looked it up, so for those who
don't know from http://qpython.com/ it's "a script engine which runs
Python programs on android devices".  I doubt if there is much
experience on this list with it although you might get lucky.



I have QPython on my Android phone. It's not much good for development 
but its ok for running a few basic Python scripts. Obviously GUIs etc 
won't work but file processing and the like are fine.


But I certainly wouldn't try programming in it! But then I don't
do any programming on my phone anyway. On my tablet I use vim as editor 
and Qpython to run the code but its still not an experience I recommend.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List of ints

2015-03-04 Thread Mark Lawrence

On 04/03/2015 00:25, Steven D'Aprano wrote:

On Tue, Mar 03, 2015 at 04:50:41PM +1000, Phil wrote:


count [0] += 1

This fails with the following error;

TypeError: 'int' object is not iterable


I know that others have already solved the problem, but here is
something which might help you solve similar problems in the future.
The way to debug simple things like this is quite simple:

print count[0]

which will show you that count[0] is a list [0], not an int 0, and you
are trying to add [0]+1 which doesn't work.

Never under-estimate the usefulness of a few print calls when debugging.




About time we threw in the use of the interactive interpreter for trying 
code snippets as well, just for good measure :)


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Fixed Vector Array

2015-03-04 Thread niyanaxx95
Need help trying to implement insert, remove, indexof, and reverse functions. 

I tried to do them but am not sure if it is correct. I am struggling with 
arrays.


This is python and using ezarrays. 


Assignment:

A fixed vector is very similar to the vector in that it is a sequence container 
that can grow and shrink as needed and include many useful operations. But the 
fixed vector has a maximum capacity beyond which it can not expand. 

• FixedVector(maxSize): Creates an empty fixed vector with the given maximum 
capacity. 

• length(): Returns the number of elements contained in the vector. 

• isFull(): Returns a Boolean indicating if the vector is full.

• getitem(index): Returns the element stored in the vector at position index. 
The value of index must be within the valid range. 

• setitem(index, item): Sets the element at position index to contain the given 
item. The value of index must be within the valid range, which includes one 
position beyond the end of the vector. In the latter case, the item is simply 
appended onto the end. • contains(item): Determines if the given item is 
contained in the vector. 

• toString(): Returns a string representation of the vector. 

• append(item): Adds the given item to the end of the vector. An item can not 
be appended to a full vector. 

• clear(): Removes all elements from the vector. 

• insert(index, item): Inserts the given item into the vector at position 
index. The elements at and following the given position are shifted down to 
make room for the new item. The index must be within the valid range and the 
vector can not be full. If index is one position beyond the end of the vector, 
the item is appended onto the vector. 

• remove(index): Removes the element at position index from the vector. The 
removed element is returned. The index must be within the valid range. 

• indexOf(item): Returns the index of the element containing the given item. 
The item must be in the list. 

• reverse(): Performs a list reversal by reversing the order of the elements 
within the vector. 





My Code:


from ezarrays import Array


class FixedVector :
   # Creates a new empty fixed vector with the given maximum capacity.
  def __init__(self, maxSize) :
self._theItems = Array(maxSize)
self._numItems = 0

   # Returns the number of elements contained in the vector.
  def __len__(self) :
return self._numItems

   # Returns a Boolean indicating if the vector is full.
  def isFull(self) :
return self._numItems == len(self._theItems)

   # Returns the element stored in the vector at position index. 
   # The value of index must be within the valid range. 
  def __getitem__(self, index) :
assert index >= 0 and index < self._numItems, "Index out of Range."
return self._theItems[index]

   # Sets the element at position index to contain the given item. The 
   # value of index must be within the valid range, which includes one 
   # position beyond the end of the vector. In the latter case, the item 
   # is simply appended onto the end.  
  def __setitem__(self, index, item) :
assert index >= 0 and index <= self._numItems, "Index out of range."

if index == self._numItems :
  self.append(item)
else :
  self._theItems[index] = item 

   # Determines if the given item is contained in the vector.
  def __contains__(self, item) :
i = 0
while i < self._numItems : 
  if self._theItems[i] == item :
return True
  i = i + 1
  
return False
  
   # Returns a string representation of the vector.
  def __repr__(self) :
if self._numItems == 0 :
  return "[]"
  
vectStr = "[" + str(self._theItems[0])
for i in range(1, self._numItems) :
  vectStr = vectStr + ", " + str(self._theItems[i])
  
vectStr = vectStr + "]"
return vectStr

   # Adds the given item to the end of the vector. An item can not be 
   # appended to a full vector.
  def append(self, item) :
assert self._numItems < len(self._theItems), \
   "Can not add to a full vector."
self._theItems[self._numItems] = item
self._numItems = self._numItems + 1
  
   # Removes all elements from the vector.
  def clear(self) :
self._theItems.clear(None) 

   # Inserts the given item into the vector at position index. The elements 
   # at and following the given position are shifted down to make room 
   # for the new item. The index must be within the valid range and the
   # vector can not be full. If index is one position beyond the end of
   # the vector, the item is appended.
  def insert(self, index, item) :
i = numItems
while i > index :
self._theItem[i] = self._theItem[i-1]
i = i-1 
self._theItems[i] = item
self._numItems = self._numItems + 1
  
   # Removes the element at position index from the vector. The removed
   # element is returned. The index must be within the valid range.
  def remove(self, index) :

Re: [Tutor] Fixed Vector Array

2015-03-04 Thread Danny Yoo
On Wed, Mar 4, 2015 at 7:40 AM,   wrote:
> Need help trying to implement insert, remove, indexof, and reverse functions.
>
> I tried to do them but am not sure if it is correct. I am struggling with 
> arrays.

Hi Niyana,


Programmers generally use unit tests to double-check their programs.
Do you have any *unit tests* to help you feel more confident which
methods are probably ok?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: Re: Fixed Vector Array

2015-03-04 Thread Danny Yoo
Can someone point out how to write and run tests to Ni'Yana?  I'm busy at
the moment.  Thanks!
-- Forwarded message --
From: 
Date: Mar 4, 2015 9:46 AM
Subject: Re: [Tutor] Fixed Vector Array
To: "Danny Yoo" 
Cc:

 I am not sure how to test this. I do not understand arrays fully but this
a lab assignment that I must turn in.

Sent from Windows Mail

*From:* Danny Yoo 
*Sent:* ‎Wednesday‎, ‎March‎ ‎4‎, ‎2015 ‎12‎:‎44‎ ‎PM
*To:* Ni'Yana Morgan 
*Cc:* tutor@python.org

On Wed, Mar 4, 2015 at 7:40 AM,   wrote:
> Need help trying to implement insert, remove, indexof, and reverse
functions.
>
> I tried to do them but am not sure if it is correct. I am struggling with
arrays.

Hi Niyana,


Programmers generally use unit tests to double-check their programs.
Do you have any *unit tests* to help you feel more confident which
methods are probably ok?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fixed Vector Array

2015-03-04 Thread Alan Gauld

On 04/03/15 15:40, niyanax...@gmail.com wrote:

Need help trying to implement insert, remove, indexof, and reverse functions.

I tried to do them but am not sure if it is correct. I am struggling with 
arrays.


I;m not sure why so many courses seem to insist on teaching old school 
arrays using Python. It involves rewriting functions that already exist, 
in superior form in the standard library. Reinventing the wheel is a bad 
practice and it seems a shame that some teachers appear to want to teach 
bad habits!



This is python and using ezarrays.


And using third party libraries to do so just compounds the wrong,
why not at least base it on a standard list? Caveat I have no idea
what this module does so some of my answers may be off track.

Anyway, rant over, it's not your fault and I doubt you
can influence your course design.


Assignment:

A fixed vector is very similar to the vector in that
it is a sequence containerthat can grow and shrink

> as needed and include many useful operations.

Just like a Python list in fact...


But the fixed vector has a maximum capacity beyond which it can not expand.


Which is rather limiting and very rarely what you want...


• FixedVector(maxSize): Creates an empty fixed vector with the given maximum 
capacity.
• length(): Returns the number of elements contained in the vector.
• isFull(): Returns a Boolean indicating if the vector is full.
• getitem(index): Returns the element stored in the vector at position index. 
The value of index must be within the valid range.
• setitem(index, item): Sets the element at position index to contain the given 
item. The value of index must be within the valid range, which includes one 
position beyond the end of the vector. In the latter case, the item is simply 
appended onto the end. • contains(item): Determines if the given item is 
contained in the vector.
• toString(): Returns a string representation of the vector.
• append(item): Adds the given item to the end of the vector. An item can not 
be appended to a full vector.
• clear(): Removes all elements from the vector.
• insert(index, item): Inserts the given item into the vector at position 
index. The elements at and following the given position are shifted down to 
make room for the new item. The index must be within the valid range and the 
vector can not be full. If index is one position beyond the end of the vector, 
the item is appended onto the vector.
• remove(index): Removes the element at position index from the vector. The 
removed element is returned. The index must be within the valid range.
• indexOf(item): Returns the index of the element containing the given item. 
The item must be in the list.
• reverse(): Performs a list reversal by reversing the order of the elements 
within the vector.



from ezarrays import Array

class FixedVector :
# Creates a new empty fixed vector with the given maximum capacity.
   def __init__(self, maxSize) :
 self._theItems = Array(maxSize)
 self._numItems = 0


Presumably the Array created has maxSize items already created? If so of 
what type? Or is it type agnostic?



# Returns the number of elements contained in the vector.
   def __len__(self) :
 return self._numItems


I'd probably prefer to return the actual number of elements rather than 
rely on your counter being accurate. is I assume len(self._theItems) 
will return an accurate result?


Of if the Array really is fixed size maybe the correct answer
is return maxSize?

Or should it only return the number of non-empty items?


# Returns a Boolean indicating if the vector is full.
   def isFull(self) :
 return self._numItems == len(self._theItems)


This suggests that your len() returns the number of non empty cells and 
len(Array) returns the maxsize value?



# Returns the element stored in the vector at position index.
# The value of index must be within the valid range.
   def __getitem__(self, index) :
 assert index >= 0 and index < self._numItems, "Index out of Range."
 return self._theItems[index]


I don;t think an assert is the correct solution here, it wouyld be 
better to raise an IndexError exception. This would be consistent with 
what Python does with the built in list type.




# Sets the element at position index to contain the given item. The
# value of index must be within the valid range, which includes one
# position beyond the end of the vector. In the latter case, the item
# is simply appended onto the end.
   def __setitem__(self, index, item) :
 assert index >= 0 and index <= self._numItems, "Index out of range."


Same issue here.


 if index == self._numItems :
   self.append(item)
 else :
   self._theItems[index] = item


If Array returns a fixed size array can't you just always assign to the 
index position. In other words does the array need to be filled
in a sequential manner or could you have a 'hole' in the middle (one of 
the few things an array allow

[Tutor] Strengths & weaknesses of Python lists compared to "old school" arrays [Was "Fixed Vector Array"]

2015-03-04 Thread boB Stepp
On Wed, Mar 4, 2015 at 12:04 PM, Alan Gauld  wrote:
> On 04/03/15 15:40, niyanax...@gmail.com wrote:
>>
>> Need help trying to implement insert, remove, indexof, and reverse
>> functions.
>>
>> I tried to do them but am not sure if it is correct. I am struggling with
>> arrays.
>
>
> I;m not sure why so many courses seem to insist on teaching old school
> arrays using Python. It involves rewriting functions that already exist, in
> superior form in the standard library. Reinventing the wheel is a bad
> practice and it seems a shame that some teachers appear to want to teach bad
> habits!

Alan's comments here have got me to wondering. I originally first
learned of programming via FORTRAN and BASIC (various incarnations).
These used multidimensional array-based thinking. Of course, these
were (then) primarily used to process numerical data. And that is what
I used them for. I don't recollect anything similar to Python lists in
those languages way back then. In doing physics-related programming it
was not unusual to have a need for 3- or 4-dimensional arrays, and
sometimes more. OTH, Python lists are *very* flexible in how they can
be implemented and especially in what can be stored (objects they
point to) in them. So I thought I would ask the question:

What are the strengths and weaknesses of Python lists compared to "old
school" arrays?
1) If the data involved is strictly numerical, does this alter
your answer? Especially if multidimensional matrix calculations are
involved?
2) If either numerical or string data is allowed to be stored,
does this change your answer?
3) Have "old school" arrays evolved into something more flexible
in most programming languages?
4) Are there other clarifying questions I should be asking on this
topic that are not occurring to me?

[...]

> If Array returns a fixed size array can't you just always assign to the
> index position. In other words does the array need to be filled
> in a sequential manner or could you have a 'hole' in the middle (one of the
> few things an array allows that a list doesn't - although you can just use a
> dictionary if that's really important!

What is a complete list of the "things" array-style thinking allow,
but that Python lists don't? And for those "things", what would be a
Python way of handling them?

One thing that I have noticed in my recent Python efforts is that when
I am implementing an array-like object as a list, that when the number
of dimensions gets beyond two, the Python method of addressing
individual data items seems a bit clunky to me. For instance, if I
wanted to address an item in a 3-dimensional array, I would use
something like (x, y, z) whereas the Python list form amounts to
[x][y][z] . Of course, all attempts to print out an entire array or
list if n >= 3 will not display well. But this slight "clunkiness" in
addressing pales in comparison to the power and flexibility of the
"stuff" I can store in a list compared to an array (unless they have
substantially evolved). And I have greatly enjoyed making use of this
flexibility!

-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List of ints

2015-03-04 Thread Albert-Jan Roskam


- Original Message -

> From: Mark Lawrence 
> To: tutor@python.org
> Cc: 
> Sent: Wednesday, March 4, 2015 10:20 AM
> Subject: Re: [Tutor] List of ints
> 
> On 04/03/2015 00:25, Steven D'Aprano wrote:
>>  On Tue, Mar 03, 2015 at 04:50:41PM +1000, Phil wrote:
>> 
>>>  count [0] += 1
>>> 
>>>  This fails with the following error;
>>> 
>>>  TypeError: 'int' object is not iterable
>> 
>>  I know that others have already solved the problem, but here is
>>  something which might help you solve similar problems in the future.
>>  The way to debug simple things like this is quite simple:
>> 
>>  print count[0]
>> 
>>  which will show you that count[0] is a list [0], not an int 0, and you
>>  are trying to add [0]+1 which doesn't work.
>> 
>>  Never under-estimate the usefulness of a few print calls when debugging.
>> 
>> 
> 
> About time we threw in the use of the interactive interpreter for trying 
> code snippets as well, just for good measure :)


Perhaps both at the same time using the PDB debugger from within IPython?

%run -d -b  myscript.py

d = debug, b = breakpoint

inside pdb: c = continue, q = quit, p = print, r = return value of current 
function, s = step, and more: https://docs.python.org/2/library/pdb.html
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fixed Vector Array

2015-03-04 Thread Danny Yoo
On Wed, Mar 4, 2015 at 9:51 AM, Danny Yoo  wrote:
> Can someone point out how to write and run tests to Ni'Yana?  I'm busy at
> the moment.  Thanks!


I'm still too busy to answer your question directly, but I can point
to you a recent post on unit testing a class:

https://mail.python.org/pipermail/tutor/2015-March/104387.html

Read that mailing list thread.  You should be testing your code.  It
appears that you haven't written tests from your earlier response, and
that's a bit concerning!  Your class should be teaching your basic
skills, and testing is one of those basic skills.  Otherwise, you
won't have any independent confirmation that the code is actually
doing something useful.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fixed Vector Array

2015-03-04 Thread Dave Angel

On 03/04/2015 10:40 AM, niyanax...@gmail.com wrote:

Need help trying to implement insert, remove, indexof, and reverse functions.

I tried to do them but am not sure if it is correct. I am struggling with 
arrays.


This is python and using ezarrays.



I don't know any Python that includes something called ezarrays in its 
library.  What version of Python are you using, and where do you get 
ezarrays?


In general, the tutor list is for learning about Python and its standard 
library.  You might get lucky, and find someone who knows your special 
library, but that's more likely on either python-list, or in a forum 
that is specifically for that library.


We'll try to help here, but it'll be based on a bunch of guesses, rather 
than experience.  Not the best way to get help.


I'm going to speculate and guess you're talking about
http://courses.necaiseweb.org/EZArrays/EZArrays




Assignment:


Seems like it's trying to teach you pascal, or java, not Python.



A fixed vector is very similar to the vector in that it is a sequence container 
that can grow and shrink as needed and include many useful operations. But the 
fixed vector has a maximum capacity beyond which it can not expand.

• FixedVector(maxSize): Creates an empty fixed vector with the given maximum 
capacity.

• length(): Returns the number of elements contained in the vector.


You called the method __len__(), rather than the requested length().
It's not clear what they want here, but it's a reasonable guess that 
they want 1+ the index of the highest item that isn't None.




• isFull(): Returns a Boolean indicating if the vector is full.

• getitem(index): Returns the element stored in the vector at position index. 
The value of index must be within the valid range.


Again, you implemented __getitem__() rather than the requested getitem()


• setitem(index, item): Sets the element at position index to contain the given 
item. The value of index must be within the valid range, which includes one 
position beyond the end of the vector. In the latter case, the item is simply 
appended onto the end.


And again here.


• contains(item): Determines if the given item is contained in the vector.


And again here.



• toString(): Returns a string representation of the vector.


But you called it __str__



• append(item): Adds the given item to the end of the vector. An item can not 
be appended to a full vector.

• clear(): Removes all elements from the vector.

• insert(index, item): Inserts the given item into the vector at position 
index. The elements at and following the given position are shifted down to 
make room for the new item. The index must be within the valid range and the 
vector can not be full. If index is one position beyond the end of the vector, 
the item is appended onto the vector.

• remove(index): Removes the element at position index from the vector. The 
removed element is returned. The index must be within the valid range.



Not clear if this is supposed to be symmetric with insert.  If so, then 
you have to slide things left, like you slid them right in the previous 
method.



• indexOf(item): Returns the index of the element containing the given item. 
The item must be in the list.

• reverse(): Performs a list reversal by reversing the order of the elements 
within the vector.





My Code:


from ezarrays import Array


class FixedVector :
# Creates a new empty fixed vector with the given maximum capacity.
   def __init__(self, maxSize) :
 self._theItems = Array(maxSize)
 self._numItems = 0

# Returns the number of elements contained in the vector.
   def __len__(self) :
 return self._numItems

# Returns a Boolean indicating if the vector is full.
   def isFull(self) :
 return self._numItems == len(self._theItems)

# Returns the element stored in the vector at position index.
# The value of index must be within the valid range.
   def __getitem__(self, index) :
 assert index >= 0 and index < self._numItems, "Index out of Range."


assert should never be used to check data values, but only for program 
invariants.  It should be an if statement with a raise statement in the 
body.  Same thing for other asserts in this code.



 return self._theItems[index]

# Sets the element at position index to contain the given item. The
# value of index must be within the valid range, which includes one
# position beyond the end of the vector. In the latter case, the item
# is simply appended onto the end.
   def __setitem__(self, index, item) :
 assert index >= 0 and index <= self._numItems, "Index out of range."

 if index == self._numItems :
   self.append(item)


   At this point, the _numItems value is incorrect


 else :
   self._theItems[index] = item

# Determines if the given item is contained in the vector.
   def __contains__(self, item) :
 i = 0
 while i < self._numItems :
   if self._theItems[i] == item :
   

[Tutor] Python 3 - bugs or installation problem

2015-03-04 Thread Phil
I hope this is not another embarrassingly obvious answer to a simple 
question.


Python 3, under Kubuntu.

xrange() fails whereas range() is accepted. Could this be an 
installation problem?


phil@Asus:~/Python$ python3
Python 3.4.2 (default, Oct  8 2014, 13:18:07)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> for row in xrange(0,12):
... print(row)
...
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'xrange' is not defined
>>>

Under IDLE 3:

for row in xrange(0,12):
print('test ',row)

xrange() is accepted but prints the following:

('test ', 0)
('test ', 1)
('test ', 2)
('test ', 3)
('test ', 4)

Whereas it should be printed as:

test 0
test 1
etc

Could this be another Python 3 installation problem, this time with print()?

Under the Python 3 interpreter using range() and not xrange() the 
printout is correct.


--
Regards,
Phil
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python 3 - bugs or installation problem

2015-03-04 Thread boB Stepp
On Wed, Mar 4, 2015 at 7:53 PM, Phil  wrote:
> I hope this is not another embarrassingly obvious answer to a simple
> question.
>
> Python 3, under Kubuntu.
>
> xrange() fails whereas range() is accepted. Could this be an installation
> problem?
> etc

This may fall into the obvious answer. ~(:>))

My Python reference says that the xrange function was discontinued in
Python 3, which you are using.

"In Python 3.x, the original range() function is changed to return an
iterable instead of producing a result in memory, and thus subsumes
and [sic?] Python 2.x's xrange(), which is removed." -- Python Pocket
Reference, 5th ed., by Mark Lutz.

-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python 3 - bugs or installation problem

2015-03-04 Thread Dave Angel

On 03/04/2015 09:11 PM, boB Stepp wrote:

On Wed, Mar 4, 2015 at 7:53 PM, Phil  wrote:

I hope this is not another embarrassingly obvious answer to a simple
question.

Python 3, under Kubuntu.

xrange() fails whereas range() is accepted. Could this be an installation
problem?
etc


This may fall into the obvious answer. ~(:>))

My Python reference says that the xrange function was discontinued in
Python 3, which you are using.

"In Python 3.x, the original range() function is changed to return an
iterable instead of producing a result in memory, and thus subsumes
and [sic?] Python 2.x's xrange(), which is removed." -- Python Pocket
Reference, 5th ed., by Mark Lutz.



And the other half of the answer is that your IDLE is apparently using 
the Python 2 interpreter, in which print is defined differently.



--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor