Python Forum

Full Version: making the code easier, list comprehension
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.

I receive a number like: ‘n’ from input, which means I will receive ‘n’ two number;
The first one is price and the second one is quality.
I should evaluated the numbers. If I can find the below condition I will print out ‘happy’ and on the other hands print out ‘poor’.
**the price is lower that other and in the same condition the qualities is higher.**

For first example:
Quote:4
2 9
5 1
The output will be ‘happy’

Second example:
Quote:4
1 5
7 9
5 6
20 30
The output will be ‘poor’

I have wrote below code is it okay? Can you see any problem in it? Or any propose to make it easier?
n = input() 
finallist=[]
for i in range (0,int(n)):
    listlap = [int(x) for x in input().split()]
    finallist.append(listlap)
l2=sum(finallist,[])
c=0
c1=0
for i in range(0, len(l2), 2):
    while str(l2[i])<str(l2[i+1]):
        c=c+1
        break
    else:
        c1=c1+1
        break

if (c+c1)==2:
    print('happy')
else:
    print('poor')
I'm not sure I entirely understand the problem, but you could certainly improve your loop. You should loop directly over lists, not over the indexes of the lists. Note that you can assign items one at a time from a list: a, b = [3, 4] makes a == 3 and b == 4. You can use this to loop over the sub-lists in l2, pulling out the price and quality.

n = input() 
finallist=[]
for i in range (0,int(n)):
    listlap = [int(x) for x in input().split()]
    finallist.append(listlap)

for price, quality in l2:
    if price < quality:  # I removed comparing numbers as strings, and changed the while loop to a simpler conditional
        c=c+1
    else:
        c1=c1+1
 
if (c+c1)==2:
    print('happy')
else:
    print('poor')
If I understand the problem, you're happy if you find two articles, one of which is strictly less expensive than the other one and has a strictly better quality. I think your code doesn't work at all for this task but I find it difficult to understand. Here is my attempt on this problem (tell me if I misunderstand it)
n = int(input('Please enter number of articles: '))

articles = []
for i in range(n):
    price, quality = (int(x) for x in input(str(i+1) + '? ').strip().split())
    articles.append((price, quality))

# Sort the articles by increasing price
# and increasing quality within a given price
articles.sort()

# Start with the leftmost article.
# It has the lowest price and the lowest quality among articles that price.
cur_price, best_quality = articles[0]

for price, quality in articles[1:]:
    if price > cur_price:
        if quality < best_quality:
            # A strictly highest quality was met before.
            # it was necessarily met by an article with a
            # strictly lower price.
            # We're happy.
            print('happy')
            break
        # The quality was >= the best quality met so far. It becomes
        # the new best quality and we have a new current price.
        # Until we meet another new price, quality can only increase.
        cur_price = price
    best_quality = quality    
else:
    print('poor')