Aggregate array values into ranges

https://codereview.stackexchange.com/questions/80080/aggregate-array-values-into-ranges // Output: // Array ( // [0] => Array ( [0] => 1, [1] => 2, [2] => 3, [3] => 4, [4] => 5, [5] => 6 ) // [1] => Array ( [0] => 10, [1] => 11, [2] => 12, [3] => 13 ) // [2] => Array ( [0] => 20 ) // [3] => Array ( [0] => 24 ) // ) static function GetRanges1( $aNumbers ) { $aNumbers = array_unique( $aNumbers ); sort( $aNumbers ); $aGroups = array(); for( $i = 0; $i < count( $aNumbers ); $i++ ) { if( $i > 0 && ( $aNumbers[$i-1] == $aNumbers[$i] - 1 )) array_push( $aGroups[count($aGroups)-1], $aNumbers[$i] ); else array_push( $aGroups, array( $aNumbers[$i] )); } return $aGroups; } // Output: Array ( [0] => 1-6, [1] => 10-13, [2] => 20, [3] => 24 ) static function GetRanges2( $aNumbers ) { $aNumbers = array_unique( $aNumbers ); sort( $aNumbers ); $aGroups = array(); for( $i = 0; $i < count( $aNumbers ); $i++ ) { if( $i > 0 && ( $aNumbers[$i-1] == $aNumbers[$i] - 1 )) array_push( $aGroups[count($aGroups)-1], $aNumbers[$i] ); else array_push( $aGroups, array( $aNumbers[$i] )); } $aRanges = array(); foreach( $aGroups as $aGroup ) { if( count( $aGroup ) == 1 ) $aRanges[] = $aGroup[0]; else $aRanges[] = $aGroup[0] . '-' . $aGroup[count($aGroup)-1]; } return $aRanges; } // Output: 1~6,10~13,20,24 static function GetRanges3( $aNumbers, $headChar='' ) { $aNumbers = array_unique( $aNumbers ); sort( $aNumbers ); $aGroups = array(); for( $i = 0; $i < count( $aNumbers ); $i++ ) { if( $i > 0 && ( $aNumbers[$i-1] == $aNumbers[$i] - 1 )) array_push( $aGroups[count($aGroups)-1], $aNumbers[$i] ); else array_push( $aGroups, array( $aNumbers[$i] )); } $aRanges = array(); foreach( $aGroups as $aGroup ) { if( count( $aGroup ) == 1 ) $aRanges[] = $headChar.$aGroup[0]; else $aRanges[] = $headChar.$aGroup[0] . '~' . $headChar.$aGroup[count($aGroup)-1]; } return implode( ',', $aRanges ); }

2022-09-23 · 2 min · 313 words · Me

[轉]疫情

2022-05-20 · 0 min · 0 words · Me

gorm manytomany many2many use go program

type RoleHasPermissionList struct { model.Roles Rolehaspermissions []CUS_Rolehaspermissions `json:"rolehaspermissions" gorm:"foreignkey:role_id; references:id"` } type CUS_Rolehaspermissions struct { PermissionId int `json:"-"` RoleId int `json:"-"` PermissinInfo model.Permission `gorm:"foreignkey:id; references:permission_id"` } func (CUS_Rolehaspermissions) TableName() string { return "RoleHasPermissions" } type FormaterResult struct { model.Roles Permission []model.Permission `json:"permission"` } func GetRoleList(c *gin.Context) serializer.Response { var req GetRoleListRequest c.ShouldBind(&req) var total int64 var list []RoleHasPermissionList page := req.Page pageSize := req.PageSize model.DB. Scopes(util.Paginate(c, page, pageSize)). Preload("Rolehaspermissions.PermissinInfo"). Find(&list).Offset(-1).Limit(-1).Count(&total) // 重新整理輸出格式 var formater_result []FormaterResult for i, v := range list { formater_result = append(formater_result, FormaterResult{Roles: v.Roles}) for _, v2 := range v.Rolehaspermissions { formater_result[i].Permission = append(formater_result[i].Permission, v2.PermissinInfo) } } return serializer.Response{ Code: 0, Message: "成功", Data: util.Pagination(c, page, pageSize, total, formater_result), } } { "id": 1, "name": "company", "display_name": "公司", "guard_name": "admin_api", "created_at": "2020-03-26 14:34:25", "updated_at": "2020-03-26 14:34:25", "permission": [ { "id": 1, "name": "add company", "display_name": "add company", "guard_name": "admin_api", "created_at": "2020-03-26 14:34:25", "updated_at": "2020-03-26 14:34:25" }, { "id": 2, "name": "edit company", "display_name": "edit company", "guard_name": "admin_api", "created_at": "2020-03-26 14:34:25", "updated_at": "2020-03-26 14:34:25" }, ] }, ...

2022-05-19 · 1 min · 170 words · Me

bytes32[] remix solidity

// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; contract Fix { bytes32[] questionList= [ bytes32(0x52a6eb687cd22e80d3342eac6fcc7f2e19209e8f83eb9b82e81c6f3e6f30743b), bytes32(0x257f3de9149acf0a49d3f7668956aeb52490202fae9ec2e92c3caa7d5223c6ef) ]; // constructor() { // // questionList = [ // // bytes32(0x52a6eb687cd22e80d3342eac6fcc7f2e19209e8f83eb9b82e81c6f3e6f30743b), // // bytes32(0x257f3de9149acf0a49d3f7668956aeb52490202fae9ec2e92c3caa7d5223c6ef) // // ]; // } // function addQuestion(bytes32 questionKey) // public // returns(bool success) // { // questionList.push(questionKey); // return true; // } function getQuestionCount() public view returns(uint questionCount) { return questionList.length; } function getQuestionAtIndex(uint row) public view returns(bytes32 questionkey) { return questionList[row]; } }

2022-05-11 · 1 min · 75 words · Me

[轉]After 5 years, I am out of the serverless compute cult

https://dev.to/brentmitchell/after-5-years-im-out-of-the-serverless-compute-cult-3f6d I have been using serverless computing and storage for nearly five years and I’m finally tired of it. I do feel like it has become a cult. In a cult, brainwashing is done so gradually, people have no idea it is going on. I feel like this has happened across the board with so many developers; many don’t even realize they are clouded. In my case, I took the serverless marketing and hype hook, line, and sinker for the first half of my serverless journey. After working with several companies small and large, I have been continually disappointed as our projects grew. The fact is, serverless technology is amazingly simple to start, but becomes a bear as projects and teams accelerate. A serverless project typically includes a fully serverless stack which can include (using a non-exhaustive list of AWS services): ...

2022-04-29 · 9 min · 1784 words · Me