Python 2 - printing raw input into an array, formatting output - python-2.7

I'd like to take raw input from a user and then re-arranging the output in a different order. I have the code working, but the output has single quotes around each number. Here is the really simple code:
CodeNumber=(raw_input("Please enter 4 digit number: "))
print "Code_A: %r%r%r" %(CodeNumber[3], CodeNumber[1], CodeNumber[0])
print "Code_B: %r%r%r" %(CodeNumber[2], CodeNumber[3], CodeNumber[1])
The output I get is:
Please enter 4 digit number: 1234
Code_A: '4''2''1'
Code_B: '3''4''2'
How do I remove the single quotation marks so I get 421 instead of '4''2''1'?

At least one way to do this is to use int() on element indices of CodeNumber to turn the strings into numbers.
This is your edited code (by me, hob!)
CodeNumber=(raw_input("Please enter 4 digit number: "))
print "Code_A: %r%r%r" %(int(CodeNumber[3]), int(CodeNumber[1]), int(CodeNumber[0]))
print "Code_A: %r%r%r" %(int(CodeNumber[2]), int(CodeNumber[3]), int(CodeNumber[1]))
Output:
Please enter 4 digit number: 1234
Code_A: 421
Code_B: 342
Running time = O(n)

Related

How to store all the inputs in a list using a single command?

I was doing some ds and algo problems from faceprep and a question came:
Amy Farrah Fowler asks her students to give 3 examples for positive odd numbers. When the student specifies a correct answer, his/her score is incremented by 1. When the student specifies a positive even number, his/her score is decremented by 0.5. When the student specifies a negative number, he/she will not be given any more chances to correct his or her mistake and his/her score will be decremented by 1. So a student's turn comes to an end when he/she has correctly specified 3 positive odd numbers or when the student has specified a negative number. Some students didn't know the difference between odd numbers and even numbers and they made many mistakes and so it was difficult for her to maintain the scores. She asks for your help. Can you please help her by writing a program to calculate the score?
INPUT & OUTPUT FORMAT:
Input consists of a list of integers.
The output consists of a single line. The score needs to be corrected to 1 decimal place.
[For this exercise, don't worry about duplicate odd numbers. Even if the students specify the same odd number thrice, it is accepted].
input is given as:
1
2
3
4
5
Now How to take input and store it in a variable by single command?
till now I was doing but now number of inputs are different for each case study.
:
a=int(input())
b=int(input())
c=int(input())
I want to do this using a single command so that each input will store in a list.
Try this code snippet to store the input in the list "List_w".
List_w = []
try:
while(1):
List_w.append(int(input()))
except:
print(List_w)
if your input is separated by white space, you can use:
arr = list(map(int, input().split()))
print(arr) # input 1 2 3 4 # output [1,2,3,4]
if your input is based on given range use:
for _ in range(int(input())):
arr = list(map(int, input().split()))
print(arr)
if the inputs separated by new line you can use append method or one liner:
arr = [int(input()) for _ in range(int(input()))]
print(arr)
# input
3
4
5
6 # output [4, 5, 6]
know more about input here https://www.geeksforgeeks.org/python-get-a-list-as-input-from-user/

Regex to return integers only without surrounding letters or decimals

As the question says so that in the following string:
"2 x 500ml 1664 êcole beer £8.0"
i would get 2 and 1664 returned only.
Ive tried: [1-9][0-9]* which is close but returns the 500 in 500ml and the 8 in #8.0
The idea is to return the quantity when reading a line in a receipt, hence the example above.
Using Python, I would try to get the each number separately like this:
First occurrence:
"(\d{+})\ x"
Second occurrence:
"\ (\d{4})\ "
Now you'll just have to find a way to get the numbers that are inside each group.. ;-)
EDIT:
You can get the numbers using a one-line solution, and then fetch each subgroup to access the numbers.
One-line Solution:
"(\d+)\ "

Regular expression for Number masking with exceptions

I want to mask phone numbers in a resume which also contains date in the for 2001, 2001-03 and percentages 45% 87% 78.45% 56.5%.
I only want to mask the phone numbers, and I don't need to mask it completely. If I could only mask 3 or 4 digits that makes it hard to guess, that does the job. Kindly help me out.
Phone number formats are
9876543210
98765 43210
98765-43210
9876 543 210
9876-543-210
Here is my answer:
(([0-9][- ]*){5})(([0-9][- ]*){5})
It will match exactly 10 digits with or without - or space.
After that, you can replace the first or the third group with ***** or anything you like.
For example:
$1*****
\d{4,5}[ -]?\d{3}[ -]?\d{2,3}
Strings matched:
9876543210, 98765 43210, 98765-43210, 9876 543 210, 9876-543-210
Strings not matched:
45% 87% 78.45% 56.5%
2001, 2001-03
I feel that a more complicated regex that doesn't match invalid phone numbers is not required since the requirement is to mask valid phone numbers of the above format.
Check here
Python code:
def fun(m):
if m:
return '*'*len(m.group(1))+m.group(2)
string = "Resume of candidate abcd. His phone numbers are : 9876543210, 98765 43210, 98765-43210.Date of birth of the candidate is 23-10-2013. His percentage is 57%. One more number 9876 543 213 His percentage in grad school is 44%. Another number 9876-543-210"
re.sub('(\d{4,5})([ -]?\d{3}[ -]?\d{2,3})',fun,string)
Output:
'Resume of candidate abcd. His phone numbers are : *****43210, *****
43210, *****-43210. Date of birth of the candidate is 23-10-2013. His
percentage is 57%. One more number **** 543 213 His percentage in grad
school is 44%. Another number ****-543-210'
More about re.sub:
re.sub(pattern, repl, string, count=0, flags=0)
Return the string obtained by replacing the leftmost non-overlapping
occurrences of pattern in string by the replacement repl. If the
pattern isn’t found, string is returned unchanged. repl can be a
string or a function;
Just to help you on your way... I would use python to do is.
Use re module to search for number-like strings:
import re
num_re = re.compile('[0-9 -]{5,}')
with open('/my/file', 'r') as f:
for l in f:
for s in num_re.findall(l):
# Do some addition testing, like 'not starting with' or any
l.replace(s, '!!!MASKED!!!')
print l
I'm not saying that this code is finished, but it should help you on your way.
By the way, why I would use this approach:
You can easily add any tests you like to fix false positives.
Its readable.

How to not print the lines that have more than a certain number of characters

I am trying to print the lines that have numbers that are either greater or equal to 3 and less than or equal to 6. The problem is that the regular expression I have prints out the files that are 3 or more and not 3 to 6.
What I am trying to get
Valid:
123
34564
234552
Invalid:
12
1453756
What I am getting
Valid:
123
34564
234552
1453756
Invalid:
12
/[0-9]{3,6}/
I tried the following, and it worked for me :
\d{3,6}
Try to use following
^[\d]{3,6}[^\d]
It is working as specified in your requirement

Python Range() for positive and negative numbers

Okay, so I'm trying to write a simple program that gives me both the positive
and negative range of the number given by the user.
For example, if the user gives number 3 then the program should prints out
-3 -2 -1 0 1 2 3
I've tried thinking but just can't think how to get the negative range outputs.
But the code I got below only gives me the positive range outputs, so I was
thinking may I need to do to make it gives me both the positive and negative
outputs.
s = int(input("Enter a number "))
for i in range(s+1):
print i+1
Range() can take two parameters: range(start, end), where start is inclusive, and end is exclusive. So the range you want for 3 is: range(-3, 4). To make it general:
s = int(input("Enter a number "))
for i in range(-s, s+1):
print i