Dynamically use both python raw_input and raw_input().split() - python-2.7

The input format is:
6
1
2 5
2 7
2 9
1
1
Input:
First line contains an integer Q, the number of queries. Q lines follow.
A Type-1 ( Customer) Query, is indicated by a single integer 1 in the line.
A Type-2 ( Chef) Query, is indicated by two space separated integers 2 and C (cost of the package prepared) .
I want to read the input from stdin console and here is my code
n = int(input())
stack1 = []
for i in range(n):
x = input()
x = int(x)
if x == 2:
y = input()
stack1.append(y)
elif x == 1:
length = len(stack1)
if length > 0:
print(stack1.pop())
else:
print("No Food")
I have tried x,y = raw_input().split() this statement also fails because sometimes input has single value. Let us know how to read the defined input from stdin ???

Use len() to find length of string based on that change your stdin.
n = int(input())
for i in range(n):
s = input()
if(len(s) > 1):
x,y = s.split()
x = int(x)
else:
x = int(s)
print(x)
Cheers.

Related

Trying to create a new list of Odd numbers from a user inputted list?

This is my code. I have looked at many similar codes and have mine set up exactly how I should. I do not receive an error!
The problem is the output I receive is [11]. When the user inputs [1,2,3,4,5,6,7,8,9,11]. Why is it only pulling one odd number??
totlist = []
max_int = 10
oddlist = []
while len(totlist) < max_int:
nums = int(input('Enter a number: '))
totlist.append(nums)
def find_odds(totlist, oddlist):
if len(totlist) == 0:
return
v = totlist.pop()
if v % 2 == 1:
oddlist.append(v)
find_odds(totlist,oddlist)
print(oddlist)
You have forgoot the loop boucle inside the function
def find_odds(totlist, oddlist):
for item in range(len(totlist)) : # here
if len(totlist) == 0:
return
v = totlist.pop()
if v % 2 == 1:
oddlist.append(v)

How To Make raw_input Not A String In python

I want to make this program do the summation of something with their input. My code thus far
def summation():
start = int(raw_input("Start value of n?: "))
end = int(raw_input("End value of n?: "))
eqn = lambda n: raw_input("Equation?: ")
sum = 0
for i in range(start , end + 1):
sum += eqn(i)
return sum
print summation() # start will be 1, end will be 5 , equation will be n + 1. Should print 20
I get the error that I can't add an integer and a string together so is there any way to make the raw_input for equation not a string. Like instead of it being 'n + 1', I want it to be n + 1.
You could use input instead of raw_input, but this isn't really a good idea, since every time eqn is called it will call a input and prompt you for the equation.
A better method is to store the equation beforehand (using raw_input), and then use eval in the lambda function. Something like:
def summation():
start = int(raw_input("Start value of n?: "))
end = int(raw_input("End value of n?: "))
fx = raw_input("Equation: ")
eqn = lambda n: eval(fx)
sum = 0
for i in range(start , end + 1):
sum += eqn(i)
return sum
print summation()
Don't you need to surround your raw_input in your eqn variable with an int()?
I use python 3, but that should fix your problems.

Python character rotation

Could you please help me with writing a function, which receives a character char (ie., a string of length one ), and an integer rotation. My function should return a new string of length one, the resulting of rotating char by rotation number of places to the right. My output for this code should be like this:
print(alphabet_position(a, 13)) = Output = n
print(alphabet_position(A, 14)) = Output = (capital) O
print(alphabet_position(6, 13)) = Output = 6
My function looks like this
def alphabet_position(letter, number):
for char in letter:
return ord(char)+ number
print(alphabet_position("g", 2))
print(alphabet_position("Z", 2))
The output is 105
The output is 92
You forgot to do the checks you have mentioned and also, you need to use the chr(returnedValue) to convert the returned integer to a character. Check out the below code for the function:
def alphabet_position(letter, number):
if len(letter) != 1:
return -1 #Invalid input
elif letter.isalpha() == False:
return letter #If its not an alphabet
else:
ans = ord(letter) + number
# the below if-statement makes sure the value does not overflow.
if ans > ord('z') and letter.islower():
ans = ans - ord('z') + ord('a')
elif ans > ord('Z') and letter.isupper():
ans = ans - ord('Z') + ord('A')
return chr(ans)

Convert from decimal to binary - python

I'm having an issue with this piece of code I wrote. I'm trying to convert an integer input and print an output with its equivalent in binary base. For example for 5 it should drop an output of '101' however it just prints '10' like if it doesn't take into account the last digit. Please any comments would be greatly appreciated
T = raw_input()
for i in range(0, int(T)):
n = raw_input()
dec_num = int(n)
cnv_bin = ''
while dec_num//2 > 0:
if dec_num%2 == 0:
cnv_bin += '0'
else:
cnv_bin += '1'
dec_num = dec_num//2
print cnv_bin[::-1]
while dec_num//2 > 0:
should be:
while dec_num > 0:
The first time through the loop, 5//2==2, so it continues.
The second time through the loop, 2//2==1, so it continues.
The third time, 1//2==0 and the loop quits without handling the last bit.
Also, you can just do the following to display a number in binary:
print format(dec_num,'b')
Format string version:
print '{0} decimal is {0:b} binary.'.format(5)
Why not use the build-in function bin()?
eg:
bin(5)
output
0b101
If you don't want the prefix(0b), you can exclude it.
bin(5)[2:]
hope to be helpful!
import math
def roundup(n):
return math.ceil(n)
D = eval(input("Enter The Decimal Value: "))
n = roundup(math.log2(D+1))-1
bi = 0
di = D
qi = 0
i = n
print("Binary Value:",end = " ")
while(i>=0):
qi = math.trunc(di/2**i)
bi = qi
print(bi,end = "")
di = di - bi*(2**i)
i = i-1

How to remove numbers in a list that contain certain digits inside them

This is my code:
def second_test(numbers):
for x in numbers:
if 1 in x:
numbers.remove(x)
elif 7 in x:
numbers.remove(x)
print numbers
second_test(numbers)
Numbers is a list that contains int values from 10 to 1000. I am trying to remove numbers within this range that contain either a 1 or a 7 in them. Any suggestions?
You'll have to check if any digit of the number is a 1 or 7. There are two ways to do this:
The first way: Keep dividing the number by 10 and check the remainder (this is done with the modulus operator), until the number becomes 0
def check_num(n):
while n:
if n%10 == 1 or n%10 == 7:
return True
n /= 10
return False
def second_test(numbers):
to_delete = []
for i,x in enumerate(numbers):
if check_num(x):
to_delete.append(i)
for d in to_delete[::-1]:
del numbers[d]
The second way: Turn the number into a string, and check each character of the string
def check_num(n):
for char in str(n):
if char=='1' or char=='7':
return True
return False
def second_test(numbers):
to_delete = []
for i,x in enumerate(numbers):
if check_num(x):
to_delete.append(i)
for d in to_delete[::-1]:
del numbers[d]