So in my discrete structures math course, our professor is having use python to do some of the questions. The trouble being I've never used python. We've been given an assignment where we will be given an input file, that will contain the results of a truth table up to 256 values. example being:

0

1

0

0

1

0

1

1

I have to write a python program that, using this input file, creates the f(n) (where n is the number of boolean variables that are needed for the truth table) based on the input file.

Later I have to modify the program so that it simplifies the functions from disjunctive normal formal to something with less operators.

As I am totally new to python, and at the moment finding myself spinning my wheels in the mud, I was hoping some of the DiC members might help put me on a good track. Not asking for someone to do my work, just want someone to tell me "start at X" "then do y". I keep looking over the description of the assignment and not a single hint of inspiration or even a clue is entering my mind.

Thanks in advance for any help someone might be able to offer.

T.

# Math assignment need python help

Page 1 of 1## 10 Replies - 1429 Views - Last Post: 08 February 2009 - 10:58 PM

##
**Replies To:** Math assignment need python help

### #2

## Re: Math assignment need python help

Posted 08 February 2009 - 08:32 PM

Alright. I'm revising my question a little bit. What I need now is a way to test the length of a text file, as in how many lines there are. I am going to use this as how I will figure out how many variables my boolean function will have.

How would I go about using the input file to either create a list/array and then what would the code be to measure the length of it. I'm used to java where I would simply input the file to an array then do array.length to check how long it is. I have no idea how to do this in python. I am searching the web but haven't found quite what I'm looking for just yet. Any help with this would be greatly appreciated.

T.

How would I go about using the input file to either create a list/array and then what would the code be to measure the length of it. I'm used to java where I would simply input the file to an array then do array.length to check how long it is. I have no idea how to do this in python. I am searching the web but haven't found quite what I'm looking for just yet. Any help with this would be greatly appreciated.

T.

### #3

## Re: Math assignment need python help

Posted 08 February 2009 - 08:51 PM

You can find out the number of lines in this way

so your file have i number of variables. what next?

f=open("filename","r") i=0 for i in f: i=i+1 print i

so your file have i number of variables. what next?

### #4

## Re: Math assignment need python help

Posted 08 February 2009 - 09:07 PM

Also u need to give filename with proper extension (like variable.txt, variable.data etc).

### #5

## Re: Math assignment need python help

Posted 08 February 2009 - 09:14 PM

I tried that but when I try to run it I get an error that says:

SyntaxError: invalid syntax

[email protected]:~/Documents/CS227/PA1$ python PA1.py

Traceback (most recent call last):

File "PA1.py", line 3, in <module>

i= i+1

TypeError: cannot concatenate 'str' and 'int' objects

my code looks like this.

Thanks for the reply by the way

SyntaxError: invalid syntax

[email protected]:~/Documents/CS227/PA1$ python PA1.py

Traceback (most recent call last):

File "PA1.py", line 3, in <module>

i= i+1

TypeError: cannot concatenate 'str' and 'int' objects

my code looks like this.

f = open("test.txt", "r") for i in f: i= i+1 print i

Thanks for the reply by the way

### #7

## Re: Math assignment need python help

Posted 08 February 2009 - 09:43 PM

Sorry i had a little mistake earlier. Try this-

f=open("filename","r") i=0 for lines in f: i=i+1 print i

### #8

## Re: Math assignment need python help

Posted 08 February 2009 - 09:56 PM

perfect. that works exactly how it should. Let me work with this a little and I'll be posting any hiccups I run into. thanks for the help shad!

### #9

## Re: Math assignment need python help

Posted 08 February 2009 - 10:10 PM

Alright. so now that I have a variable that shows the number of lines in my input file, I need to divide this by two, and then divide the result of that by 2 until I get a value of 1, with another variable showing how many times the division operation occurred.

By the way I really appreciate the help. Although learning python has been on my list of things to do, my to-do list has been rather full as of late. If I could do this in java I'd have no issue, however the structure of the two languages is very different. I understand why they teach java as a first language at my school, (multi platform etc etc) however I know as a language it's rather weak.

I also understand why my professor uses python in my math course as python has much better usability in math applications, as well as the fact that it's basically the language that sage is written in/for. Still it's leaving me with not only a tough learning curve for my math course, but I get to learn a language just to do the math problems.

Anyhow, if you know java, then this is what I'm needing in python form.

I think this should give you an idea what I'm needing, only I'm needing it in python.

Thanks again,

T.

By the way I really appreciate the help. Although learning python has been on my list of things to do, my to-do list has been rather full as of late. If I could do this in java I'd have no issue, however the structure of the two languages is very different. I understand why they teach java as a first language at my school, (multi platform etc etc) however I know as a language it's rather weak.

I also understand why my professor uses python in my math course as python has much better usability in math applications, as well as the fact that it's basically the language that sage is written in/for. Still it's leaving me with not only a tough learning curve for my math course, but I get to learn a language just to do the math problems.

Anyhow, if you know java, then this is what I'm needing in python form.

for (int ii = i; i == 1; ii++) // the value of i from the previous code section is used here { i = i/2; } return ii;

I think this should give you an idea what I'm needing, only I'm needing it in python.

Thanks again,

T.

### #10

## Re: Math assignment need python help

Posted 08 February 2009 - 10:37 PM

Actually cancel that last question. Though the mysteries of the python for loop still elude me I was able to resolve this with a simple while loop.

my code now looks like this.

From this data that I've gotten I need a print statement to read something like this:

"the function for the input is f(a,b,c)" the "(a,b,c)" part is determined by the value of count. if the value of count is 3 then it should look like "(a,b,c)" if the value of count is 5 I need it to look like "(a,b,c,d,e)"

Anyone that can help me with this formatting?

thanks in advance T.

my code now looks like this.

f=open("test.txt","r") i=0 for lines in f: i=i+1 print i count = 0 while i > 1: i = i / 2 count +=1 print count print "there are ", count, " variables in the boolean function"

From this data that I've gotten I need a print statement to read something like this:

"the function for the input is f(a,b,c)" the "(a,b,c)" part is determined by the value of count. if the value of count is 3 then it should look like "(a,b,c)" if the value of count is 5 I need it to look like "(a,b,c,d,e)"

Anyone that can help me with this formatting?

thanks in advance T.

### #11

## Re: Math assignment need python help

Posted 08 February 2009 - 10:45 PM

What are values of a,b,c? Are the values obtained from while loop? Let me know.

### #12

## Re: Math assignment need python help

Posted 08 February 2009 - 10:58 PM

shadhin, on 8 Feb, 2009 - 09:45 PM, said:

What are values of a,b,c? Are the values obtained from while loop? Let me know.

Yes they are. the value of the "count" variable in the code that I have is obtained in the while loop. It will return the 2 factorial of the value obtained in the first loop. (factorial is if we divide repeatedly until we obtain a value of 1. the number of times you divide is the value of count).

Now, to the code. I've written something but it's clearly not doing what I want it to. Here's the code I wrote. It sort of works, but it's not doing what I'd like it to.

f=open("test.txt","r") i=0 for lines in f: i=i+1 print i count = 0 while i > 1: i = i / 2 count +=1 print count print "there are ", count, " variables in the boolean function" x = 1 functionString = "a" while x < count: functionString = functionString, + x x+=1 print "the function is: f(", functionString, ")"

When I run it in terminal the output looks like this:

16 4 there are 4 variables in the boolean function the function is: f( ((('a', 1), 2), 3) )

This is close to what I want but I want the part that says "the function is: f( (((... etc" to look like this

the function is: f(a,b,c,d).

a value of '0' for count should return f(a), whereas a value of 5 for count should return f(a,b,c,d,e)

Does this make sense? thanks again for your patience and help on this shad.

P.S. I'm on AIM my SN is islandtimeaa88

if you got aim feel free to add me so any small questions you might have can be answered much quicker.

Page 1 of 1