Re: [Tutor] roman to arabic

2012-02-27 Thread Alan Gauld

On 27/02/12 14:41, Cranky Frankie wrote:


A quote worth mentioning here is:  If you need more than 3 levels
of indentation, you're screwed

I've always wondered about this quote. I'm thinking it means you might
want to have functions or subroutines, depending on the language, to
do big chunks of logic,


That's one option.

The OP also had the option of using a lookup table(dictionary)
or just using elifs instead of nested ifs.

Often a different algorithm helps.

Also functional programming (ie. not just procedural!) can reduce the 
numbers of indentation levels. (See the FP topic in my tutor for some 
examples of this.)


Simple hiding of indentation levels inside a function is kind of
the last resort in reducing indentation levels. Generally deep 
indentation reveals problems in the basic algorithm and/or

data structures.


offers almost unlimited indentation, so it's up to the programmer to
not use it?


Correct, this is a program design decision not a language feature.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] roman to arabic

2012-02-27 Thread Albert-Jan Roskam
Hi,

I wrote a little program that does the conversion (I won't post it because it 
would be a spoiler for the OP). The one thing I don't know, though, is how to 
formalise
that it is not allowed to write something like X, but instead just II. 
Or not DM but simply D. The rule is to write it the shortest possible way. Am I 
wrong or is it really not trivial at all to write an error class for such 
lengthy roman numerals?

 
Regards,
Albert-Jan


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a 
fresh water system, and public health, what have the Romans ever done for us?
~~ 



 From: Alan Gauld alan.ga...@btinternet.com
To: tutor@python.org 
Sent: Monday, February 27, 2012 4:37 PM
Subject: Re: [Tutor] roman to arabic
 
On 27/02/12 14:41, Cranky Frankie wrote:

 A quote worth mentioning here is:  If you need more than 3 levels
 of indentation, you're screwed
 
 I've always wondered about this quote. I'm thinking it means you might
 want to have functions or subroutines, depending on the language, to
 do big chunks of logic,

That's one option.

The OP also had the option of using a lookup table(dictionary)
or just using elifs instead of nested ifs.

Often a different algorithm helps.

Also functional programming (ie. not just procedural!) can reduce the numbers 
of indentation levels. (See the FP topic in my tutor for some examples of 
this.)

Simple hiding of indentation levels inside a function is kind of
the last resort in reducing indentation levels. Generally deep indentation 
reveals problems in the basic algorithm and/or
data structures.

 offers almost unlimited indentation, so it's up to the programmer to
 not use it?

Correct, this is a program design decision not a language feature.

-- Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


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


Re: [Tutor] roman to arabic

2012-02-27 Thread Alan Gauld

On 27/02/12 16:28, Albert-Jan Roskam wrote:


possible way. Am I wrong or is it really not trivial at all to write an
error class for such lengthy roman numerals?


Its non trivial, you need something like a state machine to detect valid 
transitions as you read each character.


Alan G.

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


Re: [Tutor] roman to arabic

2012-02-27 Thread Evert Rol
 I wrote a little program that does the conversion (I won't post it because it 
 would be a spoiler for the OP). The one thing I don't know, though, is how to 
 formalise
 that it is not allowed to write something like X, but instead just 
 II. Or not DM but simply D. The rule is to write it the shortest possible 
 way. Am I wrong or is it really not trivial at all to write an error class 
 for such lengthy roman numerals?

Mark Pilgrim wrote whole sections on Roman numerals in his Dive Into Python 
tutorial. While the numerals pop up in various examples throughout the chapters 
of the tutorial, for this, the tutorial on unit testing may proof helpful: 
http://www.diveintopython.net/unit_testing/romantest.html
Somewhere in that example, there's unit testing code just for examples as 
above. From the unit test, follow the tutorial into chapter 14 to see how it's 
done.

Hope that helps,

  Evert



  
 Regards,
 Albert-Jan
 
 ~~
 All right, but apart from the sanitation, the medicine, education, wine, 
 public order, irrigation, roads, a 
 fresh water system, and public health, what have the Romans ever done for us?
 ~~ 
 From: Alan Gauld alan.ga...@btinternet.com
 To: tutor@python.org 
 Sent: Monday, February 27, 2012 4:37 PM
 Subject: Re: [Tutor] roman to arabic
 
 On 27/02/12 14:41, Cranky Frankie wrote:
 
  A quote worth mentioning here is:  If you need more than 3 levels
  of indentation, you're screwed
  
  I've always wondered about this quote. I'm thinking it means you might
  want to have functions or subroutines, depending on the language, to
  do big chunks of logic,
 
 That's one option.
 
 The OP also had the option of using a lookup table(dictionary)
 or just using elifs instead of nested ifs.
 
 Often a different algorithm helps.
 
 Also functional programming (ie. not just procedural!) can reduce the numbers 
 of indentation levels. (See the FP topic in my tutor for some examples of 
 this.)
 
 Simple hiding of indentation levels inside a function is kind of
 the last resort in reducing indentation levels. Generally deep indentation 
 reveals problems in the basic algorithm and/or
 data structures.
 
  offers almost unlimited indentation, so it's up to the programmer to
  not use it?
 
 Correct, this is a program design decision not a language feature.
 
 -- Alan G
 Author of the Learn to Program web site
 http://www.alan-g.me.uk/
 
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] roman to arabic

2012-02-27 Thread Albert-Jan Roskam
Ah, nice! Thank you! Sseeing the formal rules makes it easier: 
http://www.diveintopython.net/unit_testing/stage_5.html
A regex is used to test whether the roman numeral is valid. Very elegant!

 
Regards,
Albert-Jan


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a 
fresh water system, and public health, what have the Romans ever done for us?
~~ 



 From: Evert Rol evert@gmail.com
To: Albert-Jan Roskam fo...@yahoo.com 
Cc: Python Tutor tutor@python.org 
Sent: Monday, February 27, 2012 5:42 PM
Subject: Re: [Tutor] roman to arabic
 
 I wrote a little program that does the conversion (I won't post it because 
 it would be a spoiler for the OP). The one thing I don't know, though, is 
 how to formalise
 that it is not allowed to write something like X, but instead just 
 II. Or not DM but simply D. The rule is to write it the shortest possible 
 way. Am I wrong or is it really not trivial at all to write an error class 
 for such lengthy roman numerals?

Mark Pilgrim wrote whole sections on Roman numerals in his Dive Into Python 
tutorial. While the numerals pop up in various examples throughout the 
chapters of the tutorial, for this, the tutorial on unit testing may proof 
helpful: http://www.diveintopython.net/unit_testing/romantest.html
Somewhere in that example, there's unit testing code just for examples as 
above. From the unit test, follow the tutorial into chapter 14 to see how it's 
done.

Hope that helps,

  Evert



  
 Regards,
 Albert-Jan
 
 ~~
 All right, but apart from the sanitation, the medicine, education, wine, 
 public order, irrigation, roads, a 
 fresh water system, and public health, what have the Romans ever done for us?
 ~~ 
 From: Alan Gauld alan.ga...@btinternet.com
 To: tutor@python.org 
 Sent: Monday, February 27, 2012 4:37 PM
 Subject: Re: [Tutor] roman to arabic
 
 On 27/02/12 14:41, Cranky Frankie wrote:
 
  A quote worth mentioning here is:  If you need more than 3 levels
  of indentation, you're screwed
  
  I've always wondered about this quote. I'm thinking it means you might
  want to have functions or subroutines, depending on the language, to
  do big chunks of logic,
 
 That's one option.
 
 The OP also had the option of using a lookup table(dictionary)
 or just using elifs instead of nested ifs.
 
 Often a different algorithm helps.
 
 Also functional programming (ie. not just procedural!) can reduce the 
 numbers of indentation levels. (See the FP topic in my tutor for some 
 examples of this.)
 
 Simple hiding of indentation levels inside a function is kind of
 the last resort in reducing indentation levels. Generally deep indentation 
 reveals problems in the basic algorithm and/or
 data structures.
 
  offers almost unlimited indentation, so it's up to the programmer to
  not use it?
 
 Correct, this is a program design decision not a language feature.
 
 -- Alan G
 Author of the Learn to Program web site
 http://www.alan-g.me.uk/
 
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor



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


Re: [Tutor] roman to arabic

2012-02-26 Thread Mark Lawrence

On 26/02/2012 23:29, Sukhpreet Sdhu wrote:

Hi
I just wrote python code to convert roman to arabic numerals, but its not 
working.
Can you just check where the problem is and way to correct that.
So here is my python code
import string
print Welcome to the numeric conversion program
print Please enter command
data=raw_input()
now = 0
previous = 0
total = 0
if data == r:
 print Enter roman numeric to convert in arabic
 roman_numeric=string.swapcase(raw_input(Enter the Roman Numeral to convert to 
arabic))
  if roman_numeric == (M or D or L or C or L or X or V or I):
  Length = len(roman_numeric) - 1
  i = roman_numeric[Length]
  if i == M:
  now = 1000
  if i == D:
  now = 500
  if i == C:
  now = 100
  if i == L:
  now = 50
  if i == X:
  now = 10
  if i == V:
  now = 5
  if i == I:
  now = 1
  acc = now
  if (previous= now):
  total += acc-prvious
  print The total is,total
  if (previous= now):
  total += acc-prevous
  print The total is,total
  else :
  if data == a :
  print Arabic number to 
convert

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



I'm sorry but the code is so badly formatted via Thunderbird that it's 
pretty much impossible to work out what you intend.  Try resending with 
the code correctly formatted.  Also put print statements into the code 
so that you can follow the flow and see what it's doing, then you'll be 
able to make some progress yourself.


--
Cheers.

Mark Lawrence.

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


Re: [Tutor] roman to arabic

2012-02-26 Thread Walter Prins
Hi ,

On 26 February 2012 23:29, Sukhpreet Sdhu sukhpreet2294si...@ymail.com wrote:
 Hi
 I just wrote python code to convert roman to arabic numerals, but its not 
 working.
How exactly is it not working?  Please don't make us guess or work
more than neccesary trying to help you...


 Can you just check where the problem is and way to correct that.
 So here is my python code
 import string
 print Welcome to the numeric conversion program
 print Please enter command
 data=raw_input()
 now = 0
 previous = 0
 total = 0
 if data == r:
     print Enter roman numeric to convert in arabic
     roman_numeric=string.swapcase(raw_input(Enter the Roman Numeral to 
 convert to arabic))
  if roman_numeric == (M or D or L or C or L or X or V or I):
  Length = len(roman_numeric) - 1
  i = roman_numeric[Length]
  if i == M:
  now = 1000
  if i == D:
  now = 500
  if i == C:
  now = 100
  if i == L:
  now = 50
  if i == X:
  now = 10
  if i == V:
  now = 5
  if i == I:
  now = 1
  acc = now
  if (previous = now):
  total += acc-prvious
  print The total is,total
  if (previous = now):
  total += acc-prevous
  print The total is,total
  else :
  if data == a :
  print Arabic number to 
 convert

    thanks
 sukhpreet sidhu

Is your code really indented like that?  A quote worth mentioning here
is:  If you need more than 3 levels of indentation, you're screwed
anyway, and should fix your program. --  Linus Torvalds

Now he was writing w.r.t. C/C++ but the principle holds for Python
also in general -- very highly nested levels of indentation are
indicative of some kind of program problem, and will likely cause you
to conceptually lose control of what the code's supposed to be doing.

Can you please explain in english (pseudocode) your algorithm for
converting a roman numeral string to arabic numbers, with a more
direct explanation of what you've tried and how your solution is not
working from what you're expecting.  Then we'll be able to help you
better and will not be left having to guess at how

All that said, apart from the indentation weirdness, a few more
offhand observations: I can see 3 different spellings for previous
in the code, which will obviously cause problems.  The logic to deal
with smaller numbers preceding larger numbers seem broken (though I've
not looked too closely).  The conditions both include equality (= and
=) which is likely wrong, the indentation is wrong and both
conditions seem to be doing the same thing, which must likewise be
wrong.  (You'd expect there to be some difference in handling the case
when the previous number is smaller that the current number vs when
it's larger...)

HTH,

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


Re: [Tutor] roman to arabic

2012-02-26 Thread Alan Gauld

On 26/02/12 23:29, Sukhpreet Sdhu wrote:


import string


Are you using a very old versioon opf python?
If not you should not use string.
The built in string methods can do everything you
want without resorting to string.


print Welcome to the numeric conversion program
print Please enter command
data=raw_input()
now = 0
previous = 0
total = 0
if data == r:
 print Enter roman numeric to convert in arabic
 roman_numeric=string.swapcase(raw_input(Enter the Roman Numeral to convert to 
arabic))


Are you sure you want swapcase()? I'd have thought uppercase()
would be more effective given the tests below.

Using built in methods that line becomes:

roman_numeric=raw_input(Enter the Roman Numeral to convert to 
arabic).uppercase()




  if roman_numeric == (M or D or L or C or L or X or V or I):


This is just wrong!
The parenthesised list will evaluate to True so you are testing  if the 
variable is True, which it will be if not empty.


You want:

if roman_numeric in (M,D,L,C,L,X,V,I):


  Length = len(roman_numeric) - 1


If the variable is one of the items in your list it is only 1 char long 
so you re setting Length to zero. Is that what you want?



  i = roman_numeric[Length]


If you really want to extract the last digit use a -1 index.
In which case you dshould do the same for the check on valid values...



  if i == M:
  now = 1000
  if i == D:
  now = 500


Are you sure you want this structure?
It would look a lot neater using elif

   if i == M:
   now = 1000
   elif i == D:
   now = 500
   elif i == C:
now = 100

But better still would be to use a dictionary:

values = { 'I':1, 'V':5, 'X':10,...'D':500, 'M':1000 }

now = values[i]


  acc = now
  if (previous= now):
  total += acc-prvious


spelling error in prvious


  print The total is,total
  if (previous= now):
  total += acc-prevous
  print The total is,total
  else :


This else doesn't line up with any if.

   if data == a :

else:
   if cond:

could just be

elif cond:

HTH,

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] roman to arabic

2012-02-26 Thread bob gailer

On 2/26/2012 6:29 PM, Sukhpreet Sdhu wrote:

Hi
I just wrote python code to convert roman to arabic numerals

[snip] Yuk what a  mess.

May I suggest you start with a much simpler program, which is to take a 
roman number between 1 and 3 (just i's) and convert that.


First describe how you'd convert the number by hand. Then convert that 
process to Python.


Test the program. Fix any problems. Report back to us success or a 
problem you don't know how to address. Tell us exactly what went wrong. 
(e.g. I entered iii expecting 3 and got 17 or if you get a traceback 
post it. For example when I run your program I get:


Traceback (  File interactive input, line 12
if roman_numeric == (M or D or L or C or L or X or V 
or I):

  ^
IndentationError: unindent does not match any outer indentation level

Then add v (now the number is between 1 and 8).

You will come up with a completely different program! And it will be the 
correct one.


Also provide meaningful prompts.

print Please enter command

If I was running your program and saw that I'd have to give up since I 
have no idea what is expected.


print Please enter command - r for roman-arabic

would be much better.

--
Bob Gailer
919-636-4239
Chapel Hill NC

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