472,344 Members | 2,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,344 software developers and data experts.

Help with simple math problem

Hi,

I'm new to Python and am hoping to find help with coding a Python script, applet.

I code in an old version of Visual Basic 4.0, I have a simple app that is about 3 and a half pages of code long
it does some relatively simple math additions and subtractions

The problem I have is that some numbers get to be very large integers and VB automatically converts this to scientifc notation, what I need is to have all numbers added and subtracted with exact precision even when the numbers become very big, the code sums, subtracts these numbers so that in the end there is a resultant, often again a very large number

I need to be able to perform a mod function on this end resultant

here is how it will work ;

1.) Python script opens a text file, the text file contains a variable number of numbers in sequence with spaces in between them, such as

" 23 2244 87 62 71 99 8 4 9 344 21 77 87 63 98 56 64 33 22"

The number of numbers in this text file can range from only a few to hundreds

Python scoops these out and stores and processes these as per my code in VB, but it retains the precision of the very large numbers, NOT converting these to scientific notation

2.) The end result is obtained and a mod function is used on it, the mod value can vary as well to be any number, this mod value is stored in a text file, the python script opens this text file and loads the value as a variable when ever it runs and uses that variable as the value of the mod divisor

3.) the mod value of the resultant is then printed out to a text file

Thats it!

I have the VB code and can provide it, what Im hoping for is to ask someone to convert its exact functionality, one to one to Python script

Four versions of the code needs to be made, one that adds and one that subtracts, but also from right to left and left to right... to explain better, four versions are needed;

1.) Add number sequence from left to right
2.) Add number sequence from right to left
3.) Subtract number sequence from left to right
4.) Subtract number sequence from right to left

I want to be able to call the python script from VB, so the Python dos command screen activates or at least runs and does its thing

I am willing to offer some cash for this help, if it works properly but I dont have a lot of money as I am unemployed
I can offer $50.00 and can pay you via paypal

Thanks for any help you can offer
Aug 14 '08 #1
2 1958
Laharl
849 Expert 512MB
You posted the same thing over in the C/C++ forum, which language do you really want this in?
Aug 14 '08 #2
npcarp
3
Hi.

I'm also trying to figure out how to use Python - there are some nice books out there, but finding a Class is harder. And asking a book can be pretty frustrating. (Even asking a person can be tough...)

If writing the Python code was the easy part, then this reply will probably be pretty useless. I don't know VB, nor even how to make a python module into an applet or standalone app.

Anyway, I took a hack at part of your problem (using IDLE with MacPython 2.5.1 on Mac OS 10.4.11).

I started with 3 files on my desktop:
addemup.py (the python module, below)
numbers.txt (the numbers in a text file - I used yours and 1000000000000000000)
modnumber.txt (the modulus in a text file)

I am not sure if your text file contain numbers separated by spaces:
23 2244 87 . . . or if it contained a single quoted string:
"23 2244 87 ... "
So I wrote it to handle either way.

Here 'tis:

# addemup.py (located on my desktop, along with
# numbers.txt - a file listing your numbers separated by spaces, and
# modnumber.txt - a file listing the modulus: I picked 100)


f = open('numbers.txt','r')
a_str = f.readline()
f.close()
print "a_str contains: ",a_str

if a_str[0] == "'": # if the numbers in numbers.txt are surrounded by single quotes, remove the quotes
a_str = a_str.strip("'")
if a_str[0] == '"': # if the numbers in numbers.txt are surrounded by double quotes, remove the quotes
a_str = a_str.strip('"')
# a_str should now be a string of numbers separated by spaces
print "a_str now contains: ",a_str

b = a_str.split()
# b should now be a LIST of quoted integers ['23', '2244', ... ]
print "b is: ",b

c = [ ]
for i in b:
c.append(int(i))
# c should now be a LIST of simple integers [23, 2244, ...]
print "c is: ",c

c.reverse()
print "c is now: ",c # in case you want to

total = 0
for i in c:
total = total + i
print "total is ",total

f = open('modnumber.txt','r')
m_str = f.readline()
f.close()

if m_str[0] == "'": # if the number in mod.txt are surrounded by single quotes, remove the quotes
m_str = m_str.strip("'")
if m_str[0] == '"': # if the number in mod.txt are surrounded by double quotes, remove the quotes
m_str = m_str.strip('"')
# m_str should now be a string containing the modulus
print "m_str contains: ", m_str

m = int(m_str)
# m is now the actual integer modulus
print "m is: ",m

result_str = str(total % m)
print "result is",result_str

f = open('result.txt','w')
f.write(result_str)
f.close()

# The file result.txt should appear on the desktop and contain a 72 (no quotes)


I hope this helps.
Neal C.
Aug 18 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: JS | last post by:
I've managed to create a simple script to convert between metric and imperial. It works for CMS to INCHES and vice versa but not for KGS to...
2
by: Webdiyer | last post by:
Hi, We all know that the return value of Math.Log(8,2) is 3,but how about (int)Math.Log(8,2)? On my machine,the return value of...
38
by: Jordi | last post by:
I have made a little C program that generates the following output: 227.000000 / 5 = 45.400002 Here's the code: int main (int argc, char* argv)...
3
by: knightsmastergeneral | last post by:
Hi, I'm currently working on getting junit and junireport to take some java files, convert it to xml and then display error messages / success rates...
11
by: frankie_85 | last post by:
Hi everyone, I just made a simple code which is part of my assignment but I can't figure it out what's wrong with it. (always give me error...
1
by: macklin01 | last post by:
Hi, everybody. I'm trying to do some last cleaning up on the following php page I wrote: http://www.math.uci.edu/~pmacklin/Publications.php ...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM...
9
by: Verizon News Server | last post by:
Hi, I am a newbie with Javascript. I am trying to build a simple form that calculates the cost of gasoline for a trip. It simply divides the...
1
by: astrogirl77 | last post by:
I'm new to C++ and am hoping to find help with coding a simple C program, am wanting to obtain code and functioning exe's. I code in an old...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.