Page 1 of 1

Python

Posted: Fri Apr 08, 2011 5:37 pm
by Phaedra
Hey guys,

I could use a little help here if you are good at Python.

So, let's say I have a txt file in which I have the followin lines, let's name it input.txt

Computer
10000
20000
30000
Book
3000
2000
1000
Whatever
1
3
2

Now, the task is to write a script which creates a file and stores the highest bid in it with the corresponding item. So, it should look like this:

Computer 30000
Book 3000
Whatever 3


Yea, well, some idea would be nice, I'd appreciate it:) I have done some research but still haven't figured it out.

Re: Python

Posted: Sat Apr 09, 2011 1:09 am
by EvGa
I've never coded in python. Never even looked at it's syntax. What is it that you're having a problem with? The logic of the program or the syntax?

Loop through the file testing each line to see if it is a character string or integer.
If it's a character, keep looping through each line, testing it against the previous to see which is largest. Continue saving the largest until you see a character string, at this point you print out the string found originally + highest value and repeat the above process on the character string you just encountered...

I hope that makes a little sense. I'm in a rush, but I can write out the sudo-code later if ya want...

Think through the logic...perhaps draw a picture.

Re: Python

Posted: Sat Apr 09, 2011 1:15 am
by KillAndChill
Assuming each item has only 3 bids:

open new file
while (file isn't at the end)
-read first block of chars
-for i=0 to i=2
--read bid
--convert to int
--place in static array
-end for loop
-use a max function or sort list to find max
-print item and cost
end while loop
close new file

Don't know python, but you should be able to figure out a way to code each step in any language

Re: Python

Posted: Sat Apr 09, 2011 2:27 am
by [SD]Master_Wong
never used pyton but having it show the highest value shouldnt be a problem, if it was in php id have a script work with a server or even just a simple database to store each 'bid' and have a recall that searches for the highest value in that catagory which would display it. hard part would be having it accept new bids from an outside source thats something i cant do even in php but im a bit of a noob at it so yeh, but just the recall part and even it creating a new entry in a database isnt too hard

but as i said never worked with python but id imagine you could use a similar process, same with most program lanuages

Re: Python

Posted: Sat Apr 09, 2011 3:02 am
by SM-Count
I doubt you actually researched it but here's some pseudo-code for you anyways:

Code: Select all

current_group = null
max_number = 0
file = open('/google/me/input.txt','r')
for line in file:
  if line is a number and > max_number then set as max_number
  if line is a word (any check will do, a simple regex ^\w+ for example) and current_group = null then current_group = word
  else output current_group,max_number to where ever you need set current_group = word max_number = 0 and call method again


Basic idea, I'd write something more elegant with this only as the foundation if you're turning it in for a grade though.