a=[('b', 4), ('a', 12), ('d', 7), ('h', 6), ('j', 3)]
a.sort(key=lambda a:a[1])
print(a)
寻找数组中最大和的连续子数组,保证时间复杂度O(n)
def testsum(alist):
length=len(alist)
tmp=0
if length==0:
return None
elif length==1:
return alist[0]
mymax=alist[0]
start=0
for i in range(length):
if tmp <=0:
tmp=alist[i]
start=i
else:
tmp=tmp+alist[i]
end=i
if tmp>mymax:
mymax=tmp
mystart=start
myend=end
if mystart>myend:
myend=mystart
return mymax,mystart,myend
alist=[1,-2,3,10,-4,7,2,-5]
mymax=testsum(alist)
print(mymax)
print(testsum(alist))
两个栈来实现一个队列
class Queue(object):
def __init__(self):
self.stack1=[]
self.stack2=[]
def enqueue(self, element):
self.stack1.append(element)
length=len(self.stack1)
if length>1:
for i in range(length-1,-1,-1):
self.stack2.append(self.stack1[i])
def dequeue(self):
element=self.stack2.pop()
return element
myqueue=Queue()
for i in range(10):
myqueue.enqueue(i)
for i in range(10):
print(myqueue.dequeue())
用python写一个类似C里的atoi函数
要求
4. Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function).
The algorithm for myAtoi(string s) is as follows:
Read in and ignore any leading whitespace.
Check if the next character (if not already at the end of the string) is '-' or '+'. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.
Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored.
Convert these digits into an integer (i.e. "123" -> 123, "0032" -> 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2).
If the integer is out of the 32-bit signed integer range [-2^31, 2^31 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -2^31 should be clamped to -2^31, and integers greater than 2^31 - 1 should be clamped to 2^31 - 1.
Return the integer as the final result.
Note:
Only the space character ' ' is considered a whitespace character.
Do not ignore any characters other than the leading whitespace or the rest of the string after the digits.
** You CANNOT use Python built-in "int","*strip" functions for this question **
Example 1:
22/02/17 17:04:52 major alarm occurred for ont 1/1/3/2/12 (service affecting) : ONT fails to respond to OMCI message requests
Input: s = "42"
Output: 42
Explanation: The underlined characters are what is read in, the caret is the current reader position.
Step 1: "42" (no characters read because there is no leading whitespace)
^
Step 2: "42" (no characters read because there is neither a '-' nor '+')
^
Step 3: "42" ("42" is read in)
^
The parsed integer is 42.
Since 42 is in the range [-2^31, 2^31 - 1], the final result is 42.
Example 2:
Input: s = " -42"
Output: -42
Explanation:
Step 1: " -42" (leading whitespace is read and ignored)
^
Step 2: " -42" ('-' is read, so the result should be negative)
^
Step 3: " -42" ("42" is read in)
^
The parsed integer is -42.
Since -42 is in the range [-2^31, 2^31 - 1], the final result is -42.
Example 3:
Input: s = "4193 with words"
Output: 4193
Explanation:
Step 1: "4193 with words" (no characters read because there is no leading whitespace)
^
Step 2: "4193 with words" (no characters read because there is neither a '-' nor '+')
^
Step 3: "4193 with words" ("4193" is read in; reading stops because the next character is a non-digit)
^
The parsed integer is 4193.
Since 4193 is in the range [-2^31, 2^31 - 1], the final result is 4193.
Example 4:
Input: s = "words and 987"
Output: 0
Explanation:
Step 1: "words and 987" (no characters read because there is no leading whitespace)
^
Step 2: "words and 987" (no characters read because there is neither a '-' nor '+')
^
Step 3: "words and 987" (reading stops immediately because there is a non-digit 'w')
^
The parsed integer is 0 because no digits were read.
Since 0 is in the range [-2^31, 2^31 - 1], the final result is 0.
Example 5:
Input: s = "-91283472332"
Output: -2147483648
Explanation:
Step 1: "-91283472332" (no characters read because there is no leading whitespace)
^
Step 2: "-91283472332" ('-' is read, so the result should be negative)
^
Step 3: "-91283472332" ("91283472332" is read in)
^
The parsed integer is -91283472332.
Since -91283472332 is less than the lower bound of the range [-2^31, 2^31 - 1], the final result is clamped to -2^31 = -2147483648.
mydict={ str(n):n for n in range(10)}
print(mydict)
import re
def myAtoi(s):
s=re.sub(r'^\s+','',s)
if re.match(r'[^-+0-9]',s):
print(0)
else:
mynum=re.match(r'([-+]?\d*)',s)
if mynum:
mynum=mynum.group(1)
myobj=re.search(r'\d',mynum)
if myobj:
if mynum.startswith('+'):
core(mynum[1:],1)
elif mynum.startswith('-'):
core(mynum[1:],0)
else:
core(mynum,1)
else:
if re.match(r'-',mynum):
print('-',end='')
print(0)
else:
print('+',end='')
print(0)
def core(num,flag):
mysum=0
for (i,v) in enumerate(num[::-1]):
mysum+=mydict[v]*pow(10,i)
if flag==0:
mysum=-mysum
start=-pow(2,31)
end=pow(2,31)-1
if mysum>=start and mysum<=end:
print(mysum)
elif mysum<start:
print(start)
else:
print(end)
def test(run_list):
mystart=-pow(2,31)
myend=pow(2,31)-1
test1="546goij"
test2="-426oij"
test3="+5670oj"
test4="00456"
test5=str(mystart)
test6=str(myend)
test7=str(mystart-1)
test8=str(myend+1)
test9="4193 with words"
test10="words and 987"
test11="-91283472332"
test12=" 42"
test13="+"
test14="-"
for i in run_list:
eval('myAtoi(test{})'.format(i))
run_list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14]
test(run_list)