How can I avoid duplicate digit in the cicle result - python-3.x

The result of the code includes separetly "min" and "max" values. Can't get how I can print only result of cicle?
For examle, whan I set min = 1, max = 3 the result is:
1
3
1
2
3
And I need 1 2 3 without first 1 3.
min = int(input())
max = int(input())
while min <= max:
print(min)
min = min + 1

That depends entirely on the ide or python environment you are using. May I know which environment you are using?
Because when I ran here https://ideone.com/lUmjKL everything seems fine.
min = int(input())
max = int(input())
while min <= max:
print(min)
min = min + 1
Let me know.

Related

Compare current value with n values above and below on Pandas DataFrame

I have this df:
x
0 2
1 2
2 2
3 1
4 1
5 2
6 2
I need to compare current value on column x with respect to the n previous and next values based on a defined condition, if condition is met q times then add 1 in a new column, if not, add 0.
For instance, if n is 2, q is 3 and the condition is current_value <= value / 2. In this case, the code will do 7 comparisons:
1st comparison: compare current_value = 2 to previous n = 2 numbers (in this case there are no such numbers because is the first value on the column) and then compare current_value = 2 to the next n = 2 values (in this case both numbers are 2, so condtion is not met on neither (2 <= 2/2)). In this case there are no conditions met, as q = 3 >= 0 the code adds 0 to the new column.
2nd comparison: compare current_value = 2 to previous n = 2 numbers (in this case there is just one number above, the condition is not met (2 <= 2/2)) and then compare current_value = 2 to the next n = 2 values (in this case there's a number 2 and then a number 1, so condition is not met (2 <= 2/2 and 2 <= 1/2)). In this case there are no conditions met, as q = 3 >= 0 the code adds 0 to the new column.
3rd comparison: In this case there are no condition met, as q = 3 >= 0 the code adds 0 to the new column.
4th comparison: compare current_value = 1 to previous n = 2 numbers (in this case there are two number 2 above, the condition is met on both of them (1 <= 2/2)) and then compare current_value = 1 to the next n = 2 values (in this case there's a number 1 and then a number 2, so condition is met once (1 <= 2/2 and 1 <= 1/2)). In this case there are 3 conditions met, as q = 3 >= 3 the code adds 1 to the new column.
5th comparison: In this case there are 3 conditions met, as q = 3 >= 3 the code adds 1 to the new column.
6th comparison: In this case there are no conditions met, as q = 3 >= 0 the code adds 0 to the new column.
7th comparison: In this case there are no conditions met, as q = 3 >= 0 the code adds 0 to the new column.
Desired result:
x comparison
0 2 0
1 2 0
2 2 0
3 1 1
4 1 1
5 2 0
6 2 0
I was thinking on using something like shift function but I'm not sure how to implement it. Any help?
I suggest to use numpy here, to benefit from its sliding window view:
import numpy as np
from numpy.lib.stride_tricks import sliding_window_view as swv
n = 2
q = 3
# convert to numpy array
a = df['x'].astype(float).to_numpy()
# create a sliding window
# remove central value, divide by 2
# compare to original value
# count number of matches
count = (a[:,None] <= swv(np.pad(a, n, constant_values=np.nan), 2*n+1)[:, np.r_[:n,n+1:2*n+1]]/2).sum(1)
# array([0, 0, 0, 3, 3, 0, 0])
# compare number of matches to q
df['comparison'] = (count >= q).astype(int)
print(df)
An alternative with only pandas would require to compute two rolling windows (forward and backward) as it's not trivial to access the current index in a centered rolling with min_periods=1:
n = 2
q = 3
s1 = df['x'].rolling(n+1, min_periods=2).apply(lambda x: sum(x.iloc[-1]<=x.iloc[:-1]/2))
s2 = df.loc[::-1, 'x'].rolling(n+1, min_periods=2).apply(lambda x: sum(x.iloc[-1]<=x.iloc[:-1]/2))
df['comparison'] = s1.add(s2, fill_value=0).ge(3).astype(int)
Output:
x comparison
0 2 0
1 2 0
2 2 0
3 1 1
4 1 1
5 2 0
6 2 0

Printing total using for loop

I am trying to get the total of 1,2,3,4 which I am able to achieve through code 1 but not code 2. Could someone please point out why is code 2 returning 4 and not 10?
CODE 1:
total = 0
for i in range(1,5):
total = total + i
print(total)
10
CODE 2:
total = 0
for i in range(1,5):
newtotal = total + i
print(newtotal)
4
This is because in code2 total is not updating ,it always is zero because you initialised it to 0 so every time in for loop i gets updated like
newtotal = 0+1 = 1
next time
newtotal = 0+2 = 2
nex time
newtotal = 0+3 = 3
nex time
newtotal = 0+4 = 4
But in code 1 you are updating the value of total everytime with the statement
total = total + i
first time
total = 0+1 = 1
//now total is 1
next time
total = 1+2 = 3
next time
total = 3+3 = 6
next time
total = 6+4 = 10
Hence the answer 10
Because in CODE 2, in each iteration total is 0.
total = 0
newtotal = 0
for i in range(1,5):
total = newtotal
newtotal = total + i
print(newtotal)
Now here we are updating the value of total

Return values based on the sum of array indeces

I would like to return all values in an array where the row number plus the column number is a multiple of 5.
I feel like I'm close, but I can't seem to get the (i + j) % 5 == 0 part working as a condition.
using:
(i + j) % 5 == 0
I have constructed a random 8 x 9 array using:
arr2 = np.random.normal(size = (8,9), loc=1, scale=0.5)
a = np.empty([100,100])
for i in range(0,len(a)):
for j in range(0,len(a[0])):
if (i+j)%5 == 0:
print(i,j,i+j a[i][j])
output
0 0 0 0.0
0 5 5 0.0
0 10 10 0.0
0 15 15 0.0
0 20 20 0.0
.
.
.
I don't know what you did wrong you have the correct logic. Its a simple loop where you use the index itself as a test rather than than contents at that index.
Awesome thanks. I actually tweaked this a little to get the mean of all values that meet this criteria:
def arrmean5 (arr):
total = 0
count = 0
for i in range(0,len(arr)):
for j in range(0,len(arr[0])):
if (i+j) % 5 == 0:
total = total + (arr[i][j])
count += 1
return(total/count)
I appreciate the help Sam!

Loop function advice

I am currently doing a course on the fundamentals of python and I have a question regarding the below loop function:
x = 0
for index in range(5, 10):
x = x + 10
print("The value of X is {}".format(x))
When I print it I get the following result:
The value of X is 10
The value of X is 20
The value of X is 30
The value of X is 40
The value of X is 50
This is where I get confused, I was expecting a result like:
The value of X is 60
The value of X is 70
The value of X is 80
The value of X is 90
The value of X is 100
Am I misunderstanding something?
Here is value of x, index at each loop iteration
Iteration 1. x = 10 + 0 , index = 5
Iteration 2. x = 10 + 10 , index = 6
Iteration 3. x = 10 + 20 , index = 7
Iteration 4. x = 10 + 30 , index = 8
Iteration 5. x = 10 + 40 , index = 9
Thats how your loop will execute
What your function effectively does is to call
x = x + 10
five times. Since it is initialized with x = 0, the output makes sense to me.
The index variable in the loop is not used at all. The following loops would be equivalent:
for _ in range(5, 10):
...
for i in range(0, 5):
...
In your case, it is only about repeating a certain operation 5 times.
If you wanted to have an output as described in the questions, you would need to modify the loop as follows:
x = 10
for i in range(5, 10):
# This loop will be performed for values
# of i of [5, 6, 7, 8, 9], so it includes
# the first value (5) and excludes that last
# one (10)
print(x * i)
>> 50
>> 60
>> 70
>> 80
>> 90
Look at the code line by line:
First, you initialize the variable x to the value 0.
Then you set up a loop which will run five times.
You add 10 to the original value of x, and set that new value to x
You print the value of x.
Repeat 5 times.
x starts at 0, then you add 10, then 10 again, and so on, until the final value is 50. Does that make sense?
To produce the output you are expecting, this is a loop you could use:
>>> x = 0
>>> for index in range(6, 11):
... x = index * 10
... print("The value of X is {}".format(x))
The value of X is 60
The value of X is 70
The value of X is 80
The value of X is 90
The value of X is 100
The initial value of x is set to 0 and in each iteration of the loop you're adding 10 to it. That's why you get these values.
I believe you wanted it something like this.
x = 0
for index in range(6,11):
x = index*10
print("The value of X is {}".format(x))

How to combination between digits?

For given number N(Normal number) you must output amount of N-digit numbers, such, that last digits of their square is equal to 987654321.
where 1<=N<=10^6
It may be simple combinatorics problem. I am not sure. I am trying to find algorithm for this problem. What is the best algorithm to solve this problem?
The question is:
Find the number of n-digit number x that satisfies x^2 mod 1000000000 = 987654321.
1. Solution for n <= 9
First, calculate the value of x that satisfies x^2 = 987654321, 0 <= x < 1000000000.
You can pre-count in your PC because you only have to check for 10^9 integers.
I calculated the value and the result was following:
Result: 111111111,119357639,380642361,388888889,611111111,619357639,880642361,888888889
Therefore, if n <= 8 the answer is zero, and if n = 9 the answer is 8.
2. Solution for n >= 10
You can prove this facts because x^2 mod 1000000000 is cycle with length 10^9.
There are 8 solutions in 0 <= x < 10^9.
There are 8 solutions in 10^9 <= x < 2*10^9.
There are 8 solutions in 2*10^9 <= x < 3*10^9.
There are 8 solutions in k*10^9 <= x < (k+1)*10^9 (k is an integer).
So, you can say these things.
There are 8 solutions in 0 <= x < 10^9.
There are 80 solutions in 0 <= x < 10^10.
There are 800 solutions in 0 <= x < 10^11.
Therefore,
There are 72 solutions in all 10-digit number.
There are 720 solutions in all 11-digit number.
There are 7200 solutions in all 12-digit number.
So, you only have to output "72" + (n-10 0's) if n > 9.
3. Conclusion
If n <= 8, the answer is 0.
If n = 9, the answer is 8.
If n >= 10, the answer is 72 * 10^(n-10).

Resources