Home > database >  Solve my simple linear search algorithm problem. It just simple algo problem
Solve my simple linear search algorithm problem. It just simple algo problem

Time:01-03

I can get input, but I can't think of a good idea after that. I think it would be better to save each value as a double list and use the method of processing the for statement by playing it.But that way, you have to turn the for door 3 times. Is there a simpler way? Can you solve this problem with Python? plz help me.

This is my problem

**The question
**Oh-seok wants to buy a laptop. There are n kinds of laptops on display in the store.
Oh-seok is interested in cpu speed, RAM, and the size of hard disk. (FYI, I am not interested in specification of anything else.)

Oh-seok has a rule to buy a laptop.
Rule 1) If the specifications of one laptop are all lower than those of another laptop, the laptop is not bought because it is out of fashion.
Rule 2) Choose the cheapest laptop that is judged to be out of fashion.

There are so many laptops that Oh-seok tries to find a laptop that fits his condition by creating a program with coding.

Print out the number of the laptop you want to buy with the program Oh-seok made.
Input type
The first line is given the number of laptops (1≤n≤100)
From the second line, n lines are given the specifications of the laptop. Each line is given the laptop's specifications in order of cpu speed, RAM size, hard disk size, and price.
• 1,000≤cpu speed≤4,200
• 256≤Ram size≤4,096
• 1≤Hard disk size≤500
• 100≤Price≤1,000 (all laptops are priced differently).)

Output type
Print out the number of the laptop that Oseok will buy. The number of the laptop means the order of each line in which a given laptop specification is written in the input data.

Input/output example data
Input Example 1
5
2100 512 150 200
2000 2048 240 350
2300 1024 200 320
2500 2048 80 300
2000 512 180 150



`

N = int(input())
information = [[] * (N) for _ in range(N)] # laptop's information. 

#input (cpu , ram , harddisk, price)
for _ in range(N):
    tmp = list(map(int,input().split()))
    for i in range(4):
        information[i].append(tmp[i])
print(information)

`

This is my input code. 
but I can't programming how to compare each others. 
plz coding how to compare it. 

CodePudding user response:

I see no better way than exhaustively comparing every laptop to every other one, on all criteria, to check if a machine is out of fashion.

If a machine is out of fashion, you can eliminate it for the next comparisons.

CodePudding user response:

Here is a program that finds the best laptop by creating a rating system and ranking the different laptops "CPU RAM HDD - price = ranking value"

If you want to find the worst laptop, you can turn this around: "price - CPU - RAM - HDD = ranking value" //maybe you have to adjust the price value before with * 2.

And if CPU is more important for you, then you can use a multiplier like: "CPU * 0.2 RAM HDD - price = ranking value"

The different laptops specification's are stored in a input.txt which look like

2100 512 150 200
2000 2048 240 350
2300 1024 200 320
2500 2048 80 300
2000 512 180 150

I'm not very used to python and perhaps there is a more elegant solution...

#!/usr/bin/python3

input_array = []
#Para 1 = CPU; 2 = RAM; 3 = HDD; 4 = Price
parameter = 4

#Open File with parameter's in it //only for reading
with open('input.txt', 'r') as input_file:
    #read line by line
    input_array = input_file.readlines()
    #print(len(input_array))

#make array with max size of lines times pc parameter
val = [None] * len(input_array) * parameter

#print(input_array)

#remove new line element from input_array and store it in a new array
for idx, line in enumerate(input_array):
    val[idx] = line.strip()
    #print (idx)
    #print(val[idx])

#print(val)

#remove None value from array
res = []
for tmp_val in val:
    if tmp_val != None:
        res.append(tmp_val)


j = 0
j_old = 0
best_opt = 0
for idx, str_val in enumerate(res):
    #print (idx)
    res[idx] = str_val.split()
    #print(res[idx])
    j = 0
    #calc rating for every pc
    for opt, i in enumerate(res[idx]):
        #print(i)
        #add first 3 values //because more CPU more RAM more HDD = better
        if opt < 3:
            j = j   int(i)
            #print("idx %d" % (j))
        else:
            #sub last value //because less price = better
            j = j - int(i)
            #print("last idx %d" % (j))
            #check if rating is higher as last pc
            if j > j_old:
                j_old = j
                #remember highest rated pc
                best_opt = idx
        #print(j_old)

#print best rated pc
print("Best option: %d" % (best_opt   1))
  • Related