如何使用数组创建菱形函数

时间:2019-07-02 15:39:41

标签: python arrays function

我需要创建一个函数,当给定边长时,该函数将生成菱形。菱形应包含0和1的数组

到目前为止,我已经弄清楚了如何制作菱形,但我不知道如何为不同的边长编程功能

到目前为止,我有: 边长为3

public class MessageBuilder {

    private final EventPayload messagePayload;

    public MessageBuilder(){
        messagePayload = new EventPayload();
    }

    public EventPayload build(Activity event) {


        createBaseActivity(event);

        if (event instanceof Member) {
            updateToMemberActivity(event);
            return messagePayload;
        }

        if (event instanceof MeetingActivity) {
            updateToMeetingActivity(event);
            return messagePayload;
        }

        if (event instanceof ServiceActivity) {
            updateToServiceActivity(event);
            return messagePayload;
        }

        if (event instanceof LetterActivity) {
            updateToLetterActivity(event);
            return messagePayload;
        }

        if (event instanceof ReceiptActivity) {
            updateToReceiptActivity(event);
            return messagePayload;
        }

        return messagePayload;

    }


    private EventPayload createBaseActivity(Activity event) {

        messagePayload.setId(event.getId());
        messagePayload.setMemberId(event.getMemberId());
        messagePayload.setActivityDateTime(event.getActivityDateTime());

        messagePayload.setMemberIdIsPrimaryId(event.isMemberIdIsPrimaryId());
        messagePayload.setBuCode(event.getBuCode());
        messagePayload.setStateCode(event.getStateCode());
        messagePayload.setProductCode(event.getProductCode());
        messagePayload.setBusinessUnit(event.getBusinessUnit());

        messagePayload.setActivityType(event.getActivityType());
        messagePayload.setActivityIntent(event.getActivityIntent());
        messagePayload.setActivityAction(event.getActivityAction());
        messagePayload.setActivityStatus(event.getActivityStatus());
        messagePayload.setSourceSystem(event.getSourceSystem());
        messagePayload.setCaseId(event.getCaseId());
        messagePayload.setInitiatedBy(event.getInitiatedBy());
        messagePayload.setActivityDetailsPointer(event.getActivityDetailsPointer());
        messagePayload.setBusinessLine(event.getBusinessLine());

        messagePayload.setEventInstanceType(EventInstanceType.ACTIVITY.name());

        return messagePayload;
    }

    private EventPayload updateToMemberActivity(Activity event) {

        Member subTypedEvent = (Member) event;

        messagePayload.setDeliveryType(subTypedEvent.getDeliveryType());
        messagePayload.setDocumentType(subTypedEvent.getDocumentType());
        messagePayload.setEventInstanceType(EventInstanceType.MEMBER.name());

        return messagePayload;
    }

    private EventPayload updateToMeetingActivity(Activity event) {

        MeetingActivity subTypedEvent = (MeetingActivity) event;

        messagePayload.setDeliveryType(subTypedEvent.getDeliveryType());
        messagePayload.setDocumentType(subTypedEvent.getDocumentType());
        messagePayload.setEventInstanceType(EventInstanceType.MEETING.name());

        return messagePayload;
    }


    private EventPayload updateToServiceActivity(Activity event) {

        ServiceActivity subTypedEvent = (ServiceActivity) event;

        messagePayload.setReceivedBy(subTypedEvent.getReceivedBy());
        messagePayload.setConcernedParty(subTypedEvent.getConcernedParty());
        messagePayload.setSummary(subTypedEvent.getSummary());
        messagePayload.setEventInstanceType(EventInstanceType.SERVICE.name());

        return messagePayload;
    }


    private EventPayload updateToLetterActivity(Activity event) {

        LetterActivity subTypedEvent = (LetterActivity) event;

        messagePayload.setDeliveryType(subTypedEvent.getDeliveryType());
        messagePayload.setDocumentType(subTypedEvent.getDocumentType());
        messagePayload.setPaymentType(subTypedEvent.getPaymentType());
        messagePayload.setEventInstanceType(EventInstanceType.LETTER.name());

        return messagePayload;
    }

    private EventPayload updateToReceiptActivity(Activity event) {

        ReceiptActivity subTypedEvent = (ReceiptActivity) event;

        messagePayload.setPaymentType(subTypedEvent.getPaymentType());
        messagePayload.setEventInstanceType(EventInstanceType.RECEIPT.name());

        return messagePayload;
    }

}

如何编写不同长度的函数 同样,如果给定的长度为1,它应该返回import numpy as np #line1 a=np.zeros(3+2) a[3-1]=1 #line2 b=np.zeros(3+2) b[3-2]=1 b[3]=1 #line3 c=np.zeros(3+2) c[3-3]=1 c[3+1]=1 print(np.concatenate((a,b,c,b,a),axis=1).reshape(5,5))

任何反馈将不胜感激

更新:我认为循环可能会找出行数

2 个答案:

答案 0 :(得分:1)

您可以使用水平和垂直图案的交集来做到这一点:

import numpy as np

N       = 5
H       = abs(np.arange(1-N,N+1,2))//2
V       = (N-1)//2-H[:,None]
diamond = (H==V)*1

print(diamond)

[[0 0 1 0 0]
 [0 1 0 1 0]
 [1 0 0 0 1]
 [0 1 0 1 0]
 [0 0 1 0 0]]

在视觉上,这对应于行和列之间相交的数字相等:

对于N = 7:

     [3, 2, 1, 0, 1, 2, 3]
   0  .  .  .  x  .  .  .         
   1  .  .  x  .  x  .  .
   2  .  x  .  .  .  x  .     
   3  x  .  .  .  .  .  x
   2  .  x  .  .  .  x  .
   1  .  .  x  .  x  .  .
   0  .  .  .  x  .  .  .

对于N = 8:

     [3, 2, 1, 0, 0, 1, 2, 3]
   0  .  .  .  x  x  .  .  .         
   1  .  .  x  .  .  x  .  .         
   2  .  x  .  .  .  .  x  .         
   3  x  .  .  .  .  .  .  x         
   3  x  .  .  .  .  .  .  x         
   2  .  x  .  .  .  .  x  .         
   1  .  .  x  .  .  x  .  .         
   0  .  .  .  x  x  .  .  .         

如果要填充钻石,请使用diamond = (H<=V)*1

答案 1 :(得分:0)

我花了更长的时间,所以我可以扩展功能以处理其他几何形状

import numpy as np

def diamondarray(dimension=1):

    #// initialize 2d array
    a=np.zeros((dimension,dimension))

    #// find the middle of the array
    midpoint=(dimension-1)/2

    #// initialize an offset
    offset=-1
    offsetstep=1

    #// loop through rows and columns
    for row in range(dimension):
        if dimension%2 == 0 and row == np.ceil(midpoint):
            #// repeat offset for second midpoint row
            offset=offset
        else:
            if row <= np.ceil(midpoint):
                #// increase offset for each row for top
                offset=offset+offsetstep
            else:
                #// decrease offset for each row for bottom
                offset=offset-offsetstep

        for col in range(dimension):
            #// set value to one
            if dimension%2 == 0:
                if col <= np.floor(midpoint):
                    if col == np.floor(midpoint)-offset:
                        a[row,col]=fill
                if col >= np.ceil(midpoint):
                    if col == int(midpoint)+offset+1:
                        a[row,col]=fill
            else:
                if col == midpoint+offset or col == midpoint-offset:
                    pass
                    a[row,col]=fill
    return a

对于N = 5:

print(diamondarray(5))

[[0. 0. 1. 0. 0.]
 [0. 1. 0. 1. 0.]
 [1. 0. 0. 0. 1.]
 [0. 1. 0. 1. 0.]
 [0. 0. 1. 0. 0.]]

对于N = 8:

print(diamondarray(8))

[[0. 0. 0. 1. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0. 1. 0. 0.]
 [0. 1. 0. 0. 0. 0. 1. 0.]
 [1. 0. 0. 0. 0. 0. 0. 1.]
 [1. 0. 0. 0. 0. 0. 0. 1.]
 [0. 1. 0. 0. 0. 0. 1. 0.]
 [0. 0. 1. 0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 1. 0. 0. 0.]]