You are viewing a single comment's thread. Return to all comments →
Looks good, but to avoid those repetitive input calls you could do something like:
x, y, z, n = (int(input()) for _ in range(4))
Thanks for the input. Will use this from now! :D
Please explain for me why you use range(4)
did you got the output for the above one by using list comprehension?
This expression is used for reducing the repetitive statements in reading the inputs from console.
range(x,y) will create a range of elements from x to y-1
if only one parameter is passed x is assumed to be 0
so range(y) will create elements in range from 0 till y-1
so here range(4) will create a range which includes 0,1,2,3 hence the loop will iterate 4 times as there are 4 elements
can you write the complete code snippet using the range(4), I'm getting error. Thanks!
it will take 4 inputs i.e from 0 to 3...
because we have four variables x,y,z,n
because there is 4 inputs ! hope you understand
he want to take input of 4 numbers so in range it counts from 1 then x later until 4 means till n
to get 4 inputs. we are looping the input().
@annguyen bcz its starts from 0 end to 3
Great tip! BTW is there any reason for using '_' as the dummy variable? What's wrong with using letter, for example 'i'?
_ is used to signify that even though something is being returned, we don't plan to use that variable any where.
gives the following error in above code in python 2.7.13 : at 0x7f60c947e910>
You need to put an additional  in addition to [a,b,c]. Because the output that you want is are lists within a list.
It's also an old Perl trick. If you just performed the input command without assigning the data to anything, it was still available in the $_ variable by default.
It just like a while loop (Ex: while(a<5)) . If we need to store in the seperte varible you can use like this x,y,z,a = (int(input()) for n in range(4). It will be stored seperatly
no (lol u w8ted 3yrs 4 answer ;D)
it depends on programmer what to choose , its your wish
just a small suggestion ..the 4 in for loop will be n+1
N is the number to compare the sum of x, y, z. The loop should actually have 4, as mentioned, for the inputs : x,y,z & n.
in py2 can be w/o int()
x,y,z,n = [input() for i in range(4)]
print [[a,b,c] for a in range(x+1) for b in range(y+1) for c in range(z+1) if a+b+c != n]
WHAT IS THE 4 INDICATES HERE
range(4) indicates 0,1,2,3 i.e the numbers we have given are allocated to the x,y,z,n respectively
4 input() s
what is the reason for x+1 y+1 and many more
In simple words it signifies that the range is inclusive of max limit
range() is inclusive of the first number (which is 0, by default), but exclusive of the last one. So "for i in range(5): print i" will give you 0,1,2,3,4 (but not five). In this case, we want to also include the number given, hence the +1. The 0 for the first part range(0,5), is optional, since we are starting at the beginning.
in python iteration occurs one less than the give variable
Can you help me with this logic ? why we check for a+ b +c !=n
read the question carefully you can understand easily.in the question they have given values inside the nested list must not equal to n so we have given a+b+c!=n
Doesn't work on python3.5
int is not required for input()
Yes it is in Python 3
I'm using this one line, but my compiler reads "wrong answer". Am I missing something? What else do I need to write?
u didnt arrange the list in lexicographic order
Disregard, I figured it out.
can u send the solution
I am a newbie to python and its not clear to me. WHY 4? And how the variable iterate from 0 to 1.
range(4) means range(0,4) that is 0,1,2,3 which is because we ahve to read 4 inputs
Read list comprehensions for more. It is an easy way to build a list.
How is this syntax even valid. Just wondering.
what does for _ in range(4) do ? what's the significance of the underscore? @omalsa04
I think that _ is used as a dummy variable name. Since it's meaning isn't really significant the name can be something representing that.
Wow nice way to take inputs.
why range (4) ???
why do i need to add parentheses around
int(input()) for _ in range(4)
in order for this code to work?
coz you have to assign these values to different variables
in place '_' you should 'n'
both methods are coprrect :0
Makes code less readable, also that doesn't improve time complexity or space complexity so I don't find it that useful other than save some lines of code :S
You can also take in the entire buffer at once with
from sys import stdin
x,y,z,n = map(int, stdin.readlines())
or you could use functions
x, y, z, n = (int(input()) for _ in range(4)) isn't working for me. I'm getting a EOF when reading line error.
@vorayash0510, this is great!