[Tutor] python help

Alan Gauld alan.gauld at freenet.co.uk
Wed Feb 8 23:06:57 CET 2006


>  I have a few doubts in python programming. C if any of u can help me out.

Have no doubts - Python can do all of this.

>  In a file called string_stuff.py i have to write a function called 
> frequencies
> that takes a string as a parameter and returns a dictionary where the keys
> are the characters from the string and each value is an integer indicating 
> the
> number of times the key appeared in the string.

What is your doubt here?
Can you create a function called frequencies that has a string parameter
and returns a dictionary?

Can you read a string character by character into said dictionary?

Can you increment the count for each character as you do so?

Where is the problem that you need help with?

>  Similary in the same file string_stuff.py, have to write a function 
> called locations
> that takes a string as a parameter and returns a dictionary where the keys 
> are
> the characters from the string and each value is a list indicating the 
> indices in
> the string at which the key appears, sorted in increasing order.

Similarly this is a pretty clear description of how to solve the problem
What bit are you having trouble with?
Show us your code and we might be able to help.

>  In the same file string_stuff.py, write a function called concordance 
> that
> takes an open file as a parameter and returns a dictionary where the
> keys are the strings from the file and each value is a list of line 
> numbers
> of the lines in which the key appeared, sorted in increasing order.
> Start counting at 0. Each line number should appear at most once in
> a list, even if a word appears twice on a line. You may assume that the
> input consists only of alphabetic letters (a-z, A-Z) and whitespace.

Slightly trickier but not much if you've already done the first two 
problems.
Hint: Remember that you can store lists in dictionaries too.

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list