Re: [Tutor] Roman to digital (pseudocode)

2007-03-14 Thread János Juhász
Hi All,


> - A dictionary will help you look up values, but not rules. It does
> not retain its order and order is essential. Instead, create a tuple
> of the roman numerals in ascending order (roman). Create a paired
> tuple with the base 10 value (baseten).

> Now get an element from the string you want to test -- use the index
> value in roman to test the rules -- you will know what is higher,
> lower, and the same

> Then look for things like the following:

> - does the following element have a lower index in roman? if yes, you
> know the value (match indexes from the tuples) -- step forward
> - what if the follower is the same? then check the one after, if it is
> the same (and the following digit is lower) then you have a value. --
> step over the last matching digit
> - if the following value is higher, it must be by only one unit, if so
> you have a value, but also a new rule: the following digit must be
> lower than the last digit of the pair.

> and so on.
> not pseudocode I know, and I am certain there are better ways to do
> this, but maybe something here helps.


#
roman_codec = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1}

original = raw_input('Enter a roman number:')
roman = original.upper()
roman = list(roman) # I would like to use pop instead roman = roman[1:]
roman.reverse() # pop picks from the end, so prepare for it
decimal = [roman_codec[ch] for ch in roman] # turn the chars into decimal

result = 0

while len(decimal):
act = decimal.pop()
if len(decimal) and act < max(decimal): act = -act # if there is a 
char with higher score it is minus :)
result += act

print original, '=', result


Python is nearly pseudocode.
This is the reason we like it so much.


Best regards,
Janos Juhasz
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Roman to digital (pseudocode)

2007-03-13 Thread Kent Johnson
Bob Gailer wrote:
> Deep breath  and big step back.
> 
> The problem is not just how to code this in Python, but how to parse a 
> language (in this case Roman Numbers).
> 
> I have studied formal language theory but am not an expert. So here's my 
> take on an algorithm that would save a lot of stress.
> 
> Create a dictionary with
> keys for each roman letter and each "pair" (iv, ix, xl, xc, etc)
> values are the corresponding decimal values.
> Starting at the left of the roman_input and repeating until the end of 
> the roman_input
>   Take the next pair of roman_input.
>   If it is in the dictionary
> add the decimal value and step to the next letter.
>   Else
> take the leftmost letter of that pair
> If it is in the dictionary
>   add the decimal value and step to the next letter.
>Else
>  complain about bad letter

Or make a list of all the pairs and values, followed by all the single 
letters and values, and just find the first match from the list against 
the start of the test string.

Neither of these is really a parser, though; they allow badly formed 
numbers like IICCXM.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Roman to digital (pseudocode)

2007-03-13 Thread Kent Johnson
Alan Gilfoy wrote:
> This, I heard, is more difficult than digital-to-Roman, since you have  
> to "read" the subtractive cases, with a smaller numeral placed before  
> a larger numeral, without simply adding all the numerals' values up
> 
> I'm going to use a raw_input prompt to ask the user which Roman  
> numeral he/she wants to convert. How do I "screen" for inputs that  
> have characters besides "I", "V", "X", "L", "C", "D", or "M"?

You could have a string containing all the allowed characters and make 
sure each input character is in the allowed list.
> 
> Second Python question:
> 
> I know there's a way to "find out" the name of the first item in a list
> (ListName[0]), but is there a way to find out the first character of a string?

Lists and strings are both sequences and many of the same operations 
work on both. (All of the immutable sequence operations work on both.) 
So MyString[0] is the first character in a string.
> 
> Also, is there a way to "ask" Python what characters are before and  
> after the character in the string that you're asking about?
> 
> For example, usign the sample string "MCMXVII" (1917):
> 
> How would you ask Python:
> "What's the 3rd character in this string?" (returns "M")
s[2]

> "What's before that character?" (returns "C")
s[1] or maybe s[2-1] if the 2 is actually in a variable.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Roman to digital (pseudocode)

2007-03-13 Thread Clay Wiedemann
I am new to python and programming, so not sure if I should weigh in
here. Nonetheless . . .

Creating a dictionary seems fair, but I wonder about using pairs
rather than single letters. Roman numerals are constructed from strict
rules other than the allowable letter set -- here is the relevant
wikipedia bit:

Rules regarding Roman numerals often state that a symbol representing
10x may not precede any symbol larger than 10x+1. For example, C
cannot be preceded by I or V, only by X (or, of course, by a symbol
representing a value equal to or larger than C). Thus, one should
represent the number "ninety-nine" as XCIX, not as the "shortcut" IC.
However, these rules are not universally followed.

Although the shortcut may be easier for people, the universal notation
may be easier to define programmatically. I am wondering what would be
the best way to create the rules

 - A dictionary will help you look up values, but not rules. It does
not retain its order and order is essential. Instead, create a tuple
of the roman numerals in ascending order (roman). Create a paired
tuple with the base 10 value (baseten).

Now get an element from the string you want to test -- use the index
value in roman to test the rules -- you will know what is higher,
lower, and the same

Then look for things like the following:

- does the following element have a lower index in roman? if yes, you
know the value (match indexes from the tuples) -- step forward
- what if the follower is the same? then check the one after, if it is
the same (and the following digit is lower) then you have a value. --
step over the last matching digit
- if the following value is higher, it must be by only one unit, if so
you have a value, but also a new rule: the following digit must be
lower than the last digit of the pair.

and so on.
not pseudocode I know, and I am certain there are better ways to do
this, but maybe something here helps.

-c














On 3/13/07, Bob Gailer <[EMAIL PROTECTED]> wrote:
> Deep breath  and big step back.
>
> The problem is not just how to code this in Python, but how to parse a
> language (in this case Roman Numbers).
>
> I have studied formal language theory but am not an expert. So here's my
> take on an algorithm that would save a lot of stress.
>
> Create a dictionary with
> keys for each roman letter and each "pair" (iv, ix, xl, xc, etc)
> values are the corresponding decimal values.
> Starting at the left of the roman_input and repeating until the end of
> the roman_input
>   Take the next pair of roman_input.
>   If it is in the dictionary
> add the decimal value and step to the next letter.
>   Else
> take the leftmost letter of that pair
> If it is in the dictionary
>   add the decimal value and step to the next letter.
>Else
>  complain about bad letter
>
> --
> Bob Gailer
> 510-978-4454
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


-- 

- - - - - - -

Clay S. Wiedemann

voice: 718.362.0375
aim: khlav
wii: 3905 4571 6175 2469
twitter: seastokes
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Roman to digital (pseudocode)

2007-03-13 Thread Bob Gailer
Deep breath  and big step back.

The problem is not just how to code this in Python, but how to parse a 
language (in this case Roman Numbers).

I have studied formal language theory but am not an expert. So here's my 
take on an algorithm that would save a lot of stress.

Create a dictionary with
keys for each roman letter and each "pair" (iv, ix, xl, xc, etc)
values are the corresponding decimal values.
Starting at the left of the roman_input and repeating until the end of 
the roman_input
  Take the next pair of roman_input.
  If it is in the dictionary
add the decimal value and step to the next letter.
  Else
take the leftmost letter of that pair
If it is in the dictionary
  add the decimal value and step to the next letter.
   Else
 complain about bad letter

-- 
Bob Gailer
510-978-4454

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Roman to digital (pseudocode)

2007-03-08 Thread Adam Bark

On 08/03/07, Alan Gilfoy <[EMAIL PROTECTED]> wrote:


This, I heard, is more difficult than digital-to-Roman, since you have
to "read" the subtractive cases, with a smaller numeral placed before
a larger numeral, without simply adding all the numerals' values up

I'm going to use a raw_input prompt to ask the user which Roman
numeral he/she wants to convert. How do I "screen" for inputs that
have characters besides "I", "V", "X", "L", "C", "D", or "M"?

Second Python question:

I know there's a way to "find out" the name of the first item in a list
(ListName[0]), but is there a way to find out the first character of a
string?

Also, is there a way to "ask" Python what characters are before and
after the character in the string that you're asking about?

For example, usign the sample string "MCMXVII" (1917):

How would you ask Python:
"What's the 3rd character in this string?" (returns "M")
"What's before that character?" (returns "C")

Pseudocode time:

If character Y is "M":
and the character before character Y is "C",
add 900 to digital_result
and remove that "C" and that "M" from the string.
#How would you do *that*?

and the character before character Y is another "M", or if character Y
is the first character in the string,
add 1000 to digital_result
and remove that "M" from the string.



you can index a string just like a list:

roman_numeral = "MCMXVII"
roman_numeral[2]

'M'

roman_numeral[2-1]

'C'

HTH
Adam
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Roman to digital (pseudocode)

2007-03-08 Thread Alan Gilfoy
This, I heard, is more difficult than digital-to-Roman, since you have  
to "read" the subtractive cases, with a smaller numeral placed before  
a larger numeral, without simply adding all the numerals' values up

I'm going to use a raw_input prompt to ask the user which Roman  
numeral he/she wants to convert. How do I "screen" for inputs that  
have characters besides "I", "V", "X", "L", "C", "D", or "M"?

Second Python question:

I know there's a way to "find out" the name of the first item in a list
(ListName[0]), but is there a way to find out the first character of a string?

Also, is there a way to "ask" Python what characters are before and  
after the character in the string that you're asking about?

For example, usign the sample string "MCMXVII" (1917):

How would you ask Python:
"What's the 3rd character in this string?" (returns "M")
"What's before that character?" (returns "C")

Pseudocode time:

If character Y is "M":
and the character before character Y is "C",
add 900 to digital_result
and remove that "C" and that "M" from the string.
#How would you do *that*?

and the character before character Y is another "M", or if character Y  
is the first character in the string,
add 1000 to digital_result
and remove that "M" from the string.




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor