1.判断是否是平方数
import math
def is_square(n):
if n>=0:
return True if int(math.sqrt(n))==math.sqrt(n) else False
else:
return False
import math
def is_square(n):
return n > -1 and math.sqrt(n) % 1 == 0;
def is_square(n):
if n>=0:
if int(n**.5)**2 == n:
return True
return False
2.求3和5 的倍数的和,
def solution(number):
return sum(x for x in range(number) if x % 3 == 0 or x % 5 == 0)
Let's say you are given the array {1,2,3,4,3,2,1}
:
Your function will return the index 3
, because at the 3rd position of the array, the sum of left side of the index ({1,2,3}
) and the sum of the right side of the index ({3,2,1}
) both equal 6
.
Let's look at another one.
You are given the array {1,100,50,-51,1,1}
:
Your function will return the index 1
, because at the 1st position of the array, the sum of left side of the index ({1}
) and the sum of the right side of the index ({50,-51,1,1}
) both equal 1
.
Last one:
You are given the array {20,10,-80,10,10,15,35}
At index 0 the left side is {}
The right side is {10,-80,10,10,15,35}
They both are equal to 0
when added. (Empty arrays are equal to 0 in this problem)
Index 0 is the place where the left side and right side are equal.
Note: Please remember that in most programming/scripting languages the index of an array starts at 0.
Input:
An integer array of length 0 < arr < 1000
. The numbers in the array can be any integer positive or negative.
Output:
The lowest index N
where the side to the left of N
is equal to the side to the right of N
. If you do not find an index that fits these rules, then you will return -1
.
Note:
If you are given an array with multiple answers, return the lowest correct index.
An empty array should be treated like a 0
in this problem.
Test.assert_equals(find_even_index([1,2,3,4,3,2,1]),3)
Test.assert_equals(find_even_index([1,100,50,-51,1,1]),1,)
Test.assert_equals(find_even_index([1,2,3,4,5,6]),-1)
Test.assert_equals(find_even_index([20,10,30,10,10,15,35]),3)
Test.assert_equals(find_even_index([20,10,-80,10,10,15,35]),0)
Test.assert_equals(find_even_index([10,-80,10,10,15,35,20]),6)
Test.assert_equals(find_even_index(range(1,100)),-1)
Test.assert_equals(find_even_index([0,0,0,0,0]),0,"Should pick the first index if more cases are valid")
Test.assert_equals(find_even_index([-1,-2,-3,-4,-3,-2,-1]),3)
Test.assert_equals(find_even_index(range(-100,-1)),-1)
def find_even_index(arr):
arr=list(arr)
for i in range(len(arr)):
if i==0 and sum(arr[1:])==0:
return 0
elif i==len(arr)and sum(arr[:-1])==0:
return len(arr)
elif sum(arr[:i])==sum(arr[i+1:]):
return i
return -1
def find_even_index(arr):
for i in range(len(arr)):
if sum(arr[:i]) == sum(arr[i+1:]):
return i
return -1
判断是否为素数:
负数,0,1,都不是素数
def is_prime(num):
if num < 2:
return False
for i in range(2, num):
if num % i == 0:
return False
return True
罗马数字的转换
Create a function taking a positive integer as its parameter and returning a string containing the Roman Numeral representation of that integer.
Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in descending order: MDCLXVI.
Example:
solution(1000) # should return 'M'
Help:
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1,000
test.assert_equals(solution(1),'I', "solution(1),'I'")
test.assert_equals(solution(4),'IV', "solution(4),'IV'")
test.assert_equals(solution(6),'VI', "solution(6),'VI'")
test.assert_equals(solution(14),'XIV', "solution(14),'XIV")
test.assert_equals(solution(21),'XXI', "solution(21),'XXI'")
test.assert_equals(solution(89),'LXXXIX', "solution(89),'LXXXIX'")
test.assert_equals(solution(91),'XCI', "solution(91),'XCI'")
test.assert_equals(solution(984),'CMLXXXIV', "solution(984),'CMLXXXIV'")
test.assert_equals(solution(1000), 'M', 'solution(1000), M')
test.assert_equals(solution(1889),'MDCCCLXXXIX', "solution(1889),'MDCCCLXXXIX'")
def solution(n):
rm=''
while(n>=1):
if n>=1000:
n=n-1000
rm+='M'
elif n>=900:
n=n-900
rm+='CM'
elif n>=500:
n=n-500
rm+='D'
elif n>=400:
n=n-400
rm+='CD'
elif n>=100:
n=n-100
rm+='C'
elif n>=90:
n=n-90
rm+='XC'
elif n>=50:
n=n-50
rm+='L'
elif n>=40:
n=n-40
rm+='XL'
elif n>=10:
n=n-10
rm+='X'
elif n>=9:
n=n-9
rm+='IX'
elif n>=5:
n=n-5
rm+='V'
elif n>=4:
n=n-4
rm+='IV'
elif n>=1:
n=n-1
rm+='I'
return rm
def solution(n):
roman_numerals = {1000:'M',
900: 'CM',
500: 'D',
400: 'CD',
100: 'C',
90: 'XC',
50: 'L',
40: 'XL',
10: 'X',
9: 'IX',
5: 'V',
4: 'IV',
1: 'I'
}
roman_string = ''
for key in sorted(roman_numerals.keys(),reverse=True):
while n >= key:
roman_string += roman_numerals[key]
n -= key
return roman_string
a=[1,2,3,4,5,6,7,8,9]
sorted(a,reverse=True)
[9, 8, 7, 6, 5, 4, 3, 2, 1]
判断左右括号()()())()())()((()是否左右匹配
Test.assert_equals(valid_parentheses(" ("),False)
Test.assert_equals(valid_parentheses(")test"),False)
Test.assert_equals(valid_parentheses(""),True)
Test.assert_equals(valid_parentheses("hi())("),False)
Test.assert_equals(valid_parentheses("hi(hi)()"),True)
def valid_parentheses(string):
count=0
for i in range(len(string)):
if string [i]=="(":
count+=1
elif string [i]==")":
count-=1
if count<0:
return False
return True if count==0 else False
def valid_parentheses(string):
count = 0
for i in string:
if i == "(":
count += 1
elif i == ")":
count -= 1
if count < 0:
return False
return count == 0
删除重复数字:
Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.
For example:
unique_in_order('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B']
unique_in_order('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D']
unique_in_order([1,2,2,3,3]) == [1,2,3]
def unique_in_order(iterable):
unique_list=[]
for i in range(len(iterable)):
if i==0:
unique_list.append(iterable[0])
elif unique_list[-1]!=iterable[i]:
unique_list.append(iterable[i])
return unique_list
def unique_in_order(iterable):
res = []
for item in iterable:
if len(res) == 0 or item != res[-1]:
res.append(item)
return res