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.
Python
Re: Python
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.
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.

- KillAndChill
- Active Member
- Posts: 770
- Joined: Fri Mar 14, 2008 2:12 am
- Quick Reply: Yes
- Location: leagueoflegends
Re: Python
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
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
- [SD]Master_Wong
- Forum God
- Posts: 9509
- Joined: Wed Jan 04, 2006 8:02 pm
- Quick Reply: Yes
- Location: Plymouth, University
Re: Python
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
but as i said never worked with python but id imagine you could use a similar process, same with most program lanuages
MaStEr

credits zelzin ^^

credits zelzin ^^
Re: Python
I doubt you actually researched it but here's some pseudo-code for you anyways:
Basic idea, I'd write something more elegant with this only as the foundation if you're turning it in for a grade though.
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 againBasic idea, I'd write something more elegant with this only as the foundation if you're turning it in for a grade though.

