Python character rotation - python-2.7

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)

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)

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

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.

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]

something wrong with my password generator

I made a password generator - I'm only 16 so it's probably not the best- and it outputs 8 0 and ones like 01100101 and then enderneath that it outputs the password. Well when there is a "10" in the password like FG4v10Y6 it will add another character so instead of it being FG4v10Y6 it would be FG4v10Y6M so it has nine or more characters depending on how many "10" are in it.
I'm not sure why it's doing this please help. THanx!
import pygame
import random
pygame.init()
#letters
reg = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
CAP = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
final_pass = []
num_let_list = []
new_list = []
i = 0
file = open("password_test","w")
def num_list_gen(num_list):
for i in range(8):
num_let_list.append(random.randint(0,1))
i += 1
for each in num_let_list:
each = str(each)
new_list.append(each)
print ''.join(new_list)
def CAP_reg_num(final_pass,num_let_list,CAP,reg):
for each in num_let_list:
if each == 0:
cap_reg = random.randint(0,1)
if cap_reg == 0:
let1 = random.randint(0,25)
final_pass.append(reg[let1])
if cap_reg == 1:
let1 = random.randint(0,25)
final_pass.append(CAP[let1])
if each == 1:
num1 = random.randint(0,10)
num1 = str(num1)
final_pass.append(num1)
def main(CAP,reg,num_let_list,final_pass):
num_list_gen(num_let_list)
CAP_reg_num(final_pass,num_let_list,CAP,reg)
print ''.join(final_pass)
file.write(''.join(final_pass))
file.close
main(CAP,reg,num_let_list,final_pass)
why did the code come out all weird on the post in some places and how do you fix it?
Your password generator is flipping a coin to choose between adding a letter or a number. When it chooses to add a number, you choose the number to add with:
num1 = random.randint(0,10)
However, this doesn't return a single digit number. It returns one of 11 possible values: the numbers between 0 and 10 inclusive. So one time in 11, it will add the number 10 to the string, which is, of course, two digits.
You want:
num1 = random.randint(0,9)
instead to add a single digit.