[Tutor] Help needed with Python programming

2014-04-22 Thread Suhana Vidyarthi
My knowledge of coding is fairly limited and I am having a hard time
writing a Python code which might be pretty simple for you :-)

Here is what I am doing and I need help with:

I have a python code that shows a set of shortest paths between nodes A and
B. Now I have to select the least risky path among them. To do that I have
to consider the risk values of each link. I know how to calculate the
path's risk using its link value.

For example: There is a path between node A and B wiht two links.
Probability of failure for link 1 is 0.001 and for link 2 is 0.003. Here is
the link with its risk values:
  A oo-o B
   0.001   0.003
So the probability of the link being down will be: 1 - (0.999 x 0.997) =
0.996003

You can find the attached file with disaster risk values of each link.

For instance; first line is : 1,3,5,0.03   -- this means, first disaster
affects links 1-3 and 5-0 and its occurrence rate is 0.03. So you need to
assign link (1-3)'s risk to 0.03.
Then you will continue with the next disaster which is the one in the next
line. Note that, if a link gets affected by 2 disasters, you will add the
probability of those 2 disasters to find that link's risk.

If anyone can help me code the first line, I will be able to do the rest.
You need use array list and some functions like file reader and
delimiter I guess.

Thanks in advance.
1,3,5,0.03
2,3,5,5,4,0.11
3,3,5,5,4,5,8,0.04
2,5,8,7,8,0.04
3,14,10,14,13,17,13,0.04
1,14,18,0.06
4,10,13,14,13,17,13,12,13,0.04
4,11,6,11,9,11,12,11,19,0.08
3,19,20,15,20,21,20,0.24
1,21,20,0.05
3,20,21,21,16,21,22,0.27___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help needed with Python programming

2014-04-22 Thread Alan Gauld

On 22/04/14 02:16, Suhana Vidyarthi wrote:


I have a python code that shows a set of shortest paths between nodes A
and B.


It would help if you showed us this code. Otherwise we are
just making wild guesses about how you are modelling this.

Also knowing which Python version you are using would be good.


If anyone can help me code the first line, I will be able to do the
rest. You need use array list and some functions like file reader
and delimiter I guess.


Have you written these functions already?  Are they part of some
module or library you are using? Or is it the writing of these functions 
you want help with?


Graph or network analysis is a fairly standard math problem.
There are probably algorithms (or even solutions) in other
languages (or even in Python if you are lucky) that you can
convert if you do a search.

--
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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help needed with Python programming

2014-04-22 Thread Steven D'Aprano
On Mon, Apr 21, 2014 at 06:16:20PM -0700, Suhana Vidyarthi wrote:

[...]
 I have a python code that shows a set of shortest paths between nodes A and
 B. Now I have to select the least risky path among them. To do that I have
 to consider the risk values of each link. I know how to calculate the
 path's risk using its link value.
 
 For example: There is a path between node A and B wiht two links.
 Probability of failure for link 1 is 0.001 and for link 2 is 0.003. Here is
 the link with its risk values:
   A oo-o B
0.001   0.003
 So the probability of the link being down will be: 1 - (0.999 x 0.997) =
 0.996003

I don't think that calculation is correct. I think you mean that the 
probability of the link being UP is (0.999 x 0.997) = 0.996003, and the 
prob of it being DOWN is 1-0.996003 = 0.003997. So that path has a risk 
of 0.003997.


 You can find the attached file with disaster risk values of each link.
 
 For instance; first line is : 1,3,5,0.03   -- this means, first disaster
 affects links 1-3 and 5-0 and its occurrence rate is 0.03. So you need to
 assign link (1-3)'s risk to 0.03.
 Then you will continue with the next disaster which is the one in the next
 line. Note that, if a link gets affected by 2 disasters, you will add the
 probability of those 2 disasters to find that link's risk.
 
 If anyone can help me code the first line, I will be able to do the rest.
 You need use array list and some functions like file reader and
 delimiter I guess.

Okay, let's start with reading the file. 

filename = path/to/file.txt

Notice that I use forward slashes. Even if you are on Windows, you 
should code your paths with forward slashes. Either that, or you have to 
double every backslash:

# on Windows either of these will be okay
filename = C:/path/to/file.txt
filename = C:\\path\\to\\file.txt


Now let's read the file, one line at a time:

filename = path/to/file.txt
fp = open(filename, r)
for line in fp:
# process that single line
...


How might we process the line? I'm not sure what your requirements are, 
but at a guess you'll want something like this:

- ignore leading and trailing whitespace, including the end of 
  line marker at the end of each line;
- skip blank lines;
- split non-blank lines into four fields;
- convert the first three into integers;
- and the last field into a float.


filename = path/to/file.txt
fp = open(filename, r)
for line in fp:
# process that single line
line = line.strip()  # ignore leading and trailing whitespace
if not line:
continue  # skip blank lines
a, b, c, d = line.split(,)  # Split on commas
a = int(a)  # convert to an int instead of string
b, c = int(b), int(c)
d = float(d)
# And now you can handle the values a, b, c, d ...


And finally, when you are done, close the file:

fp.close()



Does this help?


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help needed with Python programming

2014-04-22 Thread Mark Lawrence

On 22/04/2014 12:41, Steven D'Aprano wrote:

On Mon, Apr 21, 2014 at 06:16:20PM -0700, Suhana Vidyarthi wrote:



[...]



# on Windows either of these will be okay
filename = C:/path/to/file.txt
filename = C:\\path\\to\\file.txt



Or a raw string r'C:\path\to\file.txt'

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help needed with Python programming

2014-04-22 Thread Danny Yoo
Unfortunately, we can't give too much specific help on your particular
problem because it's homework.

You should use the knowledge you learned in your introductory
programming class about designing programs.  In particular, give a
name to the function or functions your are designing.  Be rigorous in
the terms you are using when you talk about the problem.  Formalize
what the types of inputs and outputs are.  Probably most importantly,
express test cases that will demonstrate what you want the output to
be.  And not hand-wavy things, but actual test cases that you can
execute.


What's the expected result of parsing the first line?  That is, you're
saying that the string:

   1,3,5,0.03

has some kind of meaning that can be parsed.

Can you express this meaning as a data structure?  Can you give that
data structure a name?

Can you write a unit test that can test that your parser is behaving properly?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor