Consider I have a list in Tcl as follows :
a : { 12 34 1 3262 921029 232 0.2732}
How can I extract the three biggest elements of list a without sorting it?
Related
I'm trying to get the regular expression to work (using jQuery) for a specific pattern I need.
I need following pattern:
First two character
s of the string need to be numbers (0-9) but maximum number is 53. for numbers below 10 a leading 0 is required
Character on position 3 needs to be a .
the next 4 characters need to be a number between 0-9, minimum number should be 2010, maximum 2050
so, Strings like 01.2020, 21.2020, or 45.2020 have to match but 54.2020 or 04.2051 must not.
I tried to write the regex without the min and max requirement first and I'm testing the string using regex101.com but I'm unable to get it to work.
acording to the definition /^[0-9]{2}\.\d[0-9]{4}$/ should allow me to insert the strings in the format NN.NNNN.
thankful for any input.
2 numbers from 00 to 53 can be matched using this : (?:[0-4][0-9]|5[0-3]) (00 -> 49 or 50 -> 53)
Character on position 3 needs to be a . : you've already got the \.
a number between 2010 and 2050 -> 20(?:[1-4][0-9]|50) (20 followed by either 10 -> 49 or 50)
This gives :
(?:[0-4][0-9]|5[0-3])\.20(?:[1-4][0-9]|50)
I have 3 lists, one list of 200 first names, one list of 200 Last names, and third list is a list that combines that previous two lists to create a 6 digit code for 1 person and my third list contains 200 randomly generated people
I.E 152208 =
52 (Bob)
and
108 (Jones)
so that 152208 would be Bob Jones.
If I were too produce a save-code from this i would get a code that would be 1,200 digits long (6 digits * 200 people) I know it would be possible to get this to 7 digits as 200 * 200 * 200 = 8,000,000 which is 7 digits long.
How would I compress this code by only using non negative whole numbers
I have
List1 = [[...11,12,13,14,7,8,9],[0,1,2,3]]
where ... are consecutive integers starting from from 0 up to 11
I want
List2 = [[11,12,13,14],[7,8,9],[0,1,2,3]]
EDIT:
Found the answer:
from Python: split list of integers based on step between them
[list(g) for k, g in groupby(listName, key=lambda i,j=count(): i-next(j))]
Turns out, that wasn't what I was looking for. I need to be able to split the list of lists of integers by consecutive order ONLY if the next integer in that list was of lesser value than the preceding integer.
e.g.
[[0,1,2,15,16,17,2,3,4,6,8,9]]
should be split into
[[0,1,2,15,16,17],[2,3,4,6,8,9]]
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
I'm trying to find the year from the date.
the dates are in the format
"Nov.-Dec. 2010"
"Aug. 30 2011-Sept. 3 2011"
"21-21 Oct. 1997"
my regular expression is
q = re.compile("\d\d\d\d")
a = q.findall(date)
so obviously in the list it has two items for a string like "Aug. 30 2011-Sept. 3 2011"
["2011","2011"]
i dont want a repetition, how do i do that?
You could use a backreference in the regex (see the syntax here):
(\d{4}).*\1
Or you could use the current regex and put this logic in the python code:
if a[0] == a[1]:
...
Use the following function :
def getUnique(date):
q = re.compile("\d\d\d\d")
output = []
for x in q.findall(date):
if x not in output:
output.append(x)
return output
It's O(n^2) though, with the repeated use of not in for each element of the input list
see How to remove duplicates from Python list and keep order?