[Tutor] Help needed

Alan Gauld alan.gauld at btinternet.com
Sat Apr 26 10:41:33 CEST 2014


On 26/04/14 01:46, Suhana Vidyarthi wrote:

> I have this file:
>
> 1,3,5,0.03
>
> 2,3,5,5,4,0.11
>
> 3,3,5,5,4,5,8,0.04
....
> And each line is interpreted as:
>
>   * 1,3,5,0.03-> This line means 1 link can be down i.e. between 3—5
>     with a probability of failure *0.03*
>   * 2,3,5,5,4,0.11 -> This line means 2 links can be down i.e. between
>     3--5 and 5--4 with a probability of failure *0.11* (for each link)
>   * 3,3,5,5,4,5,8,0.04 -> Similarly this line means 3 links can be down
>     i.e. between 3--5 , 5—4 and 5—8 with a probability of failure *0.04*
>     (for each link)
...
> I want to create two arrays using the above file (Links array and Prob
> array) that should give following output:
>
> *Links *= { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18]
> [10,13] [14,13] [17,13] [12,13] [11,6] [11,9][11,12] [11,19] [19,20]
> [15,20] [21,20] [20,21] [21,16] [21,22] }
>
> *Prob *= {[0.28] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] [0.08]
> [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]}

I don't understand how you develop this? The first list has 22 items the 
second 17. I would have expected them to be the same?

Also why do you want these two lists? What do you plan on doing with 
them? I would have thought a mapping of link to probability would be 
much more useful? (mapping => dictionary)

> So the first element in Links array is [3,5] and its probability of
> failure is the first element in Prob array i.e. 0.28
>
> Can anyone help me with this please?

Do you know how to open and read a file line by line?
Do you know how to extract the elements from a line?
Do you know how to define a list and add elements to it?
Do you know how to find an element in a list?
Do you know how to modify an element in a list?

If you know the above you have all the pieces you need to complete the 
task. If not tell us which bits you are stuck with.

And show us some code, we will not do your work for you but are happy to 
help.

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



More information about the Tutor mailing list