[Tutor] Python String Help

Mark Lawrence breamoreboy at yahoo.co.uk
Tue Oct 29 15:35:50 CET 2013


On 29/10/2013 10:18, Walter Prins wrote:
> Hi,
>
>
> On 28 October 2013 20:55, Alex Tenno <alex.tenno at gmail.com
> <mailto:alex.tenno at gmail.com>> wrote:
>
>     Hey everyone,
>
>     I'm encountering a problem with a python function that I am supposed
>     to create.
>
>
> OK.  You should try to actually write the function yourself first, then
> give concrete details about what you've tried, and how you're stuck.
>
>     I want my function to look at a string, and then replace each letter
>     in the string with its relative position in the alphabet. for
>     example, 'abcde' would return '12345', 'zabd' would return '4123',
>     and 'xpft' would return '4213'.
>
>
> Is this the exact problem statement or are you paraphrasing?  I'd like
> to make sure I understand the question.
>
>     I have been given hints that tell me "You may want to iterate over
>     the letters ch in s as in the for loop above, and inside that for
>     loop, count the number of letters that are in s and come before the
>     loop variable ch. You will also need an accumulator to build the
>     permutation the function will return." any help would be greatly
>     appreciated.
>
>
> OK so you've been given some high level idea of what to do. Try to break
> this down to more concrete steps, perhaps firstly still only pseudocode,
> and eventually translate that to Python.
>
> The idea embodied in the suggestion is based on the observation that you
> can determine the relative position in the alphabet, of each character
> in the word by, looking the letter in the word, and then counting how
> many /distinct/ letters in the word are smaller than or equal to the
> letter under scrutiny.  For the smallest letter, this will by definition
> only count the letter itself (if you compare with every distinct letter
> in the word), resulting in a count of 1.  For the second smallest
> letter, this will therefore result in the smallest letter and the 2nd
> smallest letter being counted, resulting in a count of 2.  And so on.
> These counts then give the relative position of each letter in the
> alphabet.  Amit's suggestion boils down to the same idea -- sort the set
> of distinct letters in the string, and then look up the relative
> position of each letter in your original string in this sorted set.
> Next questions to think about: How do you calculate the set of distinct
> letters in the string? (Hint: Look at set() function.) How do you
> calculate the count of smaller letters from this distinct set?  (Hint:
> Loop over the set and count as you go.) Or convert this set to a sorted
> list to then lookup against.  (Hint: Look at the list() function and the
> sorted() function.)
>
> Walter
>

An extremely strong hint for all newbies is to try any and all of the 
above suggestions at the interactive prompt.  This tool is one of 
Python's great strengths so use it.

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence



More information about the Tutor mailing list