Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
so I'm trying to create as short as possible code for printing an input backwards, and I want to go below 60B. My code takes 79B, and have no idea if it is actually possible to shorten it even more.
tab=[i for i in map(int,input().split())]
print(" ".join(map(str, tab[::-1])))
So when I input:
1 2 3 4 5
I get in output:
5 4 3 2 1
Anybody got idea if it can be even shorter?
print(*input().split()[::-1])
Splits the list by spaces, then reverses and sends to print as a bunch of arguments.
print supplies the separating space automatically.
One liner 41 Bytes
>>> print(''.join(i for i in input()[::-1]))
1 2 3 4 5
5 4 3 2 1
Altough, you could just reverse the input() output w/ subscripting, and print that.
>>> print(input()[::-1])
1 2 3 4 5
5 4 3 2 1
Maybe I don't understand what you're asking, but if it's just to reverse the string as such, then print(input()[::-1]) clocks in at 21B.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
lets say I have 29 apples and put them in boxes where I can fit 10 apples in each box.
when I do print statement I want to see
print(apple)
1 (Full)
2 (Full)
9 (EA)
have any idea how my code should be?
I tried using
full = 10
qty = 29
fullboxes = math.ceil(qty/full)
partials = qty-fullboxes
You can use divmod to get the number of full boxes and the remainder. The rest is just printing.
apples = 29
boxsize = 10
a,b = divmod(apples, boxsize)
for i in range(a):
print(f'{i+1} (Full)', end=' ')
if b:
print(f'{b} (EA)')
output: 1 (Full) 2 (Full) 9 (EA)
output for 30 apples: 1 (Full) 2 (Full) 3 (Full)
Not exactly sure what you are trying to achieve here but
the following code will do what you have described.
d = 29
j = divmod(d, 10)
for i in range(j[0]):
print(f'({i + 1}(full)', end=' ')
else:
print(f'{j[1]}(EA)')
The function divmod(x, y) takes two arguments x and y and gives a tuple of their quotient and remainder.
You can read more about it here
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Why in python the x%y result is x when x is less than y. should the result be zero?
for example when I try 4%5, python give me 4 as a result of and I believe it should be zero
% -> refers to the modulo operator
So you would get the remainder if you use this operator
4%5 will give 4 as 4 is the remainder when you divide 5 by 4
If you wish to get the quotient then you can use // which is used for absolute division
so 4%5 gives 4
and 4//5 gives 0
The % symbol in Python is called the Modulo Operator.
It returns the remainder of dividing the left hand operand by right hand operand. It's used to get the remainder of a division problem.
Example:
5%6
# results in 5
See the long division below
_____0__
6 | 5
| 0
|_____
| 5
Performing 5 / 6 results in reminder of 5, thus 5%6 is 5
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to print the numbers 1 to 10 with a for loop, but import random is affecting my for loop and the loop is running twice. Here is a screenshot of my editor. The output is displayed below. Can you explain to me why this happens?
import random
for i in range(1,10):
print(i)
My output:
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
This will never happen.
make sure you are running the latest version of your program, and make sure you didn't paste the same code at some other place in the same file. e.g. make sure you don't have a precompiled version of it. (e.g. change the file name and try again).
Update:
based on the comment by matthias, if you saved your file as random.py the above result could be reproduced.
# if you save the code below as random.py
import random
for i in range(1,10):
print(i)
# your output could be reproduced as below.
>>> python random.py
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
solution: change the file name or if you have to import it make sure you include the codition if __name__ == "__main__" in before the for loop of your random.py
11I do not see any problem, I did the example and it works perfect.
My output:
1
2
3
4
5
6
7
8
9
Could it be that you have called the file twice? It's the only request I find.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Squirrels and nuts - 1
N squirrels found K nuts and decided to divide them equally. Determine
how many nuts each squirrel will get.
Input data format
There are two positive numbers N and K, each of them is not greater
than 10000.
Sample Input:
3
14
Sample Output:
4
>>> squirrels = 3
>>> nuts = 14
>>> extra_nuts = 14 % 3
>>> distributable_nuts = nuts - extra_nuts
>>> nuts_per_squirrel = distributable_nuts / squirrels
>>> print(nuts_per_squirrel)
4.0
I am not sure why my answer is not correct. Right now I get 4.0 but it should be 4. Any guidance would be helpful. I had used % to remove the extra nuts to make them evenly distributable across 3 squirrels, but 4.0 wasn't correct.
To avoid a float result, use integer division: // instead of /.
Your nuts value is set to 14. Divided by three that's 4.6 when it's expecting 4.
Since 14 does not divide by three equally, you are receiving a floating point number. You can cast this to an integer to disregard the remainder by int(nuts_per_squirrel).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a dataframe which looks like this,
df=pd.DataFrame([2,4,6,2],columns=['x'])
df['y']=[3,2,1,2]
df['x_test']=['x_larger','x_larger','x_smaller','x_equal']
I am trying to do a if/then/else, similar to this idiom and cookbook. I am able to get one if statement correct but I am not sure how to test for equal.
df['valid']=False
df['invalid']=True
df.ix[(df.x_test=="x_larger") & (df.x>df.y),['valid','invalid']]=[True,False]
This works partially. But I would also like to test for x_equal in this line. Is that possible?
The desired output should be
x y x_test valid invalid
0 2 3 x_larger False True
1 4 2 x_larger True False
2 6 1 x_smaller False True
3 2 2 x_equal True False