Python Forum
making the code easier, list comprehension
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
making the code easier, list comprehension
#1
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')
Reply
#2
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')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 440 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,176 Apr-02-2023, 06:31 PM
Last Post: tester_V
  list comprehension 3lnyn0 4 1,360 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  List comprehension used differently coder_sw99 3 1,680 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
Star I was making code for collision for my character izmamonke 2 2,054 Aug-06-2021, 04:30 PM
Last Post: izmamonke
  How to invoke a function with return statement in list comprehension? maiya 4 2,753 Jul-17-2021, 04:30 PM
Last Post: maiya
Question Making a copy list in a function RuyCab 1 1,768 Jul-11-2021, 02:06 PM
Last Post: Yoriz
  List comprehension and Lambda cametan 2 2,201 Jun-08-2021, 08:29 AM
Last Post: cametan
  Making a code.py file to function, does not run hobbyist 6 2,846 Jan-27-2021, 07:50 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020