https://www.urionlinejudge.com.br/judge/en/problems/view/1145
Write an program that reads two numbers X and Y (X < Y). After this, show a sequence of 1 to y, passing to the next line to each X numbers.
Input
The input contains two integer numbers X (1 < X < 20) and Y (X < Y < 100000).
Output
Each sequence must be printed in one line, with a blank space between each number.
My code is here:
x,y = raw_input().split(" ")
x = int(x)
xr = x
y = int(y)
lis = []
for i in range(1, y+1):
lis.append(i)
j = 0
for i in range(1, y+1):
while j <= x:
try:
if j < x:
print str(lis[j]),
j=j+1
else:
if x == y:
break
else:
print ""
x = xr + x
except IndexError:
break
Code's output is accurate. But the website is not accepting my code for some reason. Please help me find the bug?
Maybe the problem in on the line print str(lis[j]),, which add a space after the third digit and the site don't consider this valid, the problem say "with a blank space between each number." and maybe this is considered invalid.
Another possible solution is to split the list with the X numbers in lists of Y elements and print them with something like print " ".join(lis[Xs:Xf])
If you need just print you probably don't need list.
I don't know how to format code from phone.
sx,sy = raw_input().split(" ")
x = int(sx)
y = int(sy)
lines = int(y/x)+1
for line in range(lines):
offset = x*line
for i in range(x):
print i+offset,
if i+offset>=y:
print
break
print
Related
This question already has an answer here:
Check if a number is a palindrome - fails for 11,22 etc
(1 answer)
Closed 10 months ago.
x = int(input())
if x<0 and (x%10 == 0 and x !=0):
print("false")
rev = 0
while x>0:
rev = rev*10 + x%10
x = x // 10
if rev == x:
print("true")
else:
print("false")
Since the reversed number is the same as entered one, the condition rev == x should return true but it's giving the opposite. What am I doing wrong?
Edit 1: converting to string is not allowed
Edit 2: 🤦 I see now.
Suggestion:
x = int(input())
You are reading x by input(), so a better idea to check if it's palindrome would be reversing it:
x = input()
if (x == x[::-1]):
...
To better understand [::-1] I would suggest you to read this.
Answer:
Your while loops modifies x, so it no longer has the original input value. It exits as 0
Somebody in the comments said this, which is your real problem.
while x>0:
rev = rev*10 + x%10
x = x // 10
To be clearer I would suggest you to read this part of your code, and to wonder what will be the value of x at the end of the while loop.
You are updating the 'x' value inside the while loop, irrespective of the input x equals 0 after the while loop.
Copy x value to some temp variable after the input() and compare that in the if condition.
x = 121
x1=x
if x<0 and (x%10 == 0 and x !=0):
print("false")
rev = 0
while x>0:
rev = rev*10 + x%10
x = x // 10
#print(x1,rev)
if rev == x1:
print("true")
else:
print("false")
So ive got two similar programs:
Program-1:
n = int(input())
mylist = []
x=0
for i in range(n):
t = input()
if '++' in t:
x+=1
else:
x-=1
print(x)
Program-2:
n = int(input())
mylist = []
for i in range(n):
mylist.append(input())
x=0
for x in range(n):
if '++' in mylist[x]:
x+=1
elif '--' in mylist[x]:
x-=1
print(x)
In input:
2
--X
--X
Program-1 is printing "-2" while Program-2 is printing "0".
I cant find the reason for this change in output.
Thanks for your help!
PS: It is my first question in this forum so guide me if i did anything wrong.
In program 2 you're using x as the for loop control variable:
for x in range(n):
while at the same time using it to store the accumulated sum. Those two uses conflict. Change the name of the variable.
The question is like this: Starting from 1, if I give you a number, say 6, I want you to find out what number is in position 6, after removing number containing 4. Result: 7
My script is like this:
x = int(input('What position do you want? '))
y = 0 #variable to iterate until y = x
z = 1 #count how many iteration
while y < x:
y_str = str(y)
if '4' in y_str:
z += 1
continue
else:
y += 1
z += 1
print(z)
But I don't know why the code don't stop. What's wrong with my code
You probably want something like this:
x = int( input( 'What position do you want? ' ) )
y = 0
z = 0
while y < x:
if '4' in str(y):
z += 1
y += 1
print (z+y)
if you want to also go the additional skipped steps alternate the while with this:
while y < x + z:
your old code wasn't working, since your loop was stuck at the moment, when it first encountered something containing the '4'. You then were not incrementing the y, which then resulted in finding the '4' over and over again while further not incrementing y.
your program will go into an infinite loop when it first encounters a number which contains digit 4 because in the following code portion you are not updating y and continuing.
if '4' in y_str:
z += 1
continue
I am very new to python, and I am trying to make a program that allows a user to enter three numbers and the program will tell them the sum, average, product, and the odd numbers of the numbers. I just can't seem to find a way to get the odd numbers listed on one line. Here is what I have:
def main():
x, y, z = eval(input("Enter three integers, each seperated by a comma: "))
Sum = (x+y+z)
Average = (x+y+z)/3
Product = (x*y*z)
print("Sum:", Sum)
print("Average:", Average)
print("Product:", Product)
print("Odd Numbers: ")
if (x % 2) != 0:
print(x)
if (y % 2) != 0:
print(y)
if (z % 2) != 0:
print(z)
main()
This one liner will work.
print('Odd Numbers:', ', '.join(str(num) for num in (x,y,z) if num % 2))
"if num % 2" resolves to a boolean true if the number is odd and false if even through an implicit conversion to boolean from integer. All values that are accepted need to be converted via "str(num)" to a string for use with ', '.join, which connects the string version of the odd numbers with a ', ' between them.
This will add all the odd numbers into an array, then print the array with a space between each value in the array. Got the join line from this SO answer.
odd_numbers = []
print("Odd Numbers: ")
if (x % 2) != 0:
odd_numbers.append(x)
if (y % 2) != 0:
odd_numbers.append(y)
if (z % 2) != 0:
odd_numbers.append(z)
print " ".join([str(x) for x in odd_numbers] )
Within your print, you can set your end character using end from a newline to a space to print them on one line.
print("Odd Numbers:",end=" ")
if (x % 2) != 0:
print(x,end=" ")
if (y % 2) != 0:
print(y,end=" ")
if (z % 2) != 0:
print(z,end=" ")
The output will be:
x y z if they are all odd.
Another note is that the use of eval should be carefully if not never used due to the security risks
Instead you can accomplish the user input of comma-separated values using the split function to return a list of values from the users.
userInput = input("Enter three integers, each seperated by a comma: ")
userInputList = userInput.split(",") #Split on the comma
#Assign x,y,z from userInput
x = userInput[0]
y = userInput[1]
z = userInput[2]
This is linear search code but it is not working can anyone help me out!!
data = []
n = int(raw_input('Enter how many elements you want: '))
for i in range(0, n):
x = raw_input('Enter the numbers into the array: ')
data.append(x)
print(data)
def lSearch(data, s):
for j in range(len(data)):
if data[j] == s:
return j
return -1
s = int(input('Enter the element do you want to search: '))
result = lSearch(data, s)
if result == -1:
print("The Element is not found")
else:
print("The element is an array at index %d",result)
s is an integer, but your list will be full of strings since you append x to it.
x = raw_input('Enter the numbers into the array: ')
See here x is a string. You have not converted this to an int (unlike at other places)
If this is actually Python 3 and not Python 2 (as it says in your tags), then you should be using input() not raw_input() (raw_input doesn't actually exist in Python 3). input() will return a string in Python 3 (unlike in python2, where it might return an integer).
thanks to everyone.Now , my code is running by a simple change raw_input to input().
data = []
n = int(input('Enter how many elements you want: '))
for i in range(0, n):
x = input('Enter the numbers into the array: ')
data.append(x)
k = sorted(data)
print(k)
def lSearch(k, s):
for j in range(len(k)):
if k[j] == s:
return j
return -1
s = int(input('Enter the element do you want to search: '))
result = lSearch(k, s)
if result == -1:
print("The Element is not found")
else:
print("The element is an array at index %d",result)