comparing lists in a list - python-3.x

Hi I was trying to do an exercise in which you get n lines of two numbers separated by space as inputs(for example a product info). The first number is price of the product and the second one is value of it and the goal is to find if there is any product which price is lower and it's value is higher for example :
2
4 5
3 6
This condition should print(yes), but this for example:
3
7 9
4 6
3 2
should print(no)
I came up with this :
n = int(input())
a = []
for x in range(n):
a.append(input().split( ))
for m in range(n):
a[m][0] = int(a[m][0])
a[m][1] = int(a[m][1])
b = max(a)
for z in range(n):
a[z].reverse()
c = max(a)
c.reverse()
if c == b:
print('no')
else:
print('yes')
But it's not working for this [[1,3],[3,1]] and same ones and it's obvious why but anyway can anyone help me somehow find a solution ?

hi again as I did not receive any answer on this topic so I decide to post an answer when I find one and here it is:
a = [input().split( ) for i in range(int(input()))]
b = []
for m in range(len(a)):
a[m][0] = int(a[m][0])
a[m][1] = int(a[m][1])
for x in range(len(a)):
for z in range(x+1,len(a.copy())):
if (a[x][0] < a[z][0] and a[x][1] > a[z][1]) or (a[x][0] > a[z][0] and a[x][1] < a[z][1]):
b.append([1])
elif (a[x][0] > a[z][0] and a[x][1] > a[z][1]) or (a[x][0] < a[z][0] and a[x][1] < a[z][1]):
b.append([2])
if [1] in b:
print('yes')
else:
print('no')
hope you enjoy !

Related

Sum function not working when trying to add numbers in lists using python

I am trying to create a program that takes only even numbers from a range of numbers from 1 to 100 and add all of the even numbers. I am a beginner and I have been trying to get this working since yesterday, and nothing I have tried works. This is my first post, so sorry if the format is wrong but here is my code.
for i in range(1, 100):
if i % 2 == 0:
x = [I]
y = sum(x)
print(y)
The problems with your code has multiple issues that - 1)if you want to get all even numbers from 1 to 100, your range should be (1, 101); 2) the way you build list is wrong (syntax); 3) the sum expect an iterable (list).
There are a few ways to accomplish this sum from 1 to 100 (inclusive), here it will start with yours, and try to show List Comprenshion and Generator Expression way:
lst = [] # to store the build-up list
tot = 0 # to store the answer
for i in range(1, 101):
if i % 2 == 0: # it's a even number
lst.append(i) # store it into lst
tot = sum(lst) # 2550
Generator expression:
all_evens_sum = sum(x for x in range(1, 101) if x % 2 == 0) # 2550
Or List Comprehension:
lst = [x for x in range(1, 101) if x % 2 == 0] # even nums
total = sum(lst) # 2550
I am a beginner too but it looks I can help you some.
for i in range(1, 100):
if(i % 2 == 0):
sum += i
print(sum) // do what you want

Python- Reading two n-spaced sequences as a list, subtracting the lists and printing the greatest difference.[CodeChef]

I was working with a problem on CodeChef and I am stuck with one of the sub task being incorrect.
Problem statement:
https://www.codechef.com/AUG19B/problems/MSNSADM1
You are given two sequences. For each valid i, player i scored
Ai
goals and committed
Bi
fouls. For each goal, the player that scored it gets
20
points, and for each foul,
10
points are deducted from the player that committed it. However, if the resulting number of points of some player is negative, this player will be considered to have
0
points instead.
You need to calculate the total number of points gained by each player and tell Alex the maximum of these values.
Input:
The first line of the input contains a single integer
T
denoting the number of test cases. The description of
T
test cases follows.
The first line of each test case contains a single integer
N
.
The second line contains
N
space-separated integers (for no. of goals).
The third line contains
N
space-separated integers (for no. of fouls).
Output:
For each test case, print a single line containing one integer ― the maximum number of points.
Constraints:
1≤T≤100
1≤N≤150
0≤Ai≤50
for each valid
i
0≤Bi≤50
for each valid
i
My approach to this was to create 2 lists and multiply each element of first by 20, second by 10 and then create a list c, which has the difference of each elements.
try:
t= int(input())
while(t != 0):
t -= 1
n = int(input())
a_i = list(map(int, input().split()))
b_i = list(map(int, input().split()))
a = [i * 20 for i in a_i]
b = [i * 10 for i in b_i]
for i in range(0 , len(a)):
if a[i] < 0:
a[i] = 0
for i in range(0 , len(b)):
if b[i] < 0:
b[i] = 0
c = [i - j for i, j in zip(a, b)]
print(max(c))
except:
pass
All the tasks seems to be showing correct answer except one. I can't seem to understand what I am doing wrong here.
With the given indentation you are only printing the last testcase.
You create lots of list's in between that are not needed but take time to create/instantiate etc.
You loop over your data twice to eleminate the negative values - also not needed.
Use generators instead:
try:
for _ in range(int(input())):
n = int(input())
a_i = map(int, input().split()) # dont list(...) this
b_i = map(int, input().split()) # dont list(...) this
# get the max - negatives are irrelevant, they are removed when printing
m = max(goals * 20 - fouls*10 for goals, fouls in zip(a_i,b_i))
# if _all values_ are negative, print 0 else print the max value
# you need to print _each_ testcase, not only the last as your code does
print(max( (m,0) ))
except:
pass
t= int(input())
while(t != 0):
t -= 1
n = int(input())
a_i = list(map(int, input().split()))
b_i = list(map(int, input().split()))
c=[]
a = [i * 20 for i in a_i]
b = [i * 10 for i in b_i]
c =list(map(int.__sub__, a, b))
for line in c:
if line < 0:
line = 0
print(max(c))enter code here

How to print the index/elements that are included in the final max sum of non-adjacent elements?

In the program of finding the max sum of non-adjacent elements how can we print the elements/indexes of elements which are considered in the final sum. So here I am attaching my code for that. I am using dynamic programming.
I got the correct answer when there is only one possibility of occurring max sum like we have -1, 2, 4, 5. So the output will be 5 and 2.
n = int(input())
tickets = list(map(int,input().split()))
incl = 0
excl = 0
max_list = []
for i in range(len(tickets)):
if excl>incl:
new_excl = excl
else:
new_excl = incl
incl = excl + tickets[i]
excl = new_excl
if excl > incl:
if len(max_list)>1 and (max_list[len(max_list)-1] - max_list[len(max_list)-2])==1:
del max_list[len(max_list)-2]
else:
max_list += [i]
if excl>incl:
print(excl,max_list)
else:
print(incl,max_list)
But I do not get answers when the input like this : 4, 5, 4, 3. In this input there are two possibilities : 4+4 and 5+3. I want to print that possibility that has a higher digits than the other from right side. So in this example from right side 4 > 3 so the possibility of 4 should be printed. But I got all the elements in the list.
I have to solve this problem. This is problem from the techgig's code gladiators qualification round. I find the answer for my problem but anyway this solution doesn't get me 100 marks. I haven't checked it for many test cases but you can check it and if it fails then please inform so I can get what the problem is.
from itertools import combinations
for i in range(int(input())):
n = int(input())
tickets = list(map(int,input().split()))
incl = 0
excl = 0
max_list = []
for i in range(len(tickets)):
if excl>incl:
new_excl = excl
else:
new_excl = incl
incl = excl + tickets[i]
excl = new_excl
if excl > incl:
if len(max_list)>1 and (max_list[len(max_list)-1] - max_list[len(max_list)-2])==1:
del max_list[len(max_list)-2]
else:
max_list += [i]
if excl>incl:
s=excl
else:
s=incl
a=[]
if 1 in [abs(t-s) for s,t in zip(max_list,max_list[1:])]:
for m in range(2,n):
for n in list(combinations(max_list, m)):
if sum(tickets[b] for b in n)==s:
a+=[n]
l=[]
index=[]
for m in a:
l+=[tickets[m[1]]]
index+=[m[1]]
v = index[l.index(max(l))]
for m in a:
if v in m:
ans = m
break
for d in list(reversed(ans)):
print(tickets[d],end='')
print()
else:
max_list.reverse()
for d in max_list:
print(tickets[d],end='')
print()

Prime number generator returns none at the end when inside a function sometimes

I made this prime number generator in python:
for x in range(a , b):
y = 2
while 1 == 1:
if x % y == 0:
if x == y:
print(x)
break
else:
break
y += 1
and it returns all prime numbers up to b as long as a > 2 and b > 3
for example, when a = 2 and b = 11 i get this
2
3
5
7
but when I nest it inside a function like this and print it:
def f(b,a=2):
for x in range(a , b):
y = 2
while 1 == 1:
if x % y == 0:
if x == y:
print(x)
break
else:
break
y += 1
print(f(11))
I get this:
2
3
5
7
None?
Why is it printing a none while inside a function and not without? And how would I fix this?

Code showing no output whatsoever (python)

Trying to make a little Fibonacci game where the user guesses a set amount of steps. I'm trying to make a functioning Fibonacci generator to make the lists to refer to later, but nothing is showing up. I'm sure I'm returning the values. What am I doing wrong?
""" Core Fibonacci Code
a = int(input("How many steps of fibonacci would you like? "))
def fibonacci(counter):
a = 0
b = 1
count = 2
print (a)
print (b)
fib = [0, 1]
while (counter > count):
c = a + b
print (c)
a = b
b = c
count += 1
fibonacci(a)
"""
def fibonacci(counter):
a = 0
b = 1
count = 2
print (a)
print (b)
fib_list.append = a
fib_list.append = b
while (counter > count):
c = a + b
fib_list.append = c
a = b
b = c
count += 1
def homescreen():
print = ("Which gamemode would you like to play?")
print = ("EASY - 10 Steps")
print = ("MEDIUM - 25 Steps")
print = ("HARD - 50 Steps")
request = input("")
if request is "Easy" or "easy":
z = 10
elif request is "Medium" or "medium":
z = 25
elif request is "Hard" or "hard":
z = 50
return z
homescreen()
fibonacci(z)
print (fib_list)
Use print("Which gamemode would you like to play?") and not print=.
You use such format when your returning something from the called function.
eg:
def foo():
#Something
return y
x=foo()
Note :
Use the function append(), dont use lib_list.append=a.
Also declare lib_list outside the function fibonacci() as you're mentioning it in the function call outside of the function.

Resources