# [Tutor] Help needed

Suhana Vidyarthi suhanavidyarthi at gmail.com
Sat Apr 26 19:10:37 CEST 2014

```Thanks for the response Alan. my clarifications are below:

On Sat, Apr 26, 2014 at 1:41 AM, Alan Gauld <alan.gauld at btinternet.com>wrote:

> 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*
>>
> ...
>
>> 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?
>

In the "Prob" array the elements are less because if you read the note
below: I said the links that are repeating for example [3,5] their
probabilities  get added and stored as a single value in the "Prob" array.

>
> 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)

I want these two lists because using the links and probs array, I will
check which link has the lowest probability. Here lowest probability means
the risk of failure for that link. So based on which link has least
probability of failure, I will use it to setup a connection (I have a
source and destination and the links mentioned above are the paths between
them) I want to select the path which has least failure probability.

Did it make sense?

>
>  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.
>
> I have been studying about it and have come up with my code.

> And show us some code, we will not do your work for you but are happy to
> help.
>
> I actually used the map and dictionary function to write the code, please
see the attachment. The only problem I am having is that when I try to
display the links and probabilities, they are not displayed in the order
of the file content. This is how it should display:

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.18] [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]}

However when I run my code, this is how the arrays are displayed:

[('10', '13'), ('14', '18'), ('7', '8'), ('15', '20'), ('5', '8'), ('5',
'4'), ('11', '9'), ('21', '22'), ('12', '13'), ('21', '20'), ('17', '13'),
('20', '21'), ('21', '16'), ('14', '10'), ('11', '12'), ('11', '19'),
('14', '13'), ('3', '5'), ('11', '6'), ('19', '20')]

Probability ->

[0.04, 0.06, 0.04, 0.24, 0.08, 0.15, 0.08, 0.27, 0.04, 0.29, 0.08, 0.27,
0.27, 0.04, 0.08, 0.08, 0.08, 0.18000000000000002, 0.08, 0.24]

Can you please see my code and help me find out what is wrong?

> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140426/0ded6db0/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pyth-new2.py
Type: text/x-python-script
Size: 1811 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20140426/0ded6db0/attachment-0001.bin>
```