python not accepting 09 but accepts 07 - python-2.7

Does anyone why python handles the below this way.
>>> a = 099
File "<stdin>", line 1
a = 099
^
SyntaxError: invalid token
>>> a = 088
File "<stdin>", line 1
a = 088
^
SyntaxError: invalid token
>>> a = 0559
File "<stdin>", line 1
a = 0559
^
SyntaxError: invalid token
>>> a = 077
>>>
It does not seem to accept numbers starting with 0 and preceding with 8 or 9. If it is some other number, it is not throwing any error. Why is that?

In Python 2, like in C, an integer literal that starts with a 0 is in octal. Digits 8 and 9 do not exist in octal (they are written 010 and 011 respectively) so that is a syntax error.
>>> 010
8
>>> 08
File "<stdin>", line 1
008
^
SyntaxError: invalid token
In Python 3, this feature not many people know about is gone. There, nonzero literals that start with a 0 are syntax errors.

Related

Regular expression for matching a list of numbers [duplicate]

This question already has answers here:
Regular expression for exact match of a string
(6 answers)
Closed 6 years ago.
I have a list of valid numbes which looks like this:
50, 56, 62, 68, 74, 80, 86, 92, 98, 104, 110, 116, 122, 128, 134, 140, 146, 152, 158
I need an regular expression which allows every number in this list, but nothing else. This regex is used to validate a HTML5 input control. A few examples for clarification:
50 => true
150 => false
abc => false
51 => false
110 => true
11 => false
50, 56 => false
I have tried this expression, but the problem is that numbers like 156 and 150 also get matched.
50|56|62|68|74|80|86|92|98|104|110|116|122|128|134|140|146|152|158
Can anyone help me out?
Add ^ and $ to match begining and ending of string
^(50|56|62|68|74|80|86|92|98|104|110|116|122|128|134|140|146|152|158)$
Edit
OP has mentioned in the comment that he tested his pattern using online tool and got wrong result. Also Wiktor Stribiżew mentioned in the comment that OP's pattern is correct if that pattern is used in pattern attribute of HTML 5 Input element.

R: a grep alternative for a file without using readLines?

Is there any function in any package that can read a text file with regex and return string numbers of found matches. Like gsubfn read.pattern can find and extract a pattern but can't return line number and grep can't read files directly. Example:
file:
.122448110000D+06 .400000000000D+01
3 15 3 23 10 0 0.0 .267305411398D-03 .161435309564D-10 .000000000000D+01
.510000000000D+02 .625000000000D-01 .440982654411D-08 .306376855997D+00
5 15 3 23 11 59 44.0 -.263226218521D-03 .488853402202D-11 .000000000000D+01
pattern: reg="^ *\\d+ +(?:[0-9]+ +){5}[.0-9]+.*$" for 2nd and 4th line match. So what I generally want is:
>file.grep(file,reg)
[1] 2 4
Is there anything of sorts? I get the general philosophy when dealing with such things is readLines and then getting creative with grep which is fine when files are not that big. But I read here many people having problems with large and not table-structured data sets, things that could be solved with such tool (or with readLines supporting regex skip parameter) and I wonder if anyone made something like that.
EDITED1
I just found another post relating to this question with an alternative solution:
grep while reading file
ORIGINAL POST
Is this what you are looking for?
library(gsubfn)
cat(" .122448110000D+06 .400000000000D+01
3 15 3 23 10 0 0.0 .267305411398D-03 .161435309564D-10 .000000000000D+01
.510000000000D+02 .625000000000D-01 .440982654411D-08 .306376855997D+00
5 15 3 23 11 59 44.0 -.263226218521D-03 .488853402202D-11 .000000000000D+01", file = "test.txt")
read.pattern(text = readLines("test.txt"), pattern = "^ *\\d+ +(?:[0-9]+ +){5}[.0-9]+.*$")

Sort List of Numbers according to Custom Number Sequence

Question :A set of numbers will be passed as input. Also the redefined relationship of the digits 0-9 in ascending order will be passed as input. Based on the redefined relationship, the set of numbers must be listed in ascending order.
Input Format:
The first line will contain the the set of numbers.
The next line will contain the digits 0-9 in the redefined ascending order.
Boundary Conditions:
The size of the set of numbers will be from 2 to 100.
Output Format:
The set of numbers in ascending order as per the redefined order of digits separated by a space.
Example Input/Output 1:
Input:
20 50 11 121
9231476058
Output:
50 11 20 121
Explanation:
121 is a three digit number and hence comes first.
As per the redefined order 2 > 1 > 5.
So 121 is greater than all others and comes in the end.
20 > 11 > 50 and hence in ascending order this is reversed.
Example Input/Output 2:
Input:
319 311 198 420
1948327605
Output:
319 311 420 198
Explanation:
As per the redefined order 1 > 4 > 3
Among 319 and 311, 1 > 9
Hence the final ascending order is 319 311 420 198
My Solution :
if __name__ == '__main__':
list_ = raw_input().split()
num = str(raw_input())
output = sorted(list_, key = num.index)
print(' '.join(output))
I need to know how to do multiple levels of sorting such that it compares indexes of first character, then second character & so on...
This matches your input/output examples, but I had to use descending numbers to get the example answers. Are you sure your explanation is correct? If not, just use 0123456789 instead of 9876543210 in the code below.
The algorithm is to provide a sorting key based on translating the digits of the number into their corresponding rank digits:
import string
def xsort(L,xlat):
def xform(s):
return int(str(s).translate(string.maketrans(xlat,'9876543210')))
return sorted(L,key=xform)
print xsort([20,50,11,121],'9231476058')
print xsort([319,311,198,420],'1948327605')
Output:
[50, 11, 20, 121]
[319, 311, 420, 198]
References: str.translate, string.maketrans

very strange: if x not in statement Python 2.7

I encountered a very strange thing, to me at least.
if month not in (02, 04, 06, 11):
print "Good"
whenever I add 09 to the tuple i got the error called: SyntaxError: invalid token and it's only for this particular number.
any idea?
When you use a leading 0 on a number, Python interprets that as a base-8 (octal) number. Remove the leading 0:
>>> 10
10
>>> 010
8
>>> 9
9
>>> 09
File "<stdin>", line 1
09
^
SyntaxError: invalid token
Python 3 has improved on this; all numbers with a leading 0 are considered invalid now, to create a octal number you always have to use the 0o prefix instead:
>>> 010
File "<stdin>", line 1
010
^
SyntaxError: invalid token
>>> 0o10
8
Numbers with leading 0s are considered octal, so 09 is invalid... Just drop the 0's
i think you need to get rid of the zeros before the numbers it might be interpreting it as an octal number...
you can try this.(reason is already answered why 09 provide error)
month='05'
if month not in ('02', '04', '06', '11','09'):
print "Good"

Regex digits smaller then 82

I'm trying to write a simple regex but I don't know why it is not working.
User enter 2 digits number like 01, 09, 23, 55, until 82. After 82 system will refuse.
Here is my regex, 2 digits must be smaller than 82.
0[1-9]|[1-8][0-9]|8[0-2]
You should have [1-7] for the range 10-79, not [1-8]. Don't forget the ^ and $ to specify the start and ending of the string:
^(0[1-9]|[1-7]\d|8[0-2])$
Why not cast to an integer and then just test x < 82?
Your second part is wrong. It'll match from 10 to 89, whereas you want it to match from 10 to 79 and let the third part handle 80 to 82.
0[1-9]|[1-7][0-9]|8[0-2]