[CODIlity]LESSON1&LESSON2

 Lesson1 

binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.

For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.

Write a function:

class Solution { public int solution(int N); }

that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.

For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.

Write an efficient algorithm for the following assumptions:

  • N is an integer within the range [1..2,147,483,647].

Copyright 2009–2022 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

 Answer:

def solution(N):
    # write your code in Python 3.6
    or2=0
    while N>=pow(2,or2):
        or2=or2+1
    or2=or2-1
    #or2 is the maximal order of 2
    if or2==0:
        return 0
    i=or2
    n=N
    result=0
    result_temp=0
    while n>0:
        while i>=0 and n>=pow(2,i):
            n=n-pow(2,i)
            result_temp = 0
            i=i-1
            #print("n=",n,"while i=",i)
        while n<pow(2,i) and i>=0:
            result_temp = result_temp + 1
            i=i-1
            #print("result_temp=",result_temp,"while i=",i)
        if n>=pow(2,i):
            if result<result_temp:
                result = result_temp
                #print("result=",result,"while i=",i)
    return result

Lesson2:

Task1: 

An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).

The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.

Write a function:

def solution(A, K)

that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.

For example, given

A = [3, 8, 9, 7, 6] K = 3

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

[3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7] [6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9] [7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]

For another example, given

A = [0, 0, 0] K = 1

the function should return [0, 0, 0]

Given

A = [1, 2, 3, 4] K = 4

the function should return [1, 2, 3, 4]

Assume that:

  • N and K are integers within the range [0..100];
  • each element of array A is an integer within the range [−1,000..1,000].

In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

Answer:

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A, K):
    # write your code in Python 3.6
    if len(A)==0:
        return A
    kk = K % len(A) 
    # A[0] --> solu[kk]
    if kk==0:
        return A
    i=len(A)-kk
    B=[A[i]]
    i=i+1
    while i<len(A):
        B.append(A[i])
        i=i+1
    i=0
    while i<len(A)-kk:
        B.append(A[i])
        i=i+1
    return B

Task2:

A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.

For example, in array A such that:

A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9
  • the elements at indexes 0 and 2 have value 9,
  • the elements at indexes 1 and 3 have value 3,
  • the elements at indexes 4 and 6 have value 9,
  • the element at index 5 has value 7 and is unpaired.

Write a function:

def solution(A)

that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element.

For example, given array A such that:

A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9

the function should return 7, as explained in the example above.

Write an efficient algorithm for the following assumptions:

  • N is an odd integer within the range [1..1,000,000];
  • each element of array A is an integer within the range [1..1,000,000,000];
  • all but one of the 

Answer:

在大数据集上表现不够好,综合得分66%

def solution(A):
    # write your code in Python 3.6
    if len(A)==0:
        print("The array should be nonempty!")
        return 0
    if len(A)%2==0:
        print("The lenth of the array should be odd!")
        return 0
    B=[A[0]]
    N=len(A)
    for i in range(1,len(A)):
        #当B里没有与A[i]匹配的元素时,将A[i]加入B
        #print("now dealing with A[i]=",A[i])
        tt=0
        for j in range(len(B)):
            if B[j]==A[i]:
                tt=1
                break
        if tt==0:
            B.append(A[i])
        else:
            del B[j]
        #print("now B=",B)
    if len(B)!=1:
        print("Error exits!")
    return B[0]

 后来学了新招:直接用异或……

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    # write your code in Python 3.6
    a=0
    for i in range(len(A)):
        a=a^A[i]
    return a

成功了……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值