Shell 和 Python 分别 实现工作组号匹配不重复考场号

Approach: 考场号如果是生成区间为10到19的随机数,先生成0~9的随机数,再加上10(起始值)来修正,将随机数存在数组中,然后在与工作组号依次进行拼接。

Shell Script Code name as match_G_C_ID.sh

#!/bin/bash

Match_GroupID_ClassroomID()
{

	ExamClassRoom_Start_Num=$1
	ExamClassRoom_End_Num=$2
	Officer_Start_GroupID=$3


   echo "
	ExamClassRoom_Start_Num is ${ExamClassRoom_Start_Num}
	ExamClassRoom_End_Num is ${ExamClassRoom_End_Num}
	Officer_Start_GroupID is ${Officer_Start_GroupID}
   "

	#Set Array_Length as RANDOM的被余数, it will used to contol to get the RANDOM number

	Array_Length=$((${ExamClassRoom_End_Num}-${ExamClassRoom_Start_Num}+1))

	echo "Array_Length is ${Array_Length}"

	# Define Array,it will used to store the finiall random results

	typeset RAND  

	# Generate random number for $Array_Length times

        i=0
	for ((${i};${i}<${Array_Length};i++))    
	do
		echo "___________________________"
		echo "Debug_i is ${i}....."
        Rnum=$(($RANDOM%${Array_Length}+${ExamClassRoom_Start_Num}))
        
        echo "Out IF : Rnum is $Rnum "

        #Get the array length

        Length=${#RAND[@]}  

        if [ ${i} -gt 1 ];then
           echo "Out of IF Length of Array is ${Length} , it should be $((${i}-1))...."
        fi

       # Length of Empty array and 1-element-array is the same value which equal to 1
         if [ ${i} -eq 0 ];then
                RAND[${i}]=${Rnum}
        else
                echo "Array RAND the 0th em is ${RAND[0]}"
                echo "Array RAND the 1st em is ${RAND[1]}" 
                echo "Array RAND the 2nd em is ${RAND[2]}" 
                echo "Array RAND the 3rd em is ${RAND[3]}" 

                j=0
                for ((${j};${j}<$((${Length}));j++))
                 #Compare the existing random number in array with the newly generated one
                do  
                        echo "Loop Current j is ${j}...."
                        echo "Array RAND the ${j} em is ${RAND[${j}]} ..."
                        echo "Rnum is ${Rnum} and j is the ${j}th em is ${RAND[${j}]} .."
                        echo "The Length is ${Length} which is the last value of j+1 ..."

                        if [ ${Rnum} != ${RAND[${j}]} ];then 
                        	# If the newly random number is not repeat
                                continue
                        else
                        	#If the newly random number already existing at array,then generate a newly random number
                                echo " "
                                echo "HIT SAME VALUE...."
                                echo "The ${i}th loop ,Rnum is ${Rnum} is the same with the value of the ${j}th in Array  .."
                                echo "We need to generate a newly one instead "
                                Rnum=$[RANDOM%${Array_Length}+${ExamClassRoom_Start_Num}]
                                echo "The newly Random value is ${Rnum} which replaced the previous one which is the same value of the ${j}th in the array..."  
                                j=-1
                        fi  
                done

                # Store the unique random results to array
                RAND[${i}]=${Rnum}    
                echo "Array has ${RAND[${i}]} at the ${i} location ."
        fi  
	done

	#Output the final results
        x=0
	y=${Officer_Start_GroupID}
	for ((${x};${x}<${Array_Length};x++))
	do
        echo "Group No.${y} will go to exam classroom ${RAND[${x}]}"
        y=$((${y}+1))

	done
}

#Set the Exam ClassRoom ID :

ClassRoomStartID=$1
ClassRoomEndID=$2

# Set the GroupStart number:

GroupStartID=$3

echo "Officer Group ID starts from ${GroupStartID}"
echo "Exam Classroom ID start from ${ClassRoomStartID} to ${ClassRoomEndID}"

Match_GroupID_ClassroomID ${ClassRoomStartID} ${ClassRoomEndID} ${GroupStartID}

Run bash match_G_C_ID.sh 9 20 5, it means Random number range is [9-20], and Officer ID starts from 5.

Remove the comments from the code, we will get :

Group No.10 will go to exam classroom 20
Group No.11 will go to exam classroom 12
Group No.12 will go to exam classroom 14
Group No.13 will go to exam classroom 11
Group No.14 will go to exam classroom 17
Group No.15 will go to exam classroom 19
Group No.16 will go to exam classroom 18
Group No.17 will go to exam classroom 13
Group No.18 will go to exam classroom 15
Group No.19 will go to exam classroom 10
Group No.20 will go to exam classroom 16
Group No.21 will go to exam classroom 9

 

Python 版:

# 这是一个示例 Python 脚本。

import sys
import random


def Match_GroupID_with_Classroom(Exam_Classroom_StartID,Exam_Classroom_EndID,Officer_GroupID_Start):
    print('Define Array length.')
    print('Exam_Classroom_StartID is :', Exam_Classroom_StartID)
    print('Exam_Classroom_EndID is :', Exam_Classroom_EndID)
    print('Officer_GroupID_Start is :', Officer_GroupID_Start)

    Array_Length= int(Exam_Classroom_EndID) - int(Exam_Classroom_StartID) + 1
    print('Array_length is :' , Array_Length)

    RAND=[]
    print(type(Exam_Classroom_StartID))
    print(type(Exam_Classroom_EndID))
    print(type(Array_Length))

    Exam_Classroom_EndID = int(Exam_Classroom_EndID) + 1
    print('Exam_Classroom_EndID is ' ,  Exam_Classroom_EndID)
    RAND = random.sample(range(int(Exam_Classroom_StartID) , int(Exam_Classroom_EndID)), Array_Length)

    print(RAND)
    m=int(Officer_GroupID_Start)

    for i in RAND:
        print('Officer_GroupID ', m, ' will go to Exam Classrom ', i)
        m = m + 1


if __name__ == '__main__':
    print('Pls type the 1st number of Exam Classroom ID: \n')
    Exam_Classroom_StartID = input()
    print('Pls type the last number of Exam Classroom ID: \n')
    Exam_Classroom_EndID = input()
    print('Pls type the 1st Officer Group number : \n')
    Officer_GroupID = input()

    Match_GroupID_with_Classroom(Exam_Classroom_StartID, Exam_Classroom_EndID, Officer_GroupID)
    

输出:

Pls type the 1st number of Exam Classroom ID: 

5
Pls type the last number of Exam Classroom ID: 

10
Pls type the 1st Officer Group number : 

6

Define Array length.
Exam_Classroom_StartID is : 5
Exam_Classroom_EndID is : 10
Officer_GroupID_Start is : 6
Array_length is : 6
<class 'str'>
<class 'str'>
<class 'int'>
Exam_Classroom_EndID is  11
[7, 10, 6, 8, 9, 5]
Officer_GroupID  6  will go to Exam Classrom  7
Officer_GroupID  7  will go to Exam Classrom  10
Officer_GroupID  8  will go to Exam Classrom  6
Officer_GroupID  9  will go to Exam Classrom  8
Officer_GroupID  10  will go to Exam Classrom  9
Officer_GroupID  11  will go to Exam Classrom  5

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值