Python: 2d arry

®Geovin Du Dream Park™ / 2024-06-10 / 原文

 

def find_diagonal_word(letter_array, word):
    """
    查找字列表中的字符串
    :param letter_array: 字符串列表
    :param word: 所需要查找字符
    :return:
    """
    rows = len(letter_array)
    cols = len(letter_array[0]) if rows > 0 else 0

    # Iterate over all possible starting positions for the diagonal
    for i in range(rows):
        for j in range(cols):
            # Check if the current position is within the bounds of the array
            # and if the word can fit in the diagonal
            if i + len(word) <= rows and j + len(word) <= cols:
                # Check if the word matches the diagonal
                match = True
                for k in range(len(word)):
                    if letter_array[i + k][j + k] != word[k]:
                        match = False
                        break
                if match:
                    return True

    return False


def find_column_word(letter_array, word):
    """

    :param letter_array:
    :param word:
    :return:
    """
    cols = len(letter_array[0]) if letter_array else 0

    # Iterate over all columns
    for j in range(cols):
        # Check if the word matches the current column
        match = True
        for i in range(len(letter_array)):
            # Check if the current row is within the bounds of the array
            # and if the character matches the word
            if i < len(letter_array) and letter_array[i][j] != word[i]:
                match = False
                break

                # If the word matches the column, return True
        if match and i == len(word) - 1:
            return True

    return False

def suc(score):
    s6grade=''
    if score >= 80:
        s6grade = 'A'
    if score >= 65 and score <= 79:
        s6grade = 'B'
    if score >= 50 and score < 65:
        s6grade = 'C'
    if score < 50:
        s6grade = 'F'
    return s6grade

 

调用:

  

if __name__ == '__main__':

    letter_array = [
        "JGJGDDAOYD",
        "IDGFHSPOSA",
        "FGDIOSAFSC",
        "INTERNETSO",
        "FJKCOSAFSM",
        "DJSGAPAHDP",
        "HAUSTRFBFU",
        "KDGFUCNSKT",
        "WSJDYCFXDE",
        "ODVFKXJVCR"
    ]

    word = "coMPUTER"
    word = input("please word:")
    # Convert the word to uppercase since the letter array seems to be in uppercase
    word = word.upper()

    if find_column_word(letter_array, word):
        print(f"The word '{word}' exists in the letter array as a column.")
    else:
        print(f"The word '{word}' does not exist in the letter array as a column.")



    letter_array = [
        "JGJGDDAOYD",
        "IDGFHSPOSA",
        "FGDIOSAFSC",
        "INT ERNETSO",
        "FJKCOSAFSM",
        "DJSGAPAHDP9",
        "HAUSTRFBFU",
        "KDGFUCNSKT",
        "WSJDYCFXDE",
        "ODVFKXJVCR"
    ]

    word = "CAR"
    word=input("please word:")

    if find_diagonal_word(letter_array, word):
        print(f"The word '{word}' exists in the letter array as a diagonal.")
    else:
        print(f"The word '{word}' does not exist in the letter array as a diagonal.")

  

 

 

 

 

    score=[
        [58,80,74,90,45,82],
        [71,70,64,85,50,86],
        [87,63,65,84,62,83],
        [91,66,67,92,65,90],
        [83,74,81,82,57,82]]
    k=0
    while k<5:
        subavg=0
        a=0
        while a<6:
            subavg=subavg+score[k][a]
            a=a+1
        subavg =subavg/6
        print(subavg)
        k=k+1
    print("***************8")
    rows, cols = (5, 6)
    s6grade=[['0'] * cols] * rows  # 赋值为零 #[[]] #
    ''''''
    for i in range(0,5):
        for j in range(0,6):
            s6grade[i][j]=suc(score[i][j])
            print(s6grade[i][j], end=" ")
        print()
    print('**********')
    for i in range(0,5):
        col=[]
        for j in range(0,6):
            col.append(suc(score[i][j]))
            #s6grade[i][j] =suc(score[i][j])  # 出问题
        s6grade.insert(i,col)
    #print(s6grade)
    #print(suc(score[0][4])) # 0,4 45

  

 

 

    score=[
        [58,80,74,90,45,82],
        [71,70,64,85,50,86],
        [87,63,65,84,62,83],
        [91,66,67,92,65,90],
        [83,74,81,82,57,82]]
    k=0
    while k<5:
        subavg=0
        a=0
        while a<6:
            subavg=subavg+score[k][a]
            a=a+1
        subavg =subavg/6
        print(subavg)
        k=k+1
    print("***************8")
    rows, cols = (5, 6)
    s6grade=[[]] #[['0'] * cols] * rows  # 赋值为零
    '''
    for i in range(0,5):
        for j in range(0,6):
            s6grade[i][j]='1'
            print(s6grade[i][j], end=" ")
        print()
    '''
    for ii in range(0,5):
        col=[]
        for ji in range(0,6):
            d=suc(score[ii][ji])
            col.append(d)
            #print(d)
            #s6grade[ii][ji] = d
        print(col)
        s6grade.insert(ii,col)
    print(s6grade)
    #print(suc(score[0][4])) # 0,4 45

    for i in range(0,5):
        for j in range(0,6):
            print(s6grade[i][j],end=" ")
        print()

  

 

    while True:
        char=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
        morsecode=['.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--',
                   '-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..']
        message=input('please word')
        l=len(message)
        code=""
        mos=""
        for k in range(0,l):
            for index in range(0,len(char)):
                if(message[k]==char[index]):
                    code=code+" "+str(index+1)
                    mos=mos+" "+morsecode[index]

        print(code)
        print(mos)
        yn=input('y/N')
        if yn=='Y' or yn=='y':
            continue
        else:
            break

  

 

def find_column_word(letter_array, word):  
    cols = len(letter_array[0]) if letter_array else 0  
  
    # Iterate over all columns  
    for j in range(cols):  
        # Check if the word matches the current column  
        match = True  
        for i in range(len(letter_array)):  
            # Check if the current row is within the bounds of the array  
            # and if the character matches the word  
            if i < len(letter_array) and letter_array[i][j] != word[i]:  
                match = False  
                break  
  
        # If the word matches the column, return True  
        if match and i == len(word) - 1:  
            return True  
  
    return False  
  
# Example usage  
letter_array = [  
    "JGJGDDAOYD",  
    "IDGFHSPOSA",  
    "FGDIOSAFSC",  
    "INTERNETSO",  
    "FJKCOSAFSM",  
    "DJSGAPAHDP",  
    "HAUSTRFBFU",  
    "KDGFUCNSKT",  
    "WSJDYCFXDE",  
    "ODVFKXJVCR"  
]  
  
word = "coMPUTER"  
  
# Convert the word to uppercase since the letter array seems to be in uppercase  
word = word.upper()  
  
if find_column_word(letter_array, word):  
    print(f"The word '{word}' exists in the letter array as a column.")  
else:  
    print(f"The word '{word}' does not exist in the letter array as a column.")