AABB,ABAB型数据统计及生成字母的组合; 取数字个位数做幂运算

In this kata you have to create all permutations of an input string and remove duplicates, if present. This means, you have to shuffle all letters from the input in all possible orders.

Examples:

permutations('a'); # ['a']
permutations('ab'); # ['ab', 'ba']
permutations('aabb'); # ['aabb', 'abab', 'abba', 'baab', 'baba', 'bbaa']

 

import itertools
def permutations(string):
    result=[]
    for i in set(itertools.permutations(string)):
        result.append(''.join(i))
    
    return result

 1.自身的笛卡尔积,类似于组合,会有重复

import itertools
for i in itertools.product('ABCD', repeat = 2):
    print (i)
('A', 'A')
('A', 'B')
('A', 'C')
('A', 'D')
('B', 'A')
('B', 'B')
('B', 'C')
('B', 'D')
('C', 'A')
('C', 'B')
('C', 'C')
('C', 'D')
('D', 'A')
('D', 'B')
('D', 'C')
('D', 'D')
seq1 = ['hello','good','boy','doiido']
print( ' '.join(seq1))
hello good boy doiido
import itertools
for i in itertools.product('ABCD', repeat = 2):
    print (''.join(i),end=' ')

AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD 

其中‘ABCD’为输入的字符串,itertools.product(str,repeat) 字符串str中的字母将作为重新排序的字母,repeat参数将作为可选择其中的每个字母的次数 ,即使字符串中的字母是重复的,也将重复排序出来

 例如AACD:

AAA AAA AAC AAD AAA AAA AAC AAD ACA ACA ACC ACD ADA ADA ADC ADD AAA AAA AAC AAD AAA AAA AAC AAD ACA ACA ACC ACD ADA ADA ADC ADD CAA CAA CAC CAD CAA CAA CAC CAD CCA CCA CCC CCD CDA CDA CDC CDD DAA DAA DAC DAD DAA DAA DAC DAD DCA DCA DCC DCD DDA DDA DDC DDD 

2.两个元祖的笛卡尔积

import itertools
a = (1, 2, 3)
b = ('A', 'B', 'C')
c = itertools.product(a,b)
for i in c:
    print(i,end=' ')

(1, 'A') (1, 'B') (1, 'C') (2, 'A') (2, 'B') (2, 'C') (3, 'A') (3, 'B') (3, 'C') 

3、排列:itertools.permutations(iterable[, r]),以字母顺序排列

import itertools
for i in itertools.permutations('ABCD', 2):
    print (''.join(i),end=' ')
AB AC AD BA BC BD CA CB CD DA DB DC 
AABCD :AA AB AC AD AA AB AC AD BA BA BC BD CA CA CB CD DA DA DB DC 会有重复
import itertools
for i in itertools.permutations('ABCD',3):
    print (''.join(i),end=' ')
ABC ABD ACB ACD ADB ADC BAC BAD BCA BCD BDA BDC CAB CAD CBA CBD CDA CDB DAB DAC DBA DBC DCA DCB 

4.组合,包含自身重复

import itertools
for i in itertools.combinations_with_replacement('ABCD', 3):
    print (''.join(i),end=' ')
AAA AAB AAC AAD ABB ABC ABD ACC ACD ADD BBB BBC BBD BCC BCD BDD CCC CCD CDD DDD 

函数是根据他们的位置来计算组合的,而不是他们的值.所以有重复的结果。

 

Define a function that takes in two numbers a and b and returns the last decimal digit of a^b. Note that a and b may be very large!

For example, the last decimal digit of 9^7 is 9, since 9^7 = 4782969. The last decimal digit of (2^200)^(2^300), which has over 10^92 decimal digits, is 6.

The inputs to your function will always be non-negative integers.

Examples

last_digit(4, 1)                # returns 4
last_digit(4, 2)                # returns 6
last_digit(9, 7)                # returns 9
last_digit(10, 10 ** 10)        # returns 0
last_digit(2 ** 200, 2 ** 300)  # returns 6
rules = {
    0: [0,0,0,0],   
    1: [1,1,1,1],
    2: [2,4,8,6],
    3: [3,9,7,1],
    4: [4,6,4,6], 
    5: [5,5,5,5], 
    6: [6,6,6,6], 
    7: [7,9,3,1], 
    8: [8,4,2,6], 
    9: [9,1,9,1],
}
def last_digit(n1, n2):
    ruler = rules[int(str(n1)[-1])]
    return 1 if n2 == 0 else ruler[(n2 % 4) - 1]
    

或者:
def last_digit(n1, n2):
    return digits[n1%10][(n2-1)%4] if n2 else 1

n2%4-1对n2>0且小于等于4的数容易理解,但如果n2=8,n2%4=0,那么

Test.it("Example tests")
Test.assert_equals(last_digit(2, 5),2)
Test.assert_equals(last_digit(4, 2), 6)
Test.assert_equals(last_digit(9, 7), 9)
Test.assert_equals(last_digit(10, 10 ** 10), 0)
Test.assert_equals(last_digit(2 ** 200, 2 ** 300), 6)
Test.assert_equals(last_digit(3715290469715693021198967285016729344580685479654510946723, 68819615221552997273737174557165657483427362207517952651), 7)

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值