i want have comparison among input numbers .general shape of input numbers is like this :
n
x1 x2
x3 x4
x5 x6
x7 x8
n indicates how many input line we have and on each line we have 2 number with space ,just 2 number,
the first part of line is price and the second is quality
for example like this :
4
1 5
7 9
5 6
20 30
in line two '1' is price and '5' is quality
if i can find more quality with lower price i print 'yes' if not print 'no'
i write this but i can not change list to dictionary
x = int(input())
count =0
y= []
while count!=x:
y.append(input())
count+=1
z= []
for i in y:
z.append(i.split())
def dictionary(x):
d={x[0]:x[1]}
return d
for h in z:
a=dictionary(h)
for example i want
2
1 10
7 3
'yes'
or another example :
4
1 5
7 9
5 6
20 30
'no'
i hope someone answer this
n = int(input())
q,p = map(int, input().split())
for _ in range(n-1):
q1,p1 = map(int, input().split())
if q1 > q and p1 < p:
print("yes")
break
else:
print("no")
This works. The code is really simple.
Related
problem:
Enter 10 integers and create a program that outputs the number of multiples of 3 and the number of multiples of 5 respectively.
ex)input:
10 15 36 99 100 19 46 88 87 13
ex)print:
Multiples of 3 : 4
Multiples of 5 : 3
my code:
t=0
f=0
a=list(input().split())
for i in range(11):
if int(a[i])%3==0: #index
t+=1
else:
int(a[i]) % 5==0
f += 1
print('Multiples of 3 :',t)
print('Multiples of 5 :',f)
error:
if int(a[i])%3==0:
IndexError: list index out of range
enter code here
what's wrong?
I don't know
plz help me I an so angry
Try this python code snippet
t=0
f=0
n = int(input("Enter number of elements : "))
for i in range(0, n):
ele = int(input())
if (ele % 3) == 0:
t = t + 1
elif (ele % 5) == 0:
f = f + 1
else:
print("element not counted")
print('Multiples of 3 :',t)
print('Multiples of 5 :',f)
I tried this code for list of 3 values of 3, 5 and 10 respectively
I am new to coding and I am finding this site really helpful. So I have been trying to solve this problem and I am getting erroneous results, so I would be really grateful if you could help me out here.
The Problem: Find the sum of all the multiples of 3 or 5 below 1000. (For example, if we list all the positive integers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9, which sum is 23.)
My code:
count = 0
count1 = 0
for x in range(1000):
if x % 5 == 0:
count = count + x
if x % 3 == 0:
count1 = count1 + x
print count1 + count
What am I doing wrong?
Thanks in advance!
You want an elif in your code so you don't count the same x twice but a simpler way is to use an or with a single count variable:
count = 0
for x in range(1000):
if x % 5 == 0 or x % 3 == 0:
count += x
Which can be done using sum:
print(sum(x for x in range(3, 1000) if not x % 5 or not x % 3))
For completeness, a working version using your own code:
count = 0
count1 = 0
for x in range(1000):
if x % 5 == 0:
count += x
elif x % 3 == 0:
count1 += x
print count1 + count
ifs are always evaluated; so, for instance, when x is 15 it is evenly divisible by 5 and 3 so you count 15 twice, an elif is only evaluated if the previous if/elif evaluates to False so using elif only one occurrence of x will be added to the total.
Below 10 there is no number being multiple of both 5 and 3. But below 1000 there are several numbers divided exactly by 3 and 5 also (15, 45 ...).
So you need:
count=0
for x in range(1000):
if x % 5 == 0 or x % 3 == 0:
count=count + x
print count
Problem originally is in this link. I wrote a Python code but I got 64 points (total points is 100) and this indicates that my code has some missing points. I passed 11 of 16 test cases but 5 test cases have problematic for me. Could you say where my code has some missing points and how can I fix it?
import math
m = int(raw_input())
liste = []
y_liste = []
md = 0
ad = 0
sum = 0
sum2 = 0
for k in range(m):
temp = str(raw_input())
liste.append(temp)
liste[k] = liste[k].split(" ")
liste[k] = [int(i) for i in liste[k]]
for k in range(m):
md += liste[k][k]
ad += liste[k][m-k-1]
if md == ad:
print 0
else:
for k in range(m):
for l in range(m):
sum2 += liste[l][k]
sum += liste[k][l]
if sum2 != md and -(k+1) is not y_liste:
y_liste.append(-(k+1))
if sum != md and (k+1) is not y_liste:
y_liste.append(k+1)
sum2 = 0
sum = 0
if md != ad:
y_liste.append(0)
print len(y_liste)
y_liste.sort()
for i in y_liste:
print i
Problem Statement
Magic Square
Johnny designed a magic square (square of numbers with the same sum for all rows, columns and diagonals i.e. both the main diagonal - meaning the diagonal that leads from the top-left corner towards bottom-right corner - and the antidiagonal - meaning the diagonal that leads from top-right corner towards bottom-left corner). Write a program to test it.
Task
Write a program that will check if the given square is magic (i.e. has the same sum for all rows, columns and diagonals).
Input
First line: N , the size of the square (1 <= N <= 600).
Next N lines: The square, N space separated integers pre line, representing the entries per each row of the square.
Output
First line: M , the number of lines that do not sum up to the sum of the main diagonal (i.e. the one that contains the first element of the square). If the Square is magic, the program should output 0.
Next M lines: A sorted (in incremental order ) list of the lines that do not sum up to the sum of the main diagonal. The rows are numbered 1,2,…,N; the columns are numbered -1,-2,…,-N; and the antidiagonal is numbered zero.
Note: There is a newline character at the end of the last line of the output.
Sample Input 1
3
8 1 6
3 5 7
4 9 2
Sample Output 1
0
Sample Input 2
4
16 3 2 13
5 10 11 8
6 9 7 12
4 15 14 1
Sample Output 2
3
-2
-1
0
Explanation of Sample Output 2
The input square looks as follows: http://i.stack.imgur.com/JyMgc.png
(Sorry for link but I cannot add image due to reputation)
The square has 4 rows (labeled from 1 to 4 in orange) and 4 columns (labeled from -1 to -4 in green) as depicted in the image above. The main diagonal and antidiagonal of the square are highlighted in red and blue respectively.
The main diagonal has sum = 16 + 10 + 7 +1 = 34.
The antidiagonal has sum = 13 + 11 + 9 + 4 = 37. This is different to the sum of the main diagonal so value 0 corresponding to the antidiagonal should be reported.
Row 1 has sum = 16 + 3 + 2 + 13 = 34.
Row 2 has sum = 5 + 10 + 11 + 8 = 34.
Row 3 has sum = 6 + 9 + 7 + 12 = 34.
Row 4 has sum = 4 + 15 + 14 + 1 = 34.
Column -1 has sum = 16 + 5 + 6 + 4 = 31. This is different to the sum of the main diagonal so value -1 should be reported.
Column -2 has sum = 3 + 10 + 9 + 15 = 37. This is different to the sum of the main diagonal so value -2 should be reported.
Column -3 has sum = 2 + 11 + 7 + 14 = 34.
Column -4 has sum = 13 + 8 + 12 + 1 = 34.
Based on the above, there are 3 lines that do not sum up to the sum of the elements of the main diagonal. Since they should be sorted in incremental order, the output should be:
3
-2
-1
0
Your explanation doesn't discuss this clause which is a potential source of error:
if md == ad:
print 0
else:
It says that if the main diagonal and antidiagonal add up to the same value, print just a 0 (no bad lines) indicating the magic square is valid (distinct from reporting a 0 in the list of bad lines). Consider this valid magic square:
9 6 3 16
4 15 10 5
14 1 8 11
7 12 13 2
If I swap 13 and 11, the diagonals still equal each other but the square is invalid. So the above code doesn't appear to be correct. In the else clause for the above if statement, you test:
if md != ad:
y_liste.append(0)
a fact you already know to be true from the previous/outer test so your code seems to be out of agreement with itself.
I need to write a code that inputs a non-right Pascal's triangle given the nth level as an input where the first row is the 0th level. Apart from that, at the end of each row the level must be indicated.
Here's what I've made so far:
level = input('Please input nth level: ')
x = -1
y = 1
while x < level:
x = x+1
d = str(11**x)
while y < level:
y = y+1
print " ",
for m,n in enumerate(d):
print str(n) + " ",
while y < level:
y = y+1
print " ",
print x
When I input 3, it outputs:
1 0
1 1 1
1 2 1 2
1 3 3 1 3
My desired output is:
1 0
1 1 1
1 2 1 2
1 3 3 1 3
You could use str.format to center the string for you:
level = int(raw_input('Please input nth level: '))
N = level*2 + 5
for x in range(level+1):
d = ' '.join(str(11**x))
print('{d:^{N}} {x:>}'.format(N=N, d=d, x=x))
Please input nth level: 4
1 0
1 1 1
1 2 1 2
1 3 3 1 3
1 4 6 4 1 4
Note that if d = '1331', then you can add a space between each digit using ' '.join(d):
In [29]: d = '1331'
In [30]: ' '.join(d)
Out[30]: '1 3 3 1'
Note that using d = str(11**x) is a problematic way of computing the numbers in Pascal's triangle since it does not give you the correct digits for x >= 5. For example,
Please input nth level: 5
1 0
1 1 1
1 2 1 2
1 3 3 1 3
1 4 6 4 1 4
1 6 1 0 5 1 5 <-- Should be 1 5 10 10 5 1 !
You'll probably want to compute the digits in Pascal's triangle a different way.
I have a variable age, 13 variables x1 to x13, and 802 observations in a Stata dataset. age has values ranging 1 to 9. x1 to x13 have values ranging 1 to 13.
I want to know how to count the number of 1 .. 13 in x1 to x13 according to different values of age. For example, for age 1, in x1 to x13, count the number of 1,2,3,4,...13.
I first change x1 to x13 as a matrix by using
mkmat x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13, matrix (a)
Then, I want to count using the following loop:
gen count = 0
quietly forval i = 1/802 {
quietly forval j = 1/13 {
replace count = count + inrange(a[r'i', x'j'], 0, 1), if age==1
}
}
I failed.
I am still somewhat uncertain as to what you like to achieve. But if I am understanding you correctly, here is one way to do it.
First, a simple data that has age ranging from one to three, and four variables x1-x4, each with values of integers ranging between 5 and 7.
clear
input age x1 x2 x3 x4
1 5 6 6 6
1 7 5 6 5
2 5 7 6 6
3 5 6 7 7
3 7 6 6 6
end
Then we create three count variables (n5, n6 and n7) that counts the number of 5s, 6s, and 7s for each subject across x1-x4.
forval i=5/7 {
egen n`i'=anycount(x1 x2 x3 x4),v(`i')
}
Below is how the data looks like now. To explain, the first "1" under n5 indicates that there is only one "5" for the subject across x1-x4.
+----------------------------------------+
| age x1 x2 x3 x4 n5 n6 n7 |
|----------------------------------------|
1. | 1 5 6 6 6 1 3 0 |
2. | 1 7 5 6 5 2 1 1 |
3. | 2 5 7 6 6 1 2 1 |
4. | 3 5 6 7 7 1 1 2 |
5. | 3 7 6 6 6 0 3 1 |
+----------------------------------------+
It sounds to me like your ultimate goal is to have sums calculated separately for each value in age. Assuming this is true, let's create a 3x3 matrix to store such results.
mat A=J(3,3,.) // age (1-3) and values (5-7)
mat rown A=age1 age2 age3
mat coln A=value5 value6 value7
forval i=5/7 {
forval j=1/3 {
qui su n`i' if age==`j'
loca k=`i'-4 // the first column for value5
mat A[`j',`k']=r(sum)
}
}
The matrix looks like this. To explain, the first "3" under value5 indicates that for all children of the age of 1, the value 5 appears a total of three times across x1-x4
A[3,3]
value5 value6 value7
age1 3 4 1
age2 1 2 1
age3 1 4 3
With Aspen's example, you could do this:
gen id = _n
reshape long x, i(id)
tab age x
Note that your sample code doesn't loop over different ages and there is an incorrect comma in the count command. I won't try to fix the code, as there are many more direct methods, one of which is above. tabulate has an option to save the table as a matrix.
Here is another solution closer to the original idea. Warning: code not tested.
matrix count = J(9, 13, 0)
forval i = 1/9 {
forval j = 1/13 {
forval J = 1/13 {
qui count if age == `i' & x`J' == `j'
matrix count[`i', `j'] = count[`i', `j'] + r(N)
}
}
}